Jobs

import notifications from 'notifications';
import moment from 'moment';

// load administrators.
const adminIds = org.objects.accounts
  .find({roles: '000000000000000000000004'})
  .paths('_id')
  .skipAcl(true)
  .grant(4)
  .map(doc => doc._id);

// set desired time periods.
const startDate = moment(new Date()).utc().subtract(1,'day').startOf('day').toDate(),
      endDate = moment(new Date()).utc().subtract(1,'day').endOf('day').toDate();

// aggregate counts.
const cursor = org.objects.c_widgets.aggregate()
  .match({created: {$gte: startDate, $lte: endDate}})
  .group({_id: null, count: {$count: '_id'}});
   
const count = cursor.hasNext() ? cursor.next().count : 0;

// create report.
const report = {
  c_num_new_widgets: count
};

// send notification to admins.
adminIds.forEach(_id => 
  notifications.send('c_daily_report', report, {recipient: _id})
);

Jobs are scheduled using standard cron syntax and are suited for maintenance tasks, statistics gathering and data post-processing.