Skip to content

Commit

Permalink
feat(shortcuts): add send email
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Jun 15, 2020
1 parent 2ee95b9 commit e911690
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
79 changes: 73 additions & 6 deletions lib/shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,89 @@
'use strict';

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

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

/**
* @name campaign
* @name sendEmail
* @description Send a given email
* @param {Object} optns valid email instance or definition
* @param {Function} done a callback to invoke on success or failure
* @return {Error|Object} email 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
*
* sendEmail(optns, (error, email) => { ... });
*/
exports.sendEmail = (optns, done) => {
// prepare sms
const message = copyInstance(optns);

// force message type to email
message.type = TYPE_EMAIL;

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

// ensure unique receivers emails
const receivers = uniq([].concat(message.to));
message.to = receivers;

// ensure unique cc'ed
const cced = uniq([].concat(message.cc));
message.cc = cced;

// ensure unique bcc'ed
const bcced = uniq([].concat(message.bcc));
message.bcc = bcced;

// instantiate email
const email = new Email(message);

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

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

/**
* @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.1.0
* @since 0.19.0
* @version 0.1.0
* @public
* @static
Expand Down Expand Up @@ -48,7 +115,7 @@ exports.sendCampaign = (optns, done) => {
}

// direct send campaign in development & test
// or in synchronou send mode
// or in synchronous send mode
else {
return campaign.send(done);
}
Expand Down
22 changes: 21 additions & 1 deletion test/integration/message.send.smtp.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, sendEmail } = require('../..');

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

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

sendEmail(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('smtp');
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 e911690

Please sign in to comment.