Created
October 5, 2015 17:29
-
-
Save evantahler/192337ba5ea521e00c87 to your computer and use it in GitHub Desktop.
fancy actionhero task
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
var async = require('async'); | |
var sendAbandonedCartEmail = function(api, cart, callback){ | |
api.models.find({where: {id: cart.user_id}}).then(function(error, user){ // api.models are loaded via sequelize | |
if(error){ return next(error); ) | |
if(!user){ return next(new Error('user not found')); ) | |
api.cartHelper.sendAbandonmentEmail(api, user, callback); // api.cartHelper would be dinfined in an initializer | |
}).catch(callback); | |
}; | |
var task = { | |
name: 'send_abandoned_cart_emails', | |
description: 'remind people to give me money', | |
queue: 'default', | |
plugins: [], | |
pluginOptions: [], | |
frequency: 1000 * 60 * 60, | |
run: function(api, params, next){ | |
var emailSendJobs = []; | |
api.models.cart.findAll({where: {createdAt: 'lte ' + new Date(myDate.getTime() - (1000 * 60 * 60)))}}).then(function(error, rows){ | |
if(error){ return next(error); ) | |
if( rows.length === 0 ){ return next(); ) | |
rows.forEach(function(cart){ | |
emailSendJobs.push(function(asyncCallback){ sendAbandonedCartEmail(api, cart, asyncCallback); }) | |
}); | |
async.series(emailSendJobs, next); | |
}).catch(next); | |
}); | |
}; | |
exports.task = task; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment