From 6e169768db8bc1dcd611f3c77a135242626f4cc5 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 31 May 2024 08:55:06 +0200 Subject: [PATCH] feat(salix): refs #6427 #6427 handle Send sms from app --- back/methods/sms/sendSms.js | 98 ++++++++++++++----- .../methods/sms/specs}/send.spec.js | 0 back/models/sms.js | 3 + {modules/client/back => back}/models/sms.json | 2 +- modules/client/back/methods/sms/send.js | 62 ------------ 5 files changed, 77 insertions(+), 88 deletions(-) rename {modules/client/back/methods/sms => back/methods/sms/specs}/send.spec.js (100%) create mode 100644 back/models/sms.js rename {modules/client/back => back}/models/sms.json (96%) delete mode 100644 modules/client/back/methods/sms/send.js diff --git a/back/methods/sms/sendSms.js b/back/methods/sms/sendSms.js index b50d9ec29..104dbe7b7 100644 --- a/back/methods/sms/sendSms.js +++ b/back/methods/sms/sendSms.js @@ -2,30 +2,78 @@ const got = require('got'); const isProduction = require('vn-loopback/server/boot/isProduction'); const {models} = require('vn-loopback/server/server'); -module.exports = async(destination, message) => { - const smsConfig = await models.SmsConfig.findOne(); - - const params = { - api_key: smsConfig.apiKey, - messages: [{ - from: smsConfig.title, - to: destination, - text: message - }] - }; - - let response; - try { - if (!isProduction(false)) - response = {result: [{status: 'ok'}]}; - else { - const jsonTest = { - json: params - }; - response = await got.post(smsConfig.uri, jsonTest).json(); +module.exports = Self => { + Self.remoteMethod('send', { + description: 'Sends SMS to a destination phone', + accessType: 'WRITE', + accepts: [ + { + arg: 'destination', + type: 'string', + required: true, + }, + { + arg: 'message', + type: 'string', + required: true, + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/send`, + verb: 'POST' } - } catch (e) { - response = e; - } - return response; + }); + Self.send = async(ctx, destination, message) => { + const smsConfig = await models.SmsConfig.findOne(); + const userId = ctx.req.accessToken.userId; + + if (destination.length == 9) { + const spainPrefix = '0034'; + destination = spainPrefix + destination; + } + + const params = { + api_key: smsConfig.apiKey, + messages: [{ + from: smsConfig.title, + to: destination, + text: message + }] + }; + + let response; + try { + if (!isProduction(false)) + response = {result: [{status: 'ok'}]}; + else { + const jsonTest = { + json: params + }; + response = await got.post(smsConfig.uri, jsonTest).json(); + } + } catch (e) { + console.error(e); + } + // return response; + const [result] = response.result; + const error = result.error_id; + + const newSms = { + senderFk: userId, + destination: destination, + message: message, + status: error + }; + + const sms = await Self.create(newSms); + + if (error) + throw new UserError(`We weren't able to send this SMS`); + + return sms; + }; }; diff --git a/modules/client/back/methods/sms/send.spec.js b/back/methods/sms/specs/send.spec.js similarity index 100% rename from modules/client/back/methods/sms/send.spec.js rename to back/methods/sms/specs/send.spec.js diff --git a/back/models/sms.js b/back/models/sms.js new file mode 100644 index 000000000..47a5b72d0 --- /dev/null +++ b/back/models/sms.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/sms/sendSms')(Self); +}; diff --git a/modules/client/back/models/sms.json b/back/models/sms.json similarity index 96% rename from modules/client/back/models/sms.json rename to back/models/sms.json index 4639131ef..14a3fba4f 100644 --- a/modules/client/back/models/sms.json +++ b/back/models/sms.json @@ -4,7 +4,7 @@ "base": "VnModel", "options": { "mysql": { - "table": "sms" + "table": "vn.sms" } }, "properties": { diff --git a/modules/client/back/methods/sms/send.js b/modules/client/back/methods/sms/send.js deleted file mode 100644 index 9694edf1b..000000000 --- a/modules/client/back/methods/sms/send.js +++ /dev/null @@ -1,62 +0,0 @@ -const UserError = require('vn-loopback/util/user-error'); -const sendSms = require('../../../../../back/methods/sms/sendSms'); - -module.exports = Self => { - Self.remoteMethod('send', { - description: 'Sends SMS to a destination phone', - accessType: 'WRITE', - accepts: [ - { - arg: 'destination', - type: 'string', - required: true, - }, - { - arg: 'message', - type: 'string', - required: true, - } - ], - returns: { - type: 'object', - root: true - }, - http: { - path: `/send`, - verb: 'POST' - } - }); - - Self.send = async(ctx, destination, message) => { - const userId = ctx.req.accessToken.userId; - - if (destination.length == 9) { - const spainPrefix = '0034'; - destination = spainPrefix + destination; - } - - let response; - try { - response = await sendSms(destination, message); - } catch (e) { - console.error(e); - } - - const [result] = response.result; - const error = result.error_id; - - const newSms = { - senderFk: userId, - destination: destination, - message: message, - status: error - }; - - const sms = await Self.create(newSms); - - if (error) - throw new UserError(`We weren't able to send this SMS`); - - return sms; - }; -};