fix: sms receipt data construction
This commit is contained in:
parent
9506b98b47
commit
154fe7affb
3 changed files with 47 additions and 45 deletions
|
|
@ -233,5 +233,6 @@ module.exports = {
|
|||
updateNetworkPerformance,
|
||||
updateNetworkHeartbeat,
|
||||
getNetworkPerformance,
|
||||
getNetworkHeartbeat
|
||||
getNetworkHeartbeat,
|
||||
getConfig
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ const router = express.Router()
|
|||
const semver = require('semver')
|
||||
const sms = require('../sms')
|
||||
const _ = require('lodash/fp')
|
||||
const BN = require('../bn')
|
||||
|
||||
const compliance = require('../compliance')
|
||||
const complianceTriggers = require('../compliance-triggers')
|
||||
|
|
@ -12,6 +13,10 @@ const txs = require('../new-admin/services/transactions')
|
|||
const httpError = require('../route-helpers').httpError
|
||||
const notifier = require('../notifier')
|
||||
const respond = require('../respond')
|
||||
const { getTx } = require('../new-admin/services/transactions.js')
|
||||
const { getCustomerById } = require('../customers')
|
||||
const machineLoader = require('../machine-loader')
|
||||
const { loadLatestConfig } = require('../new-settings-loader')
|
||||
|
||||
function updateCustomer (req, res, next) {
|
||||
const id = req.params.id
|
||||
|
|
@ -117,9 +122,46 @@ function updateTxCustomerPhoto (req, res, next) {
|
|||
.catch(next)
|
||||
}
|
||||
|
||||
function buildSms (data, receiptOptions) {
|
||||
return Promise.all([getTx(data.session, data.txClass), loadLatestConfig()])
|
||||
.then(([tx, config]) => {
|
||||
return Promise.all([getCustomerById(tx.customer_id), machineLoader.getMachine(tx.device_id, config)])
|
||||
.then(([customer, deviceConfig]) => {
|
||||
const formattedTx = _.mapKeys(_.camelCase)(tx)
|
||||
const localeConfig = configManager.getLocale(formattedTx.deviceId, config)
|
||||
const timezone = localeConfig.timezone.split(':')
|
||||
const dstOffset = timezone[1]
|
||||
|
||||
const cashInCommission = new BN(1).plus(new BN(formattedTx.commissionPercentage))
|
||||
|
||||
const rate = new BN(formattedTx.rawTickerPrice).multipliedBy(cashInCommission).decimalPlaces(2)
|
||||
const date = new Date()
|
||||
date.setMinutes(date.getMinutes() + parseInt(dstOffset))
|
||||
const dateString = `${date.toISOString().replace('T', ' ').slice(0, 19)}`
|
||||
|
||||
const data = {
|
||||
operatorInfo: configManager.getOperatorInfo(config),
|
||||
location: deviceConfig.machineLocation,
|
||||
customerName: customer.name,
|
||||
customerPhone: customer.phone,
|
||||
session: formattedTx.id,
|
||||
time: dateString,
|
||||
direction: formattedTx.direction === 'cashIn' ? 'Cash-in' : 'Cash-out',
|
||||
fiat: `${formattedTx.fiat.toString()} ${formattedTx.fiatCode}`,
|
||||
crypto: `${sms.toCryptoUnits(BN(formattedTx.cryptoAtoms), formattedTx.cryptoCode)} ${formattedTx.cryptoCode}`,
|
||||
rate: `1 ${formattedTx.cryptoCode} = ${rate} ${formattedTx.fiatCode}`,
|
||||
address: formattedTx.toAddress,
|
||||
txId: formattedTx.txHash
|
||||
}
|
||||
|
||||
return sms.formatSmsReceipt(data, receiptOptions)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function sendSmsReceipt (req, res, next) {
|
||||
const receiptOptions = _.omit(['active', 'sms'], configManager.getReceipt(req.settings.config))
|
||||
sms.buildSms(req.body.data, receiptOptions)
|
||||
buildSms(req.body.data, receiptOptions)
|
||||
.then(smsRequest => {
|
||||
sms.sendMessage(req.settings, smsRequest)
|
||||
.then(() => respond(req, res, {}))
|
||||
|
|
|
|||
45
lib/sms.js
45
lib/sms.js
|
|
@ -1,10 +1,5 @@
|
|||
const { BigNumber } = require('bignumber.js')
|
||||
const ph = require('./plugin-helper')
|
||||
const argv = require('minimist')(process.argv.slice(2))
|
||||
const { getTx } = require('./new-admin/services/transactions.js')
|
||||
const { getCustomerById } = require('./customers')
|
||||
const configManager = require('./new-config-manager')
|
||||
const machineLoader = require('./machine-loader')
|
||||
const { utils: coinUtils } = require('lamassu-coins')
|
||||
|
||||
function getPlugin (settings) {
|
||||
|
|
@ -32,43 +27,7 @@ function getLookup (settings, number) {
|
|||
}
|
||||
const toCryptoUnits = (cryptoAtoms, cryptoCode) => {
|
||||
const unitScale = coinUtils.getCryptoCurrency(cryptoCode).unitScale
|
||||
return cryptoAtoms.shift(-unitScale)
|
||||
}
|
||||
|
||||
function buildSms (data, receiptOptions) {
|
||||
return getTx(data.session, data.txClass)
|
||||
.then(tx => {
|
||||
Promise.all([getCustomerById(tx.customer_id), machineLoader.getConfig(), machineLoader.getMachine()])
|
||||
.then(([customer, config, deviceConfig]) => {
|
||||
const localeConfig = configManager.getLocale(tx.device_id, config)
|
||||
const timezone = localeConfig.timezone.split(':')
|
||||
const dstOffset = timezone[1]
|
||||
|
||||
const cashInCommission = BigNumber(1).add(BigNumber(tx.commissionPercentage))
|
||||
|
||||
const rate = BigNumber(tx.rawTickerPrice).mul(cashInCommission).round(2)
|
||||
const date = new Date()
|
||||
date.setMinutes(date.getMinutes() + parseInt(dstOffset))
|
||||
const dateString = `${date.toISOString().replace('T', ' ').slice(0, 19)}`
|
||||
|
||||
const data = {
|
||||
operatorInfo: configManager.getOperatorInfo(config),
|
||||
location: deviceConfig.machineLocation,
|
||||
customerName: customer.name,
|
||||
customerPhone: customer.phone,
|
||||
session: tx.id,
|
||||
time: dateString,
|
||||
direction: tx.direction === 'cashIn' ? 'Cash-in' : 'Cash-out',
|
||||
fiat: `${tx.fiat.toString()} ${tx.fiatCode}`,
|
||||
crypto: `${toCryptoUnits(tx.cryptoAtoms, tx.cryptoCode)} ${tx.cryptoCode}`,
|
||||
rate: `1 ${tx.cryptoCode} = ${rate} ${tx.fiatCode}`,
|
||||
address: tx.toAddress,
|
||||
txId: tx.txHash
|
||||
}
|
||||
|
||||
return formatSmsReceipt(data, receiptOptions)
|
||||
})
|
||||
})
|
||||
return cryptoAtoms.shiftedBy(-unitScale)
|
||||
}
|
||||
|
||||
function formatSmsReceipt (data, options) {
|
||||
|
|
@ -132,4 +91,4 @@ function formatSmsReceipt (data, options) {
|
|||
return request
|
||||
}
|
||||
|
||||
module.exports = { sendMessage, formatSmsReceipt, getLookup, buildSms }
|
||||
module.exports = { sendMessage, formatSmsReceipt, getLookup, toCryptoUnits }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue