From ac56ace4bd5ee64cd60be841f427830d184f812d Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Thu, 17 Apr 2025 09:48:56 +0100 Subject: [PATCH] fix: improve individual discount loading --- lib/customers.js | 19 +++++++++++---- .../graphql/resolvers/loyalty.resolver.js | 10 ++++++++ lib/new-admin/graphql/types/loyalty.type.js | 8 ++++++- .../LoyaltyPanel/IndividualDiscount.styles.js | 3 +++ .../LoyaltyPanel/IndividualDiscounts.jsx | 23 ++++++++----------- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/lib/customers.js b/lib/customers.js index dd370490..27589b16 100644 --- a/lib/customers.js +++ b/lib/customers.js @@ -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, diff --git a/lib/new-admin/graphql/resolvers/loyalty.resolver.js b/lib/new-admin/graphql/resolvers/loyalty.resolver.js index 9a12a89e..dd63da4b 100644 --- a/lib/new-admin/graphql/resolvers/loyalty.resolver.js +++ b/lib/new-admin/graphql/resolvers/loyalty.resolver.js @@ -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() diff --git a/lib/new-admin/graphql/types/loyalty.type.js b/lib/new-admin/graphql/types/loyalty.type.js index d8c0059c..0227a503 100644 --- a/lib/new-admin/graphql/types/loyalty.type.js +++ b/lib/new-admin/graphql/types/loyalty.type.js @@ -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! diff --git a/new-lamassu-admin/src/pages/LoyaltyPanel/IndividualDiscount.styles.js b/new-lamassu-admin/src/pages/LoyaltyPanel/IndividualDiscount.styles.js index b7918df7..68951bfc 100644 --- a/new-lamassu-admin/src/pages/LoyaltyPanel/IndividualDiscount.styles.js +++ b/new-lamassu-admin/src/pages/LoyaltyPanel/IndividualDiscount.styles.js @@ -49,6 +49,9 @@ const styles = { }, error: { color: errorColor + }, + disabled: { + cursor: 'wait' } } diff --git a/new-lamassu-admin/src/pages/LoyaltyPanel/IndividualDiscounts.jsx b/new-lamassu-admin/src/pages/LoyaltyPanel/IndividualDiscounts.jsx index 93f76169..42f18331 100644 --- a/new-lamassu-admin/src/pages/LoyaltyPanel/IndividualDiscounts.jsx +++ b/new-lamassu-admin/src/pages/LoyaltyPanel/IndividualDiscounts.jsx @@ -13,6 +13,7 @@ import { Link, Button, IconButton } from 'src/components/buttons' import styles from './IndividualDiscount.styles' import IndividualDiscountModal from './IndividualDiscountModal' +import classnames from 'classnames' const useStyles = makeStyles(styles) @@ -20,7 +21,11 @@ const GET_INDIVIDUAL_DISCOUNTS = gql` query individualDiscounts { individualDiscounts { id - customerId + customer { + id + phone + idCardData + } discount } } @@ -62,7 +67,7 @@ const IndividualDiscounts = () => { const [showModal, setShowModal] = useState(false) const toggleModal = () => setShowModal(!showModal) - const { data: discountResponse, loading: discountLoading } = useQuery( + const { data: discountResponse, loading } = useQuery( GET_INDIVIDUAL_DISCOUNTS ) 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, { onError: ({ message }) => { const errorMessage = message ?? 'Error while deleting row' @@ -96,11 +96,10 @@ const IndividualDiscounts = () => { textAlign: 'left', size: 'sm', view: t => { - const customer = getCustomer(t.customerId) return (
- {customer.phone} + {t.customer.phone}
) } @@ -111,7 +110,7 @@ const IndividualDiscounts = () => { textAlign: 'left', size: 'sm', view: t => { - const customer = getCustomer(t.customerId) + const customer = t.customer if (R.isNil(customer.idCardData)) { return <>{'-'} } @@ -153,8 +152,6 @@ const IndividualDiscounts = () => { } ] - const loading = discountLoading || customerLoading - return ( <> {!loading && !R.isEmpty(discountResponse.individualDiscounts) && ( @@ -165,7 +162,7 @@ const IndividualDiscounts = () => { className={classes.tableWidth} display="flex" justifyContent="flex-end"> - + Add new code