-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
319 lines (259 loc) · 6.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'use strict';
/**
* @module open311-infobip
* @version 0.1.0
* @description infobip sms transport for open311-messages
* @see {@link https://github.com/CodeTanzania/open311-messages}
* @see {@link https://github.com/lykmapipo/bipsms}
* @see {@link https://dev.infobip.com/}
* @see {@link https://www.infobip.com/}
* @author lally elias <[email protected]>
* @public
*/
//dependencies
const _ = require('lodash');
const kue = require('kue');
const mongoose = require('mongoose');
const Infobip = require('bipsms');
const noop = function () {};
/**
* @name defaults
* @description default configuration options
* @type {Object}
* @since 0.1.0
* @private
*/
exports.defaults = {
timeout: 5000,
concurrency: 10,
fake: false,
sync: false,
from: 'open311' //TODO hwy not use 311
};
/**
* @name queueName
* @description name of the queue that will be used by infobip sms transport
* to enqueue message for sending
* @type {String}
* @since 0.1.0
* @public
*/
exports.queueName = 'infobip';
/**
* @name transport
* @description name of the transport provided by infobip.
* This must be name of node module or file path pointing to
* a node module implement `send()`.
* @type {String}
* @since 0.1.0
* @public
*/
exports.transport = 'open311-infobip';
/**
* @name init
* @description initialize infobip internals
* @since 0.1.0
* @private
* @example
*
* const infobip = require('open311-infobip');
* infobip.options = {
* from:<your_infobip_account_sender_id>,
* username:<your_infobip_account_username>,
* password:<your_infobip_account_password>
* };
*
*/
exports.init = function () {
//merge options
exports.options = _.merge({}, exports.defaults, exports.options);
//obtain username and password
const { username, password } = exports.options;
//ensure username
if (_.isEmpty(username)) {
throw new Error('Missing Infobip Account Username');
}
//ensure password
if (_.isEmpty(password)) {
throw new Error('Missing Infobip Account Password');
}
//check for fake transport
const useFakeTransport = exports.options.fake;
//check for sync transport
const useSyncTransport = exports.options.sync;
//initiate node Infobip sender
//@see {@link https://github.com/lykmapipo/bipsms#usage}
if (!exports.infobip) {
//use fake transport
if (useFakeTransport) {
exports.infobip = new Infobip({ fake: true });
}
//use actual transport
else {
exports.infobip = new Infobip({ username, password });
}
}
//initialize worker processing queue
//for internal usage
if (!useFakeTransport && !useSyncTransport && !exports._queue) {
exports._queue = kue.createQueue(exports.options);
}
};
/**
* @name queue
* @description queue message instance for sending
* @param {Message} message valid instance of open311-message
* @since 0.1.0
* @public
* @example
*
* const Message = require('open311-messages')(<your_options>);
* const infobip = require('open311-infobip');
* const message = new Message(options);
* infobip.queue(message);
*
*/
exports.queue = function (message) {
//ensure transport is initialize
exports.init();
//update message with transport details
message.transport = exports.transport;
message.queueName = exports.queueName;
//ensure from is set-ed
if (!message.from) {
message.from = exports.options.from;
}
message.queue();
};
/**
* @name _send
* @description send sms message using infobip transport
* @see {@link https://github.com/lykmapipo/bipsms#send-single-sms-to-multiple-destination}
* @param {Message} message valid open311-message instance
* @param {Function} done a callback to invoke on success or failure
* @type {Function}
* @since 0.1.0
* @private
*/
exports._send = function (message, done) {
//prepare infobip compliant sms payload
const senderId = message.from ? message.from : exports.options.from;
let payload = _.merge({}, {
from: senderId,
to: message.to,
text: message.body
});
//ensure infobip is initializes
exports.init();
//TODO add support to retries times
exports.infobip.sendSingleSMS(payload, function (error, response) {
//TODO normalize infobip error message
//respond with error
if (error) {
done(error);
}
//respond with success result
else {
//merge default response details
response = _.merge({}, { message: 'success' }, response);
//TODO normalize infobip response
done(null, response);
}
});
};
/**
* @name send
* @description implementation of open311 message send to allow send message
* as am sms using infobip
* @param {Message} message valid open311 message instance
* @param {Function} done a callback to invoke on success or failure
* @return {Object|Error} infobip result or error during sending sms
* @type {Function}
* @since 0.1.0
* @public
* @example
*
* const Message = require('open311-messages')(<your_options>);
* const infobip = require('open311-infobip');
* const message = new Message(options);
* infobip.send(message, function(error, response){
* ...
* });
*/
exports.send = function (message, done) {
//obtain message additional send options
const options = message.options;
//simulate send
if (options && options.fake) {
done(null, {
message: 'success'
});
}
//send actual sms using infobip
else {
exports._send(message, done);
}
};
/**
* @name stop
* @description gracefull shutdown kue
* @see {@link https://github.com/Automattic/kue#graceful-shutdown}
* @param {Function} [done] a callback to invoke on succes or failure
* @type {Function}
* @since 0.1.0
* @public
* @example
*
* const infopib = require('open311-infobip');
* infopib.stop();
*
*/
exports.stop = function stop(done) {
//ensure callback
if (!done && !_.isFunction(done)) {
done = noop;
}
//ensure queue safe shutdown
if (exports._queue) {
if (exports._queue.shuttingDown) {
done();
} else {
const { timeout } = exports.options;
exports._queue.shutdown(timeout, done);
}
} else {
done();
}
};
/**
* @name start
* @description setup infobip message(s) worker and start to process `infobip` jobs
* @type {Function}
* @since 0.1.0
* @public
* @example
*
* const infobip = require('open311-infobip');
* infobip.start();
*
*/
exports.start = function () {
//ensure infobip is initialized
exports.init();
//start worker for processing jobs
if (exports._queue) {
//reference open311-message model
const Message = mongoose.model('Message');
//register worker for processing message
//and send it as sms
const { concurrency } = exports.options;
exports._queue.process(exports.queueName, concurrency, Message.process);
//listen for process termination
//and gracefull shutdown infobip worker queue
process.once('SIGTERM', function ( /*signal*/ ) {
exports._queue.shutdown(function ( /*error*/ ) {
process.exit(0);
});
});
}
};