Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions rest/nodejs/src/api/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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") {
Expand All @@ -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 });
}
Expand Down
30 changes: 30 additions & 0 deletions rest/nodejs/test/discount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Loading