Skip to content

Commit

Permalink
refactor(transports): remove smssync router from transport
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Feb 25, 2020
1 parent fe6cd10 commit f7c1f8c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 258 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const { Message, Email, SMS, Push } = require('./lib/message.model');
const messageRouter = require('./lib/message.http.router');
const Campaign = require('./lib/campaign.model');
const campaignRouter = require('./lib/campaign.http.router');
const smssyncTransport = require('./lib/transports/smssync');

/**
* @module postman
Expand All @@ -33,8 +32,10 @@ function postman(integration) {
}

// initialize smssync pull sms transport
// TODO: initialize smssync http router
// TODO: resolve passing Message model around
postman.smssyncRouter = smssyncTransport.init(integration, Message).transport;
// postman.smssyncRouter = smssyncTransport.init(integration, Message).transport;
postman.smssyncRouter = {};

// return initialized postman
return postman;
Expand Down
264 changes: 8 additions & 256 deletions lib/transports/smssync.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@

/* dependencies */
const _ = require('lodash');
const { waterfall, parallel } = require('async');
const { getString, getBoolean } = require('@lykmapipo/env');
const { smssync } = require('smssync');

const {
TYPE_SMS,
SEND_MODE_PULL,
STATE_UNKNOWN,
STATE_QUEUED,
STATE_DELIVERED,
} = require('../common');
const { TYPE_SMS, SEND_MODE_PULL } = require('../common');

