Skip to content

Instantly share code, notes, and snippets.

@eric-burel
Created September 14, 2018 16:06
Show Gist options
  • Save eric-burel/7ba2ba7ec5f9d868752eab46bcbe5504 to your computer and use it in GitHub Desktop.
Save eric-burel/7ba2ba7ec5f9d868752eab46bcbe5504 to your computer and use it in GitHub Desktop.
VendingMachines.addField([
{
fieldName: 'lastRefillAt',
fieldSchema: {
type: Date,
optional: true,
viewableBy:['runners', 'managers', 'admins'],
resolveAs: {
type: 'Date',
resolver(vendingMachine, args, context) {
const lastRefill = context.Refills.find(
{ vendingMachineId: vendingMachine._id },
{
fields: { 'date': 1 },
sort: { date: -1},
limit: 1
}
).fetch()
if (!lastRefill.length) return vendingMachine.createdAt
return lastRefill[0].date
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment