Splitting up daily volume queries

This commit is contained in:
Rafael Taranto 2019-05-31 11:43:28 -03:00 committed by Josh Harvey
parent 18bbfc6def
commit abf4dd5c32

View file

@ -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')