@unifiedcommerce/core 0.1.0 → 0.1.1

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.
Files changed (174) hide show
  1. package/package.json +1 -2
  2. package/src/adapters/console-email.ts +0 -43
  3. package/src/auth/access.ts +0 -187
  4. package/src/auth/auth-schema.ts +0 -139
  5. package/src/auth/middleware.ts +0 -161
  6. package/src/auth/org.ts +0 -41
  7. package/src/auth/permissions.ts +0 -28
  8. package/src/auth/setup.ts +0 -169
  9. package/src/auth/system-actor.ts +0 -19
  10. package/src/auth/types.ts +0 -10
  11. package/src/config/defaults.ts +0 -82
  12. package/src/config/define-config.ts +0 -53
  13. package/src/config/types.ts +0 -299
  14. package/src/generated/plugin-capabilities.d.ts +0 -20
  15. package/src/generated/plugin-manifest.ts +0 -23
  16. package/src/generated/plugin-repositories.d.ts +0 -20
  17. package/src/hooks/checkout-completion.ts +0 -262
  18. package/src/hooks/checkout.ts +0 -677
  19. package/src/hooks/order-emails.ts +0 -62
  20. package/src/index.ts +0 -214
  21. package/src/interfaces/mcp/agent-prompt.ts +0 -174
  22. package/src/interfaces/mcp/context-enrichment.ts +0 -177
  23. package/src/interfaces/mcp/server.ts +0 -617
  24. package/src/interfaces/mcp/transport.ts +0 -68
  25. package/src/interfaces/rest/customer-portal.ts +0 -299
  26. package/src/interfaces/rest/index.ts +0 -74
  27. package/src/interfaces/rest/router.ts +0 -334
  28. package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
  29. package/src/interfaces/rest/routes/audit.ts +0 -50
  30. package/src/interfaces/rest/routes/carts.ts +0 -89
  31. package/src/interfaces/rest/routes/catalog.ts +0 -493
  32. package/src/interfaces/rest/routes/checkout.ts +0 -283
  33. package/src/interfaces/rest/routes/inventory.ts +0 -70
  34. package/src/interfaces/rest/routes/media.ts +0 -86
  35. package/src/interfaces/rest/routes/orders.ts +0 -78
  36. package/src/interfaces/rest/routes/payments.ts +0 -60
  37. package/src/interfaces/rest/routes/pricing.ts +0 -57
  38. package/src/interfaces/rest/routes/promotions.ts +0 -92
  39. package/src/interfaces/rest/routes/search.ts +0 -71
  40. package/src/interfaces/rest/routes/webhooks.ts +0 -46
  41. package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
  42. package/src/interfaces/rest/schemas/audit.ts +0 -46
  43. package/src/interfaces/rest/schemas/carts.ts +0 -125
  44. package/src/interfaces/rest/schemas/catalog.ts +0 -450
  45. package/src/interfaces/rest/schemas/checkout.ts +0 -66
  46. package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
  47. package/src/interfaces/rest/schemas/inventory.ts +0 -138
  48. package/src/interfaces/rest/schemas/media.ts +0 -75
  49. package/src/interfaces/rest/schemas/orders.ts +0 -104
  50. package/src/interfaces/rest/schemas/pricing.ts +0 -80
  51. package/src/interfaces/rest/schemas/promotions.ts +0 -110
  52. package/src/interfaces/rest/schemas/responses.ts +0 -85
  53. package/src/interfaces/rest/schemas/search.ts +0 -58
  54. package/src/interfaces/rest/schemas/shared.ts +0 -62
  55. package/src/interfaces/rest/schemas/webhooks.ts +0 -68
  56. package/src/interfaces/rest/utils.ts +0 -104
  57. package/src/interfaces/rest/webhook-router.ts +0 -50
  58. package/src/kernel/compensation/executor.ts +0 -61
  59. package/src/kernel/compensation/types.ts +0 -26
  60. package/src/kernel/database/adapter.ts +0 -13
  61. package/src/kernel/database/drizzle-db.ts +0 -56
  62. package/src/kernel/database/migrate.ts +0 -76
  63. package/src/kernel/database/plugin-types.ts +0 -34
  64. package/src/kernel/database/schema.ts +0 -49
  65. package/src/kernel/database/scoped-db.ts +0 -68
  66. package/src/kernel/database/tx-context.ts +0 -46
  67. package/src/kernel/error-mapper.ts +0 -15
  68. package/src/kernel/errors.ts +0 -89
  69. package/src/kernel/factory/repository-factory.ts +0 -242
  70. package/src/kernel/hooks/create-context.ts +0 -43
  71. package/src/kernel/hooks/executor.ts +0 -88
  72. package/src/kernel/hooks/registry.ts +0 -74
  73. package/src/kernel/hooks/types.ts +0 -52
  74. package/src/kernel/http-error.ts +0 -44
  75. package/src/kernel/jobs/adapter.ts +0 -36
  76. package/src/kernel/jobs/drizzle-adapter.ts +0 -58
  77. package/src/kernel/jobs/runner.ts +0 -153
  78. package/src/kernel/jobs/schema.ts +0 -46
  79. package/src/kernel/jobs/types.ts +0 -30
  80. package/src/kernel/local-api.ts +0 -185
  81. package/src/kernel/plugin/manifest.ts +0 -253
  82. package/src/kernel/query/executor.ts +0 -184
  83. package/src/kernel/query/registry.ts +0 -46
  84. package/src/kernel/result.ts +0 -33
  85. package/src/kernel/schema/extra-columns.ts +0 -37
  86. package/src/kernel/service-registry.ts +0 -76
  87. package/src/kernel/service-timing.ts +0 -89
  88. package/src/kernel/state-machine/machine.ts +0 -101
  89. package/src/modules/analytics/drizzle-adapter.ts +0 -426
  90. package/src/modules/analytics/hooks.ts +0 -11
  91. package/src/modules/analytics/models.ts +0 -125
  92. package/src/modules/analytics/repository/index.ts +0 -6
  93. package/src/modules/analytics/service.ts +0 -245
  94. package/src/modules/analytics/types.ts +0 -180
  95. package/src/modules/audit/hooks.ts +0 -78
  96. package/src/modules/audit/schema.ts +0 -33
  97. package/src/modules/audit/service.ts +0 -151
  98. package/src/modules/cart/access.ts +0 -27
  99. package/src/modules/cart/matcher.ts +0 -26
  100. package/src/modules/cart/repository/index.ts +0 -234
  101. package/src/modules/cart/schema.ts +0 -42
  102. package/src/modules/cart/schemas.ts +0 -38
  103. package/src/modules/cart/service.ts +0 -541
  104. package/src/modules/catalog/repository/index.ts +0 -772
  105. package/src/modules/catalog/schema.ts +0 -203
  106. package/src/modules/catalog/schemas.ts +0 -104
  107. package/src/modules/catalog/service.ts +0 -1544
  108. package/src/modules/customers/repository/index.ts +0 -327
  109. package/src/modules/customers/schema.ts +0 -64
  110. package/src/modules/customers/service.ts +0 -171
  111. package/src/modules/fulfillment/repository/index.ts +0 -426
  112. package/src/modules/fulfillment/schema.ts +0 -101
  113. package/src/modules/fulfillment/service.ts +0 -555
  114. package/src/modules/fulfillment/types.ts +0 -59
  115. package/src/modules/inventory/repository/index.ts +0 -509
  116. package/src/modules/inventory/schema.ts +0 -94
  117. package/src/modules/inventory/schemas.ts +0 -38
  118. package/src/modules/inventory/service.ts +0 -490
  119. package/src/modules/media/adapter.ts +0 -17
  120. package/src/modules/media/repository/index.ts +0 -274
  121. package/src/modules/media/schema.ts +0 -41
  122. package/src/modules/media/service.ts +0 -151
  123. package/src/modules/orders/repository/index.ts +0 -287
  124. package/src/modules/orders/schema.ts +0 -66
  125. package/src/modules/orders/service.ts +0 -619
  126. package/src/modules/orders/stale-order-cleanup.ts +0 -76
  127. package/src/modules/organization/service.ts +0 -191
  128. package/src/modules/payments/adapter.ts +0 -47
  129. package/src/modules/payments/repository/index.ts +0 -6
  130. package/src/modules/payments/service.ts +0 -107
  131. package/src/modules/pricing/repository/index.ts +0 -291
  132. package/src/modules/pricing/schema.ts +0 -71
  133. package/src/modules/pricing/schemas.ts +0 -38
  134. package/src/modules/pricing/service.ts +0 -494
  135. package/src/modules/promotions/repository/index.ts +0 -325
  136. package/src/modules/promotions/schema.ts +0 -62
  137. package/src/modules/promotions/schemas.ts +0 -38
  138. package/src/modules/promotions/service.ts +0 -598
  139. package/src/modules/search/adapter.ts +0 -57
  140. package/src/modules/search/hooks.ts +0 -12
  141. package/src/modules/search/repository/index.ts +0 -6
  142. package/src/modules/search/service.ts +0 -315
  143. package/src/modules/shipping/calculator.ts +0 -188
  144. package/src/modules/shipping/repository/index.ts +0 -6
  145. package/src/modules/shipping/service.ts +0 -51
  146. package/src/modules/tax/adapter.ts +0 -60
  147. package/src/modules/tax/repository/index.ts +0 -6
  148. package/src/modules/tax/service.ts +0 -53
  149. package/src/modules/webhooks/hook.ts +0 -34
  150. package/src/modules/webhooks/repository/index.ts +0 -278
  151. package/src/modules/webhooks/schema.ts +0 -56
  152. package/src/modules/webhooks/service.ts +0 -117
  153. package/src/modules/webhooks/signing.ts +0 -6
  154. package/src/modules/webhooks/ssrf-guard.ts +0 -71
  155. package/src/modules/webhooks/tasks.ts +0 -52
  156. package/src/modules/webhooks/worker.ts +0 -134
  157. package/src/runtime/commerce.ts +0 -145
  158. package/src/runtime/kernel.ts +0 -419
  159. package/src/runtime/logger.ts +0 -36
  160. package/src/runtime/server.ts +0 -349
  161. package/src/runtime/shutdown.ts +0 -43
  162. package/src/test-utils/create-pglite-adapter.ts +0 -129
  163. package/src/test-utils/create-plugin-test-app.ts +0 -128
  164. package/src/test-utils/create-repository-test-harness.ts +0 -16
  165. package/src/test-utils/create-test-config.ts +0 -190
  166. package/src/test-utils/create-test-kernel.ts +0 -7
  167. package/src/test-utils/create-test-plugin-context.ts +0 -75
  168. package/src/test-utils/rest-api-test-utils.ts +0 -265
  169. package/src/test-utils/test-actors.ts +0 -62
  170. package/src/test-utils/typed-hooks.ts +0 -54
  171. package/src/types/commerce-types.ts +0 -34
  172. package/src/utils/id.ts +0 -3
  173. package/src/utils/logger.ts +0 -18
  174. package/src/utils/pagination.ts +0 -22
