From 154fe7affbc34b8209eca839e46ba0622706fd1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Fri, 8 Oct 2021 11:58:21 +0100 Subject: [PATCH] fix: sms receipt data construction --- lib/machine-loader.js | 3 ++- lib/routes/customerRoutes.js | 44 ++++++++++++++++++++++++++++++++++- lib/sms.js | 45 ++---------------------------------- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/lib/machine-loader.js b/lib/machine-loader.js index 5ce26a5a..41838bde 100644 --- a/lib/machine-loader.js +++ b/lib/machine-loader.js @@ -233,5 +233,6 @@ module.exports = { updateNetworkPerformance, updateNetworkHeartbeat, getNetworkPerformance, - getNetworkHeartbeat + getNetworkHeartbeat, + getConfig } diff --git a/lib/routes/customerRoutes.js b/lib/routes/customerRoutes.js index cccb9554..1093a91d 100644 --- a/lib/routes/customerRoutes.js +++ b/lib/routes/customerRoutes.js @@ -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, {})) diff --git a/lib/sms.js b/lib/sms.js index ed2de98b..597f5bd1 100644 --- a/lib/sms.js +++ b/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 }