@unifiedcommerce/core 0.0.4 → 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 (179) hide show
  1. package/dist/auth/auth-schema.d.ts +92 -0
  2. package/dist/auth/auth-schema.d.ts.map +1 -1
  3. package/dist/auth/auth-schema.js +7 -0
  4. package/dist/auth/setup.d.ts.map +1 -1
  5. package/dist/auth/setup.js +3 -1
  6. package/package.json +1 -2
  7. package/src/adapters/console-email.ts +0 -43
  8. package/src/auth/access.ts +0 -187
  9. package/src/auth/auth-schema.ts +0 -131
  10. package/src/auth/middleware.ts +0 -161
  11. package/src/auth/org.ts +0 -41
  12. package/src/auth/permissions.ts +0 -28
  13. package/src/auth/setup.ts +0 -165
  14. package/src/auth/system-actor.ts +0 -19
  15. package/src/auth/types.ts +0 -10
  16. package/src/config/defaults.ts +0 -82
  17. package/src/config/define-config.ts +0 -53
  18. package/src/config/types.ts +0 -299
  19. package/src/generated/plugin-capabilities.d.ts +0 -20
  20. package/src/generated/plugin-manifest.ts +0 -23
  21. package/src/generated/plugin-repositories.d.ts +0 -20
  22. package/src/hooks/checkout-completion.ts +0 -262
  23. package/src/hooks/checkout.ts +0 -677
  24. package/src/hooks/order-emails.ts +0 -62
  25. package/src/index.ts +0 -214
  26. package/src/interfaces/mcp/agent-prompt.ts +0 -174
  27. package/src/interfaces/mcp/context-enrichment.ts +0 -177
  28. package/src/interfaces/mcp/server.ts +0 -617
  29. package/src/interfaces/mcp/transport.ts +0 -68
  30. package/src/interfaces/rest/customer-portal.ts +0 -299
  31. package/src/interfaces/rest/index.ts +0 -74
  32. package/src/interfaces/rest/router.ts +0 -334
  33. package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
  34. package/src/interfaces/rest/routes/audit.ts +0 -50
  35. package/src/interfaces/rest/routes/carts.ts +0 -89
  36. package/src/interfaces/rest/routes/catalog.ts +0 -493
  37. package/src/interfaces/rest/routes/checkout.ts +0 -283
  38. package/src/interfaces/rest/routes/inventory.ts +0 -70
  39. package/src/interfaces/rest/routes/media.ts +0 -86
  40. package/src/interfaces/rest/routes/orders.ts +0 -78
  41. package/src/interfaces/rest/routes/payments.ts +0 -60
  42. package/src/interfaces/rest/routes/pricing.ts +0 -57
  43. package/src/interfaces/rest/routes/promotions.ts +0 -92
  44. package/src/interfaces/rest/routes/search.ts +0 -71
  45. package/src/interfaces/rest/routes/webhooks.ts +0 -46
  46. package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
  47. package/src/interfaces/rest/schemas/audit.ts +0 -46
  48. package/src/interfaces/rest/schemas/carts.ts +0 -125
  49. package/src/interfaces/rest/schemas/catalog.ts +0 -450
  50. package/src/interfaces/rest/schemas/checkout.ts +0 -66
  51. package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
  52. package/src/interfaces/rest/schemas/inventory.ts +0 -138
  53. package/src/interfaces/rest/schemas/media.ts +0 -75
  54. package/src/interfaces/rest/schemas/orders.ts +0 -104
  55. package/src/interfaces/rest/schemas/pricing.ts +0 -80
  56. package/src/interfaces/rest/schemas/promotions.ts +0 -110
  57. package/src/interfaces/rest/schemas/responses.ts +0 -85
  58. package/src/interfaces/rest/schemas/search.ts +0 -58
  59. package/src/interfaces/rest/schemas/shared.ts +0 -62
  60. package/src/interfaces/rest/schemas/webhooks.ts +0 -68
  61. package/src/interfaces/rest/utils.ts +0 -104
  62. package/src/interfaces/rest/webhook-router.ts +0 -50
  63. package/src/kernel/compensation/executor.ts +0 -61
  64. package/src/kernel/compensation/types.ts +0 -26
  65. package/src/kernel/database/adapter.ts +0 -13
  66. package/src/kernel/database/drizzle-db.ts +0 -56
  67. package/src/kernel/database/migrate.ts +0 -76
  68. package/src/kernel/database/plugin-types.ts +0 -34
  69. package/src/kernel/database/schema.ts +0 -49
  70. package/src/kernel/database/scoped-db.ts +0 -68
  71. package/src/kernel/database/tx-context.ts +0 -46
  72. package/src/kernel/error-mapper.ts +0 -15
  73. package/src/kernel/errors.ts +0 -89
  74. package/src/kernel/factory/repository-factory.ts +0 -242
  75. package/src/kernel/hooks/create-context.ts +0 -43
  76. package/src/kernel/hooks/executor.ts +0 -88
  77. package/src/kernel/hooks/registry.ts +0 -74
  78. package/src/kernel/hooks/types.ts +0 -52
  79. package/src/kernel/http-error.ts +0 -44
  80. package/src/kernel/jobs/adapter.ts +0 -36
  81. package/src/kernel/jobs/drizzle-adapter.ts +0 -58
  82. package/src/kernel/jobs/runner.ts +0 -153
  83. package/src/kernel/jobs/schema.ts +0 -46
  84. package/src/kernel/jobs/types.ts +0 -30
  85. package/src/kernel/local-api.ts +0 -185
  86. package/src/kernel/plugin/manifest.ts +0 -253
  87. package/src/kernel/query/executor.ts +0 -184
  88. package/src/kernel/query/registry.ts +0 -46
  89. package/src/kernel/result.ts +0 -33
  90. package/src/kernel/schema/extra-columns.ts +0 -37
  91. package/src/kernel/service-registry.ts +0 -76
  92. package/src/kernel/service-timing.ts +0 -89
  93. package/src/kernel/state-machine/machine.ts +0 -101
  94. package/src/modules/analytics/drizzle-adapter.ts +0 -426
  95. package/src/modules/analytics/hooks.ts +0 -11
  96. package/src/modules/analytics/models.ts +0 -125
  97. package/src/modules/analytics/repository/index.ts +0 -6
  98. package/src/modules/analytics/service.ts +0 -245
  99. package/src/modules/analytics/types.ts +0 -180
  100. package/src/modules/audit/hooks.ts +0 -78
  101. package/src/modules/audit/schema.ts +0 -33
  102. package/src/modules/audit/service.ts +0 -151
  103. package/src/modules/cart/access.ts +0 -27
  104. package/src/modules/cart/matcher.ts +0 -26
  105. package/src/modules/cart/repository/index.ts +0 -234
  106. package/src/modules/cart/schema.ts +0 -42
  107. package/src/modules/cart/schemas.ts +0 -38
  108. package/src/modules/cart/service.ts +0 -541
  109. package/src/modules/catalog/repository/index.ts +0 -772
  110. package/src/modules/catalog/schema.ts +0 -203
  111. package/src/modules/catalog/schemas.ts +0 -104
  112. package/src/modules/catalog/service.ts +0 -1544
  113. package/src/modules/customers/repository/index.ts +0 -327
  114. package/src/modules/customers/schema.ts +0 -64
  115. package/src/modules/customers/service.ts +0 -171
  116. package/src/modules/fulfillment/repository/index.ts +0 -426
  117. package/src/modules/fulfillment/schema.ts +0 -101
  118. package/src/modules/fulfillment/service.ts +0 -555
  119. package/src/modules/fulfillment/types.ts +0 -59
  120. package/src/modules/inventory/repository/index.ts +0 -509
  121. package/src/modules/inventory/schema.ts +0 -94
  122. package/src/modules/inventory/schemas.ts +0 -38
  123. package/src/modules/inventory/service.ts +0 -490
  124. package/src/modules/media/adapter.ts +0 -17
  125. package/src/modules/media/repository/index.ts +0 -274
  126. package/src/modules/media/schema.ts +0 -41
  127. package/src/modules/media/service.ts +0 -151
  128. package/src/modules/orders/repository/index.ts +0 -287
  129. package/src/modules/orders/schema.ts +0 -66
  130. package/src/modules/orders/service.ts +0 -619
  131. package/src/modules/orders/stale-order-cleanup.ts +0 -76
  132. package/src/modules/organization/service.ts +0 -191
  133. package/src/modules/payments/adapter.ts +0 -47
  134. package/src/modules/payments/repository/index.ts +0 -6
  135. package/src/modules/payments/service.ts +0 -107
  136. package/src/modules/pricing/repository/index.ts +0 -291
  137. package/src/modules/pricing/schema.ts +0 -71
  138. package/src/modules/pricing/schemas.ts +0 -38
  139. package/src/modules/pricing/service.ts +0 -494
  140. package/src/modules/promotions/repository/index.ts +0 -325
  141. package/src/modules/promotions/schema.ts +0 -62
  142. package/src/modules/promotions/schemas.ts +0 -38
  143. package/src/modules/promotions/service.ts +0 -598
  144. package/src/modules/search/adapter.ts +0 -57
  145. package/src/modules/search/hooks.ts +0 -12
  146. package/src/modules/search/repository/index.ts +0 -6
  147. package/src/modules/search/service.ts +0 -315
  148. package/src/modules/shipping/calculator.ts +0 -188
  149. package/src/modules/shipping/repository/index.ts +0 -6
  150. package/src/modules/shipping/service.ts +0 -51
  151. package/src/modules/tax/adapter.ts +0 -60
  152. package/src/modules/tax/repository/index.ts +0 -6
  153. package/src/modules/tax/service.ts +0 -53
  154. package/src/modules/webhooks/hook.ts +0 -34
  155. package/src/modules/webhooks/repository/index.ts +0 -278
  156. package/src/modules/webhooks/schema.ts +0 -56
  157. package/src/modules/webhooks/service.ts +0 -117
  158. package/src/modules/webhooks/signing.ts +0 -6
  159. package/src/modules/webhooks/ssrf-guard.ts +0 -71
  160. package/src/modules/webhooks/tasks.ts +0 -52
  161. package/src/modules/webhooks/worker.ts +0 -134
  162. package/src/runtime/commerce.ts +0 -145
  163. package/src/runtime/kernel.ts +0 -419
  164. package/src/runtime/logger.ts +0 -36
  165. package/src/runtime/server.ts +0 -349
  166. package/src/runtime/shutdown.ts +0 -43
  167. package/src/test-utils/create-pglite-adapter.ts +0 -129
  168. package/src/test-utils/create-plugin-test-app.ts +0 -128
  169. package/src/test-utils/create-repository-test-harness.ts +0 -16
  170. package/src/test-utils/create-test-config.ts +0 -190
  171. package/src/test-utils/create-test-kernel.ts +0 -7
  172. package/src/test-utils/create-test-plugin-context.ts +0 -75
  173. package/src/test-utils/rest-api-test-utils.ts +0 -265
  174. package/src/test-utils/test-actors.ts +0 -62
  175. package/src/test-utils/typed-hooks.ts +0 -54
  176. package/src/types/commerce-types.ts +0 -34
  177. package/src/utils/id.ts +0 -3
  178. package/src/utils/logger.ts +0 -18
  179. package/src/utils/pagination.ts +0 -22
