fix: correct calculation to find expired transactions on customer history

This commit is contained in:
Sérgio Salgado 2021-09-10 16:16:34 +01:00 committed by Josh Harvey
parent 1c8098fe73
commit 146f67a0b7

View file

@ -3,6 +3,9 @@ const db = require('./db')
const BN = require('./bn')
const CashInTx = require('./cash-in/cash-in-tx')
const CashOutTx = require('./cash-out/cash-out-tx')
const T = require('./time')
const REDEEMABLE_AGE = T.day
function process (tx, pi) {
const mtx = massage(tx, pi)
@ -65,21 +68,23 @@ function cancel (txId) {
}
function customerHistory (customerId, thresholdDays) {
const sql = ` SELECT txIn.id, txIn.created, txIn.fiat, 'cashIn' AS direction
FROM cash_in_txs txIn
WHERE txIn.customer_id = $1
AND txIn.created > now() - interval $2
const sql = `SELECT * FROM (
SELECT txIn.id, txIn.created, txIn.fiat, 'cashIn' AS direction,
((NOT txIn.send_confirmed) AND (txIn.created <= now() - interval $3)) AS expired
FROM cash_in_txs txIn
WHERE txIn.customer_id = $1
AND txIn.created > now() - interval $2
UNION
SELECT txOut.id, txOut.created, txOut.fiat, 'cashOut' AS direction
FROM cash_out_txs txOut
WHERE txOut.customer_id = $1
AND txOut.created > now() - interval $2
AND timedout IS false
AND error_code IS DISTINCT FROM 'operatorCancel'
ORDER BY created;`
SELECT txOut.id, txOut.created, txOut.fiat, 'cashOut' AS direction,
(extract(epoch FROM (now() - greatest(txOut.created, txOut.confirmed_at))) * 1000) >= $4 AS expired
FROM cash_out_txs txOut
WHERE txOut.customer_id = $1
AND txOut.created > now() - interval $2
AND error_code IS DISTINCT FROM 'operatorCancel'
) ch WHERE NOT ch.expired ORDER BY ch.created`
const days = _.isNil(thresholdDays) ? 0 : thresholdDays
return db.any(sql, [customerId, `${days} days`])
return db.any(sql, [customerId, `${days} days`, '60 minutes', REDEEMABLE_AGE])
}
module.exports = {post, cancel, customerHistory}