Skip to content

Commit

Permalink
feat(transport): add echo transport
Browse files Browse the repository at this point in the history
This closes #15
  • Loading branch information
lykmapipo committed Oct 16, 2019
1 parent a74bb05 commit 5422f2c
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 51 deletions.
34 changes: 7 additions & 27 deletions lib/message.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ const DEFAULT_PUSH_TRANSPORT_NAME = getString('DEFAULT_PUSH_TRANSPORT_NAME');
const transports = {
'infobip-sms': require('./transports/sms.infobip'),
'tz-ega-sms': require('./transports/sms.tz.ega'),
smtp: require('./transports/smtp'),
'smtp': require('./transports/smtp'),
'fcm-push': require('./transports/push.fcm'),
'echo': require('./transports/echo'),
};

/* message directions */
Expand Down Expand Up @@ -850,41 +851,20 @@ MessageSchema.methods.send = function send(done) {
/* @todo format <to> based on message type */
/* @todo format <to> as e164 phone numbers */

//send via echo transport
const useEchoTransport =
_.isEmpty(this.transport) || this.transport === ECHO_TRANSPORT_NAME;

//check for debug flags
// check for debug flags
const DEBUG = getBoolean('DEBUG', false);

if (useEchoTransport) {
//update message
this.sentAt = new Date();
this.deliveredAt = new Date();
this.result = {
success: true,
};
this.failedAt = undefined;
this.state = STATE_DELIVERED;

//ensure echo transport
this.transport = this.transport || ECHO_TRANSPORT_NAME;

//persist message
return this.put(done);
}

//handle debug message sending
else if (DEBUG) {
//update message
// handle debug message sending
if (DEBUG) {
// update message
this.sentAt = this.sentAt || new Date();
this.deliveredAt = new Date();
this.result = {
success: true,
};
this.state = STATE_DELIVERED;

//persist message
// persist message
return this.put(done);
}

Expand Down
145 changes: 145 additions & 0 deletions lib/transports/echo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
'use strict';

/**
* @name echo
* @module echo
* @description echo transport
*
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.1.0
* @version 0.1.0
* @public
*/

/* dependencies */
const _ = require('lodash');

/**
* @name defaults
* @description default configuration options
*
* @type {Object}
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @private
*/
exports.defaults = {};

/**
* @name countryCode
* @description transport country code
*
* @type {String}
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @public
*/
exports.countryCode = undefined;

/**
* @name countryName
* @description transport country name
*
* @type {String}
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @public
*/
exports.countryCode = undefined;

/**
* @name name
* @description name of the transport
*
* @type {String}
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @public
*/
exports.name = 'echo';

/**
* @function toObject
* @name toObject
* @description convert transport details into object
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @public
*/
exports.toObject = function toObject() {
const object = {
name: exports.name,
countryCode: exports.countryCode,
countryName: exports.countryName,
};
return object;
};

/**
* @name init
* @function init
* @description initialize transport
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @private
*/
exports.init = function (options) {
// merge options
exports.options = _.merge({}, exports.defaults, exports.options, options);
};

/**
* @name _send
* @function _send
* @description send message using echo transport
* @param {Message} message valid message instance
* @param {Function} done a callback to invoke on success or failure
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @private
*/
exports._send = function (message, done) {
// ensure transport is initialized
exports.init();

// reply with results
const result = {
success: true,
};
done(null, result);
};

/**
* @name send
* @function send
* @description echo message via transport
* @param {Message} message valid message instance
* @param {Function} done callback to invoke on success or failure
* @return {Object|Error} send result or error
*
* @author lally elias <lallyelias87@gmail.com>
* @since 0.1.0
* @version 0.1.0
* @public
*/
exports.send = function (message, done) {
// ensure message sender
message.sender = message.sender || exports.name;

// perform actual echo sending
exports._send(message, done);
};
8 changes: 5 additions & 3 deletions test/integration/message.queue.echo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { Message, worker } = require('../..');

describe('echo transport queue', () => {
before(() => {
process.env.DEBUG = true;
delete process.env.DEBUG;
});

before(done => clear(done));
Expand All @@ -19,8 +19,10 @@ describe('echo transport queue', () => {
'sentAt',
'failedAt',
'deliveredAt',
'mode'
'mode',
'result'
);
message.transport = 'echo';

// listen to queue events
worker.queue
Expand Down Expand Up @@ -59,4 +61,4 @@ describe('echo transport queue', () => {
after(() => {
delete process.env.DEBUG;
});
});
});
32 changes: 11 additions & 21 deletions test/integration/message.send.echo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,30 @@ const { Message } = require(path.join(__dirname, '..', '..'));

describe('echo transport', () => {
before(() => {
process.env.DEBUG = true;
delete process.env.DEBUG;
});

before(done => clear(done));

it('should be able to send message', done => {
const message = Message.fakeExcept('sentAt', 'failedAt', 'deliveredAt');
message.send((error, sent) => {
//assert results
expect(error).to.not.exist;
expect(sent).to.exist;
expect(sent._id).to.exist;
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);
});
});

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

message.send((error, sent) => {
//assert results
expect(error).to.not.exist;
expect(sent).to.exist;
expect(sent._id).to.exist;
expect(sent.transport).to.exist.and.be.equal('echo');
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 All @@ -54,4 +44,4 @@ describe('echo transport', () => {
after(() => {
delete process.env.DEBUG;
});
});
});

0 comments on commit 5422f2c

Please sign in to comment.