feat(api): shop coupons with per-shop checkout redemption

Templates belong to a shop; claiming copies their terms into a customer-owned
snapshot so a later edit or disable cannot rewrite a held coupon. Claim stock
is taken with a guarded decrement after locking the template, and a unique
(user, template) index makes a duplicate claim a 409. Deleting a template
leaves claimed snapshots standing via ON DELETE SET NULL.

Checkout accepts at most one owned coupon per generated shop order, locks the
selected coupons by primary key after the SKU locks, and resolves eligibility
and the discount server-side (ownership, shop, status, window, converted
threshold). The realized discount and coupon id land on the order, and a
pending-payment cancellation restores the coupon in the same transaction as
stock.

The shared contract gains the coupon types, claim/list/manage methods, and the
checkout coupon map; the fixed-data adapter implements the same surface.

Surfaces (shop-admin management, mall coupon pages, checkout selection) and
seeding still follow in tasks 3.1-4.2.
This commit is contained in:
2026-09-18 12:03:00 +00:00
parent 39d0158f29
commit 23955434c6
20 changed files with 1363 additions and 46 deletions
+8 -1
View File
@@ -19,7 +19,8 @@ type LiveDomain =
| "orders"
| "shipments"
| "invoices"
| "addresses";
| "addresses"
| "coupons";
/**
* Explicit per-domain method picks rather than a string allowlist: indexing
@@ -66,6 +67,11 @@ const LIVE_PICKS = {
deleteAddress: a.deleteAddress,
setDefaultAddress: a.setDefaultAddress,
}),
coupons: (a: ApiClient) => ({
listShopCouponTemplates: a.listShopCouponTemplates,
listMyCoupons: a.listMyCoupons,
claimCoupon: a.claimCoupon,
}),
} satisfies Record<LiveDomain, (a: ApiClient) => Partial<ApiClient>>;
const KNOWN_DOMAINS = Object.keys(LIVE_PICKS) as LiveDomain[];
@@ -84,6 +90,7 @@ const DEFAULT_LIVE_DOMAINS: LiveDomain[] = [
"shipments",
"invoices",
"addresses",
"coupons",
];
export default defineNuxtPlugin(() => {