From cd7f0e3c24412948600ed3f46deb1811902e161f Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 14 Jun 2024 15:48:30 +0200 Subject: [PATCH] feat(Salix): refs #6427 #6427 unpublish send method --- back/methods/sms/send.js | 36 ++++----------------- back/methods/vn-user/recover-passwordSMS.js | 8 +++-- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/back/methods/sms/send.js b/back/methods/sms/send.js index 104dbe7b7..2d2a87311 100644 --- a/back/methods/sms/send.js +++ b/back/methods/sms/send.js @@ -3,33 +3,8 @@ const isProduction = require('vn-loopback/server/boot/isProduction'); const {models} = require('vn-loopback/server/server'); 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) => { + Self.send = async(senderFk, destination, message, options) => { const smsConfig = await models.SmsConfig.findOne(); - const userId = ctx.req.accessToken.userId; if (destination.length == 9) { const spainPrefix = '0034'; @@ -50,25 +25,26 @@ module.exports = Self => { if (!isProduction(false)) response = {result: [{status: 'ok'}]}; else { - const jsonTest = { + const body = { json: params }; - response = await got.post(smsConfig.uri, jsonTest).json(); + response = await got.post(smsConfig.uri, body).json(); } } catch (e) { console.error(e); } + if (!options?.insert) return; // return response; const [result] = response.result; const error = result.error_id; + if (senderFk) senderFk = ctx.req.accessToken.userId; const newSms = { - senderFk: userId, + senderFk, destination: destination, message: message, status: error }; - const sms = await Self.create(newSms); if (error) diff --git a/back/methods/vn-user/recover-passwordSMS.js b/back/methods/vn-user/recover-passwordSMS.js index 3eabd9514..1ee540b35 100644 --- a/back/methods/vn-user/recover-passwordSMS.js +++ b/back/methods/vn-user/recover-passwordSMS.js @@ -1,10 +1,10 @@ +const UserError = require('vn-loopback/util/user-error'); const isProduction = require('vn-loopback/server/boot/isProduction'); const authCode = require('../../models/authCode'); module.exports = Self => { Self.remoteMethod('recoverPasswordSMS', { description: 'Send SMS to the user', accepts: [ - { arg: 'user', type: 'string', @@ -40,10 +40,12 @@ module.exports = Self => { fields: ['id', 'name', 'recoveryPhone'], where: filter }); - if (!account) return; + if (!account && !verificationCode) return; user = account; if (verificationCode) { + if (!account) + throw new UserError('Invalid or expired verification code'); await Self.validateCode(user.name, verificationCode); return { @@ -55,7 +57,7 @@ module.exports = Self => { if (!isProduction()) { try { - await Self.app.models.Sms.send(+user.recoveryPhone, code); + await Self.app.models.Sms.send(null, +user.recoveryPhone, code, {insert: false}); } catch (e) { throw new UserError(`We weren't able to send this SMS`); }