@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,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
- }