@unifiedcommerce/core 0.1.0 → 0.2.0

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