#6427 - SMS Recover Password #2037

Open
jsegarra wants to merge 72 commits from 6427_sms_resetPassword into dev
5 changed files with 77 additions and 88 deletions
Showing only changes of commit 6e169768db - Show all commits

View File

@ -2,30 +2,78 @@ const got = require('got');
const isProduction = require('vn-loopback/server/boot/isProduction'); const isProduction = require('vn-loopback/server/boot/isProduction');
const {models} = require('vn-loopback/server/server'); const {models} = require('vn-loopback/server/server');
module.exports = async(destination, message) => { module.exports = Self => {
const smsConfig = await models.SmsConfig.findOne(); Self.remoteMethod('send', {
description: 'Sends SMS to a destination phone',
const params = { accessType: 'WRITE',
api_key: smsConfig.apiKey, accepts: [
messages: [{ {
from: smsConfig.title, arg: 'destination',
to: destination, type: 'string',
text: message required: true,
}] },
}; {
arg: 'message',
let response; type: 'string',
try { required: true,
if (!isProduction(false)) }
response = {result: [{status: 'ok'}]}; ],
else { returns: {
const jsonTest = { type: 'object',
json: params root: true
}; },
response = await got.post(smsConfig.uri, jsonTest).json(); http: {
path: `/send`,
verb: 'POST'
} }
} catch (e) { });
response = e; Self.send = async(ctx, destination, message) => {
} const smsConfig = await models.SmsConfig.findOne();
return response; 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;
};
}; };

3
back/models/sms.js Normal file
View File

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

View File

@ -4,7 +4,7 @@
"base": "VnModel", "base": "VnModel",
"options": { "options": {
"mysql": { "mysql": {
"table": "sms" "table": "vn.sms"
} }
}, },
"properties": { "properties": {

View File

@ -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;
};
};