@unifiedcommerce/core 0.2.0 → 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 (186) hide show
  1. package/package.json +2 -1
  2. package/src/adapters/console-email.ts +43 -0
  3. package/src/auth/access.ts +187 -0
  4. package/src/auth/auth-schema.ts +139 -0
  5. package/src/auth/middleware.ts +161 -0
  6. package/src/auth/org.ts +41 -0
  7. package/src/auth/permissions.ts +28 -0
  8. package/src/auth/setup.ts +171 -0
  9. package/src/auth/system-actor.ts +19 -0
  10. package/src/auth/types.ts +10 -0
  11. package/src/config/defaults.ts +82 -0
  12. package/src/config/define-config.ts +53 -0
  13. package/src/config/types.ts +301 -0
  14. package/src/generated/plugin-capabilities.d.ts +20 -0
  15. package/src/generated/plugin-manifest.ts +23 -0
  16. package/src/generated/plugin-repositories.d.ts +20 -0
  17. package/src/hooks/checkout-completion.ts +262 -0
  18. package/src/hooks/checkout.ts +677 -0
  19. package/src/hooks/order-emails.ts +62 -0
  20. package/src/index.ts +215 -0
  21. package/src/interfaces/mcp/agent-prompt.ts +174 -0
  22. package/src/interfaces/mcp/context-enrichment.ts +177 -0
  23. package/src/interfaces/mcp/server.ts +47 -0
  24. package/src/interfaces/mcp/tool-builder.ts +261 -0
  25. package/src/interfaces/mcp/tools/analytics.ts +76 -0
  26. package/src/interfaces/mcp/tools/cart.ts +57 -0
  27. package/src/interfaces/mcp/tools/catalog.ts +299 -0
  28. package/src/interfaces/mcp/tools/index.ts +22 -0
  29. package/src/interfaces/mcp/tools/inventory.ts +161 -0
  30. package/src/interfaces/mcp/tools/orders.ts +104 -0
  31. package/src/interfaces/mcp/tools/pricing.ts +94 -0
  32. package/src/interfaces/mcp/tools/promotions.ts +106 -0
  33. package/src/interfaces/mcp/tools/registry.ts +101 -0
  34. package/src/interfaces/mcp/tools/search.ts +42 -0
  35. package/src/interfaces/mcp/tools/webhooks.ts +48 -0
  36. package/src/interfaces/mcp/transport.ts +128 -0
  37. package/src/interfaces/rest/customer-portal.ts +299 -0
  38. package/src/interfaces/rest/index.ts +74 -0
  39. package/src/interfaces/rest/router.ts +333 -0
  40. package/src/interfaces/rest/routes/admin-jobs.ts +58 -0
  41. package/src/interfaces/rest/routes/audit.ts +50 -0
  42. package/src/interfaces/rest/routes/carts.ts +89 -0
  43. package/src/interfaces/rest/routes/catalog.ts +493 -0
  44. package/src/interfaces/rest/routes/checkout.ts +284 -0
  45. package/src/interfaces/rest/routes/inventory.ts +70 -0
  46. package/src/interfaces/rest/routes/media.ts +86 -0
  47. package/src/interfaces/rest/routes/orders.ts +78 -0
  48. package/src/interfaces/rest/routes/payments.ts +60 -0
  49. package/src/interfaces/rest/routes/pricing.ts +57 -0
  50. package/src/interfaces/rest/routes/promotions.ts +93 -0
  51. package/src/interfaces/rest/routes/search.ts +71 -0
  52. package/src/interfaces/rest/routes/webhooks.ts +46 -0
  53. package/src/interfaces/rest/schemas/admin-jobs.ts +40 -0
  54. package/src/interfaces/rest/schemas/audit.ts +46 -0
  55. package/src/interfaces/rest/schemas/carts.ts +125 -0
  56. package/src/interfaces/rest/schemas/catalog.ts +450 -0
  57. package/src/interfaces/rest/schemas/checkout.ts +66 -0
  58. package/src/interfaces/rest/schemas/customer-portal.ts +195 -0
  59. package/src/interfaces/rest/schemas/inventory.ts +138 -0
  60. package/src/interfaces/rest/schemas/media.ts +75 -0
  61. package/src/interfaces/rest/schemas/orders.ts +104 -0
  62. package/src/interfaces/rest/schemas/pricing.ts +80 -0
  63. package/src/interfaces/rest/schemas/promotions.ts +110 -0
  64. package/src/interfaces/rest/schemas/responses.ts +85 -0
  65. package/src/interfaces/rest/schemas/search.ts +58 -0
  66. package/src/interfaces/rest/schemas/shared.ts +62 -0
  67. package/src/interfaces/rest/schemas/webhooks.ts +68 -0
  68. package/src/interfaces/rest/utils.ts +104 -0
  69. package/src/interfaces/rest/webhook-router.ts +50 -0
  70. package/src/kernel/compensation/executor.ts +61 -0
  71. package/src/kernel/compensation/types.ts +26 -0
  72. package/src/kernel/database/adapter.ts +21 -0
  73. package/src/kernel/database/drizzle-db.ts +56 -0
  74. package/src/kernel/database/migrate.ts +76 -0
  75. package/src/kernel/database/plugin-types.ts +34 -0
  76. package/src/kernel/database/schema.ts +49 -0
  77. package/src/kernel/database/scoped-db.ts +68 -0
  78. package/src/kernel/database/tx-context.ts +46 -0
  79. package/src/kernel/error-mapper.ts +15 -0
  80. package/src/kernel/errors.ts +89 -0
  81. package/src/kernel/factory/repository-factory.ts +244 -0
  82. package/src/kernel/hooks/create-context.ts +43 -0
  83. package/src/kernel/hooks/executor.ts +88 -0
  84. package/src/kernel/hooks/registry.ts +74 -0
  85. package/src/kernel/hooks/types.ts +52 -0
  86. package/src/kernel/http-error.ts +44 -0
  87. package/src/kernel/jobs/adapter.ts +36 -0
  88. package/src/kernel/jobs/drizzle-adapter.ts +58 -0
  89. package/src/kernel/jobs/runner.ts +153 -0
  90. package/src/kernel/jobs/schema.ts +46 -0
  91. package/src/kernel/jobs/types.ts +30 -0
  92. package/src/kernel/local-api.ts +187 -0
  93. package/src/kernel/plugin/manifest.ts +271 -0
  94. package/src/kernel/query/executor.ts +184 -0
  95. package/src/kernel/query/registry.ts +46 -0
  96. package/src/kernel/result.ts +33 -0
  97. package/src/kernel/schema/extra-columns.ts +37 -0
  98. package/src/kernel/service-registry.ts +76 -0
  99. package/src/kernel/service-timing.ts +89 -0
  100. package/src/kernel/state-machine/machine.ts +101 -0
  101. package/src/modules/analytics/drizzle-adapter.ts +426 -0
  102. package/src/modules/analytics/hooks.ts +11 -0
  103. package/src/modules/analytics/models.ts +125 -0
  104. package/src/modules/analytics/repository/index.ts +6 -0
  105. package/src/modules/analytics/service.ts +245 -0
  106. package/src/modules/analytics/types.ts +180 -0
  107. package/src/modules/audit/hooks.ts +78 -0
  108. package/src/modules/audit/schema.ts +33 -0
  109. package/src/modules/audit/service.ts +151 -0
  110. package/src/modules/cart/access.ts +27 -0
  111. package/src/modules/cart/matcher.ts +26 -0
  112. package/src/modules/cart/repository/index.ts +234 -0
  113. package/src/modules/cart/schema.ts +42 -0
  114. package/src/modules/cart/schemas.ts +38 -0
  115. package/src/modules/cart/service.ts +541 -0
  116. package/src/modules/catalog/repository/index.ts +772 -0
  117. package/src/modules/catalog/schema.ts +203 -0
  118. package/src/modules/catalog/schemas.ts +104 -0
  119. package/src/modules/catalog/service.ts +1544 -0
  120. package/src/modules/customers/repository/index.ts +327 -0
  121. package/src/modules/customers/schema.ts +64 -0
  122. package/src/modules/customers/service.ts +171 -0
  123. package/src/modules/fulfillment/repository/index.ts +426 -0
  124. package/src/modules/fulfillment/schema.ts +101 -0
  125. package/src/modules/fulfillment/service.ts +555 -0
  126. package/src/modules/fulfillment/types.ts +59 -0
  127. package/src/modules/inventory/repository/index.ts +509 -0
  128. package/src/modules/inventory/schema.ts +94 -0
  129. package/src/modules/inventory/schemas.ts +38 -0
  130. package/src/modules/inventory/service.ts +490 -0
  131. package/src/modules/media/adapter.ts +17 -0
  132. package/src/modules/media/repository/index.ts +274 -0
  133. package/src/modules/media/schema.ts +41 -0
  134. package/src/modules/media/service.ts +151 -0
  135. package/src/modules/orders/repository/index.ts +287 -0
  136. package/src/modules/orders/schema.ts +66 -0
  137. package/src/modules/orders/service.ts +619 -0
  138. package/src/modules/orders/stale-order-cleanup.ts +76 -0
  139. package/src/modules/organization/service.ts +191 -0
  140. package/src/modules/payments/adapter.ts +47 -0
  141. package/src/modules/payments/repository/index.ts +6 -0
  142. package/src/modules/payments/service.ts +107 -0
  143. package/src/modules/pricing/repository/index.ts +291 -0
  144. package/src/modules/pricing/schema.ts +71 -0
  145. package/src/modules/pricing/schemas.ts +38 -0
  146. package/src/modules/pricing/service.ts +494 -0
  147. package/src/modules/promotions/repository/index.ts +325 -0
  148. package/src/modules/promotions/schema.ts +62 -0
  149. package/src/modules/promotions/schemas.ts +38 -0
  150. package/src/modules/promotions/service.ts +598 -0
  151. package/src/modules/search/adapter.ts +57 -0
  152. package/src/modules/search/hooks.ts +12 -0
  153. package/src/modules/search/repository/index.ts +6 -0
  154. package/src/modules/search/service.ts +315 -0
  155. package/src/modules/shipping/calculator.ts +188 -0
  156. package/src/modules/shipping/repository/index.ts +6 -0
  157. package/src/modules/shipping/service.ts +51 -0
  158. package/src/modules/tax/adapter.ts +60 -0
  159. package/src/modules/tax/repository/index.ts +6 -0
  160. package/src/modules/tax/service.ts +53 -0
  161. package/src/modules/webhooks/hook.ts +34 -0
  162. package/src/modules/webhooks/repository/index.ts +278 -0
  163. package/src/modules/webhooks/schema.ts +56 -0
  164. package/src/modules/webhooks/service.ts +117 -0
  165. package/src/modules/webhooks/signing.ts +6 -0
  166. package/src/modules/webhooks/ssrf-guard.ts +71 -0
  167. package/src/modules/webhooks/tasks.ts +52 -0
  168. package/src/modules/webhooks/worker.ts +134 -0
  169. package/src/runtime/commerce.ts +145 -0
  170. package/src/runtime/kernel.ts +426 -0
  171. package/src/runtime/logger.ts +36 -0
  172. package/src/runtime/server.ts +355 -0
  173. package/src/runtime/shutdown.ts +43 -0
  174. package/src/test-utils/create-pglite-adapter.ts +129 -0
  175. package/src/test-utils/create-plugin-test-app.ts +128 -0
  176. package/src/test-utils/create-repository-test-harness.ts +16 -0
  177. package/src/test-utils/create-test-config.ts +190 -0
  178. package/src/test-utils/create-test-kernel.ts +7 -0
  179. package/src/test-utils/create-test-plugin-context.ts +75 -0
  180. package/src/test-utils/rest-api-test-utils.ts +265 -0
  181. package/src/test-utils/test-actors.ts +62 -0
  182. package/src/test-utils/typed-hooks.ts +54 -0
  183. package/src/types/commerce-types.ts +34 -0
  184. package/src/utils/id.ts +3 -0
  185. package/src/utils/logger.ts +18 -0
  186. package/src/utils/pagination.ts +22 -0
@@ -0,0 +1,20 @@
1
+ /* eslint-disable */
2
+ // AUTO-GENERATED by scripts/generate-plugin-types.mjs
3
+
4
+ export interface PluginRepositoryRegistryShape {
5
+ "appointments": Record<string, unknown>;
6
+ "cubejs": Record<string, unknown>;
7
+ "gift-cards": Record<string, unknown>;
8
+ "loyalty": Record<string, unknown>;
9
+ "marketplace": Record<string, unknown>;
10
+ "notifications": Record<string, unknown>;
11
+ "pos": Record<string, unknown>;
12
+ "pos-restaurant": Record<string, unknown>;
13
+ "procurement": Record<string, unknown>;
14
+ "production": Record<string, unknown>;
15
+ "reviews": Record<string, unknown>;
16
+ "scheduled-orders": Record<string, unknown>;
17
+ "uom": Record<string, unknown>;
18
+ "warehouse": Record<string, unknown>;
19
+ "wishlist": Record<string, unknown>;
20
+ }
@@ -0,0 +1,262 @@
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
+ };