feat: add list of passable tx error codes for customer volume calculation

This commit is contained in:
Sérgio Salgado 2021-09-13 20:16:29 +01:00 committed by Josh Harvey
parent 100835cacc
commit 1c652f4b54

View file

@ -21,6 +21,8 @@ const NUM_RESULTS = 1000
const idPhotoCardBasedir = _.get('idPhotoCardDir', options) const idPhotoCardBasedir = _.get('idPhotoCardDir', options)
const frontCameraBaseDir = _.get('frontCameraDir', options) const frontCameraBaseDir = _.get('frontCameraDir', options)
const TX_PASSTHROUGH_ERROR_CODES = ['operatorCancel']
/** /**
* Add new customer * Add new customer
* *
@ -453,6 +455,7 @@ function batch () {
* @returns {array} Array of customers with it's transactions aggregations * @returns {array} Array of customers with it's transactions aggregations
*/ */
function getCustomersList () { function getCustomersList () {
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,
@ -468,16 +471,16 @@ function getCustomersList () {
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,
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(case when error_code is distinct from 'operatorCancel' then t.fiat else 0 end) over (partition by c.id), 0) as total_spent 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
from customers c left outer join ( from customers c left outer join (
select 'cashIn' as tx_class, id, fiat, fiat_code, created, customer_id, error_code 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, error_code 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
limit $2` limit $3`
return db.any(sql, [ anonymous.uuid, NUM_RESULTS ]) return db.any(sql, [ passableErrorCodes, anonymous.uuid, NUM_RESULTS ])
.then(customers => Promise.all(_.map(customer => { .then(customers => Promise.all(_.map(customer => {
return populateOverrideUsernames(customer) return populateOverrideUsernames(customer)
.then(camelize) .then(camelize)
@ -492,6 +495,7 @@ function getCustomersList () {
* @returns {array} Array of customers with it's transactions aggregations * @returns {array} Array of customers with it's transactions aggregations
*/ */
function getCustomerById (id) { function getCustomerById (id) {
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,
@ -507,15 +511,15 @@ function getCustomerById (id) {
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,
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,
sum(case when error_code is distinct from 'operatorCancel' then t.fiat else 0 end) over (partition by c.id) as total_spent sum(case when error_code is null or error_code not in ($1^) then t.fiat else 0 end) over (partition by c.id) 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, error_code 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, error_code 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`
return db.oneOrNone(sql, [id]) return db.oneOrNone(sql, [passableErrorCodes, id])
.then(populateOverrideUsernames) .then(populateOverrideUsernames)
.then(camelize) .then(camelize)
} }