Skip to content

Commit

Permalink
Merge pull request #781 from telefonicaid/task/send_sms_action_one_by…
Browse files Browse the repository at this point in the history
…_one

send sms actions one by one when multiple destinations
  • Loading branch information
mrutid authored Jun 13, 2024
2 parents d60ce17 + a2e0a56 commit 3ba843a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Send multiple sms when multiple destination in sms action
60 changes: 32 additions & 28 deletions lib/models/smsAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ function doIt(action, event, callback) {
if (options.sms && options.sms.from) {
from = options.sms.from;
}
msg = { to: buildTo(options.to), message: options.text, from: from };

metrics.IncMetrics(event.service, event.subservice, metrics.actionSMS);

var url = config.sms.URL;
var apiKey = config.sms.API_KEY;
var apiSecret = config.sms.API_SECRET;
Expand All @@ -89,33 +85,41 @@ function doIt(action, event, callback) {
apiSecret = options.sms.API_SECRET;
}
}
myutils.requestHelper(
'post',
{
url: url,
json: true,
body: msg,
headers: {
'User-Agent': 'request',
API_KEY: apiKey,
API_SECRET: apiSecret
}
},
function(err, data) {
myutils.logErrorIf(err, util.format('%s -> %s', url, msg.to));
if (err) {
metrics.IncMetrics(event.service, event.subservice, metrics.failedActionSMS);
metrics.IncMetrics(event.service, event.subservice, metrics.actionSMS);

alarm.raise(alarm.SMS);
} else {
metrics.IncMetrics(event.service, event.subservice, metrics.okActionSMS);
var tos = buildTo(options.to);

alarm.release(alarm.SMS);
for (var singleto of tos) {
msg = { to: [singleto], message: options.text, from: config.sms.from };
myutils.requestHelper(
'post',
{
url: url,
json: true,
body: msg,
headers: {
'User-Agent': 'request',
API_KEY: apiKey,
API_SECRET: apiSecret
}
},
function(err, data) { // jshint ignore:line
myutils.logErrorIf(err, util.format('%s -> %s', url, msg.to));
if (err) {
metrics.IncMetrics(event.service, event.subservice, metrics.failedActionSMS);
alarm.raise(alarm.SMS);
} else {
metrics.IncMetrics(event.service, event.subservice, metrics.okActionSMS);
alarm.release(alarm.SMS);
}
logger.info('smsAction.SendSMS done url:%s result:%j, msg: %j', url, err || data, msg);
if (tos[tos.length - 1] === singleto) {
// ensure callback is returned just one time
return callback(err, data);
}
}
logger.info('smsAction.SendSMS done url:%s result:%j, msg: %j', url, err || data, msg);
return callback(err, data);
}
);
);
}
} catch (ex) {
metrics.IncMetrics(event.service, event.subservice, metrics.failedActionSMS);

Expand Down

0 comments on commit 3ba843a

Please sign in to comment.