diff --git a/rest/nodejs/src/api/checkout.ts b/rest/nodejs/src/api/checkout.ts index 6643192..fff5725 100644 --- a/rest/nodejs/src/api/checkout.ts +++ b/rest/nodejs/src/api/checkout.ts @@ -412,7 +412,12 @@ export class CheckoutService { code, title: "10% Off", amount: discountAmount, - allocations: [{ path: "subtotal", amount: discountAmount }], + allocations: [ + { + path: "$.totals[?(@.type=='subtotal')]", + amount: discountAmount, + }, + ], }); checkout.totals.push({ type: "discount", amount: -discountAmount }); } else if (upperCode === "WELCOME20") { @@ -422,7 +427,12 @@ export class CheckoutService { code, title: "Welcome 20% Off", amount: discountAmount, - allocations: [{ path: "subtotal", amount: discountAmount }], + allocations: [ + { + path: "$.totals[?(@.type=='subtotal')]", + amount: discountAmount, + }, + ], }); checkout.totals.push({ type: "discount", amount: -discountAmount }); } else if (upperCode === "FIXED500") { @@ -432,7 +442,12 @@ export class CheckoutService { code, title: "$5.00 Off", amount: discountAmount, - allocations: [{ path: "subtotal", amount: discountAmount }], + allocations: [ + { + path: "$.totals[?(@.type=='subtotal')]", + amount: discountAmount, + }, + ], }); checkout.totals.push({ type: "discount", amount: -discountAmount }); } diff --git a/rest/nodejs/test/discount.test.ts b/rest/nodejs/test/discount.test.ts index b9bc8ca..bc975c4 100644 --- a/rest/nodejs/test/discount.test.ts +++ b/rest/nodejs/test/discount.test.ts @@ -132,3 +132,33 @@ test("an applied discount's allocations sum to its amount", () => { } assert.ok(checked > 0, "expected at least one discount carrying allocations"); }); + +// An allocation target must be expressed as a JSONPath (discount.json: +// allocation.path), so a platform can resolve where the discount applied — +// not a bare label like "subtotal". +test("an applied discount's allocation path is a JSONPath", () => { + const checkout = checkoutWithCodes(["10OFF"]); + new CheckoutService()["recalculateTotals"](checkout); + + const applied = ( + checkout as unknown as { + discounts: { + applied: Array<{ + allocations?: Array<{ path: string; amount: number }>; + }>; + }; + } + ).discounts.applied; + + let checked = 0; + for (const a of applied) { + for (const alloc of a.allocations ?? []) { + checked += 1; + assert.ok( + alloc.path.startsWith("$."), + `allocation path "${alloc.path}" must be a JSONPath rooted at "$."` + ); + } + } + assert.ok(checked > 0, "expected at least one allocation to check"); +});