From abf4dd5c3225e56fe9e0089ca13bce0b7686e484 Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Fri, 31 May 2019 11:43:28 -0300 Subject: [PATCH] Splitting up daily volume queries --- lib/customers.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/customers.js b/lib/customers.js index 6bc71db7..93ccd5aa 100644 --- a/lib/customers.js +++ b/lib/customers.js @@ -117,22 +117,38 @@ function getById (id, userToken) { * @returns {Bignumber} Customer's daily volume */ 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 - ${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 - ${txId ? 'and id!=$2' : ''} - and created > now() - interval '1 day'`, [id, txId]) - ]).then(([cashIn, cashOut]) => { + const queries = txId ? getDailyVolumeMinusCurrentTxQueries(id, txId) : getDailyVolumeQueries(id) + return Promise.all(queries).then(([cashIn, cashOut]) => { const dailyVolume = BN(cashIn.total).add(cashOut.total) const hoursTillLimitClear = getHoursTillLimitClear(cashIn.maxdate, cashOut.maxdate) return { dailyVolume, hoursTillLimitClear } }) } +function getDailyVolumeQueries (id) { + return [ + 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]), + 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]) + ] +} + +function getDailyVolumeMinusCurrentTxQueries (id, txId) { + return [ + db.one(`select coalesce(sum(fiat), 0) as total, max(created) as maxdate from cash_in_txs + where customer_id=$1 + 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 id!=$2 + and created > now() - interval '1 day'`, [id, txId]) + ] +} + function getHoursTillLimitClear (cashInDate, cashOutDate) { let startDate = moment() startDate = startDate.subtract(1, 'days')