fix: customer filters not being applied

fix: customer name edge cases
fix: sql uppercasing
This commit is contained in:
Sérgio Salgado 2021-06-28 01:24:15 +01:00 committed by Josh Harvey
parent 2b93f016ac
commit 06b7f8cf8b
3 changed files with 43 additions and 29 deletions

View file

@ -451,33 +451,33 @@ 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 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,
sanctions_override, total_txs, total_spent, created as last_active, fiat as last_tx_fiat, sanctions_override, total_txs, total_spent, created AS last_active, fiat AS last_tx_fiat,
fiat_code as last_tx_fiat_code, tx_class as last_tx_class fiat_code AS last_tx_fiat_code, tx_class AS last_tx_class
from ( FROM (
select c.id, c.authorized_override, SELECT c.id, c.authorized_override,
greatest(0, date_part('day', c.suspended_until - now())) as days_suspended, greatest(0, date_part('day', c.suspended_until - now())) AS days_suspended,
c.suspended_until > now() as is_suspended, c.suspended_until > now() AS is_suspended,
c.front_camera_path, c.front_camera_override, c.front_camera_path, c.front_camera_override,
c.phone, c.sms_override, c.id_card_data, c.id_card_data_override, c.id_card_data_expiration, c.phone, c.sms_override, c.id_card_data, c.id_card_data_override, c.id_card_data_expiration,
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,
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
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
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 != $1
) as cl where rn = 1 ) AS cl WHERE rn = 1
and ($3 is null or phone = $3) AND ($3 IS NULL OR phone = $3)
and ($4 is null or concat(id_card_data::json->>'firstName', ' ', id_card_data::json->>'lastName') = $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 id_card_data::json->>'address' = $5) AND ($5 IS NULL OR id_card_data::json->>'address' = $5)
and ($6 is null or id_card_data::json->>'documentNumber' = $6) AND ($6 IS NULL OR id_card_data::json->>'documentNumber' = $6)
limit $2` limit $2`
return db.any(sql, [ anonymous.uuid, NUM_RESULTS, phone, name, address, id ]) return db.any(sql, [ anonymous.uuid, NUM_RESULTS, phone, name, address, id ])
.then(customers => Promise.all(_.map(customer => { .then(customers => Promise.all(_.map(customer => {

View file

@ -3,7 +3,7 @@ const cashInTx = require('../cash-in/cash-in-tx')
const { CASH_OUT_TRANSACTION_STATES } = require('../cash-out/cash-out-helper') const { CASH_OUT_TRANSACTION_STATES } = require('../cash-out/cash-out-helper')
function transaction() { function transaction() {
const sql = `SELECT distinct * FROM ( const sql = `SELECT DISTINCT * FROM (
SELECT 'type' AS type, 'Cash In' AS value UNION SELECT 'type' AS type, 'Cash In' AS value UNION
SELECT 'type' AS type, 'Cash Out' AS value UNION SELECT 'type' AS type, 'Cash Out' AS value UNION
SELECT 'machine' AS type, name AS value FROM devices d INNER JOIN cash_in_txs t ON d.device_id = t.device_id UNION SELECT 'machine' AS type, name AS value FROM devices d INNER JOIN cash_in_txs t ON d.device_id = t.device_id UNION
@ -28,11 +28,13 @@ function transaction() {
} }
function customer() { function customer() {
const sql = `select distinct * from ( const sql = `SELECT DISTINCT * FROM (
select 'phone' as type, phone as value from customers where phone is not null union SELECT 'phone' AS type, phone AS value FROM customers WHERE phone IS NOT NULL UNION
select 'name' as type, concat(id_card_data::json->>'firstName', ' ', id_card_data::json->>'lastName') as value from customers where concat(id_card_data::json->>'firstName', ' ', id_card_data::json->>'lastName') is not null union SELECT 'name' AS type, id_card_data::json->>'firstName' AS value FROM customers WHERE id_card_data::json->>'firstName' IS NOT NULL AND id_card_data::json->>'lastName' IS NULL UNION
select 'address' as type, id_card_data::json->>'address' as value from customers where id_card_data::json->>'address' is not null union SELECT 'name' AS type, id_card_data::json->>'lastName' AS value FROM customers WHERE id_card_data::json->>'firstName' IS NULL AND id_card_data::json->>'lastName' IS NOT NULL UNION
select 'id' as type, id_card_data::json->>'documentNumber' as value from customers where id_card_data::json->>'documentNumber' is not null SELECT 'name' AS type, concat(id_card_data::json->>'firstName', ' ', id_card_data::json->>'lastName') AS value FROM customers WHERE id_card_data::json->>'firstName' IS NOT NULL AND id_card_data::json->>'lastName' IS NOT NULL UNION
SELECT 'address' as type, id_card_data::json->>'address' AS value FROM customers WHERE id_card_data::json->>'address' IS NOT NULL UNION
SELECT 'id' AS type, id_card_data::json->>'documentNumber' AS value FROM customers WHERE id_card_data::json->>'documentNumber' IS NOT NULL
) f` ) f`
return db.any(sql) return db.any(sql)

View file

@ -30,9 +30,14 @@ const GET_CUSTOMER_FILTERS = gql`
` `
const GET_CUSTOMERS = gql` const GET_CUSTOMERS = gql`
{ query configAndCustomers(
$phone: String
$name: String
$address: String
$id: String
) {
config config
customers { customers(phone: $phone, name: $name, address: $address, id: $id) {
id id
idCardData idCardData
phone phone
@ -102,6 +107,13 @@ const Customers = () => {
id: filtersObject.id id: filtersObject.id
}) })
console.log({
phone: filtersObject.phone,
name: filtersObject.name,
address: filtersObject.address,
id: filtersObject.id
})
refetch && refetch() refetch && refetch()
} }