diff --git a/apps/backend/API-TEST-CASES.md b/apps/backend/API-TEST-CASES.md index bebe055..33d6a94 100644 --- a/apps/backend/API-TEST-CASES.md +++ b/apps/backend/API-TEST-CASES.md @@ -907,6 +907,7 @@ known stale-test bug. | UORD-106 | edge | cancel publishes cancellation | queue/publish called; failure logged | | UORD-107 | edge | double cancel race | one success/one failure, no corrupt status | | UORD-108 | edge | order with orderGroup | cancellation updates group/status consistently | +| UORD-108B | edge | grouped order with a usage-capped coupon; cancel siblings one by one | coupon stays exhausted until the last sibling is cancelled, then becomes reusable | ### 6.12 User product API diff --git a/apps/backend/tests/api/user-order-lifecycle.test.ts b/apps/backend/tests/api/user-order-lifecycle.test.ts index 9989898..af971b1 100644 --- a/apps/backend/tests/api/user-order-lifecycle.test.ts +++ b/apps/backend/tests/api/user-order-lifecycle.test.ts @@ -621,10 +621,10 @@ describe('user.order cancelOrder (UORD-98..103, 107)', () => { }) /* -------------------------------------------------------------------------- */ -/* 5. Cancellation side-effects (UORD-104..106, 108) */ +/* 5. Cancellation side-effects (UORD-104..106, 108, 108B) */ /* -------------------------------------------------------------------------- */ -describe('user.order cancellation side-effects (UORD-104..106, 108)', () => { +describe('user.order cancellation side-effects (UORD-104..106, 108, 108B)', () => { it('UORD-104: cancelling releases the coupon usage so it can be used again', async () => { const fx = await createCatalogOrderFixture({ price: 199 }) const c = await makeCoupon({ flatDiscount: 20, minOrder: 1, maxLimitForUser: 1 }) @@ -712,4 +712,44 @@ describe('user.order cancellation side-effects (UORD-104..106, 108)', () => { expect(e0.couponCode).toBeNull() expect(e1.couponCode).toBeNull() }) + + it('UORD-108B: a grouped coupon is restored only after every sibling is cancelled', async () => { + const fx = await createCatalogOrderFixture({ price: 199 }) + const slot2 = await createFutureSlot([fx.product.skuId]) + const c = await makeCoupon({ flatDiscount: 50, minOrder: 1, maxLimitForUser: 1 }) + const res = await placeCall(fx.user.token, { + addressId: fx.address.id, + items: [ + { skuId: fx.product.skuId, quantity: 1, slotId: fx.slot.slotId }, + { skuId: fx.product.skuId, quantity: 1, slotId: slot2.slotId }, + ], + couponIds: [c.id], + }) + const ids = res.data.map((o: any) => Number(o.id)) + + // Cancel one sibling: the group still has a live order, so the coupon stays used. + await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: ids[0], reason: 'first' }) + await expectTrpcError( + () => + placeCall(fx.user.token, { + addressId: fx.address.id, + skuId: fx.product.skuId, + slotId: fx.slot.slotId, + couponIds: [c.id], + }), + 'INTERNAL_SERVER_ERROR', + 'usage limit exceeded' + ) + + // Cancel the last sibling: the group is fully cancelled, so the usage is released. + await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id: ids[1], reason: 'second' }) + const after = await placeCall(fx.user.token, { + addressId: fx.address.id, + skuId: fx.product.skuId, + slotId: fx.slot.slotId, + couponIds: [c.id], + }) + expect(after.success).toBe(true) + expect(Number(after.data[0].discountAmount)).toBe(50) + }) }) diff --git a/change-log.txt b/change-log.txt index 1fb720f..3b83a98 100644 --- a/change-log.txt +++ b/change-log.txt @@ -8816,3 +8816,15 @@ 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. + +[2026-10-23 15:06:49 IST] Test grouped coupon restoration: only after the last sibling is cancelled. +The implementation already does this: `releaseCouponUsageIfGroupFullyCancelled` +deletes the group's `coupon_usage` rows only when every order in `order_group_id` +has `is_cancelled = true`. Existing UORD-108 checks couponCode disappears, but +uses no usage cap and never retries the coupon. Add a stronger regression test: +- apps/backend/tests/api/user-order-lifecycle.test.ts: UORD-108B places a + two-slot group with `maxLimitForUser: 1`; cancelling one sibling must leave the + coupon exhausted for a new order, and cancelling the last sibling must make the + same coupon usable again. +- apps/backend/API-TEST-CASES.md: document UORD-108B. +No production code change. No test suite run (repo instruction).