diff --git a/lib/customers.js b/lib/customers.js index 59fc08c4..6bc71db7 100644 --- a/lib/customers.js +++ b/lib/customers.js @@ -69,7 +69,7 @@ function get (phone) { * * @returns {Promise} Newly updated Customer */ -function update (id, data, userToken) { +function update (id, data, userToken, txId) { const formattedData = _.omit(['id'], _.mapKeys(_.snakeCase, data)) const updateData = enhanceAtFields(enhanceOverrideFields(formattedData, userToken)) @@ -81,7 +81,7 @@ function update (id, data, userToken) { .then(addComplianceOverrides(id, updateData, userToken)) .then(populateOverrideUsernames) .then(computeStatus) - .then(populateDailyVolume) + .then((it) => populateDailyVolume(it, txId)) .then(camelize) } @@ -113,16 +113,19 @@ function getById (id, userToken) { * @function * * @param {string} id Customer's id + * @param {string} txId current tx, to be ignored in the query * @returns {Bignumber} Customer's daily volume */ -function getDailyVolume (id) { +function getDailyVolume (id, txId) { return Promise.all([ db.one(`select coalesce(sum(fiat), 0) as total, max(created) as maxdate from cash_in_txs where customer_id=$1 - and created > now() - interval '1 day'`, [id]), + ${txId ? 'and id!=$2' : ''} + and created > now() - interval '1 day'`, [id, txId]), db.one(`select coalesce(sum(fiat), 0) as total, max(created) as maxdate from cash_out_txs where customer_id=$1 - and created > now() - interval '1 day'`, [id]) + ${txId ? 'and id!=$2' : ''} + and created > now() - interval '1 day'`, [id, txId]) ]).then(([cashIn, cashOut]) => { const dailyVolume = BN(cashIn.total).add(cashOut.total) const hoursTillLimitClear = getHoursTillLimitClear(cashIn.maxdate, cashOut.maxdate) @@ -167,9 +170,9 @@ function camelize (customer) { * @param {object} customer Customer object * @returns {object} Customer object populated with dailyVolume */ -function populateDailyVolume (customer) { +function populateDailyVolume (customer, txId) { if (!customer) return - return getDailyVolume(customer.id).then(({ dailyVolume, hoursTillLimitClear }) => { + return getDailyVolume(customer.id, txId).then(({ dailyVolume, hoursTillLimitClear }) => { let withHours = _.set('hours_till_limit_clear', hoursTillLimitClear, customer) return _.set('daily_volume', dailyVolume, withHours) }) diff --git a/lib/routes.js b/lib/routes.js index 2460e65b..ff7d508c 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -221,6 +221,7 @@ function getCustomerWithPhoneCode (req, res, next) { function updateCustomer (req, res, next) { const id = req.params.id + const txId = req.query.txId const patch = req.body const config = configManager.unscoped(req.settings.config) @@ -232,7 +233,7 @@ function updateCustomer (req, res, next) { return compliance.validationPatch(req.deviceId, config, mergedCustomer) .then(_.merge(patch)) .then(newPatch => customers.updatePhotoCard(id, newPatch)) - .then(newPatch => customers.update(id, newPatch)) + .then(newPatch => customers.update(id, newPatch, null, txId)) }) .then(customer => respond(req, res, { customer })) .catch(next)