fix: improve individual discount loading
This commit is contained in:
parent
3ccc202efa
commit
ac56ace4bd
5 changed files with 44 additions and 19 deletions
|
|
@ -487,6 +487,14 @@ function batch () {
|
|||
}, customers)))
|
||||
}
|
||||
|
||||
function getSlimCustomerByIdBatch (ids) {
|
||||
const sql = `SELECT id, phone, id_card_data
|
||||
FROM customers
|
||||
WHERE id = ANY($1::uuid[])`
|
||||
return db.any(sql, [ids])
|
||||
.then(customers => _.map(camelize, customers))
|
||||
}
|
||||
|
||||
// TODO: getCustomersList and getCustomerById are very similar, so this should be refactored
|
||||
|
||||
/**
|
||||
|
|
@ -612,18 +620,18 @@ function formatSubscriberInfo(customer) {
|
|||
if(!subscriberInfo) return customer
|
||||
const result = subscriberInfo.result
|
||||
if(_.isEmpty(result)) return _.omit(['subscriberInfo'], customer)
|
||||
|
||||
|
||||
const name = _.get('belongs_to.name')(result)
|
||||
const street = _.get('current_addresses[0].street_line_1')(result)
|
||||
const city = _.get('current_addresses[0].city')(result)
|
||||
const stateCode = _.get('current_addresses[0].state_code')(result)
|
||||
const postalCode = _.get('current_addresses[0].postal_code')(result)
|
||||
|
||||
customer.subscriberInfo = {
|
||||
name,
|
||||
|
||||
customer.subscriberInfo = {
|
||||
name,
|
||||
address: `${street ?? ''} ${city ?? ''}${street || city ? ',' : ''} ${stateCode ?? ''} ${postalCode ?? ''}`
|
||||
}
|
||||
|
||||
|
||||
return customer
|
||||
}
|
||||
|
||||
|
|
@ -1032,6 +1040,7 @@ module.exports = {
|
|||
get,
|
||||
getWithEmail,
|
||||
batch,
|
||||
getSlimCustomerByIdBatch,
|
||||
getCustomersList,
|
||||
getCustomerById,
|
||||
getById,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,16 @@
|
|||
const DataLoader = require('dataloader')
|
||||
|
||||
const loyalty = require('../../../loyalty')
|
||||
const { getSlimCustomerByIdBatch } = require('../../../customers')
|
||||
|
||||
const customerLoader = new DataLoader(ids => {
|
||||
return getSlimCustomerByIdBatch(ids)
|
||||
}, { cache: false })
|
||||
|
||||
const resolvers = {
|
||||
IndividualDiscount: {
|
||||
customer: parent => customerLoader.load(parent.customerId)
|
||||
},
|
||||
Query: {
|
||||
promoCodes: () => loyalty.getAvailablePromoCodes(),
|
||||
individualDiscounts: () => loyalty.getAvailableIndividualDiscounts()
|
||||
|
|
|
|||
|
|
@ -3,9 +3,15 @@ const gql = require('graphql-tag')
|
|||
const typeDef = gql`
|
||||
type IndividualDiscount {
|
||||
id: ID!
|
||||
customerId: ID!
|
||||
customer: DiscountCustomer!
|
||||
discount: Int
|
||||
}
|
||||
|
||||
type DiscountCustomer {
|
||||
id: ID!
|
||||
phone: String
|
||||
idCardData: JSONObject
|
||||
}
|
||||
|
||||
type PromoCode {
|
||||
id: ID!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue