Skip to content

Commit

Permalink
feat(shortcuts): add send sms
Browse files Browse the repository at this point in the history
This closes #5
  • Loading branch information
lykmapipo committed Jun 15, 2020
1 parent 0b2251c commit cac2329
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ DEFAULT_ENABLE_SYNC_TRANSPORT=false
# SENDER DEFAULTS
DEFAULT_SENDER_NAME=
DEFAULT_SENDER_EMAIL=
DEFAULT_SENDER_MOBILE=
DEFAULT_SENDER_SMS=
DEFAULT_SENDER_PUSH=


Expand Down
108 changes: 85 additions & 23 deletions lib/shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,68 @@
'use strict';

const { map } = require('lodash');
const { uniq } = require('@lykmapipo/common');
const {
getString,
getStringSet,
getBoolean,
isProduction,
} = require('@lykmapipo/env');
const { toE164 } = require('@lykmapipo/phone');
const { copyInstance } = require('@lykmapipo/mongoose-common');

const { CHANNEL_EMAIL, TYPE_EMAIL, TYPE_PUSH } = require('./common');
const { Email, Push } = require('./message.model');
const { CHANNEL_EMAIL, TYPE_EMAIL, TYPE_SMS, TYPE_PUSH } = require('./common');
const { Email, Push, SMS } = require('./message.model');
const Campaign = require('./campaign.model');

/**
* @function sendCampaign
* @name sendCampaign
* @description Send a given campaign
* @param {Object} optns valid campaign instance or definition
* @param {Function} done a callback to invoke on success or failure
* @return {Error|Object} campaign instance or error
* @see {@link Message}
* @see {@link Campaign}
* @author lally elias <lallyelias87@mail.com>
* @since 0.19.0
* @version 0.1.0
* @public
* @static
* @example
*
* sendCampaign(optns, (error, campaign) => { ... });
*/
exports.sendCampaign = (optns, done) => {
// prepare campaign
const options = copyInstance(optns);

// ensure campaign channels
let channels = [].concat(options.channels).concat(CHANNEL_EMAIL);
channels = getStringSet('DEFAULT_CAMPAIGN_CHANNELS', channels);
options.channels = channels;

// instantiate campaign
const campaign = new Campaign(options);

// queue campaign in production
// or if is asynchronous send mode
const enableSyncTransport = getBoolean(
'DEFAULT_ENABLE_SYNC_TRANSPORT',
false
);
if (isProduction() && !enableSyncTransport) {
campaign.queue();
return done(null, campaign);
}

// direct send campaign in development & test
// or in synchronous send mode
else {
return campaign.send(done);
}
};

