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