From ed7c51599319a19c2dbb572d345094765167f0cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Mon, 30 Aug 2021 16:02:23 +0100 Subject: [PATCH] refactor: unify sms creation into a single function --- lib/plugins.js | 14 +++++++------- lib/sms.js | 36 ++++++++++++------------------------ 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index 8c59b218..5b38eb7a 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -361,15 +361,15 @@ function plugins (settings, deviceId) { const phone = tx.phone const timestamp = dateFormat(new Date(), 'UTC:HH:MM Z') - return sms.getCashOutReadySms(phone, timestamp) - .then(msg => { + return sms.getSms('cash_out_dispense_ready', phone, { timestamp }) + .then(smsObj => { const rec = { - sms: msg + sms: smsObj } return sms.sendMessage(settings, rec) .then(() => { - const sql = 'update cash_out_txs set notified=$1 where id=$2' + const sql = 'UPDATE cash_out_txs SET notified=$1 WHERE id=$2' const values = [true, tx.id] return db.none(sql, values) @@ -723,10 +723,10 @@ function plugins (settings, deviceId) { ? '123' : randomCode() - return sms.getPhoneCodeSms(phone, code) - .then(msg => { + return sms.getSms('sms_code', phone, { code }) + .then(smsObj => { const rec = { - sms: msg + sms: smsObj } return sms.sendMessage(settings, rec) diff --git a/lib/sms.js b/lib/sms.js index 9d6e66cd..71b691c2 100644 --- a/lib/sms.js +++ b/lib/sms.js @@ -5,11 +5,18 @@ const _ = require('lodash/fp') const customSms = require('./custom-sms') -function getPhoneCodeSms (phone, code) { - return customSms.getCustomMessage('sms_code') +const getDefaultMessageContent = content => ({ + smsCode: `Your cryptomat code: ${content.code}`, + cashOutDispenseReady: `Your cash is waiting! Go to the Cryptomat and press Redeem within 24 hours. [${content.timestamp}]` +}) + +function getSms (event, phone, content) { + return customSms.getCustomMessage(event) .then(msg => { if (!_.isNil(msg)) { - const messageContent = _.replace('#code', code, msg.message) + var accMsg = msg.message + const contentKeys = _.keys(content) + const messageContent = _.reduce((acc, it) => _.replace(`#${it}`, content[it], acc), accMsg, contentKeys) return { toNumber: phone, body: messageContent @@ -18,25 +25,7 @@ function getPhoneCodeSms (phone, code) { return { toNumber: phone, - body: `Your cryptomat code: ${code}` - } - }) -} - -function getCashOutReadySms (phone, timestamp) { - return customSms.getCustomMessage('cash_out_dispense_ready') - .then(msg => { - if (!_.isNil(msg)) { - const messageContent = _.replace('#timestamp', timestamp, msg.message) - return { - toNumber: phone, - body: messageContent - } - } - - return { - toNumber: phone, - body: `Your cash is waiting! Go to the Cryptomat and press Redeem within 24 hours. [${timestamp}]` + body: getDefaultMessageContent(content)[_.camelCase(event)] } }) } @@ -131,8 +120,7 @@ function formatSmsReceipt (data, options) { } module.exports = { - getPhoneCodeSms, - getCashOutReadySms, + getSms, sendMessage, getLookup, formatSmsReceipt,