@classytic/cart 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -10
- package/dist/{cart-event-catalog-BIbi2Q_n.d.mts → cart-event-catalog-BxDJhtTM.d.mts} +1 -1
- package/dist/cart.error-BwWy4RlY.mjs +27 -0
- package/dist/{create-cart-q3CXxPP7.mjs → create-cart-B4l61iKT.mjs} +589 -543
- package/dist/domain/errors/index.d.mts +1 -1
- package/dist/domain/errors/index.mjs +5 -3
- package/dist/domain/events/index.d.mts +2 -2
- package/dist/domain/events/index.mjs +2 -2
- package/dist/domain/index.d.mts +11 -11
- package/dist/domain/index.mjs +11 -10
- package/dist/domain/ports/index.d.mts +2 -2
- package/dist/domain/ports/index.mjs +1 -1
- package/dist/domain/saga/index.d.mts +2 -2
- package/dist/domain/saga/index.mjs +2 -3
- package/dist/engine/index.d.mts +1 -1
- package/dist/engine/index.mjs +1 -1
- package/dist/{errors-CHR9RlbY.mjs → errors-DzR7Pipp.mjs} +27 -27
- package/dist/{index-Doi5uzux.d.mts → index-BFBUk6qT.d.mts} +68 -68
- package/dist/index.d.mts +73 -58
- package/dist/index.mjs +50 -49
- package/dist/{internal-event-bus-IkUfQJxn.d.mts → internal-event-bus-DKcDteh-.d.mts} +109 -84
- package/dist/{line-item.entity-B3M_i-pw.d.mts → kind-registry-C1n47C7h.d.mts} +52 -52
- package/dist/{kind-registry-Ba4PO4WC.mjs → kind-registry-C99h_-8t.mjs} +1 -1
- package/dist/{kind.error-CCjTB6kW.mjs → kind.error-DIaJpdUb.mjs} +2 -27
- package/dist/kinds/index.d.mts +1 -1
- package/dist/kinds/index.mjs +1 -1
- package/dist/{line-kind-Bndo6IS8.d.mts → line-kind-Ck3fVXzG.d.mts} +1 -1
- package/dist/{cart-event-catalog-BOP9aDhL.mjs → outbox-store-CWW8ppxS.mjs} +31 -31
- package/dist/{not-found.error-B6WYnAjX.mjs → price-changed.error-C-wGVcfc.mjs} +23 -47
- package/dist/{idempotency-record.entity-DIFXjN1V.d.mts → reservation.entity-b5cZ1ojx.d.mts} +72 -72
- package/dist/{shipping.bridge-DD4A_O0O.d.mts → shipping.bridge-CmQFTgSE.d.mts} +82 -82
- package/dist/state-transition.error-2fRLh3p0.mjs +26 -0
- package/dist/{saga-step-fn-Cyob38iU.d.mts → state-transitions-X_Eix5Iw.d.mts} +31 -30
- package/dist/state-transitions-e4z3Yhxb.mjs +61 -0
- package/dist/{bundle.kind-BdgnyKZt.d.mts → variant.kind-9P1DXdXu.d.mts} +51 -51
- package/dist/{bundle.kind-DFydSxV_.mjs → variant.kind-DfEh2oku.mjs} +133 -133
- package/package.json +14 -7
- package/dist/saga-steps-B27LI7xp.mjs +0 -27
- package/dist/state-transitions-CpJpZZTK.mjs +0 -21
- /package/dist/{cart-status.vo-2iisZGhb.d.mts → cart-status.vo-CNJw4aaB.d.mts} +0 -0
- /package/dist/{domain-event-DHNkz15G.mjs → domain-event-CIEORKF6.mjs} +0 -0
- /package/dist/{id.port-C3rlDAKP.mjs → id.port-BhbFZJPm.mjs} +0 -0
- /package/dist/{id.port-DPXLrqb2.d.mts → id.port-C7ygWLRU.d.mts} +0 -0
- /package/dist/{index-DEHBdV_z.d.mts → index-2Ag-54t8.d.mts} +0 -0
- /package/dist/{outbox-store-CTWz9pvG.d.mts → outbox-store-CBt9fSYh.d.mts} +0 -0
|
@@ -1,6 +1,36 @@
|
|
|
1
|
-
import { r as LineKind } from "./line-kind-
|
|
1
|
+
import { r as LineKind } from "./line-kind-Ck3fVXzG.mjs";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
|
|
4
|
+
//#region src/kinds/bundle.kind.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* `bundle` — a group of products sold together at a combined price.
|
|
7
|
+
*
|
|
8
|
+
* The bundle resolves as a single catalog entry whose `unitPrice` is the
|
|
9
|
+
* bundle price (not the sum of component prices). Components are stored
|
|
10
|
+
* in `payload.components` for display and fulfillment, but pricing uses
|
|
11
|
+
* only the bundle's catalog entry.
|
|
12
|
+
*
|
|
13
|
+
* Validity is `reprice` — bundle prices can drift when component prices
|
|
14
|
+
* change, and silent repricing is acceptable (unlike contested resources
|
|
15
|
+
* like seats).
|
|
16
|
+
*/
|
|
17
|
+
interface BundleComponent {
|
|
18
|
+
skuRef: string;
|
|
19
|
+
quantity: number;
|
|
20
|
+
}
|
|
21
|
+
interface BundlePayload {
|
|
22
|
+
bundleRef: string;
|
|
23
|
+
components: BundleComponent[];
|
|
24
|
+
}
|
|
25
|
+
declare const bundlePayloadSchema: z.ZodObject<{
|
|
26
|
+
bundleRef: z.ZodString;
|
|
27
|
+
components: z.ZodArray<z.ZodObject<{
|
|
28
|
+
skuRef: z.ZodString;
|
|
29
|
+
quantity: z.ZodNumber;
|
|
30
|
+
}, z.core.$strip>>;
|
|
31
|
+
}, z.core.$strip>;
|
|
32
|
+
declare const bundleKind: LineKind<BundlePayload>;
|
|
33
|
+
//#endregion
|
|
4
34
|
//#region src/kinds/sku.kind.d.ts
|
|
5
35
|
/**
|
|
6
36
|
* `sku` — the plain retail LineKind.
|
|
@@ -24,32 +54,6 @@ declare const skuPayloadSchema: z.ZodObject<{
|
|
|
24
54
|
}, z.core.$strip>;
|
|
25
55
|
declare const skuKind: LineKind<SkuPayload>;
|
|
26
56
|
//#endregion
|
|
27
|
-
//#region src/kinds/variant.kind.d.ts
|
|
28
|
-
/**
|
|
29
|
-
* `variant` — a catalog SKU with structured variant selection.
|
|
30
|
-
*
|
|
31
|
-
* Extends the `sku` kind with an explicit `variantSku` field (e.g.,
|
|
32
|
-
* "shirt-m-blue") that maps to a specific product variant. The catalog
|
|
33
|
-
* bridge resolves the variant SKU to a `ProductSummary` which carries
|
|
34
|
-
* variant-specific pricing, images, and the pre-formatted
|
|
35
|
-
* `variantLabel` ("Size: M, Color: Blue").
|
|
36
|
-
*
|
|
37
|
-
* Use this when the product has selectable options (size, color, material)
|
|
38
|
-
* and each combination has its own price and stock. Use plain `sku` when
|
|
39
|
-
* the product is a single item with no options.
|
|
40
|
-
*/
|
|
41
|
-
interface VariantPayload {
|
|
42
|
-
/** The parent product's stable identifier. */
|
|
43
|
-
productRef: string;
|
|
44
|
-
/** The specific variant's SKU — the one the catalog resolves for pricing. */
|
|
45
|
-
variantSku: string;
|
|
46
|
-
}
|
|
47
|
-
declare const variantPayloadSchema: z.ZodObject<{
|
|
48
|
-
productRef: z.ZodString;
|
|
49
|
-
variantSku: z.ZodString;
|
|
50
|
-
}, z.core.$strip>;
|
|
51
|
-
declare const variantKind: LineKind<VariantPayload>;
|
|
52
|
-
//#endregion
|
|
53
57
|
//#region src/kinds/timeslot.kind.d.ts
|
|
54
58
|
/**
|
|
55
59
|
* `timeslot` — an appointment or booking slot in a mixed basket.
|
|
@@ -81,34 +85,30 @@ declare const timeslotPayloadSchema: z.ZodObject<{
|
|
|
81
85
|
}, z.core.$strip>;
|
|
82
86
|
declare const timeslotKind: LineKind<TimeslotPayload>;
|
|
83
87
|
//#endregion
|
|
84
|
-
//#region src/kinds/
|
|
88
|
+
//#region src/kinds/variant.kind.d.ts
|
|
85
89
|
/**
|
|
86
|
-
* `
|
|
90
|
+
* `variant` — a catalog SKU with structured variant selection.
|
|
87
91
|
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
+
* Extends the `sku` kind with an explicit `variantSku` field (e.g.,
|
|
93
|
+
* "shirt-m-blue") that maps to a specific product variant. The catalog
|
|
94
|
+
* bridge resolves the variant SKU to a `ProductSummary` which carries
|
|
95
|
+
* variant-specific pricing, images, and the pre-formatted
|
|
96
|
+
* `variantLabel` ("Size: M, Color: Blue").
|
|
92
97
|
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
98
|
+
* Use this when the product has selectable options (size, color, material)
|
|
99
|
+
* and each combination has its own price and stock. Use plain `sku` when
|
|
100
|
+
* the product is a single item with no options.
|
|
96
101
|
*/
|
|
97
|
-
interface
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
bundleRef: string;
|
|
103
|
-
components: BundleComponent[];
|
|
102
|
+
interface VariantPayload {
|
|
103
|
+
/** The parent product's stable identifier. */
|
|
104
|
+
productRef: string;
|
|
105
|
+
/** The specific variant's SKU — the one the catalog resolves for pricing. */
|
|
106
|
+
variantSku: string;
|
|
104
107
|
}
|
|
105
|
-
declare const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
skuRef: z.ZodString;
|
|
109
|
-
quantity: z.ZodNumber;
|
|
110
|
-
}, z.core.$strip>>;
|
|
108
|
+
declare const variantPayloadSchema: z.ZodObject<{
|
|
109
|
+
productRef: z.ZodString;
|
|
110
|
+
variantSku: z.ZodString;
|
|
111
111
|
}, z.core.$strip>;
|
|
112
|
-
declare const
|
|
112
|
+
declare const variantKind: LineKind<VariantPayload>;
|
|
113
113
|
//#endregion
|
|
114
|
-
export {
|
|
114
|
+
export { timeslotKind as a, skuKind as c, BundlePayload as d, bundleKind as f, TimeslotPayload as i, skuPayloadSchema as l, variantKind as n, timeslotPayloadSchema as o, bundlePayloadSchema as p, variantPayloadSchema as r, SkuPayload as s, VariantPayload as t, BundleComponent as u };
|
|
@@ -1,25 +1,34 @@
|
|
|
1
|
-
import { t as KindError } from "./kind.error-
|
|
1
|
+
import { t as KindError } from "./kind.error-DIaJpdUb.mjs";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
//#region src/kinds/
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
//#region src/kinds/bundle.kind.ts
|
|
4
|
+
const bundlePayloadSchema = z.object({
|
|
5
|
+
bundleRef: z.string().min(1),
|
|
6
|
+
components: z.array(z.object({
|
|
7
|
+
skuRef: z.string().min(1),
|
|
8
|
+
quantity: z.number().int().min(1)
|
|
9
|
+
})).min(1)
|
|
10
|
+
});
|
|
11
|
+
const bundleKind = {
|
|
12
|
+
name: "bundle",
|
|
13
|
+
payloadSchema: bundlePayloadSchema,
|
|
14
|
+
validity: {
|
|
15
|
+
maxAgeMs: 900 * 1e3,
|
|
16
|
+
onStale: "reprice"
|
|
17
|
+
},
|
|
9
18
|
async validate(line, ctx) {
|
|
10
|
-
const parsed =
|
|
11
|
-
if (!parsed.success) throw new KindError(this.name, "invalid payload
|
|
19
|
+
const parsed = bundlePayloadSchema.safeParse(line.payload);
|
|
20
|
+
if (!parsed.success) throw new KindError(this.name, "invalid bundle payload", { details: { issues: parsed.error.issues } });
|
|
12
21
|
if (line.quantity < 1) throw new KindError(this.name, `quantity must be >= 1, got ${line.quantity}`);
|
|
13
|
-
const
|
|
14
|
-
if (!
|
|
15
|
-
if (!
|
|
22
|
+
const bundle = await ctx.bridges.catalog.resolve(parsed.data.bundleRef, ctx);
|
|
23
|
+
if (!bundle) throw new KindError(this.name, `unknown bundle: ${parsed.data.bundleRef}`);
|
|
24
|
+
if (!bundle.purchasable) throw new KindError(this.name, `bundle ${parsed.data.bundleRef} is not purchasable`);
|
|
16
25
|
},
|
|
17
26
|
async price(line, ctx) {
|
|
18
27
|
const typed = line;
|
|
19
|
-
const
|
|
20
|
-
if (!
|
|
21
|
-
const unitPrice = typed.overrideUnitPrice ??
|
|
22
|
-
const
|
|
28
|
+
const bundle = await ctx.bridges.catalog.resolve(typed.payload.bundleRef, ctx);
|
|
29
|
+
if (!bundle) throw new KindError(this.name, `price() on unknown bundle: ${typed.payload.bundleRef}`);
|
|
30
|
+
const unitPrice = typed.overrideUnitPrice ?? bundle.unitPrice;
|
|
31
|
+
const total = {
|
|
23
32
|
amount: unitPrice.amount * typed.quantity,
|
|
24
33
|
currency: unitPrice.currency
|
|
25
34
|
};
|
|
@@ -28,9 +37,9 @@ const skuKind = {
|
|
|
28
37
|
unitPrice,
|
|
29
38
|
quantity: typed.quantity,
|
|
30
39
|
subtotals: {
|
|
31
|
-
prePromo:
|
|
32
|
-
postPromo:
|
|
33
|
-
postPromoPostTax:
|
|
40
|
+
prePromo: total,
|
|
41
|
+
postPromo: total,
|
|
42
|
+
postPromoPostTax: total
|
|
34
43
|
},
|
|
35
44
|
discountTotal: {
|
|
36
45
|
amount: 0,
|
|
@@ -40,61 +49,86 @@ const skuKind = {
|
|
|
40
49
|
amount: 0,
|
|
41
50
|
currency: unitPrice.currency
|
|
42
51
|
},
|
|
43
|
-
validUntil:
|
|
52
|
+
validUntil: new Date(Date.now() + 900 * 1e3),
|
|
53
|
+
kindMeta: {
|
|
54
|
+
bundleRef: typed.payload.bundleRef,
|
|
55
|
+
componentCount: typed.payload.components.length
|
|
56
|
+
}
|
|
44
57
|
};
|
|
45
58
|
},
|
|
46
|
-
async revalidate(
|
|
59
|
+
async revalidate(line, priced, ctx) {
|
|
60
|
+
const typed = line;
|
|
61
|
+
const bundle = await ctx.bridges.catalog.resolve(typed.payload.bundleRef, ctx);
|
|
62
|
+
if (!bundle) return {
|
|
63
|
+
status: "invalid",
|
|
64
|
+
reason: "bundle no longer exists"
|
|
65
|
+
};
|
|
66
|
+
const currentPrice = bundle.unitPrice.amount;
|
|
67
|
+
const snapshotPrice = priced.unitPrice.amount;
|
|
68
|
+
if (currentPrice !== snapshotPrice) {
|
|
69
|
+
const delta = {
|
|
70
|
+
amount: (currentPrice - snapshotPrice) * typed.quantity,
|
|
71
|
+
currency: priced.unitPrice.currency
|
|
72
|
+
};
|
|
73
|
+
const newTotal = {
|
|
74
|
+
amount: currentPrice * typed.quantity,
|
|
75
|
+
currency: priced.unitPrice.currency
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
status: "reprice",
|
|
79
|
+
priced: {
|
|
80
|
+
...priced,
|
|
81
|
+
unitPrice: {
|
|
82
|
+
amount: currentPrice,
|
|
83
|
+
currency: priced.unitPrice.currency
|
|
84
|
+
},
|
|
85
|
+
subtotals: {
|
|
86
|
+
prePromo: newTotal,
|
|
87
|
+
postPromo: newTotal,
|
|
88
|
+
postPromoPostTax: newTotal
|
|
89
|
+
},
|
|
90
|
+
validUntil: new Date(Date.now() + 900 * 1e3)
|
|
91
|
+
},
|
|
92
|
+
delta
|
|
93
|
+
};
|
|
94
|
+
}
|
|
47
95
|
return {
|
|
48
96
|
status: "unchanged",
|
|
49
97
|
priced
|
|
50
98
|
};
|
|
51
99
|
},
|
|
52
|
-
mergeKey(line) {
|
|
53
|
-
const typed = line;
|
|
54
|
-
if (!typed.payload?.skuRef) return null;
|
|
55
|
-
return `sku:${typed.payload.skuRef}`;
|
|
56
|
-
},
|
|
57
|
-
/**
|
|
58
|
-
* Populate the display snapshot at add-time. Called by the draft
|
|
59
|
-
* service; the resulting snapshot is stored on `LineItem.display` so
|
|
60
|
-
* the UI renders the cart directly without a second catalog call.
|
|
61
|
-
*/
|
|
62
100
|
async displayOf(line, ctx) {
|
|
63
101
|
const typed = line;
|
|
64
|
-
const
|
|
65
|
-
if (!
|
|
102
|
+
const bundle = await ctx.bridges.catalog.resolve(typed.payload.bundleRef, ctx);
|
|
103
|
+
if (!bundle) return null;
|
|
66
104
|
return {
|
|
67
|
-
name:
|
|
68
|
-
imageUrl:
|
|
69
|
-
slug:
|
|
70
|
-
variantLabel:
|
|
71
|
-
|
|
72
|
-
capturedAt: ctx.bridges ? /* @__PURE__ */ new Date() : /* @__PURE__ */ new Date()
|
|
105
|
+
name: bundle.name,
|
|
106
|
+
imageUrl: bundle.primaryImage,
|
|
107
|
+
slug: bundle.slug,
|
|
108
|
+
variantLabel: `${typed.payload.components.length} items`,
|
|
109
|
+
capturedAt: /* @__PURE__ */ new Date()
|
|
73
110
|
};
|
|
74
111
|
}
|
|
75
112
|
};
|
|
76
113
|
//#endregion
|
|
77
|
-
//#region src/kinds/
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const variantKind = {
|
|
83
|
-
name: "variant",
|
|
84
|
-
payloadSchema: variantPayloadSchema,
|
|
114
|
+
//#region src/kinds/sku.kind.ts
|
|
115
|
+
const skuPayloadSchema = z.object({ skuRef: z.string().min(1) });
|
|
116
|
+
const skuKind = {
|
|
117
|
+
name: "sku",
|
|
118
|
+
payloadSchema: skuPayloadSchema,
|
|
85
119
|
validity: { onStale: "extend" },
|
|
86
120
|
async validate(line, ctx) {
|
|
87
|
-
const parsed =
|
|
88
|
-
if (!parsed.success) throw new KindError(this.name, "invalid
|
|
121
|
+
const parsed = skuPayloadSchema.safeParse(line.payload);
|
|
122
|
+
if (!parsed.success) throw new KindError(this.name, "invalid payload shape", { details: { issues: parsed.error.issues } });
|
|
89
123
|
if (line.quantity < 1) throw new KindError(this.name, `quantity must be >= 1, got ${line.quantity}`);
|
|
90
|
-
const product = await ctx.bridges.catalog.resolve(parsed.data.
|
|
91
|
-
if (!product) throw new KindError(this.name, `unknown
|
|
92
|
-
if (!product.purchasable) throw new KindError(this.name, `
|
|
124
|
+
const product = await ctx.bridges.catalog.resolve(parsed.data.skuRef, ctx);
|
|
125
|
+
if (!product) throw new KindError(this.name, `unknown skuRef: ${parsed.data.skuRef}`);
|
|
126
|
+
if (!product.purchasable) throw new KindError(this.name, `product ${parsed.data.skuRef} is not purchasable`);
|
|
93
127
|
},
|
|
94
128
|
async price(line, ctx) {
|
|
95
129
|
const typed = line;
|
|
96
|
-
const product = await ctx.bridges.catalog.resolve(typed.payload.
|
|
97
|
-
if (!product) throw new KindError(this.name, `price() on unknown
|
|
130
|
+
const product = await ctx.bridges.catalog.resolve(typed.payload.skuRef, ctx);
|
|
131
|
+
if (!product) throw new KindError(this.name, `price() called on unknown skuRef: ${typed.payload.skuRef}`);
|
|
98
132
|
const unitPrice = typed.overrideUnitPrice ?? product.unitPrice;
|
|
99
133
|
const lineTotal = {
|
|
100
134
|
amount: unitPrice.amount * typed.quantity,
|
|
@@ -127,13 +161,18 @@ const variantKind = {
|
|
|
127
161
|
};
|
|
128
162
|
},
|
|
129
163
|
mergeKey(line) {
|
|
130
|
-
const
|
|
131
|
-
if (!
|
|
132
|
-
return `
|
|
164
|
+
const typed = line;
|
|
165
|
+
if (!typed.payload?.skuRef) return null;
|
|
166
|
+
return `sku:${typed.payload.skuRef}`;
|
|
133
167
|
},
|
|
168
|
+
/**
|
|
169
|
+
* Populate the display snapshot at add-time. Called by the draft
|
|
170
|
+
* service; the resulting snapshot is stored on `LineItem.display` so
|
|
171
|
+
* the UI renders the cart directly without a second catalog call.
|
|
172
|
+
*/
|
|
134
173
|
async displayOf(line, ctx) {
|
|
135
174
|
const typed = line;
|
|
136
|
-
const product = await ctx.bridges.catalog.resolve(typed.payload.
|
|
175
|
+
const product = await ctx.bridges.catalog.resolve(typed.payload.skuRef, ctx);
|
|
137
176
|
if (!product) return null;
|
|
138
177
|
return {
|
|
139
178
|
name: product.name,
|
|
@@ -141,7 +180,7 @@ const variantKind = {
|
|
|
141
180
|
slug: product.slug,
|
|
142
181
|
variantLabel: product.variantLabel,
|
|
143
182
|
compareAtPrice: product.compareAtPrice,
|
|
144
|
-
capturedAt: /* @__PURE__ */ new Date()
|
|
183
|
+
capturedAt: ctx.bridges ? /* @__PURE__ */ new Date() : /* @__PURE__ */ new Date()
|
|
145
184
|
};
|
|
146
185
|
}
|
|
147
186
|
};
|
|
@@ -248,35 +287,29 @@ const timeslotKind = {
|
|
|
248
287
|
}
|
|
249
288
|
};
|
|
250
289
|
//#endregion
|
|
251
|
-
//#region src/kinds/
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
skuRef: z.string().min(1),
|
|
256
|
-
quantity: z.number().int().min(1)
|
|
257
|
-
})).min(1)
|
|
290
|
+
//#region src/kinds/variant.kind.ts
|
|
291
|
+
const variantPayloadSchema = z.object({
|
|
292
|
+
productRef: z.string().min(1),
|
|
293
|
+
variantSku: z.string().min(1)
|
|
258
294
|
});
|
|
259
|
-
const
|
|
260
|
-
name: "
|
|
261
|
-
payloadSchema:
|
|
262
|
-
validity: {
|
|
263
|
-
maxAgeMs: 900 * 1e3,
|
|
264
|
-
onStale: "reprice"
|
|
265
|
-
},
|
|
295
|
+
const variantKind = {
|
|
296
|
+
name: "variant",
|
|
297
|
+
payloadSchema: variantPayloadSchema,
|
|
298
|
+
validity: { onStale: "extend" },
|
|
266
299
|
async validate(line, ctx) {
|
|
267
|
-
const parsed =
|
|
268
|
-
if (!parsed.success) throw new KindError(this.name, "invalid
|
|
300
|
+
const parsed = variantPayloadSchema.safeParse(line.payload);
|
|
301
|
+
if (!parsed.success) throw new KindError(this.name, "invalid variant payload", { details: { issues: parsed.error.issues } });
|
|
269
302
|
if (line.quantity < 1) throw new KindError(this.name, `quantity must be >= 1, got ${line.quantity}`);
|
|
270
|
-
const
|
|
271
|
-
if (!
|
|
272
|
-
if (!
|
|
303
|
+
const product = await ctx.bridges.catalog.resolve(parsed.data.variantSku, ctx);
|
|
304
|
+
if (!product) throw new KindError(this.name, `unknown variant SKU: ${parsed.data.variantSku}`);
|
|
305
|
+
if (!product.purchasable) throw new KindError(this.name, `variant ${parsed.data.variantSku} is not purchasable`);
|
|
273
306
|
},
|
|
274
307
|
async price(line, ctx) {
|
|
275
308
|
const typed = line;
|
|
276
|
-
const
|
|
277
|
-
if (!
|
|
278
|
-
const unitPrice = typed.overrideUnitPrice ??
|
|
279
|
-
const
|
|
309
|
+
const product = await ctx.bridges.catalog.resolve(typed.payload.variantSku, ctx);
|
|
310
|
+
if (!product) throw new KindError(this.name, `price() on unknown variant: ${typed.payload.variantSku}`);
|
|
311
|
+
const unitPrice = typed.overrideUnitPrice ?? product.unitPrice;
|
|
312
|
+
const lineTotal = {
|
|
280
313
|
amount: unitPrice.amount * typed.quantity,
|
|
281
314
|
currency: unitPrice.currency
|
|
282
315
|
};
|
|
@@ -285,9 +318,9 @@ const bundleKind = {
|
|
|
285
318
|
unitPrice,
|
|
286
319
|
quantity: typed.quantity,
|
|
287
320
|
subtotals: {
|
|
288
|
-
prePromo:
|
|
289
|
-
postPromo:
|
|
290
|
-
postPromoPostTax:
|
|
321
|
+
prePromo: lineTotal,
|
|
322
|
+
postPromo: lineTotal,
|
|
323
|
+
postPromoPostTax: lineTotal
|
|
291
324
|
},
|
|
292
325
|
discountTotal: {
|
|
293
326
|
amount: 0,
|
|
@@ -297,66 +330,33 @@ const bundleKind = {
|
|
|
297
330
|
amount: 0,
|
|
298
331
|
currency: unitPrice.currency
|
|
299
332
|
},
|
|
300
|
-
validUntil:
|
|
301
|
-
kindMeta: {
|
|
302
|
-
bundleRef: typed.payload.bundleRef,
|
|
303
|
-
componentCount: typed.payload.components.length
|
|
304
|
-
}
|
|
333
|
+
validUntil: null
|
|
305
334
|
};
|
|
306
335
|
},
|
|
307
|
-
async revalidate(
|
|
308
|
-
const typed = line;
|
|
309
|
-
const bundle = await ctx.bridges.catalog.resolve(typed.payload.bundleRef, ctx);
|
|
310
|
-
if (!bundle) return {
|
|
311
|
-
status: "invalid",
|
|
312
|
-
reason: "bundle no longer exists"
|
|
313
|
-
};
|
|
314
|
-
const currentPrice = bundle.unitPrice.amount;
|
|
315
|
-
const snapshotPrice = priced.unitPrice.amount;
|
|
316
|
-
if (currentPrice !== snapshotPrice) {
|
|
317
|
-
const delta = {
|
|
318
|
-
amount: (currentPrice - snapshotPrice) * typed.quantity,
|
|
319
|
-
currency: priced.unitPrice.currency
|
|
320
|
-
};
|
|
321
|
-
const newTotal = {
|
|
322
|
-
amount: currentPrice * typed.quantity,
|
|
323
|
-
currency: priced.unitPrice.currency
|
|
324
|
-
};
|
|
325
|
-
return {
|
|
326
|
-
status: "reprice",
|
|
327
|
-
priced: {
|
|
328
|
-
...priced,
|
|
329
|
-
unitPrice: {
|
|
330
|
-
amount: currentPrice,
|
|
331
|
-
currency: priced.unitPrice.currency
|
|
332
|
-
},
|
|
333
|
-
subtotals: {
|
|
334
|
-
prePromo: newTotal,
|
|
335
|
-
postPromo: newTotal,
|
|
336
|
-
postPromoPostTax: newTotal
|
|
337
|
-
},
|
|
338
|
-
validUntil: new Date(Date.now() + 900 * 1e3)
|
|
339
|
-
},
|
|
340
|
-
delta
|
|
341
|
-
};
|
|
342
|
-
}
|
|
336
|
+
async revalidate(_line, priced) {
|
|
343
337
|
return {
|
|
344
338
|
status: "unchanged",
|
|
345
339
|
priced
|
|
346
340
|
};
|
|
347
341
|
},
|
|
342
|
+
mergeKey(line) {
|
|
343
|
+
const { productRef, variantSku } = line.payload ?? {};
|
|
344
|
+
if (!productRef || !variantSku) return null;
|
|
345
|
+
return `variant:${productRef}:${variantSku}`;
|
|
346
|
+
},
|
|
348
347
|
async displayOf(line, ctx) {
|
|
349
348
|
const typed = line;
|
|
350
|
-
const
|
|
351
|
-
if (!
|
|
349
|
+
const product = await ctx.bridges.catalog.resolve(typed.payload.variantSku, ctx);
|
|
350
|
+
if (!product) return null;
|
|
352
351
|
return {
|
|
353
|
-
name:
|
|
354
|
-
imageUrl:
|
|
355
|
-
slug:
|
|
356
|
-
variantLabel:
|
|
352
|
+
name: product.name,
|
|
353
|
+
imageUrl: product.primaryImage,
|
|
354
|
+
slug: product.slug,
|
|
355
|
+
variantLabel: product.variantLabel,
|
|
356
|
+
compareAtPrice: product.compareAtPrice,
|
|
357
357
|
capturedAt: /* @__PURE__ */ new Date()
|
|
358
358
|
};
|
|
359
359
|
}
|
|
360
360
|
};
|
|
361
361
|
//#endregion
|
|
362
|
-
export {
|
|
362
|
+
export { skuKind as a, bundlePayloadSchema as c, timeslotPayloadSchema as i, variantPayloadSchema as n, skuPayloadSchema as o, timeslotKind as r, bundleKind as s, variantKind as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classytic/cart",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Framework-agnostic universal cart engine for commerce — drafts, checkouts, reservations, the LineKind strategy, and a disciplined finalization saga. Powered by MongoDB via @classytic/mongokit. Zero framework deps — usable from Arc, Express, Nest, Next.js, or raw Node.",
|
|
5
5
|
"author": "Classytic",
|
|
6
6
|
"type": "module",
|
|
@@ -69,25 +69,32 @@
|
|
|
69
69
|
"build": "tsdown",
|
|
70
70
|
"dev": "tsdown --watch",
|
|
71
71
|
"typecheck": "tsc --noEmit",
|
|
72
|
+
"lint": "biome check src/ tests/",
|
|
73
|
+
"format": "biome format --write src/ tests/",
|
|
74
|
+
"check": "biome check --write --unsafe src/ tests/",
|
|
72
75
|
"test": "vitest run --project unit --project integration",
|
|
73
76
|
"test:unit": "vitest run --project unit",
|
|
74
77
|
"test:integration": "vitest run --project integration",
|
|
75
78
|
"test:all": "vitest run",
|
|
76
|
-
"test:watch": "vitest --project unit --project integration"
|
|
79
|
+
"test:watch": "vitest --project unit --project integration",
|
|
80
|
+
"prepublishOnly": "npm run lint && npm run typecheck && npm run test && npm run build",
|
|
81
|
+
"push": "classytic-push",
|
|
82
|
+
"release:tag": "node -e \"require('child_process').execSync('npm run push -- v'+require('./package.json').version,{stdio:'inherit'})\"",
|
|
83
|
+
"release": "npm run push -- main && npm run release:tag && npm publish"
|
|
77
84
|
},
|
|
78
85
|
"peerDependencies": {
|
|
79
86
|
"@classytic/mongokit": ">=3.11.0",
|
|
80
|
-
"@classytic/primitives": ">=0.
|
|
81
|
-
"@classytic/repo-core": ">=0.2.0",
|
|
87
|
+
"@classytic/primitives": ">=0.2.0",
|
|
82
88
|
"mongoose": ">=9.4.1",
|
|
83
89
|
"zod": ">=4.0.0"
|
|
84
90
|
},
|
|
85
91
|
"devDependencies": {
|
|
92
|
+
"@biomejs/biome": "^2.4.13",
|
|
93
|
+
"@classytic/dev-tools": "^0.2.0",
|
|
86
94
|
"@classytic/mongokit": "^3.11.0",
|
|
87
|
-
"@classytic/primitives": "
|
|
88
|
-
"@classytic/repo-core": ">=0.2.0",
|
|
95
|
+
"@classytic/primitives": ">=0.2.0",
|
|
89
96
|
"@types/node": "^25.5.0",
|
|
90
|
-
"
|
|
97
|
+
"knip": "^6.7.0",
|
|
91
98
|
"mongodb-memory-server": "^10.2.3",
|
|
92
99
|
"mongoose": "^9.4.1",
|
|
93
100
|
"tsdown": "^0.21.5",
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
//#region src/domain/saga/saga-steps.ts
|
|
2
|
-
/**
|
|
3
|
-
* Checkout flow steps. Deliberately minimal — cart's job is to freeze a
|
|
4
|
-
* snapshot and transition its state. Everything external (payment, order
|
|
5
|
-
* creation, fulfillment) is the host's problem.
|
|
6
|
-
*
|
|
7
|
-
* This enum exists purely for logging / metrics / ordered state-transition
|
|
8
|
-
* dispatch. Cart does not run a "saga engine" — the `CheckoutRepository`
|
|
9
|
-
* domain methods run these steps in plain sequential code and let
|
|
10
|
-
* exceptions propagate. Compensation is one operation (`cancel`) rather
|
|
11
|
-
* than a reverse walk of a multi-step chain.
|
|
12
|
-
*/
|
|
13
|
-
const CheckoutStep = {
|
|
14
|
-
/** Step 1 — load draft + validate status + version. */
|
|
15
|
-
LoadDraft: 1,
|
|
16
|
-
/** Step 2 — re-run kind.revalidate() on stale lines. */
|
|
17
|
-
Revalidate: 2,
|
|
18
|
-
/** Step 3 — re-run pricing pipeline, compare expectedPricingHash. */
|
|
19
|
-
Reprice: 3,
|
|
20
|
-
/** Step 4 — create the checkout document with the frozen snapshot. */
|
|
21
|
-
CreateCheckout: 4,
|
|
22
|
-
/** Step 5 — commit (host-called after its own external work succeeds). */
|
|
23
|
-
Commit: 5
|
|
24
|
-
};
|
|
25
|
-
const SagaStep = CheckoutStep;
|
|
26
|
-
//#endregion
|
|
27
|
-
export { SagaStep as n, CheckoutStep as t };
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
//#region src/domain/saga/state-transitions.ts
|
|
2
|
-
/**
|
|
3
|
-
* Frozen state transitions for Checkout.state. Three states, two
|
|
4
|
-
* terminal outcomes — finalized (success) or canceled (failure / manual
|
|
5
|
-
* cancel / abandonment).
|
|
6
|
-
*
|
|
7
|
-
* Enforced by `CheckoutRepository.updateWithVersion` callers: the
|
|
8
|
-
* service methods pass the expected current state in the filter so
|
|
9
|
-
* racing writers see exactly one winner.
|
|
10
|
-
*/
|
|
11
|
-
const CHECKOUT_STATE_TRANSITIONS = {
|
|
12
|
-
open: ["finalized", "canceled"],
|
|
13
|
-
finalized: [],
|
|
14
|
-
canceled: []
|
|
15
|
-
};
|
|
16
|
-
/** Predicate — is `to` a legal transition from `from`? */
|
|
17
|
-
function isLegalCheckoutTransition(from, to) {
|
|
18
|
-
return CHECKOUT_STATE_TRANSITIONS[from]?.includes(to) ?? false;
|
|
19
|
-
}
|
|
20
|
-
//#endregion
|
|
21
|
-
export { isLegalCheckoutTransition as n, CHECKOUT_STATE_TRANSITIONS as t };
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|