Skip to content

Commit

Permalink
feat(smssync): return undelivered messages on http get
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Feb 28, 2020
1 parent a8697dd commit 9a3d1f2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
57 changes: 48 additions & 9 deletions lib/smssync.http.router.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const onReceive = integration => (sms, done) => {
* @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/}
* @see {@link http://smssync.ushahidi.com/developers/}
* @private
* @since 0.1.0
* @version 0.1.0
Expand All @@ -102,13 +102,12 @@ const onSend = (/*integration*/) => done => {
transport: transport.name,
state: STATE_UNKNOWN,
};
Message.unsent(criteria, next);
return Message.unsent(criteria, next);
};

// normalize unset message to sms
const normalizeUnsent = (messages, next) => {
const smss = [];

_.forEach(messages, message => {
const recipients = [].concat(message.to);
_.forEach(recipients, to => {
Expand All @@ -119,8 +118,7 @@ const onSend = (/*integration*/) => done => {
});
});
});

next(null, smss);
return next(null, smss);
};

// return sms to send
Expand All @@ -133,9 +131,9 @@ const onSend = (/*integration*/) => done => {
* @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
* @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/}
* @see {@link http://smssync.ushahidi.com/developers/}
* @private
* @since 0.1.0
* @version 0.1.0
Expand Down Expand Up @@ -171,7 +169,7 @@ const onSent = (/*integration*/) => (queued, done) => {
message.queuedAt = time;
message.save((error, saved) => {
// TODO: handle error & ignore
then(error, saved);
return then(error, saved);
});
};
});
Expand All @@ -188,14 +186,54 @@ const onSent = (/*integration*/) => (queued, done) => {
uuids.push([message._id, to].join(':'));
});
});
next(null, uuids);
return next(null, uuids);
};

// update message status to queued(and sent?)
const tasks = [findMessages, updateStatesToQueued, returnProcessedUUIDs];
return waterfall(tasks, 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
*/
const onQueued = (/*integration*/) => done => {
// obtain queued messages
const findUndelivered = next => {
const criteria = {
type: TYPE_SMS,
mode: SEND_MODE_PULL,
transport: transport.name,
state: STATE_QUEUED,
};
return Message.find(criteria, next);
};

// prepare undelivered sms uuids
const returnUndeliveredUuids = (messages, next) => {
const uuids = [];
_.forEach(messages, message => {
_.forEach([].concat(message.to), to => {
uuids.push([message._id, to].join(':'));
});
});
return next(null, uuids);
};

// return message wait delivery
const tasks = [findUndelivered, returnUndeliveredUuids];
return waterfall(tasks, done);
};

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

const options = mergeObjects(transport.options, handlers, integration);
Expand Down
22 changes: 16 additions & 6 deletions test/integration/smssync.http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,25 @@ describe.only('SMSSync Http API', () => {
});
});

it('should receive delivery reports sent by a device', done => {
done();
});

it('should return sms waiting delivery report to a device', done => {
done();
testGet('/smssync?task=result&secret=smssync')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end((error, { body }) => {
expect(error).to.not.exist;

expect(body).to.exist;
expect(_.get(body, 'message_uuids')).to.exist;
expect(_.get(body, 'message_uuids')[0]).to.be.exist.and.be.equal(
[unsent._id, _.first(unsent.to)].join(':')
);

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

it('should reject request with no secret key sent by device', done => {
it('should receive delivery reports sent by a device', done => {
done();
});

Expand Down

0 comments on commit 9a3d1f2

Please sign in to comment.