Prevent compliance from triggering prematurely

This commit is contained in:
Rafael Taranto 2019-05-31 10:37:10 -03:00 committed by Josh Harvey
parent 09332c37db
commit 18bbfc6def
2 changed files with 12 additions and 8 deletions

View file

@ -69,7 +69,7 @@ function get (phone) {
* *
* @returns {Promise} Newly updated Customer * @returns {Promise} Newly updated Customer
*/ */
function update (id, data, userToken) { function update (id, data, userToken, txId) {
const formattedData = _.omit(['id'], _.mapKeys(_.snakeCase, data)) const formattedData = _.omit(['id'], _.mapKeys(_.snakeCase, data))
const updateData = enhanceAtFields(enhanceOverrideFields(formattedData, userToken)) const updateData = enhanceAtFields(enhanceOverrideFields(formattedData, userToken))
@ -81,7 +81,7 @@ function update (id, data, userToken) {
.then(addComplianceOverrides(id, updateData, userToken)) .then(addComplianceOverrides(id, updateData, userToken))
.then(populateOverrideUsernames) .then(populateOverrideUsernames)
.then(computeStatus) .then(computeStatus)
.then(populateDailyVolume) .then((it) => populateDailyVolume(it, txId))
.then(camelize) .then(camelize)
} }
@ -113,16 +113,19 @@ function getById (id, userToken) {
* @function * @function
* *
* @param {string} id Customer's id * @param {string} id Customer's id
* @param {string} txId current tx, to be ignored in the query
* @returns {Bignumber} Customer's daily volume * @returns {Bignumber} Customer's daily volume
*/ */
function getDailyVolume (id) { function getDailyVolume (id, txId) {
return Promise.all([ return Promise.all([
db.one(`select coalesce(sum(fiat), 0) as total, max(created) as maxdate from cash_in_txs db.one(`select coalesce(sum(fiat), 0) as total, max(created) as maxdate from cash_in_txs
where customer_id=$1 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 db.one(`select coalesce(sum(fiat), 0) as total, max(created) as maxdate from cash_out_txs
where customer_id=$1 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]) => { ]).then(([cashIn, cashOut]) => {
const dailyVolume = BN(cashIn.total).add(cashOut.total) const dailyVolume = BN(cashIn.total).add(cashOut.total)
const hoursTillLimitClear = getHoursTillLimitClear(cashIn.maxdate, cashOut.maxdate) const hoursTillLimitClear = getHoursTillLimitClear(cashIn.maxdate, cashOut.maxdate)
@ -167,9 +170,9 @@ function camelize (customer) {
* @param {object} customer Customer object * @param {object} customer Customer object
* @returns {object} Customer object populated with dailyVolume * @returns {object} Customer object populated with dailyVolume
*/ */
function populateDailyVolume (customer) { function populateDailyVolume (customer, txId) {
if (!customer) return 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) let withHours = _.set('hours_till_limit_clear', hoursTillLimitClear, customer)
return _.set('daily_volume', dailyVolume, withHours) return _.set('daily_volume', dailyVolume, withHours)
}) })

View file

@ -221,6 +221,7 @@ function getCustomerWithPhoneCode (req, res, next) {
function updateCustomer (req, res, next) { function updateCustomer (req, res, next) {
const id = req.params.id const id = req.params.id
const txId = req.query.txId
const patch = req.body const patch = req.body
const config = configManager.unscoped(req.settings.config) const config = configManager.unscoped(req.settings.config)
@ -232,7 +233,7 @@ function updateCustomer (req, res, next) {
return compliance.validationPatch(req.deviceId, config, mergedCustomer) return compliance.validationPatch(req.deviceId, config, mergedCustomer)
.then(_.merge(patch)) .then(_.merge(patch))
.then(newPatch => customers.updatePhotoCard(id, newPatch)) .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 })) .then(customer => respond(req, res, { customer }))
.catch(next) .catch(next)