Merge pull request '3490-sms' (#844) from 3490-sms into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #844 Reviewed-by: Carlos Jimenez Ruiz <carlosjr@verdnatura.es>
This commit is contained in:
commit
2d94993423
|
@ -120,7 +120,7 @@ module.exports = Self => {
|
|||
}
|
||||
|
||||
const defaultOptions = {
|
||||
body: params
|
||||
form: params
|
||||
};
|
||||
|
||||
if (options) Object.assign(defaultOptions, options);
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
ALTER TABLE `vn`.`smsConfig` ADD apiKey varchar(50) NULL;
|
||||
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;
|
||||
ALTER TABLE `vn`.`sms` MODIFY COLUMN statusCode smallint(9) DEFAULT 0 NULL;
|
||||
ALTER TABLE `vn`.`sms` MODIFY COLUMN status varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT 'OK' NULL;
|
|
@ -0,0 +1,7 @@
|
|||
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;
|
|
@ -1906,9 +1906,9 @@ INSERT INTO `postgresql`.`calendar_employee` (`business_id`, `calendar_state_id`
|
|||
(1107, 1, IF(MONTH(CURDATE()) >= 1 AND DAY(CURDATE()) > 20, DATE_ADD(CURDATE(), INTERVAL -14 DAY), DATE_ADD(CURDATE(), INTERVAL 9 DAY))),
|
||||
(1107, 2, IF(MONTH(CURDATE()) >= 1 AND DAY(CURDATE()) > 20, DATE_ADD(CURDATE(), INTERVAL -15 DAY), DATE_ADD(CURDATE(), INTERVAL 7 DAY)));
|
||||
|
||||
INSERT INTO `vn`.`smsConfig` (`id`, `uri`, `title`)
|
||||
INSERT INTO `vn`.`smsConfig` (`id`, `uri`, `title`, `apiKey`)
|
||||
VALUES
|
||||
('1', 'https://websms.xtratelecom.es/api_php/server.wsdl', 'Verdnatura');
|
||||
('1', 'https://api.gateway360.com/api/3.0/sms/send', 'Verdnatura', '5715476da95b46d686a5a255e6459523');
|
||||
|
||||
INSERT INTO `vn`.`sharingClient`(`id`, `workerFk`, `started`, `ended`, `clientFk`)
|
||||
VALUES
|
||||
|
|
|
@ -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,57 +34,48 @@ 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);
|
||||
|
||||
if (destination.length == 9) {
|
||||
const spainPrefix = '0034';
|
||||
destination = spainPrefix + destination;
|
||||
}
|
||||
|
||||
const params = {
|
||||
user: smsConfig.user,
|
||||
pass: smsConfig.password,
|
||||
src: smsConfig.title,
|
||||
dst: destination,
|
||||
msg: message
|
||||
api_key: smsConfig.apiKey,
|
||||
messages: [{
|
||||
from: smsConfig.title,
|
||||
to: destination,
|
||||
text: message
|
||||
}]
|
||||
};
|
||||
|
||||
let xmlResponse;
|
||||
let xmlResult;
|
||||
let xmlParsed;
|
||||
let status;
|
||||
|
||||
let response;
|
||||
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 jsonTest = {
|
||||
json: params
|
||||
};
|
||||
response = await got.post(smsConfig.uri, jsonTest).json();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
const statusCode = status.codigo[0];
|
||||
const statusDescription = status.descripcion[0];
|
||||
const [result] = response.result;
|
||||
const error = result.error_id;
|
||||
|
||||
const newSms = {
|
||||
senderFk: userId,
|
||||
destinationFk: destinationFk || null,
|
||||
destination: destination,
|
||||
message: message,
|
||||
statusCode: statusCode,
|
||||
status: statusDescription
|
||||
status: error
|
||||
};
|
||||
|
||||
const sms = await Self.create(newSms);
|
||||
|
||||
if (statusCode != 200)
|
||||
if (error)
|
||||
throw new UserError(`We weren't able to send this SMS`);
|
||||
|
||||
return sms;
|
||||
|
|
|
@ -1,14 +1,10 @@
|
|||
const app = require('vn-loopback/server/server');
|
||||
const soap = require('soap');
|
||||
|
||||
describe('sms send()', () => {
|
||||
it('should return the expected message and status code', async() => {
|
||||
const code = 200;
|
||||
spyOn(soap, 'createClientAsync').and.returnValue('a so fake client');
|
||||
let ctx = {req: {accessToken: {userId: 1}}};
|
||||
let result = await app.models.Sms.send(ctx, 1105, 'destination', 'My SMS Body');
|
||||
it('should not return status error', async() => {
|
||||
const ctx = {req: {accessToken: {userId: 1}}};
|
||||
const result = await app.models.Sms.send(ctx, 1105, '123456789', 'My SMS Body');
|
||||
|
||||
expect(result.statusCode).toEqual(code);
|
||||
expect(result.status).toContain('Fake response');
|
||||
expect(result.status).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -284,7 +284,7 @@ module.exports = Self => {
|
|||
replyTo: worker.email
|
||||
};
|
||||
await got.get(`${origin}/api/email/payment-update`, {
|
||||
query: params
|
||||
searchParams: params
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -16,10 +16,7 @@
|
|||
"uri": {
|
||||
"type": "String"
|
||||
},
|
||||
"user": {
|
||||
"type": "String"
|
||||
},
|
||||
"password": {
|
||||
"apiKey": {
|
||||
"type": "String"
|
||||
},
|
||||
"title": {
|
||||
|
|
|
@ -26,8 +26,7 @@
|
|||
"required": true
|
||||
},
|
||||
"statusCode": {
|
||||
"type": "Number",
|
||||
"required": true
|
||||
"type": "Number"
|
||||
},
|
||||
"status": {
|
||||
"type": "String"
|
||||
|
|
|
@ -58,7 +58,7 @@ module.exports = Self => {
|
|||
}, myOptions);
|
||||
|
||||
const response = got.stream(`${origin}/api/report/invoice`, {
|
||||
query: {
|
||||
searchParams: {
|
||||
authorization: auth.id,
|
||||
invoiceId: id
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -15,7 +15,7 @@
|
|||
"bmp-js": "^0.1.0",
|
||||
"compression": "^1.7.3",
|
||||
"fs-extra": "^5.0.0",
|
||||
"got": "^6.7.1",
|
||||
"got": "^10.7.0",
|
||||
"helmet": "^3.21.2",
|
||||
"i18n": "^0.8.4",
|
||||
"image-type": "^4.1.0",
|
||||
|
|
Loading…
Reference in New Issue