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 c74835659b
commit 6975156c45

View file

@ -3,6 +3,9 @@ const db = require('./db')
const BN = require('./bn') const BN = require('./bn')
const CashInTx = require('./cash-in/cash-in-tx') const CashInTx = require('./cash-in/cash-in-tx')
const CashOutTx = require('./cash-out/cash-out-tx') const CashOutTx = require('./cash-out/cash-out-tx')
const T = require('./time')
const REDEEMABLE_AGE = T.day
function process (tx, pi) { function process (tx, pi) {
const mtx = massage(tx, pi) const mtx = massage(tx, pi)
@ -64,24 +67,27 @@ function cancel (txId) {
}) })
} }
function customerHistory (customerId, thresholdDays) { function customerHistory (customerId, thresholdDays) {
const sql = ` SELECT txIn.id, txIn.created, txIn.fiat, 'cashIn' AS direction 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 FROM cash_in_txs txIn
WHERE txIn.customer_id = $1 WHERE txIn.customer_id = $1
AND txIn.created > now() - interval $2 AND txIn.created > now() - interval $2
AND fiat > 0 AND fiat > 0
UNION UNION
SELECT txOut.id, txOut.created, txOut.fiat, 'cashOut' AS direction 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 FROM cash_out_txs txOut
WHERE txOut.customer_id = $1 WHERE txOut.customer_id = $1
AND txOut.created > now() - interval $2 AND txOut.created > now() - interval $2
AND timedout IS false
AND error_code IS DISTINCT FROM 'operatorCancel' AND error_code IS DISTINCT FROM 'operatorCancel'
AND fiat > 0 AND fiat > 0
ORDER BY created;` ) ch WHERE NOT ch.expired ORDER BY ch.created`
const days = _.isNil(thresholdDays) ? 0 : thresholdDays 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 } module.exports = { post, cancel, customerHistory }