@@ -1,490 +0,0 @@
1
- import { resolveOrgId } from "../../auth/org.js";
2
- import { 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 } from "../../kernel/hooks/executor.js";
11
- import { createHookContext } from "../../kernel/hooks/create-context.js";
12
- import type { HookContext } from "../../kernel/hooks/types.js";
13
- import type { HookRegistry } from "../../kernel/hooks/registry.js";
14
- import { Err, Ok, type Result } from "../../kernel/result.js";
15
- import { createLogger } from "../../utils/logger.js";
16
- import type { DatabaseAdapter } from "../../kernel/database/adapter.js";
17
- import { createTxContext, type TxContext } from "../../kernel/database/tx-context.js";
18
- import {
19
- InventoryRepository,
20
- type Warehouse,
21
- type InventoryLevel,
22
- } from "./repository/index.js";
23
-
24
- export type { InventoryAdjustInput, InventoryReserveInput, InventoryReleaseInput } from "./schemas.js";
25
- import type { InventoryAdjustInput, InventoryReserveInput, InventoryReleaseInput } from "./schemas.js";
26
-
27
- export interface InventoryServiceDeps {
28
- repository: InventoryRepository;
29
- hooks: HookRegistry;
30
- config: CommerceConfig;
31
- services: Record<string, unknown>;
32
- database: DatabaseAdapter;
33
- }
34
-
35
- export class InventoryService {
36
- private readonly repo: InventoryRepository;
37
-
38
- constructor(private deps: InventoryServiceDeps) {
39
- this.repo = deps.repository;
40
- }
41
-
42
- private async pickWarehouse(actor?: Actor | null, ctx?: TxContext): Promise<string> {
43
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
44
- const warehouses = await this.repo.findAllWarehouses(orgId, ctx);
45
- const sorted = warehouses.sort((a, b) => a.priority - b.priority);
46
- if (sorted.length > 0) {
47
- return sorted[0]!.id;
48
- }
49
-
50
- // Create default warehouse if none exists
51
- const defaultWarehouse = await this.repo.createWarehouse(
52
- {
53
- organizationId: orgId,
54
- name: "Default Warehouse",
55
- code: "DEFAULT",
56
- isActive: true,
57
- priority: 0,
58
- metadata: {},
59
- },
60
- ctx,
61
- );
62
- return defaultWarehouse.id;
63
- }
64
-
65
- async createWarehouse(
66
- input: Partial<Warehouse>,
67
- actor?: Actor | null,
68
- ctx?: TxContext,
69
- ): Promise<Result<Warehouse>> {
70
- if (!input.name || !input.code) {
71
- return Err(
72
- new CommerceValidationError("Warehouse name and code are required."),
73
- );
74
- }
75
-
76
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
77
-
78
- const warehouse = await this.repo.createWarehouse(
79
- {
80
- organizationId: orgId,
81
- name: input.name,
82
- code: input.code,
83
- isActive: input.isActive ?? true,
84
- priority: input.priority ?? 0,
85
- metadata: input.metadata ?? {},
86
- ...(input.address !== undefined ? { address: input.address } : {}),
87
- },
88
- ctx,
89
- );
90
-
91
- return Ok(warehouse);
92
- }
93
-
94
- async listWarehouses(actor?: Actor | null, ctx?: TxContext): Promise<Result<Warehouse[]>> {
95
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
96
- const warehouses = await this.repo.findAllWarehouses(orgId, ctx);
97
- return Ok(warehouses.sort((a, b) => a.priority - b.priority));
98
- }
99
-
100
- async getAvailable(
101
- entityId: string,
102
- variantId?: string,
103
- ctx?: TxContext,
104
- ): Promise<Result<number>> {
105
- const available = await this.repo.getAvailableQuantity(
106
- entityId,
107
- variantId,
108
- ctx,
109
- );
110
- return Ok(available);
111
- }
112
-
113
- async checkMultiple(
114
- entityIds: string[],
115
- ctx?: TxContext,
116
- ): Promise<Result<Record<string, number>>> {
117
- const data = await this.repo.getAvailableQuantities(entityIds, ctx);
118
- return Ok(data);
119
- }
120
-
121
- async getLevelsByEntityId(
122
- entityId: string,
123
- ctx?: TxContext,
124
- ): Promise<Result<InventoryLevel[]>> {
125
- const levels = await this.repo.findLevelsByEntityId(entityId, ctx);
126
- return Ok(levels);
127
- }
128
-
129
- async reserve(
130
- input: InventoryReserveInput,
131
- actor?: Actor | null,
132
- ctx?: TxContext,
133
- ): Promise<Result<void>> {
134
- if (input.quantity <= 0) {
135
- return Err(
136
- new CommerceValidationError(
137
- "Reservation quantity must be greater than zero.",
138
- ),
139
- );
140
- }
141
-
142
- const warehouseId = input.warehouseId ?? (await this.pickWarehouse(actor, ctx));
143
- const performedBy = input.performedBy ?? actor?.userId ?? "system";
144
- const variantId = input.variantId ?? null;
145
-
146
- // If we have a transaction context, use the concurrency-safe
147
- // reserveWithLock (SELECT FOR UPDATE). This is the correct path
148
- // for checkout — it prevents two concurrent requests from both
149
- // reserving the last unit.
150
- if (ctx?.tx) {
151
- const reserveResult = await this.repo.reserveWithLock(
152
- input.entityId,
153
- variantId,
154
- warehouseId,
155
- input.quantity,
156
- ctx,
157
- );
158
-
159
- if (!reserveResult.ok) {
160
- return Err(new CommerceValidationError(reserveResult.reason));
161
- }
162
-
163
- await this.repo.createMovement(
164
- {
165
- entityId: input.entityId,
166
- warehouseId,
167
- type: "reservation",
168
- quantity: input.quantity,
169
- performedBy,
170
- referenceType: "order",
171
- referenceId: input.orderId,
172
- ...(input.variantId != null
173
- ? { variantId: input.variantId }
174
- : {}),
175
- },
176
- ctx,
177
- );
178
-
179
- return Ok(undefined);
180
- }
181
-
182
- // Fallback: no transaction provided. Wrap in a transaction so we can
183
- // use reserveWithLock (SELECT FOR UPDATE) for concurrency safety.
184
- const result = await this.deps.database.transaction(async (tx) => {
185
- const txCtx = createTxContext(tx, { actor: actor ?? null });
186
- const reserveResult = await this.repo.reserveWithLock(
187
- input.entityId,
188
- variantId,
189
- warehouseId,
190
- input.quantity,
191
- txCtx,
192
- );
193
-
194
- if (!reserveResult.ok) {
195
- return Err(new CommerceValidationError(reserveResult.reason));
196
- }
197
-
198
- await this.repo.createMovement(
199
- {
200
- entityId: input.entityId,
201
- warehouseId,
202
- type: "reservation",
203
- quantity: input.quantity,
204
- performedBy,
205
- referenceType: "order",
206
- referenceId: input.orderId,
207
- ...(input.variantId != null
208
- ? { variantId: input.variantId }
209
- : {}),
210
- },
211
- txCtx,
212
- );
213
-
214
- return Ok(undefined);
215
- });
216
-
217
- return result;
218
- }
219
-
220
- async release(
221
- input: InventoryReleaseInput,
222
- actor?: Actor | null,
223
- ctx?: TxContext,
224
- ): Promise<Result<void>> {
225
- const warehouseId = input.warehouseId ?? (await this.pickWarehouse(actor, ctx));
226
- const performedBy = input.performedBy ?? actor?.userId ?? "system";
227
- const variantId = input.variantId ?? null;
228
-
229
- const doRelease = async (txCtx: TxContext): Promise<Result<void>> => {
230
- const releaseResult = await this.repo.releaseWithLock(
231
- input.entityId,
232
- variantId,
233
- warehouseId,
234
- input.quantity,
235
- txCtx,
236
- );
237
-
238
- if (!releaseResult.ok) {
239
- return Err(new CommerceValidationError(releaseResult.reason));
240
- }
241
-
242
- await this.repo.createMovement(
243
- {
244
- entityId: input.entityId,
245
- warehouseId,
246
- type: "release",
247
- quantity: input.quantity,
248
- performedBy,
249
- referenceType: "order",
250
- referenceId: input.orderId,
251
- ...(input.variantId != null
252
- ? { variantId: input.variantId }
253
- : {}),
254
- },
255
- txCtx,
256
- );
257
-
258
- return Ok(undefined);
259
- };
260
-
261
- // If we already have a transaction, use it directly
262
- if (ctx?.tx) {
263
- return doRelease(ctx);
264
- }
265
-
266
- // Otherwise wrap in a new transaction for locking safety
267
- return this.deps.database.transaction(async (tx) => {
268
- return doRelease(createTxContext(tx, { actor: actor ?? null }));
269
- });
270
- }
271
-
272
- async adjust(
273
- input: InventoryAdjustInput,
274
- actor?: Actor | null,
275
- ctx?: TxContext,
276
- ): Promise<Result<InventoryLevel>> {
277
- try {
278
- assertPermission(actor ?? null, "inventory:adjust");
279
- } catch (error) {
280
- return Err(toCommerceError(error));
281
- }
282
-
283
- const warehouseId = input.warehouseId ?? (await this.pickWarehouse(actor, ctx));
284
-
285
- // Use upsertLevel to create or update
286
- const existingLevel = await this.repo.findLevelByKey(
287
- input.entityId,
288
- warehouseId,
289
- input.variantId,
290
- ctx,
291
- );
292
-
293
- let level: InventoryLevel;
294
- if (existingLevel) {
295
- const newQuantity = Math.max(
296
- 0,
297
- existingLevel.quantityOnHand + input.adjustment,
298
- );
299
- const updated = await this.repo.updateLevel(
300
- existingLevel.id,
301
- { quantityOnHand: newQuantity },
302
- ctx,
303
- );
304
- level = updated!;
305
- } else {
306
- level = await this.repo.createLevel(
307
- {
308
- entityId: input.entityId,
309
- warehouseId,
310
- quantityOnHand: Math.max(0, input.adjustment),
311
- quantityReserved: 0,
312
- quantityIncoming: 0,
313
- ...(input.variantId !== undefined
314
- ? { variantId: input.variantId }
315
- : {}),
316
- },
317
- ctx,
318
- );
319
- }
320
-
321
- await this.repo.createMovement(
322
- {
323
- entityId: input.entityId,
324
- warehouseId,
325
- type: "adjustment",
326
- quantity: input.adjustment,
327
- reason: input.reason,
328
- performedBy: input.performedBy ?? actor?.userId ?? "system",
329
- ...(input.variantId !== undefined
330
- ? { variantId: input.variantId }
331
- : {}),
332
- ...(input.referenceType !== undefined
333
- ? { referenceType: input.referenceType }
334
- : {}),
335
- ...(input.referenceId !== undefined
336
- ? { referenceId: input.referenceId }
337
- : {}),
338
- },
339
- ctx,
340
- );
341
-
342
- const context: HookContext = createHookContext({
343
- actor: actor ?? null,
344
- tx: ctx?.tx ?? null,
345
- logger: createLogger("inventory.adjust"),
346
- services: this.deps.services,
347
- context: { moduleName: "inventory" },
348
- });
349
-
350
- const afterHooks = this.deps.hooks.resolve("inventory.afterAdjust");
351
- await runAfterHooks(
352
- afterHooks as Parameters<typeof runAfterHooks>[0],
353
- null,
354
- level,
355
- "update",
356
- context,
357
- );
358
-
359
- return Ok(level);
360
- }
361
-
362
- /**
363
- * Set inventory to an absolute quantity (not a delta).
364
- *
365
- * Used by external store webhooks where the source system reports
366
- * the current stock level (e.g., Shopify sends `{ available: 6 }`).
367
- * Computes the delta internally so audit movements stay correct.
368
- */
369
- async setAbsolute(
370
- input: {
371
- entityId: string;
372
- quantity: number;
373
- warehouseId?: string;
374
- variantId?: string;
375
- reason?: string;
376
- },
377
- actor?: Actor | null,
378
- ctx?: TxContext,
379
- ): Promise<Result<InventoryLevel>> {
380
- const warehouseId = input.warehouseId ?? (await this.pickWarehouse(actor, ctx));
381
-
382
- const existingLevel = await this.repo.findLevelByKey(
383
- input.entityId,
384
- warehouseId,
385
- input.variantId,
386
- ctx,
387
- );
388
-
389
- const currentOnHand = existingLevel?.quantityOnHand ?? 0;
390
- const delta = input.quantity - currentOnHand;
391
-
392
- // Delegate to adjust() so hooks, movements, and permission checks are consistent
393
- return this.adjust(
394
- {
395
- entityId: input.entityId,
396
- warehouseId,
397
- adjustment: delta,
398
- reason: input.reason ?? "External store absolute inventory sync",
399
- ...(input.variantId !== undefined ? { variantId: input.variantId } : {}),
400
- },
401
- actor,
402
- ctx,
403
- );
404
- }
405
-
406
- /**
407
- * Deduct inventory on fulfillment (system-level, no permission check).
408
- *
409
- * Decrements quantityOnHand for fulfilled items. This is a system
410
- * operation triggered by order status → fulfilled, not a manual
411
- * stock adjustment. Creates a "fulfillment" type movement for audit.
412
- *
413
- * Net effect when paired with release():
414
- * Before: on_hand=100, reserved=5, available=95
415
- * After deduct+release: on_hand=95, reserved=0, available=95
416
- */
417
- async deductForFulfillment(
418
- input: {
419
- entityId: string;
420
- variantId?: string;
421
- warehouseId?: string;
422
- quantity: number;
423
- orderId: string;
424
- orgId?: string;
425
- },
426
- ctx?: TxContext,
427
- ): Promise<Result<void>> {
428
- const warehouseId = input.warehouseId ?? (await this.pickWarehouse(null, ctx));
429
- const variantId = input.variantId ?? null;
430
-
431
- const level = await this.repo.findLevelByKey(
432
- input.entityId,
433
- warehouseId,
434
- variantId != null ? variantId : undefined,
435
- ctx,
436
- );
437
-
438
- if (!level) {
439
- // No inventory record — nothing to deduct
440
- return Ok(undefined);
441
- }
442
-
443
- const newOnHand = Math.max(0, level.quantityOnHand - input.quantity);
444
- await this.repo.updateLevel(
445
- level.id,
446
- { quantityOnHand: newOnHand },
447
- ctx,
448
- );
449
-
450
- await this.repo.createMovement(
451
- {
452
- entityId: input.entityId,
453
- warehouseId,
454
- type: "fulfillment",
455
- quantity: -input.quantity,
456
- performedBy: "system",
457
- referenceType: "order",
458
- referenceId: input.orderId,
459
- ...(variantId != null ? { variantId } : {}),
460
- },
461
- ctx,
462
- );
463
-
464
- return Ok(undefined);
465
- }
466
-
467
- async setUnitCost(
468
- entityId: string,
469
- warehouseId: string,
470
- unitCost: number,
471
- variantId?: string,
472
- ctx?: TxContext,
473
- ): Promise<Result<InventoryLevel>> {
474
- const level = await this.repo.findLevelByKey(
475
- entityId,
476
- warehouseId,
477
- variantId,
478
- ctx,
479
- );
480
- if (!level) {
481
- return Err(
482
- new CommerceNotFoundError(
483
- `Inventory level not found for entity ${entityId} at warehouse ${warehouseId}.`,
484
- ),
485
- );
486
- }
487
- const updated = await this.repo.updateLevel(level.id, { unitCost }, ctx);
488
- return Ok(updated!);
489
- }
490
- }
@@ -1,17 +0,0 @@
1
- import type { Result } from "../../kernel/result.js";
2
-
3
- export interface StoredFile {
4
- key: string;
5
- url: string;
6
- contentType: string;
7
- size?: number;
8
- }
9
-
10
- export interface StorageAdapter {
11
- readonly providerId: string;
12
- upload(key: string, data: ArrayBuffer | ReadableStream, contentType: string): Promise<Result<StoredFile>>;
13
- getUrl(key: string): Promise<Result<string>>;
14
- getSignedUrl(key: string, expiresIn: number): Promise<Result<string>>;
15
- delete(key: string): Promise<Result<void>>;
16
- list(prefix: string): Promise<Result<StoredFile[]>>;
17
- }