@@ -1,541 +0,0 @@
1
- import { resolveOrgId } from "../../auth/org.js";
2
- import { assertOwnership, assertPermission } from "../../auth/permissions.js";
3
- import type { Actor } from "../../auth/types.js";
4
- import type { CommerceConfig } from "../../config/types.js";
5
- import {
6
- CommerceNotFoundError,
7
- CommerceValidationError,
8
- toCommerceError,
9
- } from "../../kernel/errors.js";
10
- import { runAfterHooks, runBeforeHooks } from "../../kernel/hooks/executor.js";
11
- import { createHookContext } from "../../kernel/hooks/create-context.js";
12
- import type {
13
- AfterHook,
14
- BeforeHook,
15
- HookContext,
16
- } from "../../kernel/hooks/types.js";
17
- import type { HookRegistry } from "../../kernel/hooks/registry.js";
18
- import { Err, Ok, type Result } from "../../kernel/result.js";
19
- import { createLogger } from "../../utils/logger.js";
20
- import type { TxContext } from "../../kernel/database/tx-context.js";
21
- import { CartRepository, type Cart, type CartLineItem } from "./repository/index.js";
22
- import type { CatalogRepository } from "../catalog/repository/index.js";
23
-
24
- export type {
25
- CreateCartInput,
26
- AddCartItemInput,
27
- UpdateCartItemInput,
28
- } from "./schemas.js";
29
-
30
- import type {
31
- CreateCartInput,
32
- AddCartItemInput,
33
- UpdateCartItemInput,
34
- } from "./schemas.js";
35
-
36
- import { defaultCartItemMatcher, type CartItemMatcher } from "./matcher.js";
37
-
38
- export interface CartServiceDeps {
39
- repository: CartRepository;
40
- catalogRepository: CatalogRepository;
41
- hooks: HookRegistry;
42
- config: CommerceConfig;
43
- services: Record<string, unknown>;
44
- cartItemMatcher?: CartItemMatcher;
45
- }
46
-
47
- type CartAddBeforeHook = BeforeHook<AddCartItemInput>;
48
- type CartAddAfterHook = AfterHook<CartLineItem>;
49
- type CartRemoveBeforeHook = BeforeHook<CartLineItem>;
50
- type CartRemoveAfterHook = AfterHook<CartLineItem>;
51
- type CartUpdateBeforeHook = BeforeHook<UpdateCartItemInput>;
52
- type CartUpdateAfterHook = AfterHook<CartLineItem>;
53
-
54
- function makeContext(
55
- actor: Actor | null,
56
- services: Record<string, unknown>,
57
- tx: unknown = null,
58
- ): HookContext {
59
- return createHookContext({
60
- actor,
61
- tx,
62
- logger: createLogger("cart"),
63
- services,
64
- });
65
- }
66
-
67
- function isExpired(cart: Cart): boolean {
68
- return cart.expiresAt.getTime() < Date.now();
69
- }
70
-
71
- export class CartService {
72
- private readonly repo: CartRepository;
73
- private readonly catalogRepo: CatalogRepository;
74
-
75
- constructor(private deps: CartServiceDeps) {
76
- this.repo = deps.repository;
77
- this.catalogRepo = deps.catalogRepository;
78
- }
79
-
80
- async create(
81
- input: CreateCartInput,
82
- actor?: Actor | null,
83
- ctx?: TxContext,
84
- ): Promise<Result<Cart>> {
85
- try {
86
- assertPermission(actor ?? null, "cart:create");
87
- } catch (error) {
88
- return Err(toCommerceError(error));
89
- }
90
-
91
- const ttlMinutes = this.deps.config.cart?.ttlMinutes ?? 60 * 24 * 7;
92
- const now = new Date();
93
- const orgId = resolveOrgId(actor ?? null);
94
-
95
- const cart = await this.repo.create(
96
- {
97
- organizationId: orgId,
98
- status: "active",
99
- currency: input.currency ?? "USD",
100
- metadata: input.metadata ?? {},
101
- expiresAt: new Date(now.getTime() + ttlMinutes * 60 * 1000),
102
- ...(input.customerId !== undefined
103
- ? { customerId: input.customerId }
104
- : {}),
105
- },
106
- ctx,
107
- );
108
-
109
- return Ok(cart);
110
- }
111
-
112
- async getById(
113
- id: string,
114
- actor?: Actor | null,
115
- ctx?: TxContext,
116
- ): Promise<Result<Cart & { lineItems: CartLineItem[] }>> {
117
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
118
- const cart = await this.repo.findById(orgId, id, ctx);
119
- if (!cart) return Err(new CommerceNotFoundError("Cart not found."));
120
-
121
- // H2 fix: Non-admin actors can only access their own carts.
122
- // Guest carts (customerId is null) are accessible by anyone (token-gated).
123
- try {
124
- this.assertCartOwnership(actor ?? null, cart);
125
- } catch (error) {
126
- return Err(toCommerceError(error));
127
- }
128
-
129
- if (isExpired(cart) && cart.status === "active") {
130
- await this.repo.updateStatus(cart.id, "abandoned", ctx);
131
- cart.status = "abandoned";
132
- }
133
-
134
- const lineItems = await this.repo.findLineItemsByCartId(id, ctx);
135
- return Ok({
136
- ...cart,
137
- lineItems,
138
- });
139
- }
140
-
141
- async addItem(
142
- input: AddCartItemInput,
143
- actor?: Actor | null,
144
- ctx?: TxContext,
145
- ): Promise<Result<CartLineItem>> {
146
- try {
147
- assertPermission(actor ?? null, "cart:update");
148
- } catch (error) {
149
- return Err(toCommerceError(error));
150
- }
151
-
152
- const orgId = resolveOrgId(actor ?? null);
153
- const cart = await this.repo.findById(orgId, input.cartId, ctx);
154
- if (!cart) return Err(new CommerceNotFoundError("Cart not found."));
155
-
156
- try {
157
- this.assertCartOwnership(actor ?? null, cart);
158
- } catch (error) {
159
- return Err(toCommerceError(error));
160
- }
161
-
162
- if (cart.status !== "active") {
163
- return Err(new CommerceValidationError("Cart is not active."));
164
- }
165
-
166
- const quantity = input.quantity ?? 1;
167
- if (quantity <= 0) {
168
- return Err(
169
- new CommerceValidationError("Quantity must be greater than zero."),
170
- );
171
- }
172
-
173
- // Validate entity exists
174
- const entity = await this.catalogRepo.findEntityById(input.entityId, ctx);
175
- if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
176
-
177
- // Check if entity has variants
178
- const entityVariants = await this.catalogRepo.findVariantsByEntityId(
179
- input.entityId,
180
- ctx,
181
- );
182
- const hasVariants = entityVariants.length > 0;
183
- if (hasVariants && !input.variantId) {
184
- const variantIds = entityVariants.map((v) => v.id);
185
- return Err(
186
- new CommerceValidationError(
187
- `Entity "${entity.slug}" has variants enabled, but no variantId was provided.`,
188
- [
189
- {
190
- field: "variantId",
191
- message: `Available variants: ${variantIds.join(", ")}`,
192
- },
193
- ],
194
- ),
195
- );
196
- }
197
-
198
- const context = makeContext(actor ?? null, this.deps.services, ctx?.tx);
199
- const beforeHooks = this.deps.hooks.resolve(
200
- "cart.beforeAddItem",
201
- ) as CartAddBeforeHook[];
202
- const afterHooks = this.deps.hooks.resolve(
203
- "cart.afterAddItem",
204
- ) as CartAddAfterHook[];
205
-
206
- const processed = await runBeforeHooks(
207
- beforeHooks,
208
- input,
209
- "addItem",
210
- context,
211
- );
212
-
213
- // CartItemMatcher: deduplicate by merging quantity into existing matching item
214
- const matcher = this.deps.cartItemMatcher ?? defaultCartItemMatcher;
215
- const existingItems = await this.repo.findLineItemsByCartId(
216
- input.cartId,
217
- ctx,
218
- );
219
- const match = existingItems.find((existing) =>
220
- matcher({
221
- existingItem: existing,
222
- newItem: {
223
- ...processed,
224
- variantId: processed.variantId ?? null,
225
- },
226
- }),
227
- );
228
-
229
- let item: CartLineItem;
230
- if (match) {
231
- const updated = await this.repo.updateLineItem(
232
- match.id,
233
- { quantity: match.quantity + quantity },
234
- ctx,
235
- );
236
- item = updated!;
237
- } else {
238
- item = await this.repo.createLineItem(
239
- {
240
- cartId: input.cartId,
241
- entityId: processed.entityId,
242
- quantity,
243
- unitPriceSnapshot: processed.unitPriceSnapshot ?? 1000,
244
- currency: processed.currency ?? cart.currency,
245
- metadata: processed.metadata ?? {},
246
- ...(processed.variantId !== undefined
247
- ? { variantId: processed.variantId }
248
- : {}),
249
- },
250
- ctx,
251
- );
252
- }
253
-
254
- await runAfterHooks(afterHooks, null, item, "addItem", context);
255
-
256
- return Ok(item);
257
- }
258
-
259
- async removeItem(
260
- cartId: string,
261
- itemId: string,
262
- actor?: Actor | null,
263
- ctx?: TxContext,
264
- ): Promise<Result<void>> {
265
- try {
266
- assertPermission(actor ?? null, "cart:update");
267
- } catch (error) {
268
- return Err(toCommerceError(error));
269
- }
270
-
271
- const orgId = resolveOrgId(actor ?? null);
272
- const cart = await this.repo.findById(orgId, cartId, ctx);
273
- if (!cart) return Err(new CommerceNotFoundError("Cart not found."));
274
-
275
- try {
276
- this.assertCartOwnership(actor ?? null, cart);
277
- } catch (error) {
278
- return Err(toCommerceError(error));
279
- }
280
-
281
- const existing = await this.repo.findLineItemById(itemId, ctx);
282
- if (!existing || existing.cartId !== cartId) {
283
- return Err(new CommerceNotFoundError("Cart item not found."));
284
- }
285
-
286
- const context = makeContext(actor ?? null, this.deps.services, ctx?.tx);
287
- const beforeHooks = this.deps.hooks.resolve(
288
- "cart.beforeRemoveItem",
289
- ) as CartRemoveBeforeHook[];
290
- const afterHooks = this.deps.hooks.resolve(
291
- "cart.afterRemoveItem",
292
- ) as CartRemoveAfterHook[];
293
- await runBeforeHooks(beforeHooks, existing, "removeItem", context);
294
-
295
- await this.repo.deleteLineItem(itemId, ctx);
296
-
297
- await runAfterHooks(afterHooks, existing, existing, "removeItem", context);
298
- return Ok(undefined);
299
- }
300
-
301
- async updateQuantity(
302
- input: UpdateCartItemInput,
303
- actor?: Actor | null,
304
- ctx?: TxContext,
305
- ): Promise<Result<CartLineItem>> {
306
- try {
307
- assertPermission(actor ?? null, "cart:update");
308
- } catch (error) {
309
- return Err(toCommerceError(error));
310
- }
311
-
312
- const orgId = resolveOrgId(actor ?? null);
313
- const cart = await this.repo.findById(orgId, input.cartId, ctx);
314
- if (!cart) return Err(new CommerceNotFoundError("Cart not found."));
315
-
316
- try {
317
- this.assertCartOwnership(actor ?? null, cart);
318
- } catch (error) {
319
- return Err(toCommerceError(error));
320
- }
321
-
322
- const item = await this.repo.findLineItemById(input.itemId, ctx);
323
- if (!item || item.cartId !== input.cartId) {
324
- return Err(new CommerceNotFoundError("Cart item not found."));
325
- }
326
-
327
- if (input.quantity <= 0) {
328
- return Err(
329
- new CommerceValidationError("Quantity must be greater than zero."),
330
- );
331
- }
332
-
333
- const context = makeContext(actor ?? null, this.deps.services, ctx?.tx);
334
- const beforeHooks = this.deps.hooks.resolve(
335
- "cart.beforeUpdateQuantity",
336
- ) as CartUpdateBeforeHook[];
337
- const afterHooks = this.deps.hooks.resolve(
338
- "cart.afterUpdateQuantity",
339
- ) as CartUpdateAfterHook[];
340
-
341
- await runBeforeHooks(beforeHooks, input, "update", context);
342
-
343
- const updated = await this.repo.updateLineItem(
344
- input.itemId,
345
- { quantity: input.quantity },
346
- ctx,
347
- );
348
-
349
- if (!updated) {
350
- return Err(new CommerceNotFoundError("Cart item not found."));
351
- }
352
-
353
- await runAfterHooks(afterHooks, item, updated, "update", context);
354
- return Ok(updated);
355
- }
356
-
357
- async merge(
358
- sourceCartId: string,
359
- targetCartId: string,
360
- actor?: Actor | null,
361
- ctx?: TxContext,
362
- ): Promise<Result<void>> {
363
- try {
364
- assertPermission(actor ?? null, "cart:update");
365
- } catch (error) {
366
- return Err(toCommerceError(error));
367
- }
368
-
369
- const orgId = resolveOrgId(actor ?? null);
370
- const source = await this.repo.findById(orgId, sourceCartId, ctx);
371
- const target = await this.repo.findById(orgId, targetCartId, ctx);
372
- if (!source || !target)
373
- return Err(new CommerceNotFoundError("Cart not found."));
374
-
375
- const sourceItems = await this.repo.findLineItemsByCartId(
376
- sourceCartId,
377
- ctx,
378
- );
379
-
380
- // Move items from source to target
381
- for (const item of sourceItems) {
382
- await this.repo.createLineItem(
383
- {
384
- cartId: targetCartId,
385
- entityId: item.entityId,
386
- variantId: item.variantId,
387
- quantity: item.quantity,
388
- unitPriceSnapshot: item.unitPriceSnapshot,
389
- currency: item.currency,
390
- metadata: item.metadata ?? {},
391
- },
392
- ctx,
393
- );
394
- }
395
-
396
- // Clear source cart items and mark as merged
397
- await this.repo.deleteLineItemsByCartId(sourceCartId, ctx);
398
- await this.repo.updateStatus(sourceCartId, "merged", ctx);
399
-
400
- return Ok(undefined);
401
- }
402
-
403
- async abandon(cartId: string, actor?: Actor | null, ctx?: TxContext): Promise<Result<void>> {
404
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
405
- const cart = await this.repo.findById(orgId, cartId, ctx);
406
- if (!cart) return Err(new CommerceNotFoundError("Cart not found."));
407
-
408
- await this.repo.updateStatus(cartId, "abandoned", ctx);
409
- return Ok(undefined);
410
- }
411
-
412
- async markAsCheckedOut(
413
- cartId: string,
414
- actor?: Actor | null,
415
- ctx?: TxContext,
416
- ): Promise<Result<void>> {
417
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
418
- const cart = await this.repo.findById(orgId, cartId, ctx);
419
- if (!cart) return Err(new CommerceNotFoundError("Cart not found."));
420
-
421
- await this.repo.updateStatus(cartId, "checked_out", ctx);
422
- return Ok(undefined);
423
- }
424
-
425
- /**
426
- * Atomically transitions a cart from "active" to "checking_out".
427
- * Returns Err if the cart was already claimed by a concurrent checkout.
428
- * This prevents TOCTOU race conditions on double-checkout.
429
- */
430
- async claimForCheckout(
431
- cartId: string,
432
- ctx?: TxContext,
433
- ): Promise<Result<Cart>> {
434
- const claimed = await this.repo.transitionToCheckingOut(cartId, ctx);
435
- if (!claimed) {
436
- return Err(
437
- new CommerceValidationError(
438
- "Cart is not available for checkout. It may have already been checked out by a concurrent request.",
439
- ),
440
- );
441
- }
442
- return Ok(claimed);
443
- }
444
-
445
- /**
446
- * Creates an anonymous guest cart with a secret token for access control.
447
- * The secret must be stored client-side (cookie/local storage) and sent
448
- * with subsequent requests to identify the cart owner.
449
- */
450
- async createGuestCart(
451
- currency = "USD",
452
- ctx?: TxContext,
453
- ): Promise<Result<{ cart: Cart; secret: string }>> {
454
- const secret = crypto.randomUUID();
455
- const ttlMinutes = this.deps.config.cart?.ttlMinutes ?? 60 * 24 * 7;
456
- const now = new Date();
457
-
458
- const cart = await this.repo.create(
459
- {
460
- organizationId: resolveOrgId(null),
461
- customerId: undefined,
462
- status: "active",
463
- currency,
464
- secret,
465
- metadata: {},
466
- expiresAt: new Date(now.getTime() + ttlMinutes * 60 * 1000),
467
- },
468
- ctx,
469
- );
470
-
471
- return Ok({ cart, secret });
472
- }
473
-
474
- /**
475
- * Merges a guest (source) cart into an authenticated (target) cart on login.
476
- * Uses addItem() internally so CartItemMatcher deduplication is applied.
477
- * The source cart's secret must be provided for access control.
478
- */
479
- async mergeCarts(
480
- targetCartId: string,
481
- sourceCartId: string,
482
- sourceSecret: string,
483
- actor: Actor,
484
- ctx?: TxContext,
485
- ): Promise<Result<Cart>> {
486
- const orgId = resolveOrgId(actor);
487
- const sourceCart = await this.repo.findById(orgId, sourceCartId, ctx);
488
- if (!sourceCart || sourceCart.secret !== sourceSecret) {
489
- return Err(
490
- new CommerceValidationError("Invalid cart or cart secret."),
491
- );
492
- }
493
-
494
- const targetCart = await this.repo.findById(orgId, targetCartId, ctx);
495
- if (!targetCart) {
496
- return Err(new CommerceNotFoundError("Target cart not found."));
497
- }
498
-
499
- const sourceItems = await this.repo.findLineItemsByCartId(
500
- sourceCartId,
501
- ctx,
502
- );
503
-
504
- for (const item of sourceItems) {
505
- await this.addItem(
506
- {
507
- cartId: targetCartId,
508
- entityId: item.entityId,
509
- quantity: item.quantity,
510
- ...(item.variantId != null ? { variantId: item.variantId } : {}),
511
- unitPriceSnapshot: item.unitPriceSnapshot,
512
- currency: item.currency,
513
- metadata: item.metadata ?? {},
514
- },
515
- actor,
516
- ctx,
517
- );
518
- }
519
-
520
- // Mark source cart as merged
521
- await this.repo.updateStatus(sourceCartId, "merged", ctx);
522
-
523
- const mergedCart = await this.repo.findById(orgId, targetCartId, ctx);
524
- return Ok(mergedCart!);
525
- }
526
-
527
- /**
528
- * Asserts the actor owns the cart. Bypassed for:
529
- * - Admin/staff actors (permissions include `*:*`)
530
- * - Guest carts (customerId is null) — access is token-gated instead
531
- * - Null actors (unauthenticated guest access)
532
- */
533
- private assertCartOwnership(actor: Actor | null, cart: Cart): void {
534
- // Guest carts have no customerId — access is controlled by cart secret/token
535
- if (cart.customerId == null) return;
536
- // Unauthenticated access to a customer cart is blocked by assertPermission elsewhere
537
- if (!actor) return;
538
- // Admin/staff bypass
539
- assertOwnership(actor, cart.customerId);
540
- }
541
- }