@unifiedcommerce/core 0.2.0 → 0.2.2

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,598 @@
1
+ import {
2
+ CommerceNotFoundError,
3
+ CommerceValidationError,
4
+ } from "../../kernel/errors.js";
5
+ import { Err, Ok, type Result } from "../../kernel/result.js";
6
+ import type { TxContext } from "../../kernel/database/tx-context.js";
7
+ import type {
8
+ PromotionsRepository,
9
+ Promotion,
10
+ PromotionUsage,
11
+ } from "./repository/index.js";
12
+ import type { CatalogRepository } from "../catalog/repository/index.js";
13
+ import type { OrdersRepository } from "../orders/repository/index.js";
14
+ import { DEFAULT_ORG_ID, resolveOrgId } from "../../auth/org.js";
15
+ import type { Actor } from "../../auth/types.js";
16
+
17
+ // Re-export PromotionType for external use
18
+ export type PromotionType =
19
+ | "percentage_off_order"
20
+ | "fixed_off_order"
21
+ | "percentage_off_item"
22
+ | "fixed_off_item"
23
+ | "free_shipping"
24
+ | "buy_x_get_y";
25
+
26
+ interface PromotionServiceDeps {
27
+ repository: PromotionsRepository;
28
+ catalogRepository: CatalogRepository;
29
+ ordersRepository: OrdersRepository;
30
+ }
31
+
32
+ export interface PromotionLineItem {
33
+ entityId: string;
34
+ entityType: string;
35
+ quantity: number;
36
+ unitPrice: number;
37
+ totalPrice: number;
38
+ }
39
+
40
+ export interface PromotionEvaluationContext {
41
+ orgId?: string;
42
+ cartId?: string;
43
+ customerId?: string;
44
+ customerGroupIds?: string[];
45
+ currency: string;
46
+ subtotal: number;
47
+ lineItems: PromotionLineItem[];
48
+ promotionCodes?: string[];
49
+ timestamp?: Date;
50
+ }
51
+
52
+ export interface PromotionConditions {
53
+ minimumOrderValue?: number;
54
+ minimumQuantity?: number;
55
+ entityTypes?: string[];
56
+ categories?: string[];
57
+ customerGroups?: string[];
58
+ firstOrderOnly?: boolean;
59
+ }
60
+
61
+ export type { CreatePromotionInput } from "./schemas.js";
62
+ import type { CreatePromotionInput } from "./schemas.js";
63
+
64
+ export interface AppliedPromotion {
65
+ promotionId: string;
66
+ code?: string;
67
+ type: PromotionType;
68
+ discountAmount: number;
69
+ freeShipping: boolean;
70
+ description: string;
71
+ }
72
+
73
+ export interface PromotionApplicationResult {
74
+ totalDiscount: number;
75
+ freeShipping: boolean;
76
+ applied: AppliedPromotion[];
77
+ rejectedCodes: Array<{ code: string; reason: string }>;
78
+ }
79
+
80
+ function matchesWindow(promotion: Promotion, timestamp: Date): boolean {
81
+ if (!promotion.isActive) return false;
82
+ if (promotion.validFrom && timestamp < promotion.validFrom) return false;
83
+ if (promotion.validUntil && timestamp > promotion.validUntil) return false;
84
+ return true;
85
+ }
86
+
87
+ function toConditions(
88
+ raw: Record<string, unknown> | null | undefined,
89
+ ): PromotionConditions {
90
+ if (!raw) return {};
91
+ const conditions: PromotionConditions = {};
92
+ if (typeof raw.minimumOrderValue === "number") {
93
+ conditions.minimumOrderValue = raw.minimumOrderValue;
94
+ }
95
+ if (typeof raw.minimumQuantity === "number") {
96
+ conditions.minimumQuantity = raw.minimumQuantity;
97
+ }
98
+ if (Array.isArray(raw.entityTypes)) {
99
+ conditions.entityTypes = raw.entityTypes.filter(
100
+ (item): item is string => typeof item === "string",
101
+ );
102
+ }
103
+ if (Array.isArray(raw.categories)) {
104
+ conditions.categories = raw.categories.filter(
105
+ (item): item is string => typeof item === "string",
106
+ );
107
+ }
108
+ if (Array.isArray(raw.customerGroups)) {
109
+ conditions.customerGroups = raw.customerGroups.filter(
110
+ (item): item is string => typeof item === "string",
111
+ );
112
+ }
113
+ if (typeof raw.firstOrderOnly === "boolean") {
114
+ conditions.firstOrderOnly = raw.firstOrderOnly;
115
+ }
116
+ return conditions;
117
+ }
118
+
119
+ function roundMoney(amount: number): number {
120
+ return Math.max(0, Math.round(amount));
121
+ }
122
+
123
+ function totalQuantity(items: PromotionLineItem[]): number {
124
+ return items.reduce((sum, item) => sum + item.quantity, 0);
125
+ }
126
+
127
+ function sortByPriority(promotions: Promotion[]): Promotion[] {
128
+ return [...promotions].sort((a, b) => a.priority - b.priority);
129
+ }
130
+
131
+ export class PromotionService {
132
+ private readonly repo: PromotionsRepository;
133
+ private readonly catalogRepo: CatalogRepository;
134
+ private readonly ordersRepo: OrdersRepository;
135
+
136
+ constructor(private deps: PromotionServiceDeps) {
137
+ this.repo = deps.repository;
138
+ this.catalogRepo = deps.catalogRepository;
139
+ this.ordersRepo = deps.ordersRepository;
140
+ }
141
+
142
+ async create(
143
+ input: CreatePromotionInput,
144
+ actor?: Actor | null,
145
+ ctx?: TxContext,
146
+ ): Promise<Result<Promotion>> {
147
+ if (!input.name) {
148
+ return Err(new CommerceValidationError("Promotion name is required."));
149
+ }
150
+ if (input.value < 0) {
151
+ return Err(
152
+ new CommerceValidationError("Promotion value cannot be negative."),
153
+ );
154
+ }
155
+
156
+ // Validate promotion type
157
+ const validTypes: PromotionType[] = [
158
+ "percentage_off_order",
159
+ "fixed_off_order",
160
+ "percentage_off_item",
161
+ "fixed_off_item",
162
+ "free_shipping",
163
+ "buy_x_get_y",
164
+ ];
165
+ if (!validTypes.includes(input.type)) {
166
+ return Err(
167
+ new CommerceValidationError(
168
+ `Invalid promotion type "${input.type}". Must be one of: ${validTypes.join(", ")}`,
169
+ ),
170
+ );
171
+ }
172
+
173
+ const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
174
+
175
+ if (input.code) {
176
+ const normalized = input.code.trim().toUpperCase();
177
+ const existing = await this.repo.findByCode(orgId, normalized, ctx);
178
+ if (existing) {
179
+ return Err(
180
+ new CommerceValidationError(
181
+ `Promotion code ${normalized} already exists.`,
182
+ ),
183
+ );
184
+ }
185
+ }
186
+
187
+ const promotion = await this.repo.create(
188
+ {
189
+ organizationId: orgId,
190
+ name: input.name,
191
+ type: input.type,
192
+ value: roundMoney(input.value),
193
+ isAutomatic: input.isAutomatic ?? false,
194
+ isActive: input.isActive ?? true,
195
+ priority: input.priority ?? 100,
196
+ conditions: (input.conditions ?? {}) as Record<string, unknown>,
197
+ metadata: input.metadata ?? {},
198
+ code: input.code ? input.code.trim().toUpperCase() : null,
199
+ buyQuantity: input.buyQuantity ?? null,
200
+ getQuantity: input.getQuantity ?? null,
201
+ usageLimitTotal: input.usageLimitTotal ?? null,
202
+ usageLimitPerCustomer: input.usageLimitPerCustomer ?? null,
203
+ validFrom: input.validFrom ?? null,
204
+ validUntil: input.validUntil ?? null,
205
+ },
206
+ ctx,
207
+ );
208
+
209
+ return Ok(promotion);
210
+ }
211
+
212
+ async deactivate(orgId: string, id: string, ctx?: TxContext): Promise<Result<Promotion>> {
213
+ const promotion = await this.repo.findById(orgId, id, ctx);
214
+ if (!promotion) {
215
+ return Err(new CommerceNotFoundError("Promotion not found."));
216
+ }
217
+
218
+ const updated = await this.repo.update(id, { isActive: false }, ctx);
219
+ if (!updated) {
220
+ return Err(new CommerceNotFoundError("Promotion not found."));
221
+ }
222
+ return Ok(updated);
223
+ }
224
+
225
+ async listActive(
226
+ actor?: Actor | null,
227
+ timestamp = new Date(),
228
+ ctx?: TxContext,
229
+ ): Promise<Result<Promotion[]>> {
230
+ const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
231
+ const active = await this.repo.findActive(orgId, ctx);
232
+ // Further filter by timestamp in case repo returns slightly stale results
233
+ const filtered = active.filter((p) => matchesWindow(p, timestamp));
234
+ return Ok(sortByPriority(filtered));
235
+ }
236
+
237
+ async validate(
238
+ code: string,
239
+ context: PromotionEvaluationContext,
240
+ ctx?: TxContext,
241
+ ): Promise<Result<Promotion>> {
242
+ const normalized = code.trim().toUpperCase();
243
+ const evalOrgId = context.orgId ?? DEFAULT_ORG_ID;
244
+ const promotion = await this.repo.findByCode(evalOrgId, normalized, ctx);
245
+ if (!promotion) {
246
+ return Err(
247
+ new CommerceNotFoundError(`Promotion code ${normalized} not found.`),
248
+ );
249
+ }
250
+
251
+ const validation = await this.validatePromotionForContext(
252
+ promotion,
253
+ context,
254
+ context.timestamp ?? new Date(),
255
+ ctx,
256
+ );
257
+ if (validation !== undefined) {
258
+ return Err(new CommerceValidationError(validation));
259
+ }
260
+ return Ok(promotion);
261
+ }
262
+
263
+ async apply(
264
+ code: string,
265
+ context: PromotionEvaluationContext,
266
+ ctx?: TxContext,
267
+ ): Promise<Result<PromotionApplicationResult>> {
268
+ const validation = await this.validate(code, context, ctx);
269
+ if (!validation.ok) return validation;
270
+
271
+ const evaluated = await this.evaluatePromotion(
272
+ validation.value,
273
+ context,
274
+ ctx,
275
+ );
276
+ return Ok({
277
+ totalDiscount: evaluated.discountAmount,
278
+ freeShipping: evaluated.freeShipping,
279
+ applied: [evaluated],
280
+ rejectedCodes: [],
281
+ });
282
+ }
283
+
284
+ async applyPromotions(
285
+ context: PromotionEvaluationContext,
286
+ ctx?: TxContext,
287
+ ): Promise<Result<PromotionApplicationResult>> {
288
+ const timestamp = context.timestamp ?? new Date();
289
+ const applyOrgId = context.orgId ?? DEFAULT_ORG_ID;
290
+ const result: PromotionApplicationResult = {
291
+ totalDiscount: 0,
292
+ freeShipping: false,
293
+ applied: [],
294
+ rejectedCodes: [],
295
+ };
296
+
297
+ const selectedPromotions: Promotion[] = [];
298
+
299
+ // Process explicit promotion codes
300
+ const codeSet = new Set(
301
+ (context.promotionCodes ?? []).map((code) => code.trim().toUpperCase()),
302
+ );
303
+ if (codeSet.size > 0) {
304
+ for (const code of codeSet) {
305
+ const promotion = await this.repo.findByCode(applyOrgId, code, ctx);
306
+ if (!promotion) {
307
+ result.rejectedCodes.push({ code, reason: "Code not found." });
308
+ continue;
309
+ }
310
+ const reason = await this.validatePromotionForContext(
311
+ promotion,
312
+ context,
313
+ timestamp,
314
+ ctx,
315
+ );
316
+ if (reason !== undefined) {
317
+ result.rejectedCodes.push({ code, reason });
318
+ continue;
319
+ }
320
+ selectedPromotions.push(promotion);
321
+ }
322
+ }
323
+
324
+ // Process automatic promotions
325
+ const automaticPromotions = await this.repo.findAutomatic(applyOrgId, ctx);
326
+ for (const promotion of automaticPromotions) {
327
+ const reason = await this.validatePromotionForContext(
328
+ promotion,
329
+ context,
330
+ timestamp,
331
+ ctx,
332
+ );
333
+ if (reason !== undefined) continue;
334
+ selectedPromotions.push(promotion);
335
+ }
336
+
337
+ for (const promotion of sortByPriority(selectedPromotions)) {
338
+ const evaluated = await this.evaluatePromotion(promotion, context, ctx);
339
+ if (evaluated.discountAmount <= 0 && !evaluated.freeShipping) continue;
340
+ result.totalDiscount += evaluated.discountAmount;
341
+ result.freeShipping = result.freeShipping || evaluated.freeShipping;
342
+ result.applied.push(evaluated);
343
+ }
344
+
345
+ result.totalDiscount = Math.min(
346
+ roundMoney(result.totalDiscount),
347
+ roundMoney(context.subtotal),
348
+ );
349
+ return Ok(result);
350
+ }
351
+
352
+ async recordUsage(
353
+ input: {
354
+ promotions: AppliedPromotion[];
355
+ customerId?: string;
356
+ orderId?: string;
357
+ },
358
+ ctx?: TxContext,
359
+ ): Promise<Result<PromotionUsage[]>> {
360
+ const usages: PromotionUsage[] = [];
361
+ for (const applied of input.promotions) {
362
+ const usage = await this.repo.createUsage(
363
+ {
364
+ promotionId: applied.promotionId,
365
+ customerId: input.customerId ?? null,
366
+ orderId: input.orderId ?? null,
367
+ },
368
+ ctx,
369
+ );
370
+ usages.push(usage);
371
+ }
372
+ return Ok(usages);
373
+ }
374
+
375
+ private async validatePromotionForContext(
376
+ promotion: Promotion,
377
+ context: PromotionEvaluationContext,
378
+ timestamp: Date,
379
+ ctx?: TxContext,
380
+ ): Promise<string | undefined> {
381
+ if (!matchesWindow(promotion, timestamp)) {
382
+ return "Promotion is inactive or outside validity window.";
383
+ }
384
+
385
+ const conditions = toConditions(
386
+ promotion.conditions as Record<string, unknown> | null,
387
+ );
388
+ if (
389
+ conditions.minimumOrderValue !== undefined &&
390
+ roundMoney(context.subtotal) < roundMoney(conditions.minimumOrderValue)
391
+ ) {
392
+ return `Requires minimum order value of ${conditions.minimumOrderValue}.`;
393
+ }
394
+
395
+ if (
396
+ conditions.minimumQuantity !== undefined &&
397
+ totalQuantity(context.lineItems) < conditions.minimumQuantity
398
+ ) {
399
+ return `Requires minimum quantity of ${conditions.minimumQuantity}.`;
400
+ }
401
+
402
+ if (conditions.customerGroups && conditions.customerGroups.length > 0) {
403
+ const set = new Set(context.customerGroupIds ?? []);
404
+ const matchesGroup = conditions.customerGroups.some((group) =>
405
+ set.has(group),
406
+ );
407
+ if (!matchesGroup) {
408
+ return "Customer group not eligible for this promotion.";
409
+ }
410
+ }
411
+
412
+ if (conditions.entityTypes && conditions.entityTypes.length > 0) {
413
+ const hasType = context.lineItems.some((lineItem) =>
414
+ conditions.entityTypes!.includes(lineItem.entityType),
415
+ );
416
+ if (!hasType) return "Cart does not include required entity type.";
417
+ }
418
+
419
+ if (conditions.categories && conditions.categories.length > 0) {
420
+ const categoryMatches = await this.checkCategoryMatch(
421
+ context.lineItems,
422
+ conditions.categories,
423
+ ctx,
424
+ );
425
+ if (!categoryMatches) return "Cart does not include required category.";
426
+ }
427
+
428
+ if (conditions.firstOrderOnly) {
429
+ if (!context.customerId) {
430
+ return "First-order promotion requires authenticated customer.";
431
+ }
432
+ const orders = await this.ordersRepo.findByCustomerId(
433
+ context.orgId ?? DEFAULT_ORG_ID,
434
+ context.customerId,
435
+ ctx,
436
+ );
437
+ if (orders.length > 0) {
438
+ return "Promotion is valid for first order only.";
439
+ }
440
+ }
441
+
442
+ // Check usage limits
443
+ const usageCount = await this.repo.countUsages(promotion.id, ctx);
444
+ if (
445
+ promotion.usageLimitTotal !== null &&
446
+ usageCount >= promotion.usageLimitTotal
447
+ ) {
448
+ return "Promotion usage limit reached.";
449
+ }
450
+
451
+ if (promotion.usageLimitPerCustomer !== null && context.customerId) {
452
+ const customerUses = await this.repo.countUsagesByCustomer(
453
+ promotion.id,
454
+ context.customerId,
455
+ ctx,
456
+ );
457
+ if (customerUses >= promotion.usageLimitPerCustomer) {
458
+ return "Promotion per-customer usage limit reached.";
459
+ }
460
+ }
461
+
462
+ return undefined;
463
+ }
464
+
465
+ private async checkCategoryMatch(
466
+ lineItems: PromotionLineItem[],
467
+ categorySlugs: string[],
468
+ ctx?: TxContext,
469
+ ): Promise<boolean> {
470
+ for (const lineItem of lineItems) {
471
+ const entityCategories = await this.catalogRepo.findEntityCategories(
472
+ lineItem.entityId,
473
+ ctx,
474
+ );
475
+ for (const link of entityCategories) {
476
+ const category = await this.catalogRepo.findCategoryById(
477
+ link.categoryId,
478
+ ctx,
479
+ );
480
+ if (category && categorySlugs.includes(category.slug)) {
481
+ return true;
482
+ }
483
+ }
484
+ }
485
+ return false;
486
+ }
487
+
488
+ private async evaluatePromotion(
489
+ promotion: Promotion,
490
+ context: PromotionEvaluationContext,
491
+ ctx?: TxContext,
492
+ ): Promise<AppliedPromotion> {
493
+ const conditions = toConditions(
494
+ promotion.conditions as Record<string, unknown> | null,
495
+ );
496
+ const eligibleItems = await this.filterEligibleLineItems(
497
+ context.lineItems,
498
+ conditions,
499
+ ctx,
500
+ );
501
+ const eligibleSubtotal = eligibleItems.reduce(
502
+ (sum, item) => sum + item.totalPrice,
503
+ 0,
504
+ );
505
+
506
+ let discountAmount = 0;
507
+ let freeShipping = false;
508
+
509
+ switch (promotion.type) {
510
+ case "percentage_off_order":
511
+ discountAmount = Math.round((context.subtotal * promotion.value) / 100);
512
+ break;
513
+ case "fixed_off_order":
514
+ discountAmount = promotion.value;
515
+ break;
516
+ case "percentage_off_item":
517
+ discountAmount = Math.round((eligibleSubtotal * promotion.value) / 100);
518
+ break;
519
+ case "fixed_off_item": {
520
+ const totalUnits = eligibleItems.reduce(
521
+ (sum, item) => sum + item.quantity,
522
+ 0,
523
+ );
524
+ discountAmount = totalUnits * promotion.value;
525
+ break;
526
+ }
527
+ case "free_shipping":
528
+ freeShipping = true;
529
+ break;
530
+ case "buy_x_get_y": {
531
+ const buy = promotion.buyQuantity ?? 0;
532
+ const get = promotion.getQuantity ?? 0;
533
+ const totalUnits = eligibleItems.reduce(
534
+ (sum, item) => sum + item.quantity,
535
+ 0,
536
+ );
537
+ if (buy > 0 && get > 0 && totalUnits > 0) {
538
+ const groups = Math.floor(totalUnits / (buy + get));
539
+ const freeUnits = groups * get;
540
+ const minUnitPrice = eligibleItems.length
541
+ ? Math.min(...eligibleItems.map((item) => item.unitPrice))
542
+ : 0;
543
+ discountAmount = freeUnits * minUnitPrice;
544
+ }
545
+ break;
546
+ }
547
+ default:
548
+ discountAmount = 0;
549
+ }
550
+
551
+ return {
552
+ promotionId: promotion.id,
553
+ type: promotion.type as PromotionType,
554
+ discountAmount: roundMoney(discountAmount),
555
+ freeShipping,
556
+ description: promotion.code
557
+ ? `Promotion ${promotion.code}`
558
+ : `Promotion ${promotion.name}`,
559
+ ...(promotion.code !== null ? { code: promotion.code } : {}),
560
+ };
561
+ }
562
+
563
+ private async filterEligibleLineItems(
564
+ lineItems: PromotionLineItem[],
565
+ conditions: PromotionConditions,
566
+ ctx?: TxContext,
567
+ ): Promise<PromotionLineItem[]> {
568
+ const eligible: PromotionLineItem[] = [];
569
+
570
+ for (const lineItem of lineItems) {
571
+ if (conditions.entityTypes && conditions.entityTypes.length > 0) {
572
+ if (!conditions.entityTypes.includes(lineItem.entityType)) continue;
573
+ }
574
+ if (conditions.categories && conditions.categories.length > 0) {
575
+ const entityCats = await this.catalogRepo.findEntityCategories(
576
+ lineItem.entityId,
577
+ ctx,
578
+ );
579
+ const hasCategory = await (async () => {
580
+ for (const link of entityCats) {
581
+ const category = await this.catalogRepo.findCategoryById(
582
+ link.categoryId,
583
+ ctx,
584
+ );
585
+ if (category && conditions.categories!.includes(category.slug)) {
586
+ return true;
587
+ }
588
+ }
589
+ return false;
590
+ })();
591
+ if (!hasCategory) continue;
592
+ }
593
+ eligible.push(lineItem);
594
+ }
595
+
596
+ return eligible;
597
+ }
598
+ }
@@ -0,0 +1,57 @@
1
+ import type { Result } from "../../kernel/result.js";
2
+
3
+ export interface SearchDocument {
4
+ id: string;
5
+ type: string;
6
+ slug: string;
7
+ title: string;
8
+ description?: string;
9
+ status?: string;
10
+ categories: string[];
11
+ brands: string[];
12
+ text: string;
13
+ payload?: Record<string, unknown>;
14
+ }
15
+
16
+ export interface SearchFilters {
17
+ type?: string;
18
+ category?: string;
19
+ brand?: string;
20
+ status?: string;
21
+ }
22
+
23
+ export interface SearchQueryParams {
24
+ query: string;
25
+ page?: number;
26
+ limit?: number;
27
+ filters?: SearchFilters;
28
+ facets?: string[];
29
+ }
30
+
31
+ export interface SearchSuggestParams {
32
+ prefix: string;
33
+ type?: string;
34
+ limit?: number;
35
+ }
36
+
37
+ export interface SearchHit {
38
+ id: string;
39
+ score?: number;
40
+ document: SearchDocument;
41
+ }
42
+
43
+ export interface SearchQueryResult {
44
+ hits: SearchHit[];
45
+ total: number;
46
+ page: number;
47
+ limit: number;
48
+ facets: Record<string, Record<string, number>>;
49
+ }
50
+
51
+ export interface SearchAdapter {
52
+ readonly providerId: string;
53
+ index(documents: SearchDocument[]): Promise<Result<void>>;
54
+ remove(ids: string[]): Promise<Result<void>>;
55
+ search(params: SearchQueryParams): Promise<Result<SearchQueryResult>>;
56
+ suggest(params: SearchSuggestParams): Promise<Result<string[]>>;
57
+ }
@@ -0,0 +1,12 @@
1
+ import type { HookContext } from "../../kernel/hooks/types.js";
2
+ import type { SearchService } from "./service.js";
3
+
4
+ export async function syncToSearchIndex(args: {
5
+ result: { id: string };
6
+ context: HookContext;
7
+ }): Promise<void> {
8
+ const service = args.context.services.search as SearchService | undefined;
9
+ if (!service) return;
10
+
11
+ await service.syncEntity(args.result.id);
12
+ }
@@ -0,0 +1,6 @@
1
+ import type { TxContext } from "../../../kernel/database/tx-context.js";
2
+
3
+ export interface SearchRepository {
4
+ // RFC-002 scaffold: module repositories become the only persistence boundary.
5
+ ping(ctx: TxContext): Promise<void>;
6
+ }