Skip to content

Commit

Permalink
feat(smssync): process delivered messages on http post
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Feb 28, 2020
1 parent 9a3d1f2 commit 0784072
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
52 changes: 52 additions & 0 deletions lib/smssync.http.router.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
STATE_UNKNOWN,
STATE_QUEUED,
STATE_RECEIVED,
STATE_DELIVERED,
SEND_MODE_PULL,
} = require('./common');

Expand Down Expand Up @@ -234,6 +235,56 @@ const onQueued = (/*integration*/) => done => {
return waterfall(tasks, done);
};

/**
* @name onDelivered
* @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
*/
const onDelivered = (/*integration*/) => (delivered, done) => {
// obtained delivered sms ids
let ids = _.map(delivered, report => {
let uuid = report.uuid || '';
return _.first(uuid.split(':')); // obtain sms id
});
ids = _.uniq(_.compact(ids));

const findMessages = next => {
const criteria = {
type: TYPE_SMS,
mode: SEND_MODE_PULL,
transport: transport.name,
_id: { $in: ids }, //TODO use status=queued
};
return Message.find(criteria, next);
};

// update messages state to delivered
const updateStatesToDelivered = (messages, next) => {
const updates = _.map(messages, message => {
// update message state to delivered
return then => {
message.state = STATE_DELIVERED;
message.deliveredAt = new Date();
message.save((error, saved) => {
return then(error, saved);
});
};
});

// run updates in parallel
return parallel(updates, next);
};

// update message status to delivered
const tasks = [findMessages, updateStatesToDelivered];
waterfall(tasks, done);
};

/* expose smssync integration router */
module.exports = exports = integration => {
// initialize transport to get options
Expand All @@ -246,6 +297,7 @@ module.exports = exports = integration => {
onSend: onSend(integration),
onSent: onSent(integration),
onQueued: onQueued(integration),
onDelivered: onDelivered(integration),
};

const options = mergeObjects(transport.options, handlers, integration);
Expand Down
31 changes: 27 additions & 4 deletions test/integration/smssync.http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const { smssyncRouter } = require('../..')({
onMessageReceived,
});

describe.only('SMSSync Http API', () => {
describe('SMSSync Http API', () => {
before(() => clearHttp());
before(done => clearDatabase(done));

Expand Down Expand Up @@ -86,8 +86,7 @@ describe.only('SMSSync Http API', () => {
queued_messages: [[unsent._id, _.first(unsent.to)].join(':')],
};

testPost('/smssync?task=sent&secret=smssync')
.send(queued)
testPost('/smssync?task=sent&secret=smssync', queued)
.expect('Content-Type', /json/)
.expect(200)
.end((error, { body }) => {
Expand Down Expand Up @@ -121,7 +120,31 @@ describe.only('SMSSync Http API', () => {
});

it('should receive delivery reports sent by a device', done => {
done();
const delivered = {
message_result: [
{
uuid: [unsent._id, _.first(unsent.to)].join(':'),
sent_result_code: 0,
sent_result_message: 'SMSSync Message Sent',
delivered_result_code: -1,
delivered_result_message: '',
},
],
};

testPost('/smssync?task=result&secret=smssync', delivered)
.expect(200)
.expect('Content-Type', /json/)
.end((error, { body }) => {
expect(error).to.not.exist;
expect(body).to.exist;

expect(body).to.exist;
expect(body.payload).to.exist;
expect(body.payload.success).to.be.true;

done(error, body);
});
});

it('should reject request with no secret key sent by device', done => {
Expand Down

0 comments on commit 0784072

Please sign in to comment.