@unifiedcommerce/core 0.1.0 → 0.2.0

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 (245) hide show
  1. package/dist/auth/setup.d.ts.map +1 -1
  2. package/dist/auth/setup.js +8 -3
  3. package/dist/config/types.d.ts +3 -1
  4. package/dist/config/types.d.ts.map +1 -1
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -0
  8. package/dist/interfaces/mcp/server.d.ts +3 -5
  9. package/dist/interfaces/mcp/server.d.ts.map +1 -1
  10. package/dist/interfaces/mcp/server.js +25 -510
  11. package/dist/interfaces/mcp/tool-builder.d.ts +120 -0
  12. package/dist/interfaces/mcp/tool-builder.d.ts.map +1 -0
  13. package/dist/interfaces/mcp/tool-builder.js +224 -0
  14. package/dist/interfaces/mcp/tools/analytics.d.ts +42 -0
  15. package/dist/interfaces/mcp/tools/analytics.d.ts.map +1 -0
  16. package/dist/interfaces/mcp/tools/analytics.js +70 -0
  17. package/dist/interfaces/mcp/tools/cart.d.ts +14 -0
  18. package/dist/interfaces/mcp/tools/cart.d.ts.map +1 -0
  19. package/dist/interfaces/mcp/tools/cart.js +47 -0
  20. package/dist/interfaces/mcp/tools/catalog.d.ts +53 -0
  21. package/dist/interfaces/mcp/tools/catalog.d.ts.map +1 -0
  22. package/dist/interfaces/mcp/tools/catalog.js +284 -0
  23. package/dist/interfaces/mcp/tools/index.d.ts +3 -0
  24. package/dist/interfaces/mcp/tools/index.d.ts.map +1 -0
  25. package/dist/interfaces/mcp/tools/index.js +20 -0
  26. package/dist/interfaces/mcp/tools/inventory.d.ts +27 -0
  27. package/dist/interfaces/mcp/tools/inventory.d.ts.map +1 -0
  28. package/dist/interfaces/mcp/tools/inventory.js +143 -0
  29. package/dist/interfaces/mcp/tools/orders.d.ts +18 -0
  30. package/dist/interfaces/mcp/tools/orders.d.ts.map +1 -0
  31. package/dist/interfaces/mcp/tools/orders.js +82 -0
  32. package/dist/interfaces/mcp/tools/pricing.d.ts +29 -0
  33. package/dist/interfaces/mcp/tools/pricing.d.ts.map +1 -0
  34. package/dist/interfaces/mcp/tools/pricing.js +90 -0
  35. package/dist/interfaces/mcp/tools/promotions.d.ts +44 -0
  36. package/dist/interfaces/mcp/tools/promotions.d.ts.map +1 -0
  37. package/dist/interfaces/mcp/tools/promotions.js +109 -0
  38. package/dist/interfaces/mcp/tools/registry.d.ts +32 -0
  39. package/dist/interfaces/mcp/tools/registry.d.ts.map +1 -0
  40. package/dist/interfaces/mcp/tools/registry.js +55 -0
  41. package/dist/interfaces/mcp/tools/search.d.ts +14 -0
  42. package/dist/interfaces/mcp/tools/search.d.ts.map +1 -0
  43. package/dist/interfaces/mcp/tools/search.js +39 -0
  44. package/dist/interfaces/mcp/tools/webhooks.d.ts +15 -0
  45. package/dist/interfaces/mcp/tools/webhooks.d.ts.map +1 -0
  46. package/dist/interfaces/mcp/tools/webhooks.js +48 -0
  47. package/dist/interfaces/mcp/transport.d.ts +17 -2
  48. package/dist/interfaces/mcp/transport.d.ts.map +1 -1
  49. package/dist/interfaces/mcp/transport.js +91 -44
  50. package/dist/interfaces/rest/router.d.ts.map +1 -1
  51. package/dist/interfaces/rest/routes/checkout.d.ts.map +1 -1
  52. package/dist/interfaces/rest/routes/checkout.js +1 -1
  53. package/dist/interfaces/rest/routes/promotions.d.ts.map +1 -1
  54. package/dist/interfaces/rest/routes/promotions.js +3 -2
  55. package/dist/kernel/database/adapter.d.ts +8 -0
  56. package/dist/kernel/database/adapter.d.ts.map +1 -1
  57. package/dist/kernel/factory/repository-factory.d.ts.map +1 -1
  58. package/dist/kernel/factory/repository-factory.js +3 -1
  59. package/dist/kernel/local-api.d.ts.map +1 -1
  60. package/dist/kernel/local-api.js +2 -0
  61. package/dist/kernel/plugin/manifest.d.ts +3 -3
  62. package/dist/kernel/plugin/manifest.d.ts.map +1 -1
  63. package/dist/kernel/plugin/manifest.js +36 -7
  64. package/dist/runtime/kernel.d.ts +1 -2
  65. package/dist/runtime/kernel.d.ts.map +1 -1
  66. package/dist/runtime/kernel.js +16 -8
  67. package/dist/runtime/server.d.ts.map +1 -1
  68. package/dist/runtime/server.js +8 -3
  69. package/dist/test-utils/create-pglite-adapter.d.ts.map +1 -1
  70. package/dist/test-utils/create-pglite-adapter.js +7 -6
  71. package/dist/tsconfig.tsbuildinfo +1 -0
  72. package/package.json +2 -2
  73. package/src/adapters/console-email.ts +0 -43
  74. package/src/auth/access.ts +0 -187
  75. package/src/auth/auth-schema.ts +0 -139
  76. package/src/auth/middleware.ts +0 -161
  77. package/src/auth/org.ts +0 -41
  78. package/src/auth/permissions.ts +0 -28
  79. package/src/auth/setup.ts +0 -169
  80. package/src/auth/system-actor.ts +0 -19
  81. package/src/auth/types.ts +0 -10
  82. package/src/config/defaults.ts +0 -82
  83. package/src/config/define-config.ts +0 -53
  84. package/src/config/types.ts +0 -299
  85. package/src/generated/plugin-capabilities.d.ts +0 -20
  86. package/src/generated/plugin-manifest.ts +0 -23
  87. package/src/generated/plugin-repositories.d.ts +0 -20
  88. package/src/hooks/checkout-completion.ts +0 -262
  89. package/src/hooks/checkout.ts +0 -677
  90. package/src/hooks/order-emails.ts +0 -62
  91. package/src/index.ts +0 -214
  92. package/src/interfaces/mcp/agent-prompt.ts +0 -174
  93. package/src/interfaces/mcp/context-enrichment.ts +0 -177
  94. package/src/interfaces/mcp/server.ts +0 -617
  95. package/src/interfaces/mcp/transport.ts +0 -68
  96. package/src/interfaces/rest/customer-portal.ts +0 -299
  97. package/src/interfaces/rest/index.ts +0 -74
  98. package/src/interfaces/rest/router.ts +0 -334
  99. package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
  100. package/src/interfaces/rest/routes/audit.ts +0 -50
  101. package/src/interfaces/rest/routes/carts.ts +0 -89
  102. package/src/interfaces/rest/routes/catalog.ts +0 -493
  103. package/src/interfaces/rest/routes/checkout.ts +0 -283
  104. package/src/interfaces/rest/routes/inventory.ts +0 -70
  105. package/src/interfaces/rest/routes/media.ts +0 -86
  106. package/src/interfaces/rest/routes/orders.ts +0 -78
  107. package/src/interfaces/rest/routes/payments.ts +0 -60
  108. package/src/interfaces/rest/routes/pricing.ts +0 -57
  109. package/src/interfaces/rest/routes/promotions.ts +0 -92
  110. package/src/interfaces/rest/routes/search.ts +0 -71
  111. package/src/interfaces/rest/routes/webhooks.ts +0 -46
  112. package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
  113. package/src/interfaces/rest/schemas/audit.ts +0 -46
  114. package/src/interfaces/rest/schemas/carts.ts +0 -125
  115. package/src/interfaces/rest/schemas/catalog.ts +0 -450
  116. package/src/interfaces/rest/schemas/checkout.ts +0 -66
  117. package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
  118. package/src/interfaces/rest/schemas/inventory.ts +0 -138
  119. package/src/interfaces/rest/schemas/media.ts +0 -75
  120. package/src/interfaces/rest/schemas/orders.ts +0 -104
  121. package/src/interfaces/rest/schemas/pricing.ts +0 -80
  122. package/src/interfaces/rest/schemas/promotions.ts +0 -110
  123. package/src/interfaces/rest/schemas/responses.ts +0 -85
  124. package/src/interfaces/rest/schemas/search.ts +0 -58
  125. package/src/interfaces/rest/schemas/shared.ts +0 -62
  126. package/src/interfaces/rest/schemas/webhooks.ts +0 -68
  127. package/src/interfaces/rest/utils.ts +0 -104
  128. package/src/interfaces/rest/webhook-router.ts +0 -50
  129. package/src/kernel/compensation/executor.ts +0 -61
  130. package/src/kernel/compensation/types.ts +0 -26
  131. package/src/kernel/database/adapter.ts +0 -13
  132. package/src/kernel/database/drizzle-db.ts +0 -56
  133. package/src/kernel/database/migrate.ts +0 -76
  134. package/src/kernel/database/plugin-types.ts +0 -34
  135. package/src/kernel/database/schema.ts +0 -49
  136. package/src/kernel/database/scoped-db.ts +0 -68
  137. package/src/kernel/database/tx-context.ts +0 -46
  138. package/src/kernel/error-mapper.ts +0 -15
  139. package/src/kernel/errors.ts +0 -89
  140. package/src/kernel/factory/repository-factory.ts +0 -242
  141. package/src/kernel/hooks/create-context.ts +0 -43
  142. package/src/kernel/hooks/executor.ts +0 -88
  143. package/src/kernel/hooks/registry.ts +0 -74
  144. package/src/kernel/hooks/types.ts +0 -52
  145. package/src/kernel/http-error.ts +0 -44
  146. package/src/kernel/jobs/adapter.ts +0 -36
  147. package/src/kernel/jobs/drizzle-adapter.ts +0 -58
  148. package/src/kernel/jobs/runner.ts +0 -153
  149. package/src/kernel/jobs/schema.ts +0 -46
  150. package/src/kernel/jobs/types.ts +0 -30
  151. package/src/kernel/local-api.ts +0 -185
  152. package/src/kernel/plugin/manifest.ts +0 -253
  153. package/src/kernel/query/executor.ts +0 -184
  154. package/src/kernel/query/registry.ts +0 -46
  155. package/src/kernel/result.ts +0 -33
  156. package/src/kernel/schema/extra-columns.ts +0 -37
  157. package/src/kernel/service-registry.ts +0 -76
  158. package/src/kernel/service-timing.ts +0 -89
  159. package/src/kernel/state-machine/machine.ts +0 -101
  160. package/src/modules/analytics/drizzle-adapter.ts +0 -426
  161. package/src/modules/analytics/hooks.ts +0 -11
  162. package/src/modules/analytics/models.ts +0 -125
  163. package/src/modules/analytics/repository/index.ts +0 -6
  164. package/src/modules/analytics/service.ts +0 -245
  165. package/src/modules/analytics/types.ts +0 -180
  166. package/src/modules/audit/hooks.ts +0 -78
  167. package/src/modules/audit/schema.ts +0 -33
  168. package/src/modules/audit/service.ts +0 -151
  169. package/src/modules/cart/access.ts +0 -27
  170. package/src/modules/cart/matcher.ts +0 -26
  171. package/src/modules/cart/repository/index.ts +0 -234
  172. package/src/modules/cart/schema.ts +0 -42
  173. package/src/modules/cart/schemas.ts +0 -38
  174. package/src/modules/cart/service.ts +0 -541
  175. package/src/modules/catalog/repository/index.ts +0 -772
  176. package/src/modules/catalog/schema.ts +0 -203
  177. package/src/modules/catalog/schemas.ts +0 -104
  178. package/src/modules/catalog/service.ts +0 -1544
  179. package/src/modules/customers/repository/index.ts +0 -327
  180. package/src/modules/customers/schema.ts +0 -64
  181. package/src/modules/customers/service.ts +0 -171
  182. package/src/modules/fulfillment/repository/index.ts +0 -426
  183. package/src/modules/fulfillment/schema.ts +0 -101
  184. package/src/modules/fulfillment/service.ts +0 -555
  185. package/src/modules/fulfillment/types.ts +0 -59
  186. package/src/modules/inventory/repository/index.ts +0 -509
  187. package/src/modules/inventory/schema.ts +0 -94
  188. package/src/modules/inventory/schemas.ts +0 -38
  189. package/src/modules/inventory/service.ts +0 -490
  190. package/src/modules/media/adapter.ts +0 -17
  191. package/src/modules/media/repository/index.ts +0 -274
  192. package/src/modules/media/schema.ts +0 -41
  193. package/src/modules/media/service.ts +0 -151
  194. package/src/modules/orders/repository/index.ts +0 -287
  195. package/src/modules/orders/schema.ts +0 -66
  196. package/src/modules/orders/service.ts +0 -619
  197. package/src/modules/orders/stale-order-cleanup.ts +0 -76
  198. package/src/modules/organization/service.ts +0 -191
  199. package/src/modules/payments/adapter.ts +0 -47
  200. package/src/modules/payments/repository/index.ts +0 -6
  201. package/src/modules/payments/service.ts +0 -107
  202. package/src/modules/pricing/repository/index.ts +0 -291
  203. package/src/modules/pricing/schema.ts +0 -71
  204. package/src/modules/pricing/schemas.ts +0 -38
  205. package/src/modules/pricing/service.ts +0 -494
  206. package/src/modules/promotions/repository/index.ts +0 -325
  207. package/src/modules/promotions/schema.ts +0 -62
  208. package/src/modules/promotions/schemas.ts +0 -38
  209. package/src/modules/promotions/service.ts +0 -598
  210. package/src/modules/search/adapter.ts +0 -57
  211. package/src/modules/search/hooks.ts +0 -12
  212. package/src/modules/search/repository/index.ts +0 -6
  213. package/src/modules/search/service.ts +0 -315
  214. package/src/modules/shipping/calculator.ts +0 -188
  215. package/src/modules/shipping/repository/index.ts +0 -6
  216. package/src/modules/shipping/service.ts +0 -51
  217. package/src/modules/tax/adapter.ts +0 -60
  218. package/src/modules/tax/repository/index.ts +0 -6
  219. package/src/modules/tax/service.ts +0 -53
  220. package/src/modules/webhooks/hook.ts +0 -34
  221. package/src/modules/webhooks/repository/index.ts +0 -278
  222. package/src/modules/webhooks/schema.ts +0 -56
  223. package/src/modules/webhooks/service.ts +0 -117
  224. package/src/modules/webhooks/signing.ts +0 -6
  225. package/src/modules/webhooks/ssrf-guard.ts +0 -71
  226. package/src/modules/webhooks/tasks.ts +0 -52
  227. package/src/modules/webhooks/worker.ts +0 -134
  228. package/src/runtime/commerce.ts +0 -145
  229. package/src/runtime/kernel.ts +0 -419
  230. package/src/runtime/logger.ts +0 -36
  231. package/src/runtime/server.ts +0 -349
  232. package/src/runtime/shutdown.ts +0 -43
  233. package/src/test-utils/create-pglite-adapter.ts +0 -129
  234. package/src/test-utils/create-plugin-test-app.ts +0 -128
  235. package/src/test-utils/create-repository-test-harness.ts +0 -16
  236. package/src/test-utils/create-test-config.ts +0 -190
  237. package/src/test-utils/create-test-kernel.ts +0 -7
  238. package/src/test-utils/create-test-plugin-context.ts +0 -75
  239. package/src/test-utils/rest-api-test-utils.ts +0 -265
  240. package/src/test-utils/test-actors.ts +0 -62
  241. package/src/test-utils/typed-hooks.ts +0 -54
  242. package/src/types/commerce-types.ts +0 -34
  243. package/src/utils/id.ts +0 -3
  244. package/src/utils/logger.ts +0 -18
  245. 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
- }