import cron  from 'cron';
import mongo from './mongo';
import email from './email';

let c = mongo.collections;

// public api ---------------------------------------------

let notifications = {

	/**
	 * Add
	 *
	 * Takes a notification object and
	 * adds it to the database to be processed by
	 * the cron.
	 */
	add (notification, callback) {
		c.notifications.insertOne(notification, callback);
	},

	/**
	 * Send
	 */
	send (notification, callback) {
		if (notification.type === 'email') {
			email.send(notification.email, callback);
		}
	}

};

// notifications cron -------------------------------------

new cron.CronJob('*/30 * * * * *', () => {
	console.log('cron heart beat');
	c.notifications.find({}).toArray((err, docs) => {
		for (let notification of docs) {
			notifications.send(notification, (err) => {
				if (!err) {
					c.notifications.removeOne({_id: notification._id}, {}, (err, doc) => {});
				}
			});
		}
	});
}, null, true, 'America/Los_Angeles');

export default notifications
@@ -655,41 +655,8 @@
 %3E %7B%0A
-%09console.log('cron heart beat');%0A
 %09c.n

Remove heartbeat log