Skip to content

Instantly share code, notes, and snippets.

@davidworkman9
Last active August 29, 2015 13:56
Show Gist options
  • Save davidworkman9/9222212 to your computer and use it in GitHub Desktop.
Save davidworkman9/9222212 to your computer and use it in GitHub Desktop.
Meteor.methods({
average_sales: function(options) {
var self, start_day, total_sales, yesterday;
self = this;
this.unblock();
start_day = moment().tz("America/Los_Angeles").subtract("days", 77).startOf('day');
yesterday = moment().tz("America/Los_Angeles").subtract("days", 1).endOf("day");
total_sales = 0;
var query = {
$and: [{
created_at_unix: {
$gte: start_day.unix(),
$lte: yesterday.unix()
}
}]
};
if (options && (options.day_of_week != null)) {
query.$and.push({ day_of_week: options.day_of_week });
}
if (options && (options.start_hour != null)) {
query.$and.push({ start_hour: { $gte: options.start_hour } });
}
if (options && (options.end_hour != null)) {
query.$and.push({ end_hour: { $lt: options.end_hour } });
}
Sales.aggregate({ $match: query }, { $group: { _id : null, sum : { $sum: "$net_total_money.amount" } }});
return (total_sales / 100) / 11;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment