Skip to content

Commit

Permalink
feat: implement campaign queue
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Jun 19, 2019
1 parent 8ed527b commit 1e215fd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
63 changes: 61 additions & 2 deletions lib/campaign.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const {
} = require('@lykmapipo/mongoose-common');
const actions = require('mongoose-rest-actions');
const exportable = require('@lykmapipo/mongoose-exportable');
const { Email } = require('./message.model');
const { plugin: runInBackground, worker } = require('mongoose-kue');
const { Message, Email } = require('./message.model');

/* constants */
const MODEL_NAME = getString('CAMPAIGN_MODEL_NAME', 'Campaign');
Expand Down Expand Up @@ -346,7 +347,7 @@ CampaignSchema.methods.send = function send(done) {
to: to,
subject: campaign.subject,
body: campaign.message,
bulk: campaign._id
bulk: campaign._id,
});
return email.send((error, message) => next(null, message));
};
Expand Down Expand Up @@ -374,6 +375,61 @@ CampaignSchema.methods.send = function send(done) {
);
};

/**
* @name queue
* @function queue
* @description queue campaign for later send
* @events job error, job success
* @fire {Message|Error} an instance of queued message or error
* @since 0.1.0
* @instance
* @example
*
* message.queue();
*
*/
CampaignSchema.methods.queue = function queue(done) {
//this refer to Campaign instance context

// normalize arguments
const cb = _.isFunction(done) ? done : _.noop;

//persist campaign
this.save(function(error, campaign) {
//notify campaign queue error
if (error) {
worker.queue.emit('job error', error);
return cb(error);
}

//queue campaign for later send
else {
//prepare job details
const jobType = getString('KUE_DEFAULT_JOB_TYPE'); // TODO campaign.type
const title = campaign.subject || campaign.form || jobType;
const jobDefaults = {
method: 'send',
title: title,
type: jobType,
};
const jobDetails = _.merge({}, jobDefaults, campaign.toObject());

const job = campaign.runInBackground(jobDetails);

//ensure campaign has been queued
job.save(function(error) {
if (error) {
worker.queue.emit('job error', error);
return cb(error);
} else {
worker.queue.emit('job queued', campaign);
return cb(null, campaign);
}
});
}
});
};

/*
*------------------------------------------------------------------------------
* Statics
Expand Down Expand Up @@ -407,6 +463,9 @@ CampaignSchema.statics.AUDIENCES = AUDIENCES;
*/
CampaignSchema.plugin(actions);
CampaignSchema.plugin(exportable);
CampaignSchema.plugin(runInBackground, {
types: Message.TYPES,
});

/* export campaign model */
module.exports = exports = model(MODEL_NAME, CampaignSchema);
6 changes: 3 additions & 3 deletions lib/message.http.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ router.get(
router.post(
PATH_LIST,
postFor({
post: (body, done) => Message.post(body, done), // TODO create & send
post: (body, done) => Message.post(body, done), // TODO Message.send
})
);

Expand Down Expand Up @@ -279,7 +279,7 @@ router.get(
* @apiUse AuthorizationHeaderError
* @apiUse AuthorizationHeaderErrorExample
*/
router.patch(PATH_SINGLE, patchFor());
router.patch(PATH_SINGLE, patchFor()); //TODO Message.resend

/**
* @api {put} /messages/:id Put Existing Message
Expand All @@ -297,7 +297,7 @@ router.patch(PATH_SINGLE, patchFor());
* @apiUse AuthorizationHeaderError
* @apiUse AuthorizationHeaderErrorExample
*/
router.put(PATH_SINGLE, putFor());
router.put(PATH_SINGLE, putFor()); //TODO Message.resend

/**
* @api {delete} /messages/:id Delete Existing Message
Expand Down

0 comments on commit 1e215fd

Please sign in to comment.