From 146f67a0b72bbe8237fcbeaf80fe617117dd605a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Fri, 10 Sep 2021 16:16:34 +0100 Subject: [PATCH] fix: correct calculation to find expired transactions on customer history --- lib/tx.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/tx.js b/lib/tx.js index f42f9b61..d08b2af5 100644 --- a/lib/tx.js +++ b/lib/tx.js @@ -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}