Skip to content

Commit

Permalink
fix(campaign): send using specified channels only
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Oct 28, 2019
1 parent a771622 commit 736cf5d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
30 changes: 18 additions & 12 deletions lib/campaign.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const CampaignSchema = new Schema({
channels: {
type: [String],
enum: CHANNELS,
default: [CHANNEL_SMS],
default: [CHANNEL_EMAIL],
index: true,
searchable: true,
taggable: true,
Expand Down Expand Up @@ -316,7 +316,7 @@ CampaignSchema.methods.preValidate = function preValidate(next) {
* @function send
* @description send campaign to recipients
* @param {Function} done a callback to invoke on success or failure
* @return {Campaing|Error} an instance of campaing or error
* @return {Campaing|Error} an instance of campaign or error
* @since 0.1.0
* @version 0.1.0
* @instance
Expand Down Expand Up @@ -465,17 +465,23 @@ CampaignSchema.methods.send = function send(done) {
return next(null, []);
};

// TODO send push
// send in paralles(sendEmails, sendSMS, sendPush)
// TODO check allowed channels
const doSend = (campaign, next) => parallel({
email: then => sendEmails(campaign, then),
sms: then => sendSMSs(campaign, then),
push: then => sendPushs(campaign, then),
}, (error, results) => {
// TODO update statistics after send
return next(error, campaign, results);
});
const doSend = (campaign, next) => {
// collect all send work
let sendWork = {
email: then => sendEmails(campaign, then),
sms: then => sendSMSs(campaign, then),
push: then => sendPushs(campaign, then),
};
// reduce for allowed channels
const allowedChannels = _.map(campaign.channels, _.toLower);
sendWork = _.pick(sendWork, ...allowedChannels);

return parallel(sendWork, (error, results) => {
// TODO update statistics after send
return next(error, campaign, results);
});
};

// do sending
return waterfall([
Expand Down
15 changes: 10 additions & 5 deletions test/integration/campaign.send.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
/* dependencies */
const _ = require('lodash');
const { faker, clear, expect } = require('@lykmapipo/mongoose-test-helpers');
const { CHANNEL_EMAIL, CHANNEL_PUSH } = require('../..');
const { Campaign } = require('../..');

const fakeContacts = (limit = 1) => {
const contacts = _.map(_.range(limit), () => {
const name = faker.name.findName();
const email = faker.internet.email();
const mobile = faker.helpers.replaceSymbolWithNumber('255714######');
return { name, email, mobile };
const pushToken = faker.random.uuid();
return { name, email, mobile, pushToken };
});
return limit === 1 ? _.first(contacts) : contacts;
};
Expand All @@ -24,39 +26,42 @@ describe('campaign send', () => {

it('should be able to send without fetchContacts', done => {
const to = fakeContacts();
const channels = [CHANNEL_EMAIL, CHANNEL_PUSH];

Campaign.fetchContacts = undefined;

const campaign = Campaign.fake();
campaign.to = [to];
campaign.set({ to, channels });
expect(campaign.send).to.exist;

campaign.send((error, sent, messages) => {
expect(error).to.not.exist;
expect(sent).to.exist;
expect(messages.email).to.exist.and.have.length(1);
// expect(messages.sms).to.exist.and.have.length(1);
// expect(messages.push).to.exist.and.have.length(1);
expect(messages.push).to.exist.and.have.length(1);
done(error, sent);
});
});

it('should be able to send with fetchContacts', done => {
const to = fakeContacts(4);
const channels = [CHANNEL_EMAIL, CHANNEL_PUSH];

Campaign.fetchContacts = (criteria, done) => {
return done(null, to);
};

const campaign = Campaign.fake();
campaign.set({ channels });
expect(campaign.send).to.exist;

campaign.send((error, sent, messages) => {
expect(error).to.not.exist;
expect(sent).to.exist;
expect(messages.email).to.exist.and.have.length(5);
// expect(messages.sms).to.exist.and.have.length(5);
// expect(messages.push).to.exist.and.have.length(5);
expect(messages.push).to.exist.and.have.length(5);
done(error, sent);
});
});
Expand Down Expand Up @@ -90,7 +95,7 @@ describe('campaign send', () => {
expect(error).to.not.exist;
expect(sent).to.exist;
expect(messages.email).to.exist;
expect(messages.sms).to.exist;
// expect(messages.sms).to.exist;
expect(messages.push).to.exist;
done(error, sent);
done(error, sent, messages);
Expand Down
1 change: 0 additions & 1 deletion test/integration/message.send.echo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('echo transport', () => {
expect(sent.sentAt).to.exist;
expect(sent.deliveredAt).to.exist;
expect(sent.failedAt).to.not.exist;
console.log(sent.result);
expect(sent.result).to.exist;
expect(sent.result.success).to.exist;
expect(sent.result.success).to.be.true;
Expand Down

0 comments on commit 736cf5d

Please sign in to comment.