fix: edge case with suspensions shorter than 1 day

This commit is contained in:
Sérgio Salgado 2021-07-19 19:12:05 +01:00 committed by Josh Harvey
parent 34a49db2d0
commit 1a9368b290

View file

@ -450,7 +450,10 @@ function batch () {
* *
* @returns {array} Array of customers with it's transactions aggregations * @returns {array} Array of customers with it's transactions aggregations
*/ */
function getCustomersList (phone = null, name = null, address = null, id = null) { function getCustomersList (phone = null, name = null, address = null, id = null) {
const passableErrorCodes = _.map(Pgp.as.text, TX_PASSTHROUGH_ERROR_CODES).join(',')
const sql = `SELECT id, authorized_override, days_suspended, is_suspended, front_camera_path, front_camera_override, const sql = `SELECT id, authorized_override, days_suspended, is_suspended, front_camera_path, front_camera_override,
phone, sms_override, id_card_data, id_card_data_override, id_card_data_expiration, phone, sms_override, id_card_data, id_card_data_override, id_card_data_expiration,
id_card_photo_path, id_card_photo_override, us_ssn, us_ssn_override, sanctions, sanctions_at, id_card_photo_path, id_card_photo_override, us_ssn, us_ssn_override, sanctions, sanctions_at,
@ -465,21 +468,22 @@ function getCustomersList (phone = null, name = null, address = null, id = null)
c.id_card_photo_path, c.id_card_photo_override, c.us_ssn, c.us_ssn_override, c.sanctions, c.id_card_photo_path, c.id_card_photo_override, c.us_ssn, c.us_ssn_override, c.sanctions,
c.sanctions_at, c.sanctions_override, t.tx_class, t.fiat, t.fiat_code, t.created, c.sanctions_at, c.sanctions_override, t.tx_class, t.fiat, t.fiat_code, t.created,
row_number() OVER (partition by c.id order by t.created desc) AS rn, row_number() OVER (partition by c.id order by t.created desc) AS rn,
coalesce(sum(case when error_code is null or error_code not in ($1^) then t.fiat else 0 end) over (partition by c.id), 0) as total_spent
sum(CASE WHEN t.id IS NOT NULL THEN 1 ELSE 0 END) OVER (partition by c.id) AS total_txs, sum(CASE WHEN t.id IS NOT NULL THEN 1 ELSE 0 END) OVER (partition by c.id) AS total_txs,
coalesce(sum(t.fiat) OVER (partition by c.id), 0) AS total_spent coalesce(sum(t.fiat) OVER (partition by c.id), 0) AS total_spent
FROM customers c LEFT OUTER JOIN ( FROM customers c LEFT OUTER JOIN (
SELECT 'cashIn' AS tx_class, id, fiat, fiat_code, created, customer_id SELECT 'cashIn' AS tx_class, id, fiat, fiat_code, created, customer_id, error_code
FROM cash_in_txs WHERE send_confirmed = true UNION FROM cash_in_txs WHERE send_confirmed = true UNION
SELECT 'cashOut' AS tx_class, id, fiat, fiat_code, created, customer_id SELECT 'cashOut' AS tx_class, id, fiat, fiat_code, created, customer_id, error_code
FROM cash_out_txs WHERE confirmed_at IS NOT NULL) t ON c.id = t.customer_id FROM cash_out_txs WHERE confirmed_at IS NOT NULL) t ON c.id = t.customer_id
WHERE c.id != $1 WHERE c.id != $2
) AS cl WHERE rn = 1 ) AS cl WHERE rn = 1
AND ($3 IS NULL OR phone = $3) AND ($4 IS NULL OR phone = $4)
AND ($4 IS NULL OR concat(id_card_data::json->>'firstName', ' ', id_card_data::json->>'lastName') = $4 OR id_card_data::json->>'firstName' = $4 OR id_card_data::json->>'lastName' = $4) AND ($5 IS NULL OR concat(id_card_data::json->>'firstName', ' ', id_card_data::json->>'lastName') = $5 OR id_card_data::json->>'firstName' = $5 OR id_card_data::json->>'lastName' = $5)
AND ($5 IS NULL OR id_card_data::json->>'address' = $5) AND ($6 IS NULL OR id_card_data::json->>'address' = $6)
AND ($6 IS NULL OR id_card_data::json->>'documentNumber' = $6) AND ($7 IS NULL OR id_card_data::json->>'documentNumber' = $7)
limit $2` limit $3`
return db.any(sql, [ anonymous.uuid, NUM_RESULTS, phone, name, address, id ]) return db.any(sql, [ passableErrorCdoes, anonymous.uuid, NUM_RESULTS, phone, name, address, id ])
.then(customers => Promise.all(_.map(customer => { .then(customers => Promise.all(_.map(customer => {
return populateOverrideUsernames(customer) return populateOverrideUsernames(customer)
.then(camelize) .then(camelize)