@unifiedcommerce/core 0.2.0 → 0.2.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/package.json +2 -1
- package/src/adapters/console-email.ts +43 -0
- package/src/auth/access.ts +187 -0
- package/src/auth/auth-schema.ts +139 -0
- package/src/auth/middleware.ts +161 -0
- package/src/auth/org.ts +41 -0
- package/src/auth/permissions.ts +28 -0
- package/src/auth/setup.ts +171 -0
- package/src/auth/system-actor.ts +19 -0
- package/src/auth/types.ts +10 -0
- package/src/config/defaults.ts +82 -0
- package/src/config/define-config.ts +53 -0
- package/src/config/types.ts +301 -0
- package/src/generated/plugin-capabilities.d.ts +20 -0
- package/src/generated/plugin-manifest.ts +23 -0
- package/src/generated/plugin-repositories.d.ts +20 -0
- package/src/hooks/checkout-completion.ts +262 -0
- package/src/hooks/checkout.ts +677 -0
- package/src/hooks/order-emails.ts +62 -0
- package/src/index.ts +215 -0
- package/src/interfaces/mcp/agent-prompt.ts +174 -0
- package/src/interfaces/mcp/context-enrichment.ts +177 -0
- package/src/interfaces/mcp/server.ts +47 -0
- package/src/interfaces/mcp/tool-builder.ts +261 -0
- package/src/interfaces/mcp/tools/analytics.ts +76 -0
- package/src/interfaces/mcp/tools/cart.ts +57 -0
- package/src/interfaces/mcp/tools/catalog.ts +299 -0
- package/src/interfaces/mcp/tools/index.ts +22 -0
- package/src/interfaces/mcp/tools/inventory.ts +161 -0
- package/src/interfaces/mcp/tools/orders.ts +104 -0
- package/src/interfaces/mcp/tools/pricing.ts +94 -0
- package/src/interfaces/mcp/tools/promotions.ts +106 -0
- package/src/interfaces/mcp/tools/registry.ts +101 -0
- package/src/interfaces/mcp/tools/search.ts +42 -0
- package/src/interfaces/mcp/tools/webhooks.ts +48 -0
- package/src/interfaces/mcp/transport.ts +128 -0
- package/src/interfaces/rest/customer-portal.ts +299 -0
- package/src/interfaces/rest/index.ts +74 -0
- package/src/interfaces/rest/router.ts +333 -0
- package/src/interfaces/rest/routes/admin-jobs.ts +58 -0
- package/src/interfaces/rest/routes/audit.ts +50 -0
- package/src/interfaces/rest/routes/carts.ts +89 -0
- package/src/interfaces/rest/routes/catalog.ts +493 -0
- package/src/interfaces/rest/routes/checkout.ts +284 -0
- package/src/interfaces/rest/routes/inventory.ts +70 -0
- package/src/interfaces/rest/routes/media.ts +86 -0
- package/src/interfaces/rest/routes/orders.ts +78 -0
- package/src/interfaces/rest/routes/payments.ts +60 -0
- package/src/interfaces/rest/routes/pricing.ts +57 -0
- package/src/interfaces/rest/routes/promotions.ts +93 -0
- package/src/interfaces/rest/routes/search.ts +71 -0
- package/src/interfaces/rest/routes/webhooks.ts +46 -0
- package/src/interfaces/rest/schemas/admin-jobs.ts +40 -0
- package/src/interfaces/rest/schemas/audit.ts +46 -0
- package/src/interfaces/rest/schemas/carts.ts +125 -0
- package/src/interfaces/rest/schemas/catalog.ts +450 -0
- package/src/interfaces/rest/schemas/checkout.ts +66 -0
- package/src/interfaces/rest/schemas/customer-portal.ts +195 -0
- package/src/interfaces/rest/schemas/inventory.ts +138 -0
- package/src/interfaces/rest/schemas/media.ts +75 -0
- package/src/interfaces/rest/schemas/orders.ts +104 -0
- package/src/interfaces/rest/schemas/pricing.ts +80 -0
- package/src/interfaces/rest/schemas/promotions.ts +110 -0
- package/src/interfaces/rest/schemas/responses.ts +85 -0
- package/src/interfaces/rest/schemas/search.ts +58 -0
- package/src/interfaces/rest/schemas/shared.ts +62 -0
- package/src/interfaces/rest/schemas/webhooks.ts +68 -0
- package/src/interfaces/rest/utils.ts +104 -0
- package/src/interfaces/rest/webhook-router.ts +50 -0
- package/src/kernel/compensation/executor.ts +61 -0
- package/src/kernel/compensation/types.ts +26 -0
- package/src/kernel/database/adapter.ts +21 -0
- package/src/kernel/database/drizzle-db.ts +56 -0
- package/src/kernel/database/migrate.ts +76 -0
- package/src/kernel/database/plugin-types.ts +34 -0
- package/src/kernel/database/schema.ts +49 -0
- package/src/kernel/database/scoped-db.ts +68 -0
- package/src/kernel/database/tx-context.ts +46 -0
- package/src/kernel/error-mapper.ts +15 -0
- package/src/kernel/errors.ts +89 -0
- package/src/kernel/factory/repository-factory.ts +244 -0
- package/src/kernel/hooks/create-context.ts +43 -0
- package/src/kernel/hooks/executor.ts +88 -0
- package/src/kernel/hooks/registry.ts +74 -0
- package/src/kernel/hooks/types.ts +52 -0
- package/src/kernel/http-error.ts +44 -0
- package/src/kernel/jobs/adapter.ts +36 -0
- package/src/kernel/jobs/drizzle-adapter.ts +58 -0
- package/src/kernel/jobs/runner.ts +153 -0
- package/src/kernel/jobs/schema.ts +46 -0
- package/src/kernel/jobs/types.ts +30 -0
- package/src/kernel/local-api.ts +187 -0
- package/src/kernel/plugin/manifest.ts +271 -0
- package/src/kernel/query/executor.ts +184 -0
- package/src/kernel/query/registry.ts +46 -0
- package/src/kernel/result.ts +33 -0
- package/src/kernel/schema/extra-columns.ts +37 -0
- package/src/kernel/service-registry.ts +76 -0
- package/src/kernel/service-timing.ts +89 -0
- package/src/kernel/state-machine/machine.ts +101 -0
- package/src/modules/analytics/drizzle-adapter.ts +426 -0
- package/src/modules/analytics/hooks.ts +11 -0
- package/src/modules/analytics/models.ts +125 -0
- package/src/modules/analytics/repository/index.ts +6 -0
- package/src/modules/analytics/service.ts +245 -0
- package/src/modules/analytics/types.ts +180 -0
- package/src/modules/audit/hooks.ts +78 -0
- package/src/modules/audit/schema.ts +33 -0
- package/src/modules/audit/service.ts +151 -0
- package/src/modules/cart/access.ts +27 -0
- package/src/modules/cart/matcher.ts +26 -0
- package/src/modules/cart/repository/index.ts +234 -0
- package/src/modules/cart/schema.ts +42 -0
- package/src/modules/cart/schemas.ts +38 -0
- package/src/modules/cart/service.ts +541 -0
- package/src/modules/catalog/repository/index.ts +772 -0
- package/src/modules/catalog/schema.ts +203 -0
- package/src/modules/catalog/schemas.ts +104 -0
- package/src/modules/catalog/service.ts +1544 -0
- package/src/modules/customers/repository/index.ts +327 -0
- package/src/modules/customers/schema.ts +64 -0
- package/src/modules/customers/service.ts +171 -0
- package/src/modules/fulfillment/repository/index.ts +426 -0
- package/src/modules/fulfillment/schema.ts +101 -0
- package/src/modules/fulfillment/service.ts +555 -0
- package/src/modules/fulfillment/types.ts +59 -0
- package/src/modules/inventory/repository/index.ts +509 -0
- package/src/modules/inventory/schema.ts +94 -0
- package/src/modules/inventory/schemas.ts +38 -0
- package/src/modules/inventory/service.ts +490 -0
- package/src/modules/media/adapter.ts +17 -0
- package/src/modules/media/repository/index.ts +274 -0
- package/src/modules/media/schema.ts +41 -0
- package/src/modules/media/service.ts +151 -0
- package/src/modules/orders/repository/index.ts +287 -0
- package/src/modules/orders/schema.ts +66 -0
- package/src/modules/orders/service.ts +619 -0
- package/src/modules/orders/stale-order-cleanup.ts +76 -0
- package/src/modules/organization/service.ts +191 -0
- package/src/modules/payments/adapter.ts +47 -0
- package/src/modules/payments/repository/index.ts +6 -0
- package/src/modules/payments/service.ts +107 -0
- package/src/modules/pricing/repository/index.ts +291 -0
- package/src/modules/pricing/schema.ts +71 -0
- package/src/modules/pricing/schemas.ts +38 -0
- package/src/modules/pricing/service.ts +494 -0
- package/src/modules/promotions/repository/index.ts +325 -0
- package/src/modules/promotions/schema.ts +62 -0
- package/src/modules/promotions/schemas.ts +38 -0
- package/src/modules/promotions/service.ts +598 -0
- package/src/modules/search/adapter.ts +57 -0
- package/src/modules/search/hooks.ts +12 -0
- package/src/modules/search/repository/index.ts +6 -0
- package/src/modules/search/service.ts +315 -0
- package/src/modules/shipping/calculator.ts +188 -0
- package/src/modules/shipping/repository/index.ts +6 -0
- package/src/modules/shipping/service.ts +51 -0
- package/src/modules/tax/adapter.ts +60 -0
- package/src/modules/tax/repository/index.ts +6 -0
- package/src/modules/tax/service.ts +53 -0
- package/src/modules/webhooks/hook.ts +34 -0
- package/src/modules/webhooks/repository/index.ts +278 -0
- package/src/modules/webhooks/schema.ts +56 -0
- package/src/modules/webhooks/service.ts +117 -0
- package/src/modules/webhooks/signing.ts +6 -0
- package/src/modules/webhooks/ssrf-guard.ts +71 -0
- package/src/modules/webhooks/tasks.ts +52 -0
- package/src/modules/webhooks/worker.ts +134 -0
- package/src/runtime/commerce.ts +145 -0
- package/src/runtime/kernel.ts +426 -0
- package/src/runtime/logger.ts +36 -0
- package/src/runtime/server.ts +355 -0
- package/src/runtime/shutdown.ts +43 -0
- package/src/test-utils/create-pglite-adapter.ts +129 -0
- package/src/test-utils/create-plugin-test-app.ts +128 -0
- package/src/test-utils/create-repository-test-harness.ts +16 -0
- package/src/test-utils/create-test-config.ts +190 -0
- package/src/test-utils/create-test-kernel.ts +7 -0
- package/src/test-utils/create-test-plugin-context.ts +75 -0
- package/src/test-utils/rest-api-test-utils.ts +265 -0
- package/src/test-utils/test-actors.ts +62 -0
- package/src/test-utils/typed-hooks.ts +54 -0
- package/src/types/commerce-types.ts +34 -0
- package/src/utils/id.ts +3 -0
- package/src/utils/logger.ts +18 -0
- package/src/utils/pagination.ts +22 -0
|
@@ -0,0 +1,541 @@
|
|
|
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
|
+
}
|