Merge pull request #1825 from RafaelTaranto/fix/individual-discount-loading
LAM-1312 fix: improve individual discount loading
This commit is contained in:
commit
39aa25b58d
5 changed files with 44 additions and 19 deletions
|
|
@ -487,6 +487,14 @@ function batch () {
|
||||||
}, customers)))
|
}, 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
|
// TODO: getCustomersList and getCustomerById are very similar, so this should be refactored
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -612,18 +620,18 @@ function formatSubscriberInfo(customer) {
|
||||||
if(!subscriberInfo) return customer
|
if(!subscriberInfo) return customer
|
||||||
const result = subscriberInfo.result
|
const result = subscriberInfo.result
|
||||||
if(_.isEmpty(result)) return _.omit(['subscriberInfo'], customer)
|
if(_.isEmpty(result)) return _.omit(['subscriberInfo'], customer)
|
||||||
|
|
||||||
const name = _.get('belongs_to.name')(result)
|
const name = _.get('belongs_to.name')(result)
|
||||||
const street = _.get('current_addresses[0].street_line_1')(result)
|
const street = _.get('current_addresses[0].street_line_1')(result)
|
||||||
const city = _.get('current_addresses[0].city')(result)
|
const city = _.get('current_addresses[0].city')(result)
|
||||||
const stateCode = _.get('current_addresses[0].state_code')(result)
|
const stateCode = _.get('current_addresses[0].state_code')(result)
|
||||||
const postalCode = _.get('current_addresses[0].postal_code')(result)
|
const postalCode = _.get('current_addresses[0].postal_code')(result)
|
||||||
|
|
||||||
customer.subscriberInfo = {
|
customer.subscriberInfo = {
|
||||||
name,
|
name,
|
||||||
address: `${street ?? ''} ${city ?? ''}${street || city ? ',' : ''} ${stateCode ?? ''} ${postalCode ?? ''}`
|
address: `${street ?? ''} ${city ?? ''}${street || city ? ',' : ''} ${stateCode ?? ''} ${postalCode ?? ''}`
|
||||||
}
|
}
|
||||||
|
|
||||||
return customer
|
return customer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1032,6 +1040,7 @@ module.exports = {
|
||||||
get,
|
get,
|
||||||
getWithEmail,
|
getWithEmail,
|
||||||
batch,
|
batch,
|
||||||
|
getSlimCustomerByIdBatch,
|
||||||
getCustomersList,
|
getCustomersList,
|
||||||
getCustomerById,
|
getCustomerById,
|
||||||
getById,
|
getById,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,16 @@
|
||||||
|
const DataLoader = require('dataloader')
|
||||||
|
|
||||||
const loyalty = require('../../../loyalty')
|
const loyalty = require('../../../loyalty')
|
||||||
|
const { getSlimCustomerByIdBatch } = require('../../../customers')
|
||||||
|
|
||||||
|
const customerLoader = new DataLoader(ids => {
|
||||||
|
return getSlimCustomerByIdBatch(ids)
|
||||||
|
}, { cache: false })
|
||||||
|
|
||||||
const resolvers = {
|
const resolvers = {
|
||||||
|
IndividualDiscount: {
|
||||||
|
customer: parent => customerLoader.load(parent.customerId)
|
||||||
|
},
|
||||||
Query: {
|
Query: {
|
||||||
promoCodes: () => loyalty.getAvailablePromoCodes(),
|
promoCodes: () => loyalty.getAvailablePromoCodes(),
|
||||||
individualDiscounts: () => loyalty.getAvailableIndividualDiscounts()
|
individualDiscounts: () => loyalty.getAvailableIndividualDiscounts()
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,15 @@ const gql = require('graphql-tag')
|
||||||
const typeDef = gql`
|
const typeDef = gql`
|
||||||
type IndividualDiscount {
|
type IndividualDiscount {
|
||||||
id: ID!
|
id: ID!
|
||||||
customerId: ID!
|
customer: DiscountCustomer!
|
||||||
discount: Int
|
discount: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DiscountCustomer {
|
||||||
|
id: ID!
|
||||||
|
phone: String
|
||||||
|
idCardData: JSONObject
|
||||||
|
}
|
||||||
|
|
||||||
type PromoCode {
|
type PromoCode {
|
||||||
id: ID!
|
id: ID!
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,9 @@ const styles = {
|
||||||
},
|
},
|
||||||
error: {
|
error: {
|
||||||
color: errorColor
|
color: errorColor
|
||||||
|
},
|
||||||
|
disabled: {
|
||||||
|
cursor: 'wait'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import { Link, Button, IconButton } from 'src/components/buttons'
|
||||||
|
|
||||||
import styles from './IndividualDiscount.styles'
|
import styles from './IndividualDiscount.styles'
|
||||||
import IndividualDiscountModal from './IndividualDiscountModal'
|
import IndividualDiscountModal from './IndividualDiscountModal'
|
||||||
|
import classnames from 'classnames'
|
||||||
|
|
||||||
const useStyles = makeStyles(styles)
|
const useStyles = makeStyles(styles)
|
||||||
|
|
||||||
|
|
@ -20,7 +21,11 @@ const GET_INDIVIDUAL_DISCOUNTS = gql`
|
||||||
query individualDiscounts {
|
query individualDiscounts {
|
||||||
individualDiscounts {
|
individualDiscounts {
|
||||||
id
|
id
|
||||||
customerId
|
customer {
|
||||||
|
id
|
||||||
|
phone
|
||||||
|
idCardData
|
||||||
|
}
|
||||||
discount
|
discount
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +67,7 @@ const IndividualDiscounts = () => {
|
||||||
const [showModal, setShowModal] = useState(false)
|
const [showModal, setShowModal] = useState(false)
|
||||||
const toggleModal = () => setShowModal(!showModal)
|
const toggleModal = () => setShowModal(!showModal)
|
||||||
|
|
||||||
const { data: discountResponse, loading: discountLoading } = useQuery(
|
const { data: discountResponse, loading } = useQuery(
|
||||||
GET_INDIVIDUAL_DISCOUNTS
|
GET_INDIVIDUAL_DISCOUNTS
|
||||||
)
|
)
|
||||||
const { data: customerData, loading: customerLoading } =
|
const { data: customerData, loading: customerLoading } =
|
||||||
|
|
@ -75,11 +80,6 @@ const IndividualDiscounts = () => {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const getCustomer = id => {
|
|
||||||
const customers = R.path(['customers'])(customerData)
|
|
||||||
return R.find(R.propEq('id', id))(customers)
|
|
||||||
}
|
|
||||||
|
|
||||||
const [deleteDiscount] = useMutation(DELETE_DISCOUNT, {
|
const [deleteDiscount] = useMutation(DELETE_DISCOUNT, {
|
||||||
onError: ({ message }) => {
|
onError: ({ message }) => {
|
||||||
const errorMessage = message ?? 'Error while deleting row'
|
const errorMessage = message ?? 'Error while deleting row'
|
||||||
|
|
@ -96,11 +96,10 @@ const IndividualDiscounts = () => {
|
||||||
textAlign: 'left',
|
textAlign: 'left',
|
||||||
size: 'sm',
|
size: 'sm',
|
||||||
view: t => {
|
view: t => {
|
||||||
const customer = getCustomer(t.customerId)
|
|
||||||
return (
|
return (
|
||||||
<div className={classes.identification}>
|
<div className={classes.identification}>
|
||||||
<PhoneIdIcon />
|
<PhoneIdIcon />
|
||||||
<span>{customer.phone}</span>
|
<span>{t.customer.phone}</span>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -111,7 +110,7 @@ const IndividualDiscounts = () => {
|
||||||
textAlign: 'left',
|
textAlign: 'left',
|
||||||
size: 'sm',
|
size: 'sm',
|
||||||
view: t => {
|
view: t => {
|
||||||
const customer = getCustomer(t.customerId)
|
const customer = t.customer
|
||||||
if (R.isNil(customer.idCardData)) {
|
if (R.isNil(customer.idCardData)) {
|
||||||
return <>{'-'}</>
|
return <>{'-'}</>
|
||||||
}
|
}
|
||||||
|
|
@ -153,8 +152,6 @@ const IndividualDiscounts = () => {
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
const loading = discountLoading || customerLoading
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{!loading && !R.isEmpty(discountResponse.individualDiscounts) && (
|
{!loading && !R.isEmpty(discountResponse.individualDiscounts) && (
|
||||||
|
|
@ -165,7 +162,7 @@ const IndividualDiscounts = () => {
|
||||||
className={classes.tableWidth}
|
className={classes.tableWidth}
|
||||||
display="flex"
|
display="flex"
|
||||||
justifyContent="flex-end">
|
justifyContent="flex-end">
|
||||||
<Link color="primary" onClick={toggleModal}>
|
<Link color="primary" onClick={toggleModal} className={classnames({[classes.disabled]: customerLoading})} disabled={customerLoading}>
|
||||||
Add new code
|
Add new code
|
||||||
</Link>
|
</Link>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue