Created
September 14, 2018 16:06
-
-
Save eric-burel/7ba2ba7ec5f9d868752eab46bcbe5504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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