From 168192b708d89c573afb03d017227380a110122b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Wed, 13 Oct 2021 18:21:45 +0100 Subject: [PATCH] fix: expired column in customer transactions history --- lib/admin/transactions.js | 4 ++-- lib/cash-out/cash-out-helper.js | 6 ++++-- lib/new-admin/transactions.js | 6 +++--- lib/tx.js | 6 ++++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/admin/transactions.js b/lib/admin/transactions.js index 7ae7ceea..f565bada 100644 --- a/lib/admin/transactions.js +++ b/lib/admin/transactions.js @@ -33,7 +33,7 @@ function batch () { order by created desc limit $2` const cashOutSql = `select 'cashOut' as tx_class, cash_out_txs.*, - (extract(epoch from (now() - greatest(created, confirmed_at))) * 1000) >= $2 as expired + (NOT dispense AND extract(epoch from (now() - greatest(created, confirmed_at))) >= $2) as expired from cash_out_txs order by created desc limit $1` @@ -51,7 +51,7 @@ function single (txId) { where id=$2` const cashOutSql = `select 'cashOut' as tx_class, - (extract(epoch from (now() - greatest(created, confirmed_at))) * 1000) >= $2 as expired, + (NOT dispense AND extract(epoch from (now() - greatest(created, confirmed_at))) >= $2) as expired, cash_out_txs.* from cash_out_txs where id=$1` diff --git a/lib/cash-out/cash-out-helper.js b/lib/cash-out/cash-out-helper.js index dc4597fd..3ef9f7ab 100644 --- a/lib/cash-out/cash-out-helper.js +++ b/lib/cash-out/cash-out-helper.js @@ -4,7 +4,9 @@ const db = require('../db') const T = require('../time') const BN = require('../bn') -const REDEEMABLE_AGE = T.day +// FP operations on Postgres result in very big errors. +// E.g.: 1853.013808 * 1000 = 1866149.494 +const REDEEMABLE_AGE = T.day / 1000 module.exports = { redeemableTxs, toObj, toDb, REDEEMABLE_AGE } @@ -101,7 +103,7 @@ function redeemableTxs (deviceId) { and redeem=$2 and dispense=$3 and provisioned_1 is not null - and (extract(epoch from (now() - greatest(created, confirmed_at))) * 1000) < $4` + and extract(epoch from (now() - greatest(created, confirmed_at))) < $4` return db.any(sql, [deviceId, true, false, REDEEMABLE_AGE]) .then(_.map(toObj)) diff --git a/lib/new-admin/transactions.js b/lib/new-admin/transactions.js index 917ca0c1..62b3af47 100644 --- a/lib/new-admin/transactions.js +++ b/lib/new-admin/transactions.js @@ -55,7 +55,7 @@ function batch (from = new Date(0).toISOString(), until = new Date().toISOString c.name as customer_name, c.front_camera_path as customer_front_camera_path, c.id_card_photo_path as customer_id_card_photo_path, - (extract(epoch from (now() - greatest(txs.created, txs.confirmed_at))) * 1000) >= $1 as expired + (NOT txs.dispense AND extract(epoch from (now() - greatest(txs.created, txs.confirmed_at))) >= $1) as expired from cash_out_txs txs inner join cash_out_actions actions on txs.id = actions.tx_id and actions.action = 'provisionAddress' @@ -156,7 +156,7 @@ function getCustomerTransactionsBatch (ids) { c.name as customer_name, c.front_camera_path as customer_front_camera_path, c.id_card_photo_path as customer_id_card_photo_path, - (extract(epoch from (now() - greatest(txs.created, txs.confirmed_at))) * 1000) >= $3 as expired + (NOT txs.dispense AND extract(epoch from (now() - greatest(txs.created, txs.confirmed_at))) >= $3) as expired from cash_out_txs txs inner join cash_out_actions actions on txs.id = actions.tx_id and actions.action = 'provisionAddress' @@ -199,7 +199,7 @@ function single (txId) { c.name as customer_name, c.front_camera_path as customer_front_camera_path, c.id_card_photo_path as customer_id_card_photo_path, - (extract(epoch from (now() - greatest(txs.created, txs.confirmed_at))) * 1000) >= $2 as expired + (NOT txs.dispense AND extract(epoch from (now() - greatest(txs.created, txs.confirmed_at))) >= $2) as expired from cash_out_txs txs inner join cash_out_actions actions on txs.id = actions.tx_id and actions.action = 'provisionAddress' diff --git a/lib/tx.js b/lib/tx.js index d08b2af5..6e6820c1 100644 --- a/lib/tx.js +++ b/lib/tx.js @@ -5,7 +5,9 @@ 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 +// FP operations on Postgres result in very big errors. +// E.g.: 1853.013808 * 1000 = 1866149.494 +const REDEEMABLE_AGE = T.day / 1000 function process (tx, pi) { const mtx = massage(tx, pi) @@ -76,7 +78,7 @@ function customerHistory (customerId, thresholdDays) { AND txIn.created > now() - interval $2 UNION SELECT txOut.id, txOut.created, txOut.fiat, 'cashOut' AS direction, - (extract(epoch FROM (now() - greatest(txOut.created, txOut.confirmed_at))) * 1000) >= $4 AS expired + (NOT txOut.dispense AND extract(epoch FROM (now() - greatest(txOut.created, txOut.confirmed_at))) >= $4) AS expired FROM cash_out_txs txOut WHERE txOut.customer_id = $1 AND txOut.created > now() - interval $2