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 got = require('got');
const xmlParser = require('xml2js').parseString;
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
@ -35,44 +34,27 @@ module.exports = Self => {
Self.send = async(ctx, destinationFk, destination, message) => { Self.send = async(ctx, destinationFk, destination, message) => {
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const smsConfig = await Self.app.models.SmsConfig.findOne(); const smsConfig = await Self.app.models.SmsConfig.findOne();
const soapClient = await soap.createClientAsync(smsConfig.uri);
const params = { const params = {
user: smsConfig.user, 'api_key': smsConfig.apiKey,
pass: smsConfig.password, 'mensage': [
src: smsConfig.title, {
dst: destination, 'from': '693474205',
msg: message 'to': destination,
'text': message
}
]
}; };
let xmlResponse;
let xmlResult;
let xmlParsed;
let status;
try { try {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production')
status = { params.fake = 1;
codigo: [200],
descripcion: ['Fake response'] const response = got.post(smsConfig.uri, params);
};
} 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;
}
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
const statusCode = status.codigo[0]; const statusCode = response.status;
const statusDescription = status.descripcion[0];
const newSms = { const newSms = {
senderFk: userId, senderFk: userId,