feat: add search functionality to customer page

This commit is contained in:
Sérgio Salgado 2021-05-31 15:58:24 +01:00 committed by Josh Harvey
parent b99f98982b
commit 2b93f016ac
7 changed files with 151 additions and 24 deletions

View file

@ -450,7 +450,7 @@ function batch () {
*
* @returns {array} Array of customers with it's transactions aggregations
*/
function getCustomersList () {
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,
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,
@ -474,8 +474,12 @@ function getCustomersList () {
from cash_out_txs where confirmed_at is not null) t on c.id = t.customer_id
where c.id != $1
) as cl where rn = 1
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 ($5 is null or id_card_data::json->>'address' = $5)
and ($6 is null or id_card_data::json->>'documentNumber' = $6)
limit $2`
return db.any(sql, [ anonymous.uuid, NUM_RESULTS ])
return db.any(sql, [ anonymous.uuid, NUM_RESULTS, phone, name, address, id ])
.then(customers => Promise.all(_.map(customer => {
return populateOverrideUsernames(customer)
.then(camelize)

View file

@ -27,4 +27,15 @@ function transaction() {
return db.any(sql)
}
module.exports = { transaction }
function customer() {
const sql = `select distinct * from (
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 '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`
return db.any(sql)
}
module.exports = { transaction, customer }

View file

@ -1,13 +1,15 @@
const anonymous = require('../../../constants').anonymousCustomer
const customers = require('../../../customers')
const filters = require('../../filters')
const resolvers = {
Customer: {
isAnonymous: parent => (parent.customerId === anonymous.uuid)
},
Query: {
customers: () => customers.getCustomersList(),
customer: (...[, { customerId }]) => customers.getCustomerById(customerId)
customers: (...[, { phone, name, address, id }]) => customers.getCustomersList(phone, name, address, id),
customer: (...[, { customerId }]) => customers.getCustomerById(customerId),
customerFilters: () => filters.customer()
},
Mutation: {
setCustomer: (root, { customerId, customerInput }, context, info) => {

View file

@ -56,8 +56,9 @@ const typeDef = gql`
}
type Query {
customers: [Customer] @auth
customers(phone: String, name: String, address: String, id: String): [Customer] @auth
customer(customerId: ID!): Customer @auth
customerFilters: [Filter] @auth
}
type Mutation {