From ff62878cd6eef60fca5a30901556d70d00e2d024 Mon Sep 17 00:00:00 2001 From: Urvish Date: Tue, 13 Oct 2026 14:33:58 +0530 Subject: [PATCH] fixed-issue(3):automatically apply and display loyalty discounts in cart summary --- src/App.tsx | 30 ++++++++++++++++++++++++++++-- src/components/cart/CartDrawer.tsx | 8 +++++++- src/types/index.ts | 8 ++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c812df5..5ee82fb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -291,14 +291,14 @@ export default function App() { } }, [userToken]); - // Recalculate price whenever cart, coupon, or currency changes + // Recalculate price whenever cart, coupon, currency, or logged-in user changes useEffect(() => { if (cart.length === 0) { setPricingCalc(null); return; } calculatePricing(); - }, [cart, appliedCoupon, currency]); + }, [cart, appliedCoupon, currency, loggedInUser]); // Update currency and dial code automatically on country selection during registration useEffect(() => { @@ -649,8 +649,34 @@ export default function App() { setLoadingPricing(true); try { const headers = userToken ? { 'Authorization': `Bearer ${userToken}` } : {}; + + // Derive owned categories from loggedInUser to ensure loyalty discounts apply automatically + const userOwnedCategories: string[] = []; + if (loggedInUser?.active_subscriptions) { + for (const sub of loggedInUser.active_subscriptions) { + const code = (sub.plan_code || '').toUpperCase(); + if (code.startsWith('POINT')) userOwnedCategories.push('Points'); + else if (code.startsWith('PATTERN')) userOwnedCategories.push('Pattern'); + else if (code.startsWith('NUTRITION')) userOwnedCategories.push('Nutrition'); + else if (code.startsWith('THERAPEUTIC')) userOwnedCategories.push('Therapeutics'); + else if (code === 'LIFETIME_ALL' || code === 'LIFETIME_ALL_USD') userOwnedCategories.push('Lifetime'); + } + } + if (loggedInUser?.unlocked_module_slugs) { + for (const slug of loggedInUser.unlocked_module_slugs) { + const s = slug.toLowerCase(); + if (s.includes('point') || s.includes('ear') || s.includes('scalp') || s.includes('tung')) userOwnedCategories.push('Points'); + else if (s.includes('pattern') || s.includes('eight') || s.includes('principles') || s.includes('zang') || s.includes('stages') || s.includes('burners') || s.includes('channels') || s.includes('elements') || s.includes('vessel')) userOwnedCategories.push('Pattern'); + else if (s.includes('nutrition') || s.includes('food')) userOwnedCategories.push('Nutrition'); + else if (s.includes('therapeutic') || s.includes('gynae') || s.includes('auriculo')) userOwnedCategories.push('Therapeutics'); + } + } + const uniqueOwnedCategories = Array.from(new Set(userOwnedCategories)); + const res = await axios.post(`${API_BASE}/api/cart/calculate`, { cart_item_codes: cart.map(item => item.code), + owned_category_names: uniqueOwnedCategories, + is_first_purchase: uniqueOwnedCategories.length === 0, coupon_code: appliedCoupon || undefined, currency: currency }, { headers }); diff --git a/src/components/cart/CartDrawer.tsx b/src/components/cart/CartDrawer.tsx index 9c7c244..1e69f7e 100644 --- a/src/components/cart/CartDrawer.tsx +++ b/src/components/cart/CartDrawer.tsx @@ -198,7 +198,13 @@ export const CartDrawer: React.FC = ({ {pricingCalc.bundle_discount_amount > 0 && (
- Bundle Discount + + {pricingCalc.discount_policy && pricingCalc.discount_policy !== 'None' + ? (pricingCalc.discount_policy.startsWith('Staggered Buy:') + ? pricingCalc.discount_policy.replace('Staggered Buy:', 'Loyalty Discount:') + : pricingCalc.discount_policy) + : 'Loyalty Discount'} + -{currency === 'INR' ? `₹${pricingCalc.bundle_discount_amount}` diff --git a/src/types/index.ts b/src/types/index.ts index b8a5891..d3f3a40 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -34,12 +34,17 @@ export interface UserProfile { roles?: string[]; external_ref?: string; active_subscriptions: UserSubscription[]; + unlocked_module_slugs?: string[]; created_at?: string; } export interface PricingCalculation { + base_subtotal?: number; original_price: number; discount_amount: number; + discount_policy?: string; + bundle_discount_amount?: number; + total_discounts?: number; taxable_amount: number; gst_amount: number; convenience_fee?: number; @@ -47,6 +52,9 @@ export interface PricingCalculation { grand_total: number; total_amount?: number; currency: Currency; + coupon_applied?: string; + coupon_discount_amount?: number; + coupon_error?: string | null; applied_coupon?: { code: string; discount_type: 'percentage' | 'fixed';