enh
This commit is contained in:
parent
12fb0db4c9
commit
c1becb82a7
10 changed files with 116 additions and 12 deletions
|
|
@ -840,6 +840,7 @@ known stale-test bug.
|
|||
| UORD-45 | edge | usage-limit coupon | rejected |
|
||||
| UORD-46 | edge | min-order not met | rejected/not applied |
|
||||
| UORD-47 | edge | targeted SKU coupon | discount only target |
|
||||
| UORD-47B | edge | targeted SKU coupon, target below minOrder but whole cart above it | accepted; discount only on target |
|
||||
| UORD-48 | edge | first-order-only | first works, second rejects |
|
||||
| UORD-49 | edge | exclusive coupon + another | reject or exclusive wins per contract |
|
||||
| UORD-50 | edge | 100%+ percentage coupon | total never negative; delivery still correct |
|
||||
|
|
|
|||
|
|
@ -1050,6 +1050,30 @@ describe('user.order.placeOrder — coupons (UORD-39..56)', () => {
|
|||
expect(Number(res.data[0].totalAmount)).toBe(100 + 300 * quantityOther - 50)
|
||||
})
|
||||
|
||||
it('UORD-47B: a targeted coupon measures minOrder on the whole cart, discounts only its SKU', async () => {
|
||||
const store = await createStore()
|
||||
const target = await createProduct({ storeId: store.storeId, price: 100 })
|
||||
const other = await createProduct({ storeId: store.storeId, price: 300 })
|
||||
const slot = await createFutureSlot([target.skuId, other.skuId])
|
||||
const quantityOther = 10
|
||||
// Target value is ₹100 (< ₹500), but the cart is ₹3,100.
|
||||
const c = await makeCoupon({ flatDiscount: 50, skuIds: [target.skuId], minOrder: 500 })
|
||||
|
||||
const user = await registerThrowawayUser()
|
||||
const address = await createAddress(user.token)
|
||||
const res = await placeCall(user.token, {
|
||||
addressId: address.id,
|
||||
items: [
|
||||
{ skuId: target.skuId, quantity: 1, slotId: slot.slotId },
|
||||
{ skuId: other.skuId, quantity: quantityOther, slotId: slot.slotId },
|
||||
],
|
||||
couponIds: [c.id],
|
||||
})
|
||||
// The discount base stays the targeted ₹100, so the ₹50 flat fully applies.
|
||||
expect(Number(res.data[0].discountAmount)).toBe(50)
|
||||
expect(Number(res.data[0].totalAmount)).toBe(100 + 300 * quantityOther - 50)
|
||||
})
|
||||
|
||||
it('UORD-48: a first-order-only coupon works once and is rejected afterwards', async () => {
|
||||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||||
const c = await makeCoupon({ flatDiscount: 20, minOrder: 1, isFirstOrderOnly: true })
|
||||
|
|
|
|||
|
|
@ -760,6 +760,7 @@ real.
|
|||
| CART-63 | edge | flat capped at item total | never below zero |
|
||||
| CART-64 | edge | discount drops total below threshold | delivery fee re-added |
|
||||
| CART-65 | edge | minOrder not met | disabled option with reason; cannot select |
|
||||
| CART-65B | edge | targeted coupon, target subtotal below minOrder but whole cart above it | option stays enabled; discount lands only on the targeted SKU |
|
||||
| CART-66 | edge | usage limit reached | disabled option |
|
||||
| CART-67 | edge | other-region coupon | disabled reason |
|
||||
| CART-68 | edge | first-order-only coupon and non-first order | disabled reason |
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ const UpdateChecker: React.FC<ReactParentComponent> = ({ children }) => {
|
|||
|
||||
useEffect(() => {
|
||||
// Never nag in development; the dev build talks to a dev backend.
|
||||
if (__DEV__) return;
|
||||
// if (__DEV__) return;
|
||||
if (prompted.current) return;
|
||||
|
||||
const remoteVersion = data?.internalAppVersion;
|
||||
|
|
|
|||
|
|
@ -876,6 +876,33 @@ describe('Cart (scheduled) — CART-01..110', () => {
|
|||
expect(queryByTestId('coupon-chip-MIN1000')).toBeNull()
|
||||
})
|
||||
|
||||
it('CART-65B: a targeted coupon allows minOrder on the whole cart but discounts only its SKU', async () => {
|
||||
const targeted = {
|
||||
...C3,
|
||||
id: 65,
|
||||
couponCode: 'TGT65',
|
||||
minOrder: 500,
|
||||
flatDiscount: 50,
|
||||
skuIds: [2],
|
||||
}
|
||||
const { getByTestId, getByText } = await mount({
|
||||
// Chicken (targeted) is ₹215, cucumber (other) is ₹300 -> cart ₹515.
|
||||
lines: [line(1, 2, 1, 1849), line(2, 5, 20, 1849)],
|
||||
setup: () =>
|
||||
setProcedure('user.coupon.getEligible', {
|
||||
data: { data: [targeted], isFirstOrder: true },
|
||||
}),
|
||||
})
|
||||
fireEvent.press(getByTestId('coupon-dropdown'))
|
||||
await flush(1)
|
||||
// ₹215 of eligible product is below ₹500, but the cart is ₹515.
|
||||
expect(getByTestId('coupon-dropdown-option-65')).not.toBeDisabled()
|
||||
fireEvent.press(getByTestId('coupon-dropdown-option-65'))
|
||||
await flush(2)
|
||||
expect(getByTestId('coupon-bill-65')).toBeTruthy()
|
||||
expect(getByText('-₹50')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('CART-66: a coupon at its usage limit is disabled', async () => {
|
||||
const used = { ...C6, usages: [{ id: 1 }], maxLimitForUser: 1 }
|
||||
const { getByTestId, getByText } = await mount({
|
||||
|
|
|
|||
9
bun.lock
9
bun.lock
|
|
@ -30,6 +30,7 @@
|
|||
"devDependencies": {
|
||||
"@babel/core": "^7.25.2",
|
||||
"@playwright/test": "^1.63.0",
|
||||
"@types/bun": "^1.4.4",
|
||||
"@types/canvas-confetti": "^1.9.0",
|
||||
"@types/node": "^22.10.2",
|
||||
"@types/react": "~19.0.10",
|
||||
|
|
@ -1124,6 +1125,8 @@
|
|||
|
||||
"@types/better-sqlite3": ["@types/better-sqlite3@7.6.13", "", { "dependencies": { "@types/node": "*" } }, "sha512-NMv9ASNARoKksWtsq/SHakpYAYnhBrQgGD8zkLYk/jaK8jUGn08CfEdTRgYhMypUQAfzSP8W6gNLe0q19/t4VA=="],
|
||||
|
||||
"@types/bun": ["@types/bun@1.4.4", "", { "dependencies": { "bun-types": "1.4.4" } }, "sha512-9wkRDXUXK124pEnAutE6n/l18j+i9GC5IAHK8TmX/2nlySETmky0OU6yL5U8pW1AoKh++AN9auDCr647BfurjQ=="],
|
||||
|
||||
"@types/canvas-confetti": ["@types/canvas-confetti@1.9.0", "", {}, "sha512-aBGj/dULrimR1XDZLtG9JwxX1b4HPRF6CX9Yfwh3NvstZEm1ZL7RBnel4keCPSqs1ANRu1u2Aoz9R+VmtjYuTg=="],
|
||||
|
||||
"@types/draco3d": ["@types/draco3d@1.4.10", "", {}, "sha512-AX22jp8Y7wwaBgAixaSvkoG4M/+PlAcm3Qs4OW8yT9DM4xUpWKeFhLueTAyZF39pviAdcDdeJoACapiAceqNcw=="],
|
||||
|
|
@ -1420,6 +1423,8 @@
|
|||
|
||||
"buffer-from": ["buffer-from@1.1.2", "", {}, "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ=="],
|
||||
|
||||
"bun-types": ["bun-types@1.4.4", "", { "dependencies": { "@types/node": "*", "undici-types": "*" } }, "sha512-yjrJh5oGX7mMPKGd19YXyIjmEuj4G4zNC30bs7PM9Az7NQ3ndF3j6hbeRdvJTRQEhJ/FYQwW0a2moweEi2hMxQ=="],
|
||||
|
||||
"bytes": ["bytes@3.1.2", "", {}, "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg=="],
|
||||
|
||||
"call-bind": ["call-bind@1.0.8", "", { "dependencies": { "call-bind-apply-helpers": "^1.0.0", "es-define-property": "^1.0.0", "get-intrinsic": "^1.2.4", "set-function-length": "^1.2.2" } }, "sha512-oKlSFMcMwpUg2ednkhQ454wfWiU/ul3CkJe/PEHcTKuiX6RpbehUiFMXu13HalGZxfUwCQzZG747YXBn1im9ww=="],
|
||||
|
|
@ -3408,6 +3413,10 @@
|
|||
|
||||
"body-parser/debug": ["debug@2.6.9", "", { "dependencies": { "ms": "2.0.0" } }, "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA=="],
|
||||
|
||||
"bun-types/@types/node": ["@types/node@24.12.0", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-GYDxsZi3ChgmckRT9HPU0WEhKLP08ev/Yfcq2AstjrDASOYCSXeyjDsHg4v5t4jOj7cyDX3vmprafKlWIG9MXQ=="],
|
||||
|
||||
"bun-types/undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
|
||||
|
||||
"caller-callsite/callsites": ["callsites@2.0.0", "", {}, "sha512-ksWePWBloaWPxJYQ8TL0JHvtci6G5QTKwQ95RcWAa/lzoAKuAOflGdAK92hpHXjkwb8zLxoLNUoNYZgVsaJzvQ=="],
|
||||
|
||||
"chokidar/glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="],
|
||||
|
|
|
|||
|
|
@ -8793,4 +8793,26 @@ typing error (unrelated to this change).
|
|||
Cause: ProductCard.tsx sets an unused `updater` state every 5000ms; the state
|
||||
value is never read, but it forces every card to re-render forever.
|
||||
- Me was idle after HMR settled (~2 commits/minute). Home was idle after entry.
|
||||
A >15 commits/s burst during HMR was the only high-frequency window.
|
||||
A >15 commits/s during HMR was the only high-frequency window.
|
||||
|
||||
[2026-10-23 14:17:42 IST] Coupons: minimum order is the whole cart, discount stays on targeted SKUs.
|
||||
Planned diffs:
|
||||
- packages/shared/cart-pricing.ts: add `computeCartSubtotal`; compare
|
||||
`minOrder` against the full in-stock goods subtotal in
|
||||
`evaluateCouponEligibility`, while keeping `eligibleSubtotal` (targeted lines)
|
||||
as the discount base; update the invariant/doc comments.
|
||||
- packages/db_helper_sqlite/src/user-apis/order.ts: compare `minOrder` against
|
||||
the full cart subtotal in `validateAndGetCoupons`; update comments.
|
||||
- apps/user-ui/components/cart-page.tsx / checkout-page.tsx: no logic change —
|
||||
they call `evaluateCouponEligibility`, so the new rule flows through.
|
||||
- apps/user-ui/tests/pages/cart.test.tsx: add a regression test (CART-65B) where
|
||||
the targeted SKU is below minOrder but the whole cart is above it.
|
||||
- apps/backend/tests/api/user-order-place.test.ts: add UORD-47B covering the same
|
||||
server-side rule.
|
||||
- apps/user-ui/JEST-TEST-CASES.md: document CART-65B.
|
||||
- apps/backend/API-TEST-CASES.md: document UORD-47B.
|
||||
No migrations run. No test suite run (repo instruction).
|
||||
Verified: `npx tsc --noEmit` clean in packages/db_helper_sqlite and apps/backend;
|
||||
`apps/backend/tests/tsconfig.json` clean. `apps/user-ui` typecheck reports only
|
||||
the two pre-existing `typeof fetch as Mock` errors in addresses/payment tests;
|
||||
no cart/checkout errors from this change.
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@
|
|||
"devDependencies": {
|
||||
"@babel/core": "^7.25.2",
|
||||
"@playwright/test": "^1.63.0",
|
||||
"@types/bun": "^1.4.4",
|
||||
"@types/canvas-confetti": "^1.9.0",
|
||||
"@types/node": "^22.10.2",
|
||||
"@types/react": "~19.0.10",
|
||||
|
|
|
|||
|
|
@ -25,7 +25,11 @@ import { runBatched } from '../lib/run-batched'
|
|||
import { attachOrderItems } from '../lib/order-items'
|
||||
import { composeSkuName, composeUnitNotation } from '../lib/sku-features'
|
||||
import { marketStatsForRegion } from '../lib/market-stats'
|
||||
import { computeEligibleSubtotal, type CartLine } from '@packages/shared'
|
||||
import {
|
||||
computeCartSubtotal,
|
||||
computeEligibleSubtotal,
|
||||
type CartLine,
|
||||
} from '@packages/shared'
|
||||
|
||||
export interface OrderWithRelations {
|
||||
id: number
|
||||
|
|
@ -110,8 +114,9 @@ export interface ValidatedCoupon extends CouponValidationResult {
|
|||
* - user-based applicability, invalidated, expiry, per-user usage limit
|
||||
* - region: a regional coupon is only valid in its own region
|
||||
* - first-order-only (via countEffectiveOrders)
|
||||
* - product targeting: the coupon must match at least one cart line, and
|
||||
* minOrder is compared against the ELIGIBLE (targeted) subtotal
|
||||
* - product targeting: the coupon must match at least one cart line
|
||||
* - minOrder: compared against the WHOLE cart's goods subtotal (delivery
|
||||
* excluded), even when the coupon only discounts targeted lines
|
||||
*/
|
||||
export async function validateAndGetCoupons(params: {
|
||||
couponIds: number[]
|
||||
|
|
@ -185,13 +190,15 @@ export async function validateAndGetCoupons(params: {
|
|||
const eligibleSubtotal = computeEligibleSubtotal(params.items, {
|
||||
applicableSkuIds: targets,
|
||||
})
|
||||
// The minimum is cart-wide; targeting only limits what the discount lands on.
|
||||
const cartSubtotal = computeCartSubtotal(params.items)
|
||||
|
||||
if (targets && eligibleSubtotal <= 0) {
|
||||
throw new Error('Coupon does not apply to any items in your cart')
|
||||
}
|
||||
if (
|
||||
coupon.minOrder &&
|
||||
parseFloat(coupon.minOrder.toString()) > eligibleSubtotal
|
||||
parseFloat(coupon.minOrder.toString()) > cartSubtotal
|
||||
) {
|
||||
throw new Error('Order amount does not meet coupon minimum requirement')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
* is never part of the base.
|
||||
* - A coupon with no `applicableSkuIds` applies to the whole cart; otherwise it
|
||||
* only discounts the line items whose skuId is in the target set.
|
||||
* - `minOrder` is measured on the WHOLE cart's goods subtotal, even for a
|
||||
* targeted coupon; targeting only narrows the discount base.
|
||||
* - Multiple coupons apply SEQUENTIALLY, in the order they are given (the user's
|
||||
* selection order): the first coupon discounts the subtotal, the second discounts
|
||||
* whatever is left, and so on. Each coupon's discount is rounded to the nearest
|
||||
|
|
@ -85,6 +87,11 @@ export function lineEligibleForCoupon(
|
|||
return (coupon.applicableSkuIds as number[]).includes(skuId)
|
||||
}
|
||||
|
||||
/** Sum of every line's goods value — the whole cart, before delivery. */
|
||||
export function computeCartSubtotal(items: CartLine[]): number {
|
||||
return items.reduce((sum, item) => sum + item.unitPrice * item.quantity, 0)
|
||||
}
|
||||
|
||||
/** Sum of the line items this coupon discounts (whole cart when untargeted). */
|
||||
export function computeEligibleSubtotal(
|
||||
items: CartLine[],
|
||||
|
|
@ -152,7 +159,10 @@ export interface CouponEligibilityFields {
|
|||
}
|
||||
|
||||
export interface CouponEligibilityContext {
|
||||
/** in-stock cart lines — exactly what the coupon would discount */
|
||||
/**
|
||||
* in-stock cart lines — the targeted ones form the discount base; all of them
|
||||
* form the minOrder base
|
||||
*/
|
||||
cartLines: CartLine[]
|
||||
/** the region the cart is being built in */
|
||||
regionId: number | null
|
||||
|
|
@ -172,9 +182,10 @@ export interface CouponEligibility {
|
|||
* Whether a coupon can be applied to this cart, and why not when it can't.
|
||||
* Rule order matches the backend: region, then first-order-only, then product
|
||||
* targeting, then minOrder — targeting BEFORE minOrder, because a coupon whose
|
||||
* targets are all missing has an eligible subtotal of 0 and "add ₹X more" would
|
||||
* be the wrong reason (the server reports "does not apply to any items").
|
||||
* minOrder is compared against the ELIGIBLE subtotal, exactly as the server does.
|
||||
* targets are all missing must report "no applicable items" instead of a
|
||||
* misleading "add ₹X more". minOrder is compared against the WHOLE cart's goods
|
||||
* subtotal (delivery excluded), exactly as the server does; `eligibleSubtotal`
|
||||
* remains the targeted-line base the discount is calculated on.
|
||||
*/
|
||||
export function evaluateCouponEligibility(
|
||||
coupon: CouponEligibilityFields,
|
||||
|
|
@ -209,9 +220,10 @@ export function evaluateCouponEligibility(
|
|||
return fail('No applicable items in your cart')
|
||||
}
|
||||
|
||||
const cartSubtotal = computeCartSubtotal(context.cartLines)
|
||||
const minOrder = toNum(coupon.minOrder)
|
||||
if (minOrder > eligibleSubtotal) {
|
||||
return fail(`Add ₹${Math.ceil(minOrder - eligibleSubtotal)} more to use this coupon`)
|
||||
if (minOrder > cartSubtotal) {
|
||||
return fail(`Add ₹${Math.ceil(minOrder - cartSubtotal)} more to use this coupon`)
|
||||
}
|
||||
|
||||
return { isEligible: true, eligibleSubtotal }
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue