@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,262 +0,0 @@
1
- import type { Step } from "../kernel/compensation/types.js";
2
- import type { CheckoutData } from "./checkout.js";
3
- import { Ok, Err } from "../kernel/result.js";
4
- import { CommerceValidationError } from "../kernel/errors.js";
5
-
6
- // Service type narrowing — these mirror the actual service interfaces
7
- // without creating circular imports.
8
-
9
- interface InventoryServiceLike {
10
- reserve(input: {
11
- entityId: string;
12
- variantId?: string;
13
- quantity: number;
14
- orderId: string;
15
- performedBy: string;
16
- }): Promise<{ ok: boolean; error?: { message: string } }>;
17
- release(input: {
18
- entityId: string;
19
- variantId?: string;
20
- quantity: number;
21
- orderId: string;
22
- performedBy: string;
23
- }): Promise<unknown>;
24
- }
25
-
26
- interface PaymentsServiceLike {
27
- capture(
28
- paymentIntentId: string,
29
- amount?: number,
30
- ): Promise<{ ok: boolean; error?: { message: string } }>;
31
- refund(
32
- paymentId: string,
33
- amount: number,
34
- reason?: string,
35
- ): Promise<unknown>;
36
- }
37
-
38
- interface OrdersServiceLike {
39
- updateStatus?(
40
- orderId: string,
41
- status: string,
42
- reason?: string,
43
- ): Promise<unknown>;
44
- }
45
-
46
- /**
47
- * Reservation record produced by the reserve-inventory step.
48
- * Passed to compensate() so it knows exactly what to undo.
49
- */
50
- export interface InventoryReservation {
51
- entityId: string;
52
- variantId: string | undefined;
53
- quantity: number;
54
- orderId: string;
55
- }
56
-
57
- /**
58
- * Step 1: Reserve inventory.
59
- *
60
- * Output: the list of reservations created.
61
- * Compensate: release each reservation.
62
- *
63
- * Inventory reservation runs BEFORE payment capture. This is deliberate:
64
- * if stock is unavailable, we should find out before charging the customer.
65
- * The compensation for this step releases the reserved quantities.
66
- */
67
- export const reserveInventoryStep: Step<
68
- CheckoutData,
69
- InventoryReservation[]
70
- > = {
71
- id: "reserve-inventory",
72
-
73
- async run(data, ctx) {
74
- const inventory = ctx.hook.services.inventory as InventoryServiceLike;
75
- const reservations: InventoryReservation[] = [];
76
- const performedBy = ctx.hook.actor?.userId ?? "system";
77
-
78
- for (const item of data.lineItems) {
79
- const result = await inventory.reserve({
80
- entityId: item.entityId,
81
- ...(item.variantId != null ? { variantId: item.variantId } : {}),
82
- quantity: item.quantity,
83
- orderId: data.checkoutId,
84
- performedBy,
85
- });
86
-
87
- if (!result.ok) {
88
- return Err(
89
- new CommerceValidationError(
90
- `Inventory reservation failed for ${item.title ?? item.entityId}: ${result.error?.message ?? "unknown"}`,
91
- ),
92
- );
93
- }
94
-
95
- reservations.push({
96
- entityId: item.entityId,
97
- variantId: item.variantId,
98
- quantity: item.quantity,
99
- orderId: data.checkoutId,
100
- });
101
- }
102
-
103
- return Ok(reservations);
104
- },
105
-
106
- async compensate(reservations, ctx) {
107
- const inventory = ctx.hook.services.inventory as InventoryServiceLike;
108
- const performedBy = ctx.hook.actor?.userId ?? "system";
109
-
110
- for (const r of reservations) {
111
- await inventory.release({
112
- entityId: r.entityId,
113
- ...(r.variantId != null ? { variantId: r.variantId } : {}),
114
- quantity: r.quantity,
115
- orderId: r.orderId,
116
- performedBy,
117
- });
118
- }
119
- },
120
- };
121
-
122
- /**
123
- * Step 2: Capture payment.
124
- *
125
- * Output: the captured payment intent ID and amount.
126
- * Compensate: issue a full refund via the payments service.
127
- *
128
- * Runs AFTER inventory reservation. If capture fails, inventory reservations
129
- * are released by the compensation chain. If capture succeeds but a later step
130
- * fails, a refund is issued.
131
- */
132
- export const capturePaymentStep: Step<
133
- CheckoutData,
134
- { paymentIntentId: string; amount: number }
135
- > = {
136
- id: "capture-payment",
137
-
138
- async run(data, ctx) {
139
- if (!data.paymentIntentId) {
140
- return Err(
141
- new CommerceValidationError(
142
- "No authorized payment intent to capture.",
143
- ),
144
- );
145
- }
146
-
147
- const payments = ctx.hook.services.payments as PaymentsServiceLike;
148
- const result = await payments.capture(data.paymentIntentId);
149
-
150
- if (!result.ok) {
151
- return Err(
152
- new CommerceValidationError(
153
- `Payment capture failed: ${result.error?.message ?? "unknown"}`,
154
- ),
155
- );
156
- }
157
-
158
- return Ok({ paymentIntentId: data.paymentIntentId, amount: data.total });
159
- },
160
-
161
- async compensate({ paymentIntentId, amount }, ctx) {
162
- const payments = ctx.hook.services.payments as PaymentsServiceLike;
163
- await payments.refund(
164
- paymentIntentId,
165
- amount,
166
- "Checkout compensation: downstream step failed after payment capture",
167
- );
168
- },
169
- };
170
-
171
- /**
172
- * Step 3: Initiate fulfillment.
173
- *
174
- * Output: the order ID (for logging).
175
- * No compensate: fulfillment initiation is best-effort and idempotent.
176
- * A failed fulfillment should be retried through the job queue, not
177
- * compensated by rolling back the entire checkout.
178
- */
179
- export const initiateFulfillmentStep: Step<
180
- CheckoutData,
181
- { orderId: string }
182
- > = {
183
- id: "initiate-fulfillment",
184
-
185
- async run(data, ctx) {
186
- const fulfillment = ctx.hook.services.fulfillment as {
187
- fulfillOrder(orderId: string, actor?: unknown): Promise<unknown>;
188
- };
189
-
190
- try {
191
- await fulfillment.fulfillOrder(data.checkoutId, ctx.hook.actor);
192
- } catch (error) {
193
- // Fulfillment initiation failure should not fail the checkout.
194
- // The order is paid and inventory is reserved — fulfillment can be retried.
195
- ctx.hook.logger.warn(
196
- `Fulfillment initiation failed for order ${data.checkoutId}. Will need manual retry.`,
197
- { error },
198
- );
199
- }
200
-
201
- return Ok({ orderId: data.checkoutId });
202
- },
203
-
204
- // No compensate — fulfillment is best-effort at this stage
205
- };
206
-
207
- /**
208
- * Step 4: Send confirmation email.
209
- *
210
- * Output: void (best-effort).
211
- * No compensate: you cannot unsend an email.
212
- */
213
- export const sendConfirmationStep: Step<
214
- CheckoutData,
215
- { sent: boolean }
216
- > = {
217
- id: "send-confirmation",
218
-
219
- async run(data, ctx) {
220
- const customers = ctx.hook.services.customers as {
221
- getByUserId(
222
- userId: string,
223
- actor?: unknown,
224
- ): Promise<{ ok: boolean; value?: { email?: string } }>;
225
- };
226
- const email = ctx.hook.services.email as
227
- | {
228
- send(input: {
229
- template: string;
230
- to: string;
231
- data?: Record<string, unknown>;
232
- }): Promise<void>;
233
- }
234
- | undefined;
235
-
236
- if (!data.customerId || !email?.send) {
237
- return Ok({ sent: false });
238
- }
239
-
240
- try {
241
- const customer = await customers.getByUserId(data.customerId, ctx.hook.actor);
242
- if (customer.ok && customer.value?.email) {
243
- await email.send({
244
- template: "order-confirmation",
245
- to: customer.value.email,
246
- data: { orderId: data.checkoutId, total: data.total, currency: data.currency },
247
- });
248
- return Ok({ sent: true });
249
- }
250
- } catch (error) {
251
- // Email failure should not fail checkout
252
- ctx.hook.logger.warn(
253
- `Confirmation email failed for order ${data.checkoutId}.`,
254
- { error },
255
- );
256
- }
257
-
258
- return Ok({ sent: false });
259
- },
260
-
261
- // No compensate — cannot unsend an email
262
- };