/**
* @name sendEmail
* @description Send a given email
Expand All @@ -38,7 +88,10 @@ exports.sendEmail = (optns, done) => {
message.type = TYPE_EMAIL;

// ensure message sender
message.sender = message.sender || getString('SMTP_FROM');
message.sender =
message.sender ||
getString('SMTP_FROM') ||
getString('DEFAULT_SENDER_EMAIL');

// ensure unique receivers emails
const receivers = uniq([].concat(message.to));
Expand Down Expand Up @@ -97,6 +150,9 @@ exports.sendPush = (optns, done) => {
// force message type to push
message.type = TYPE_PUSH;

// ensure message sender
message.sender = message.sender || getString('DEFAULT_SENDER_PUSH');

// ensure unique receivers pushs
const receivers = uniq([].concat(message.to));
message.to = receivers;
Expand All @@ -123,12 +179,11 @@ exports.sendPush = (optns, done) => {
};

/**
* @function sendCampaign
* @name sendCampaign
* @description Send a given campaign
* @param {Object} optns valid campaign instance or definition
* @name sendSMS
* @description Send a given sms
* @param {Object} optns valid sms instance or definition
* @param {Function} done a callback to invoke on success or failure
* @return {Error|Object} campaign instance or error
* @return {Error|Object} sms instance or error
* @see {@link Message}
* @see {@link Campaign}
* @author lally elias <lallyelias87@mail.com>
Expand All @@ -138,34 +193,41 @@ exports.sendPush = (optns, done) => {
* @static
* @example
*
* sendCampaign(optns, (error, campaign) => { ... });
* sendSMS(optns, (error, sms) => { ... });
*/
exports.sendCampaign = (optns, done) => {
// prepare campaign
const options = copyInstance(optns);
exports.sendSMS = (optns, done) => {
// prepare message
const message = copyInstance(optns);

// ensure campaign channels
let channels = [].concat(options.channels).concat(CHANNEL_EMAIL);
channels = getStringSet('DEFAULT_CAMPAIGN_CHANNELS', channels);
options.channels = channels;
// force message type to sms
message.type = TYPE_SMS;

// instantiate campaign
const campaign = new Campaign(options);
// ensure message sender
message.sender = message.sender || getString('DEFAULT_SENDER_SMS');

// queue campaign in production
// ensure unique receivers smss
const receivers = map(uniq([].concat(message.to)), (receiver) => {
return toE164(receiver);
});
message.to = receivers;

// instantiate sms
const sms = new SMS(message);

// queue sms in production
// or if is asynchronous send mode
const enableSyncTransport = getBoolean(
'DEFAULT_ENABLE_SYNC_TRANSPORT',
false
);
if (isProduction() && !enableSyncTransport) {
campaign.queue();
return done(null, campaign);
sms.queue();
return done(null, sms);
}

// direct send campaign in development & test
// direct send sms in development & test
// or in synchronous send mode
else {
return campaign.send(done);
return sms.send(done);
}
};
22 changes: 21 additions & 1 deletion test/integration/message.send.infobip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* dependencies */
const { clear, expect } = require('@lykmapipo/mongoose-test-helpers');
const { Message } = require('../..');
const { Message, sendSMS } = require('../..');

describe('infobip transport', () => {
before((done) => clear(done));
Expand Down Expand Up @@ -32,6 +32,26 @@ describe('infobip transport', () => {
});
});

it('should be able to send using shortcut', (done) => {
const message = Message.fakeExcept('sentAt', 'failedAt', 'deliveredAt');
message.transport = 'infobip-sms';

sendSMS(message, (error, sent) => {
//assert results
expect(error).to.not.exist;
expect(sent).to.exist;
expect(sent._id).to.exist;
expect(sent.transport).to.be.equal('infobip-sms');
expect(sent.sentAt).to.exist;
expect(sent.deliveredAt).to.exist;
expect(sent.failedAt).to.not.exist;
expect(sent.result).to.exist;
expect(sent.result.success).to.exist;
expect(sent.result.success).to.be.true;
done(error, sent);
});
});

after(() => {
delete process.env.DEBUG;
});
Expand Down
22 changes: 21 additions & 1 deletion test/integration/message.send.tz.ega.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* dependencies */
const { clear, expect } = require('@lykmapipo/mongoose-test-helpers');
const { Message } = require('../..');
const { Message, sendSMS } = require('../..');

describe('tz ega transport', () => {
before((done) => clear(done));
Expand Down Expand Up @@ -32,6 +32,26 @@ describe('tz ega transport', () => {
});
});

it('should be able to send using shortcut', (done) => {
const message = Message.fakeExcept('sentAt', 'failedAt', 'deliveredAt');
message.transport = 'tz-ega-sms';

sendSMS(message, (error, sent) => {
//assert results
expect(error).to.not.exist;
expect(sent).to.exist;
expect(sent._id).to.exist;
expect(sent.transport).to.be.equal('tz-ega-sms');
expect(sent.sentAt).to.exist;
expect(sent.deliveredAt).to.exist;
expect(sent.failedAt).to.not.exist;
expect(sent.result).to.exist;
expect(sent.result.success).to.exist;
expect(sent.result.success).to.be.true;
done(error, sent);
});
});

after(() => {
delete process.env.DEBUG;
});
Expand Down

0 comments on commit cac2329

Please sign in to comment.