/**
* @name defaults
Expand All @@ -38,6 +30,7 @@ const {
* @private
*/
exports.defaults = {
// TODO: from blacklist
endpoint: getString('SMSSYNC_ENDPOINT', 'smssync'),
secret: getString('SMSSYNC_SECRET', 'smssync'),
reply: getBoolean('SMSSYNC_REPLY', true),
Expand Down Expand Up @@ -111,28 +104,9 @@ exports.toObject = function toObject() {
* @version 0.1.0
* @private
*/
exports.init = function(options, Message) {
exports.init = function(options) {
// merge options
if (!exports.transport) {
const handlers = {
onReceive: exports.onReceive(Message), // TODO: accept from options
onSend: exports.onSend(Message),
onSent: exports.onSent(Message),
onQueued: exports.onQueued(Message),
onDelivered: exports.onDelivered(Message),
};
exports.options = _.merge(
{},
exports.defaults,
exports.options,
handlers,
options
);
exports.transport = smssync(exports.options);
}

// return self
return exports;
exports.options = _.merge({}, exports.defaults, exports.options, options);
};

/**
Expand All @@ -148,10 +122,11 @@ exports.init = function(options, Message) {
* @private
*/
exports._send = function(message, done) {
// ensure transport is initialized
exports.init();

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

Expand Down Expand Up @@ -180,226 +155,3 @@ exports.send = function(message, done) {
// perform actual smssync sending
exports._send(message, done);
};

/**
* @name onReceived
* @description handle received sms from smssync device
* @param {Object} sms valid smssync message
* @param {Function} done a callback to invoke after receiving sms
* @return {Object} a message to return to a sender
* @see {@link http://smssync.ushahidi.com/developers/}
* @private
* @since 0.1.0
* @version 0.1.0
*/
exports.onReceive = (/*Message*/) => (sms, done) => {
//see http://smssync.ushahidi.com/developers/
//for the structure of the sms send by smssync to be saved

// TODO: implement
return done(new Error('Not Allowed'));
};

/**
* @name onSend
* @description obtain list of sms(message) to be send by smssync device
* @param {Function} done a callback to invoke after receiving sms
* @return {[Object]} collection of message to be send by smssync device
* @see {@link http://smssync.ushahidi.com/developers/}
* @private
* @since 0.1.0
* @version 0.1.0
*/
exports.onSend = Message => done => {
//TODO generate individual message to send
//TODO update status to sent
//TODO clear once sent

waterfall(
[
function findUnsent(next) {
//TODO update status=sent use update
const criteria = {
type: TYPE_SMS,
mode: SEND_MODE_PULL,
transport: exports.name,
state: STATE_UNKNOWN,
};
Message.unsent(criteria, next);
},

function normalize(messages, next) {
const smss = [];
_.forEach(messages, function(message) {
_.forEach([].concat(message.to), function(to) {
smss.push({
to: to,
message: message.body,
uuid: [message._id, to].join(':'),
});
});
});

next(null, smss);
},
],
done
);
};

/**
* @name onSent
* @description received queued sms from smssync device
* @param {[String]} queued collection of message(sms) uuids from smssync
* device
* @param {Function} done a callback to invoke after receiving sms
* @return {[Object]} collection of message to be send by smssync device
* @see {@link http://smssync.ushahidi.com/developers/}
* @private
* @since 0.1.0
* @version 0.1.0
*/
exports.onSent = Message => (queued, done) => {
//obtained queued sms ids
let ids = _.map(queued, function(sms) {
return _.first(sms.split(':')); //obtain sms id
});
ids = _.uniq(ids);

//update message status to sent
waterfall(
[
function findMessages(next) {
Message.find(
{
type: TYPE_SMS,
mode: SEND_MODE_PULL,
transport: exports.name,
_id: { $in: ids }, //TODO use status=sent
},
next
);
},

function updateMessageStateToQueued(messages, next) {
const updates = _.map(messages, function(message) {
//update message state to queued
return function(then) {
message.state = STATE_QUEUED;
message.save(function(error, saved) {
then(error, saved);
});
};
});

//update in parallel fashion
parallel(updates, next);
},

function returnUuidsOfProcessedMessage(messages, next) {
const uuids = [];
_.forEach(messages, function(message) {
_.forEach([].concat(message.to), function(to) {
uuids.push([message._id, to].join(':'));
});
});
next(null, uuids);
},
],
done
);
};

/**
* @name onQueued
* @description obtain message(sms) waiting delivery report and send them to
* smssync device
* @param {Function} done a callback to invoke on success or failure
* @return {[Object]} collection of message uuids waiting delivery status
* from smssync device
* @see {@link http://smssync.ushahidi.com/developers/}
* @private
* @since 0.1.0
* @version 0.1.0
*/
exports.onQueued = Message => done => {
waterfall(
[
function findMessagesWaitingDeliveryReport(next) {
Message.find(
{
type: TYPE_SMS,
mode: SEND_MODE_PULL,
transport: exports.name,
state: STATE_QUEUED,
},
next
);
},

function returnUuidsOfQueuedMessages(messages, next) {
const uuids = [];
_.forEach(messages, function(message) {
_.forEach([].concat(message.to), function(to) {
uuids.push([message._id, to].join(':'));
});
});

next(null, uuids);
},
],
done
);
};

/**
* @name onQueued
* @description receive delivery status from smssync device
* @param {Function} done a callback to invoke on success or failure
* @return {[Object]} collection of message
* @see {@link http://smssync.ushahidi.com/developers/}
* @private
* @since 0.1.0
* @version 0.1.0
*/
exports.onDelivered = Message => (delivered, done) => {
//obtained delivered sms ids
let ids = _.map(delivered, function(report) {
let uuid = report.uuid || '';
return _.first(uuid.split(':')); //obtain sms id
});
ids = _.uniq(_.compact(ids));

//update message status to delivered
waterfall(
[
function findMessages(next) {
Message.find(
{
type: TYPE_SMS,
mode: SEND_MODE_PULL,
transport: exports.name,
_id: { $in: ids }, //TODO use status=queued
},
next
);
},

function updateMessageStateToDelivered(messages, next) {
const updates = _.map(messages, function(message) {
//update message state to delivered
return function(then) {
message.state = STATE_DELIVERED;
message.save(function(error, saved) {
then(error, saved);
});
};
});

//update in parallel fashion
parallel(updates, next);
},
],
done
);
};

0 comments on commit f7c1f8c

Please sign in to comment.