added sms endpoint #1149
gitea/salix/dev This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-03-27 08:48:13 +01:00
parent 4e27581045
commit 56f352686c
25 changed files with 306 additions and 36 deletions

View File

@ -13,6 +13,12 @@ for file in changes/*.sql; do
mysql -u root -fc < $file
done
# Import changes for local db
for file in changes/only-local/*.sql; do
echo "[INFO] -> Imported ./$file"
mysql -u root -fc < $file
done
# Import fixtures
echo "[INFO] -> Imported ./dump/dumpedFixtures.sql"
mysql -u root -f < ./dump/dumpedFixtures.sql

View File

@ -0,0 +1 @@
INSERT INTO `vn`.`smsConfig` (`id`, `uri`, `user`, `password`, `title`) VALUES ('1', 'https://websms.xtratelecom.es/api_php/server.wsdl', 'FERRERTORIBIO', 'HERMANOS', 'Verdnatura');

View File

@ -0,0 +1,80 @@
const soap = require('soap');
const xmlParser = require('xml2js').parseString;
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('send', {
description: 'Sends SMS to a destination phone',
accepts: [{
arg: 'recipientFk',
type: 'Integer'
},
{
arg: 'recipient',
type: 'String',
required: true,
},
{
arg: 'message',
type: 'String',
required: true,
}],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/send`,
verb: 'POST'
}
});
Self.send = async(ctx, recipientFk, recipient, 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: recipient,
msg: message
};
let xmlResponse;
let xmlResult;
let xmlParsed;
let status;
try {
[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) {
console.error(e);
}
const newSms = {
senderFk: userId,
destinationFk: recipientFk || null,
destination: recipient,
message: message,
statusCode: status.codigo,
status: status.descripcion
};
const sms = Self.create(newSms);
if (status.codigo != 200)
throw new UserError(`We weren't able to send this SMS`);
return sms;
};
};

View File

@ -77,6 +77,12 @@
"ClientSample": {
"dataSource": "vn"
},
"Sms": {
"dataSource": "vn"
},
"SmsConfig": {
"dataSource": "vn"
},
"TpvError": {
"dataSource": "vn"
},

View File

@ -0,0 +1,29 @@
{
"name": "SmsConfig",
"description": "Sms configuration",
"base": "VnModel",
"options": {
"mysql": {
"table": "smsConfig"
}
},
"properties": {
"id": {
"type": "Number",
"id": true,
"description": "Identifier"
},
"uri": {
"type": "String"
},
"user": {
"type": "String"
},
"password": {
"type": "String"
},
"title": {
"type": "String"
}
}
}

View File

@ -0,0 +1,4 @@
module.exports = Self => {
// Methods
require('../methods/sms/send')(Self);
};

View File

@ -0,0 +1,51 @@
{
"name": "Sms",
"description": "Sms sent to client",
"base": "VnModel",
"options": {
"mysql": {
"table": "sms"
}
},
"properties": {
"id": {
"type": "Number",
"id": true,
"description": "Identifier"
},
"sender": {
"type": "String",
"required": true
},
"destination": {
"type": "String",
"required": true
},
"message": {
"type": "String",
"required": true
},
"statusCode": {
"type": "Number",
"required": true
},
"status": {
"type": "String"
},
"created": {
"type": "Date"
}
},
"relations": {
"sender": {
"type": "belongsTo",
"model": "Account",
"foreignKey": "senderFk"
},
"recipient": {
"type": "belongsTo",
"model": "Client",
"foreignKey": "destinationFk"
}
}
}

161
package-lock.json generated
View File

@ -2187,8 +2187,7 @@
"buffer-from": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
"dev": true
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A=="
},
"buffer-indexof": {
"version": "1.1.1",
@ -2838,7 +2837,6 @@
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz",
"integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==",
"dev": true,
"requires": {
"buffer-from": "^1.0.0",
"inherits": "^2.0.3",
@ -3665,17 +3663,17 @@
"integrity": "sha1-sXrtguirWeUt2cGbF1bg/BhyBMI="
},
"domhandler": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.4.1.tgz",
"integrity": "sha1-iS5HAAqZvlW783dP/qBWHYh5wlk=",
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.4.2.tgz",
"integrity": "sha512-JiK04h0Ht5u/80fdLMCEmV4zkNh2BcoMFBmZ/91WtYZ8qVXSKjiw7fXMgFPnHcSZgOo3XdinHvmnDUeMf5R4wA==",
"requires": {
"domelementtype": "1"
}
},
"domutils": {
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-1.6.2.tgz",
"integrity": "sha1-GVjMC0yUJuntNn+xyOhUiRsPo/8=",
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-1.7.0.tgz",
"integrity": "sha512-Lgd2XcJ/NjEw+7tFvfKxOzCYKZsdct5lczQ2ZaQY8Djz7pfAD3Gbp8ySJWtreII/vDlMVmxwa6pHmdxIYgttDg==",
"requires": {
"dom-serializer": "0",
"domelementtype": "1"
@ -4881,7 +4879,6 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.0.tgz",
"integrity": "sha1-zgtoVbRYU+eRsvzGgARtiCU91/U=",
"dev": true,
"requires": {
"debug": "2.6.9",
"encodeurl": "~1.0.1",
@ -4895,8 +4892,7 @@
"statuses": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz",
"integrity": "sha1-+vUbnrdKrvOzrPStX2Gr8ky3uT4=",
"dev": true
"integrity": "sha1-+vUbnrdKrvOzrPStX2Gr8ky3uT4="
}
}
},
@ -4953,6 +4949,11 @@
"parse-filepath": "^1.0.1"
}
},
"first-chunk-stream": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/first-chunk-stream/-/first-chunk-stream-0.1.0.tgz",
"integrity": "sha1-dV0+wU1JqG49L8wIvurVwMornAo="
},
"flagged-respawn": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/flagged-respawn/-/flagged-respawn-1.0.1.tgz",
@ -5180,7 +5181,8 @@
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"aproba": {
"version": "1.2.0",
@ -5201,12 +5203,14 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@ -5221,17 +5225,20 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"core-util-is": {
"version": "1.0.2",
@ -5348,7 +5355,8 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"ini": {
"version": "1.3.5",
@ -5360,6 +5368,7 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@ -5374,6 +5383,7 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@ -5381,12 +5391,14 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"minipass": {
"version": "2.2.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.1",
"yallist": "^3.0.0"
@ -5405,6 +5417,7 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@ -5485,7 +5498,8 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"object-assign": {
"version": "4.1.1",
@ -5497,6 +5511,7 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@ -5582,7 +5597,8 @@
"safe-buffer": {
"version": "5.1.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"safer-buffer": {
"version": "2.1.2",
@ -5618,6 +5634,7 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@ -5637,6 +5654,7 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@ -5680,12 +5698,14 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"yallist": {
"version": "3.0.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
}
}
},
@ -6799,16 +6819,41 @@
}
},
"htmlparser2": {
"version": "3.9.2",
"resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.9.2.tgz",
"integrity": "sha1-G9+HrMoPP55T+k/M6w9LTLsAszg=",
"version": "3.10.1",
"resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.10.1.tgz",
"integrity": "sha512-IgieNijUMbkDovyoKObU1DUhm1iwNYE/fuifEoEHfd1oZKZDaONBSkal7Y01shxsM49R4XaMdGez3WnF9UfiCQ==",
"requires": {
"domelementtype": "^1.3.0",
"domelementtype": "^1.3.1",
"domhandler": "^2.3.0",
"domutils": "^1.5.1",
"entities": "^1.1.1",
"inherits": "^2.0.1",
"readable-stream": "^2.0.2"
"readable-stream": "^3.1.1"
},
"dependencies": {
"domelementtype": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.1.tgz",
"integrity": "sha512-BSKB+TSpMpFI/HOxCNr1O8aMOTZ8hT3pM3GQ0w/mWRmkhEDSFJkkyzz4XQsBV44BChwGkrDfMyjVD0eA2aFV3w=="
},
"readable-stream": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.2.0.tgz",
"integrity": "sha512-RV20kLjdmpZuTF1INEb9IA3L68Nmi+Ri7ppZqo78wj//Pn62fCoJyV9zalccNzDD/OuJpMG4f+pfMl8+L6QdGw==",
"requires": {
"inherits": "^2.0.3",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
}
},
"string_decoder": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.2.0.tgz",
"integrity": "sha512-6YqyX6ZWEYguAxgZzHGL7SsCeGx3V2TtOTqZz1xSTSWnqsbWwbptafNyvf/ACquZUXV3DANr5BDIwNYe1mN42w==",
"requires": {
"safe-buffer": "~5.1.0"
}
}
}
},
"http-deceiver": {
@ -7492,8 +7537,7 @@
"is-utf8": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz",
"integrity": "sha1-Sw2hRCEE0bM2NA6AeX6GXPOffXI=",
"dev": true
"integrity": "sha1-Sw2hRCEE0bM2NA6AeX6GXPOffXI="
},
"is-valid-glob": {
"version": "1.0.0",
@ -10776,7 +10820,6 @@
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@ -10945,7 +10988,6 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@ -13612,6 +13654,26 @@
}
}
},
"soap": {
"version": "0.26.0",
"resolved": "https://registry.npmjs.org/soap/-/soap-0.26.0.tgz",
"integrity": "sha512-tTS3lnGl6lfjQQuJgNnWOgC0Xa6qYQSwl2G7DX3kCdGmek/FTNmHDM/7icKP1KBMFfCrKpAWEbZiGefa92SCYw==",
"requires": {
"bluebird": "^3.5.0",
"concat-stream": "^1.5.1",
"debug": "^2.6.9",
"ejs": "~2.5.5",
"finalhandler": "^1.0.3",
"httpntlm": "^1.5.2",
"lodash": "^4.17.5",
"request": ">=2.9.0",
"sax": ">=0.6",
"serve-static": "^1.11.1",
"strip-bom": "~0.3.1",
"uuid": "^3.1.0",
"xml-crypto": "~0.8.0"
}
},
"socket.io": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-2.1.1.tgz",
@ -14229,6 +14291,15 @@
"ansi-regex": "^2.0.0"
}
},
"strip-bom": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-0.3.1.tgz",
"integrity": "sha1-noo57/RW/5q8LwWfXyIluw8/fKU=",
"requires": {
"first-chunk-stream": "^0.1.0",
"is-utf8": "^0.2.0"
}
},
"strip-eof": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz",
@ -15138,8 +15209,7 @@
"typedarray": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
"integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=",
"dev": true
"integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c="
},
"uglify-js": {
"version": "3.4.9",
@ -16634,6 +16704,22 @@
"integrity": "sha1-SWsswQnsqNus/i3HK2A8F8WHCtQ=",
"dev": true
},
"xml-crypto": {
"version": "0.8.5",
"resolved": "https://registry.npmjs.org/xml-crypto/-/xml-crypto-0.8.5.tgz",
"integrity": "sha1-K7z7PrM/OoKiGLgiv2craxwg5Tg=",
"requires": {
"xmldom": "=0.1.19",
"xpath.js": ">=0.0.3"
},
"dependencies": {
"xmldom": {
"version": "0.1.19",
"resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.19.tgz",
"integrity": "sha1-Yx/Ad3bv2EEYvyUXGzftTQdaCrw="
}
}
},
"xml2js": {
"version": "0.4.19",
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz",
@ -16645,7 +16731,7 @@
},
"xmlbuilder": {
"version": "9.0.7",
"resolved": "http://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz",
"resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz",
"integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0="
},
"xmlcreate": {
@ -16665,6 +16751,11 @@
"integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4=",
"dev": true
},
"xpath.js": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/xpath.js/-/xpath.js-1.1.0.tgz",
"integrity": "sha512-jg+qkfS4K8E7965sqaUl8mRngXiKb3WZGfONgE18pr03FUQiuSV6G+Ej4tS55B+rIQSFEIw3phdVAQ4pPqNWfQ=="
},
"xregexp": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/xregexp/-/xregexp-2.0.0.tgz",

View File

@ -25,9 +25,11 @@
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"require-yaml": "0.0.1",
"soap": "^0.26.0",
"strong-error-handler": "^2.3.2",
"uuid": "^3.3.2",
"vn-loopback": "file:./loopback"
"vn-loopback": "file:./loopback",
"xml2js": "^0.4.19"
},
"devDependencies": {
"@babel/core": "^7.3.3",