refactor: unify sms creation into a single function
This commit is contained in:
parent
ee8c46b51e
commit
ed7c515993
2 changed files with 19 additions and 31 deletions
|
|
@ -361,15 +361,15 @@ function plugins (settings, deviceId) {
|
||||||
|
|
||||||
const phone = tx.phone
|
const phone = tx.phone
|
||||||
const timestamp = dateFormat(new Date(), 'UTC:HH:MM Z')
|
const timestamp = dateFormat(new Date(), 'UTC:HH:MM Z')
|
||||||
return sms.getCashOutReadySms(phone, timestamp)
|
return sms.getSms('cash_out_dispense_ready', phone, { timestamp })
|
||||||
.then(msg => {
|
.then(smsObj => {
|
||||||
const rec = {
|
const rec = {
|
||||||
sms: msg
|
sms: smsObj
|
||||||
}
|
}
|
||||||
|
|
||||||
return sms.sendMessage(settings, rec)
|
return sms.sendMessage(settings, rec)
|
||||||
.then(() => {
|
.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]
|
const values = [true, tx.id]
|
||||||
|
|
||||||
return db.none(sql, values)
|
return db.none(sql, values)
|
||||||
|
|
@ -723,10 +723,10 @@ function plugins (settings, deviceId) {
|
||||||
? '123'
|
? '123'
|
||||||
: randomCode()
|
: randomCode()
|
||||||
|
|
||||||
return sms.getPhoneCodeSms(phone, code)
|
return sms.getSms('sms_code', phone, { code })
|
||||||
.then(msg => {
|
.then(smsObj => {
|
||||||
const rec = {
|
const rec = {
|
||||||
sms: msg
|
sms: smsObj
|
||||||
}
|
}
|
||||||
|
|
||||||
return sms.sendMessage(settings, rec)
|
return sms.sendMessage(settings, rec)
|
||||||
|
|
|
||||||
32
lib/sms.js
32
lib/sms.js
|
|
@ -5,29 +5,18 @@ const _ = require('lodash/fp')
|
||||||
|
|
||||||
const customSms = require('./custom-sms')
|
const customSms = require('./custom-sms')
|
||||||
|
|
||||||
function getPhoneCodeSms (phone, code) {
|
const getDefaultMessageContent = content => ({
|
||||||
return customSms.getCustomMessage('sms_code')
|
smsCode: `Your cryptomat code: ${content.code}`,
|
||||||
.then(msg => {
|
cashOutDispenseReady: `Your cash is waiting! Go to the Cryptomat and press Redeem within 24 hours. [${content.timestamp}]`
|
||||||
if (!_.isNil(msg)) {
|
|
||||||
const messageContent = _.replace('#code', code, msg.message)
|
|
||||||
return {
|
|
||||||
toNumber: phone,
|
|
||||||
body: messageContent
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
toNumber: phone,
|
|
||||||
body: `Your cryptomat code: ${code}`
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
function getCashOutReadySms (phone, timestamp) {
|
function getSms (event, phone, content) {
|
||||||
return customSms.getCustomMessage('cash_out_dispense_ready')
|
return customSms.getCustomMessage(event)
|
||||||
.then(msg => {
|
.then(msg => {
|
||||||
if (!_.isNil(msg)) {
|
if (!_.isNil(msg)) {
|
||||||
const messageContent = _.replace('#timestamp', timestamp, msg.message)
|
var accMsg = msg.message
|
||||||
|
const contentKeys = _.keys(content)
|
||||||
|
const messageContent = _.reduce((acc, it) => _.replace(`#${it}`, content[it], acc), accMsg, contentKeys)
|
||||||
return {
|
return {
|
||||||
toNumber: phone,
|
toNumber: phone,
|
||||||
body: messageContent
|
body: messageContent
|
||||||
|
|
@ -36,7 +25,7 @@ function getCashOutReadySms (phone, timestamp) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
toNumber: phone,
|
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 = {
|
module.exports = {
|
||||||
getPhoneCodeSms,
|
getSms,
|
||||||
getCashOutReadySms,
|
|
||||||
sendMessage,
|
sendMessage,
|
||||||
getLookup,
|
getLookup,
|
||||||
formatSmsReceipt,
|
formatSmsReceipt,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue