refactor(sms): got and apiKey
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2022-01-10 12:28:39 +01:00
parent 36bac4725d
commit 1983eb012b
2 changed files with 26 additions and 32 deletions

View File

@ -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;

View File

@ -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,