From 1983eb012bf11262acaa5d5fae00f1d3f3f62e6d Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 10 Jan 2022 12:28:39 +0100 Subject: [PATCH] refactor(sms): got and apiKey --- db/changes/10410-noname/00-smsConfig.sql | 12 +++++++ modules/client/back/methods/sms/send.js | 46 ++++++++---------------- 2 files changed, 26 insertions(+), 32 deletions(-) create mode 100644 db/changes/10410-noname/00-smsConfig.sql diff --git a/db/changes/10410-noname/00-smsConfig.sql b/db/changes/10410-noname/00-smsConfig.sql new file mode 100644 index 000000000..0798bcd31 --- /dev/null +++ b/db/changes/10410-noname/00-smsConfig.sql @@ -0,0 +1,12 @@ +ALTER TABLE `vn`.`smsConfig` ADD apiKey varchar(50) NULL; + +UPDATE `vn`.`smsConfig` + SET uri='https://api.gateway360.com/api/3.0/sms/send' + WHERE id=1; + +UPDATE `vn`.`smsConfig` + SET apiKey='5715476da95b46d686a5a255e6459523' + WHERE id=1; + +ALTER TABLE `vn`.`smsConfig` CHANGE `user` user__ varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL; +ALTER TABLE `vn`.`smsConfig` CHANGE password password__ varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL; \ No newline at end of file diff --git a/modules/client/back/methods/sms/send.js b/modules/client/back/methods/sms/send.js index c5cb6ace2..40a49ce1d 100644 --- a/modules/client/back/methods/sms/send.js +++ b/modules/client/back/methods/sms/send.js @@ -1,5 +1,4 @@ -const soap = require('soap'); -const xmlParser = require('xml2js').parseString; +const got = require('got'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { @@ -35,44 +34,27 @@ module.exports = Self => { Self.send = async(ctx, destinationFk, destination, message) => { const userId = ctx.req.accessToken.userId; const smsConfig = await Self.app.models.SmsConfig.findOne(); - const soapClient = await soap.createClientAsync(smsConfig.uri); const params = { - user: smsConfig.user, - pass: smsConfig.password, - src: smsConfig.title, - dst: destination, - msg: message + 'api_key': smsConfig.apiKey, + 'mensage': [ + { + 'from': '693474205', + 'to': destination, + 'text': message + } + ] }; - let xmlResponse; - let xmlResult; - let xmlParsed; - let status; - try { - if (process.env.NODE_ENV !== 'production') { - status = { - codigo: [200], - descripcion: ['Fake response'] - }; - } else { - [xmlResponse] = await soapClient.sendSMSAsync(params); - xmlResult = xmlResponse.result.$value; - xmlParsed = await new Promise((resolve, reject) => { - xmlParser(xmlResult, (err, result) => { - if (err) - reject(err); - resolve(result); - }); - }); - [status] = xmlParsed['xtratelecom-sms-response'].sms; - } + if (process.env.NODE_ENV !== 'production') + params.fake = 1; + + const response = got.post(smsConfig.uri, params); } catch (e) { console.error(e); } - const statusCode = status.codigo[0]; - const statusDescription = status.descripcion[0]; + const statusCode = response.status; const newSms = { senderFk: userId,