@unifiedcommerce/core 0.0.4 → 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 (179) hide show
  1. package/dist/auth/auth-schema.d.ts +92 -0
  2. package/dist/auth/auth-schema.d.ts.map +1 -1
  3. package/dist/auth/auth-schema.js +7 -0
  4. package/dist/auth/setup.d.ts.map +1 -1
  5. package/dist/auth/setup.js +3 -1
  6. package/package.json +1 -2
  7. package/src/adapters/console-email.ts +0 -43
  8. package/src/auth/access.ts +0 -187
  9. package/src/auth/auth-schema.ts +0 -131
  10. package/src/auth/middleware.ts +0 -161
  11. package/src/auth/org.ts +0 -41
  12. package/src/auth/permissions.ts +0 -28
  13. package/src/auth/setup.ts +0 -165
  14. package/src/auth/system-actor.ts +0 -19
  15. package/src/auth/types.ts +0 -10
  16. package/src/config/defaults.ts +0 -82
  17. package/src/config/define-config.ts +0 -53
  18. package/src/config/types.ts +0 -299
  19. package/src/generated/plugin-capabilities.d.ts +0 -20
  20. package/src/generated/plugin-manifest.ts +0 -23
  21. package/src/generated/plugin-repositories.d.ts +0 -20
  22. package/src/hooks/checkout-completion.ts +0 -262
  23. package/src/hooks/checkout.ts +0 -677
  24. package/src/hooks/order-emails.ts +0 -62
  25. package/src/index.ts +0 -214
  26. package/src/interfaces/mcp/agent-prompt.ts +0 -174
  27. package/src/interfaces/mcp/context-enrichment.ts +0 -177
  28. package/src/interfaces/mcp/server.ts +0 -617
  29. package/src/interfaces/mcp/transport.ts +0 -68
  30. package/src/interfaces/rest/customer-portal.ts +0 -299
  31. package/src/interfaces/rest/index.ts +0 -74
  32. package/src/interfaces/rest/router.ts +0 -334
  33. package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
  34. package/src/interfaces/rest/routes/audit.ts +0 -50
  35. package/src/interfaces/rest/routes/carts.ts +0 -89
  36. package/src/interfaces/rest/routes/catalog.ts +0 -493
  37. package/src/interfaces/rest/routes/checkout.ts +0 -283
  38. package/src/interfaces/rest/routes/inventory.ts +0 -70
  39. package/src/interfaces/rest/routes/media.ts +0 -86
  40. package/src/interfaces/rest/routes/orders.ts +0 -78
  41. package/src/interfaces/rest/routes/payments.ts +0 -60
  42. package/src/interfaces/rest/routes/pricing.ts +0 -57
  43. package/src/interfaces/rest/routes/promotions.ts +0 -92
  44. package/src/interfaces/rest/routes/search.ts +0 -71
  45. package/src/interfaces/rest/routes/webhooks.ts +0 -46
  46. package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
  47. package/src/interfaces/rest/schemas/audit.ts +0 -46
  48. package/src/interfaces/rest/schemas/carts.ts +0 -125
  49. package/src/interfaces/rest/schemas/catalog.ts +0 -450
  50. package/src/interfaces/rest/schemas/checkout.ts +0 -66
  51. package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
  52. package/src/interfaces/rest/schemas/inventory.ts +0 -138
  53. package/src/interfaces/rest/schemas/media.ts +0 -75
  54. package/src/interfaces/rest/schemas/orders.ts +0 -104
  55. package/src/interfaces/rest/schemas/pricing.ts +0 -80
  56. package/src/interfaces/rest/schemas/promotions.ts +0 -110
  57. package/src/interfaces/rest/schemas/responses.ts +0 -85
  58. package/src/interfaces/rest/schemas/search.ts +0 -58
  59. package/src/interfaces/rest/schemas/shared.ts +0 -62
  60. package/src/interfaces/rest/schemas/webhooks.ts +0 -68
  61. package/src/interfaces/rest/utils.ts +0 -104
  62. package/src/interfaces/rest/webhook-router.ts +0 -50
  63. package/src/kernel/compensation/executor.ts +0 -61
  64. package/src/kernel/compensation/types.ts +0 -26
  65. package/src/kernel/database/adapter.ts +0 -13
  66. package/src/kernel/database/drizzle-db.ts +0 -56
  67. package/src/kernel/database/migrate.ts +0 -76
  68. package/src/kernel/database/plugin-types.ts +0 -34
  69. package/src/kernel/database/schema.ts +0 -49
  70. package/src/kernel/database/scoped-db.ts +0 -68
  71. package/src/kernel/database/tx-context.ts +0 -46
  72. package/src/kernel/error-mapper.ts +0 -15
  73. package/src/kernel/errors.ts +0 -89
  74. package/src/kernel/factory/repository-factory.ts +0 -242
  75. package/src/kernel/hooks/create-context.ts +0 -43
  76. package/src/kernel/hooks/executor.ts +0 -88
  77. package/src/kernel/hooks/registry.ts +0 -74
  78. package/src/kernel/hooks/types.ts +0 -52
  79. package/src/kernel/http-error.ts +0 -44
  80. package/src/kernel/jobs/adapter.ts +0 -36
  81. package/src/kernel/jobs/drizzle-adapter.ts +0 -58
  82. package/src/kernel/jobs/runner.ts +0 -153
  83. package/src/kernel/jobs/schema.ts +0 -46
  84. package/src/kernel/jobs/types.ts +0 -30
  85. package/src/kernel/local-api.ts +0 -185
  86. package/src/kernel/plugin/manifest.ts +0 -253
  87. package/src/kernel/query/executor.ts +0 -184
  88. package/src/kernel/query/registry.ts +0 -46
  89. package/src/kernel/result.ts +0 -33
  90. package/src/kernel/schema/extra-columns.ts +0 -37
  91. package/src/kernel/service-registry.ts +0 -76
  92. package/src/kernel/service-timing.ts +0 -89
  93. package/src/kernel/state-machine/machine.ts +0 -101
  94. package/src/modules/analytics/drizzle-adapter.ts +0 -426
  95. package/src/modules/analytics/hooks.ts +0 -11
  96. package/src/modules/analytics/models.ts +0 -125
  97. package/src/modules/analytics/repository/index.ts +0 -6
  98. package/src/modules/analytics/service.ts +0 -245
  99. package/src/modules/analytics/types.ts +0 -180
  100. package/src/modules/audit/hooks.ts +0 -78
  101. package/src/modules/audit/schema.ts +0 -33
  102. package/src/modules/audit/service.ts +0 -151
  103. package/src/modules/cart/access.ts +0 -27
  104. package/src/modules/cart/matcher.ts +0 -26
  105. package/src/modules/cart/repository/index.ts +0 -234
  106. package/src/modules/cart/schema.ts +0 -42
  107. package/src/modules/cart/schemas.ts +0 -38
  108. package/src/modules/cart/service.ts +0 -541
  109. package/src/modules/catalog/repository/index.ts +0 -772
  110. package/src/modules/catalog/schema.ts +0 -203
  111. package/src/modules/catalog/schemas.ts +0 -104
  112. package/src/modules/catalog/service.ts +0 -1544
  113. package/src/modules/customers/repository/index.ts +0 -327
  114. package/src/modules/customers/schema.ts +0 -64
  115. package/src/modules/customers/service.ts +0 -171
  116. package/src/modules/fulfillment/repository/index.ts +0 -426
  117. package/src/modules/fulfillment/schema.ts +0 -101
  118. package/src/modules/fulfillment/service.ts +0 -555
  119. package/src/modules/fulfillment/types.ts +0 -59
  120. package/src/modules/inventory/repository/index.ts +0 -509
  121. package/src/modules/inventory/schema.ts +0 -94
  122. package/src/modules/inventory/schemas.ts +0 -38
  123. package/src/modules/inventory/service.ts +0 -490
  124. package/src/modules/media/adapter.ts +0 -17
  125. package/src/modules/media/repository/index.ts +0 -274
  126. package/src/modules/media/schema.ts +0 -41
  127. package/src/modules/media/service.ts +0 -151
  128. package/src/modules/orders/repository/index.ts +0 -287
  129. package/src/modules/orders/schema.ts +0 -66
  130. package/src/modules/orders/service.ts +0 -619
  131. package/src/modules/orders/stale-order-cleanup.ts +0 -76
  132. package/src/modules/organization/service.ts +0 -191
  133. package/src/modules/payments/adapter.ts +0 -47
  134. package/src/modules/payments/repository/index.ts +0 -6
  135. package/src/modules/payments/service.ts +0 -107
  136. package/src/modules/pricing/repository/index.ts +0 -291
  137. package/src/modules/pricing/schema.ts +0 -71
  138. package/src/modules/pricing/schemas.ts +0 -38
  139. package/src/modules/pricing/service.ts +0 -494
  140. package/src/modules/promotions/repository/index.ts +0 -325
  141. package/src/modules/promotions/schema.ts +0 -62
  142. package/src/modules/promotions/schemas.ts +0 -38
  143. package/src/modules/promotions/service.ts +0 -598
  144. package/src/modules/search/adapter.ts +0 -57
  145. package/src/modules/search/hooks.ts +0 -12
  146. package/src/modules/search/repository/index.ts +0 -6
  147. package/src/modules/search/service.ts +0 -315
  148. package/src/modules/shipping/calculator.ts +0 -188
  149. package/src/modules/shipping/repository/index.ts +0 -6
  150. package/src/modules/shipping/service.ts +0 -51
  151. package/src/modules/tax/adapter.ts +0 -60
  152. package/src/modules/tax/repository/index.ts +0 -6
  153. package/src/modules/tax/service.ts +0 -53
  154. package/src/modules/webhooks/hook.ts +0 -34
  155. package/src/modules/webhooks/repository/index.ts +0 -278
  156. package/src/modules/webhooks/schema.ts +0 -56
  157. package/src/modules/webhooks/service.ts +0 -117
  158. package/src/modules/webhooks/signing.ts +0 -6
  159. package/src/modules/webhooks/ssrf-guard.ts +0 -71
  160. package/src/modules/webhooks/tasks.ts +0 -52
  161. package/src/modules/webhooks/worker.ts +0 -134
  162. package/src/runtime/commerce.ts +0 -145
  163. package/src/runtime/kernel.ts +0 -419
  164. package/src/runtime/logger.ts +0 -36
  165. package/src/runtime/server.ts +0 -349
  166. package/src/runtime/shutdown.ts +0 -43
  167. package/src/test-utils/create-pglite-adapter.ts +0 -129
  168. package/src/test-utils/create-plugin-test-app.ts +0 -128
  169. package/src/test-utils/create-repository-test-harness.ts +0 -16
  170. package/src/test-utils/create-test-config.ts +0 -190
  171. package/src/test-utils/create-test-kernel.ts +0 -7
  172. package/src/test-utils/create-test-plugin-context.ts +0 -75
  173. package/src/test-utils/rest-api-test-utils.ts +0 -265
  174. package/src/test-utils/test-actors.ts +0 -62
  175. package/src/test-utils/typed-hooks.ts +0 -54
  176. package/src/types/commerce-types.ts +0 -34
  177. package/src/utils/id.ts +0 -3
  178. package/src/utils/logger.ts +0 -18
  179. package/src/utils/pagination.ts +0 -22
@@ -1,325 +0,0 @@
1
- import { eq, and, lte, gte, or, isNull, desc, sql } from "drizzle-orm";
2
- import type { TxContext } from "../../../kernel/database/tx-context.js";
3
- import type {
4
- DrizzleDatabase,
5
- DbOrTx,
6
- } from "../../../kernel/database/drizzle-db.js";
7
- import { promotions, promotionUsages } from "../schema.js";
8
-
9
- // Infer types from Drizzle schema
10
- export type Promotion = typeof promotions.$inferSelect;
11
- export type PromotionInsert = typeof promotions.$inferInsert;
12
- export type PromotionUsage = typeof promotionUsages.$inferSelect;
13
- export type PromotionUsageInsert = typeof promotionUsages.$inferInsert;
14
-
15
- /**
16
- * PromotionsRepository provides type-safe database operations for promotions.
17
- *
18
- * This repository manages promotions and their usage tracking.
19
- * All methods support an optional TxContext parameter for transaction participation.
20
- */
21
- export class PromotionsRepository {
22
- constructor(private readonly db: DrizzleDatabase) {}
23
-
24
- private getDb(ctx?: TxContext): DbOrTx {
25
- return (ctx?.tx as DbOrTx | undefined) ?? this.db;
26
- }
27
-
28
- // ─────────────────────────────────────────────────────────────────────────────
29
- // Promotions
30
- // ─────────────────────────────────────────────────────────────────────────────
31
-
32
- async findById(orgId: string, id: string, ctx?: TxContext): Promise<Promotion | undefined> {
33
- const db = this.getDb(ctx);
34
- const rows = await db
35
- .select()
36
- .from(promotions)
37
- .where(and(eq(promotions.organizationId, orgId), eq(promotions.id, id)));
38
- return rows[0];
39
- }
40
-
41
- async findByCode(
42
- orgId: string,
43
- code: string,
44
- ctx?: TxContext,
45
- ): Promise<Promotion | undefined> {
46
- const db = this.getDb(ctx);
47
- const rows = await db
48
- .select()
49
- .from(promotions)
50
- .where(
51
- and(
52
- eq(promotions.organizationId, orgId),
53
- eq(promotions.code, code),
54
- ),
55
- );
56
- return rows[0];
57
- }
58
-
59
- async findAll(orgId: string, ctx?: TxContext): Promise<Promotion[]> {
60
- const db = this.getDb(ctx);
61
- return db
62
- .select()
63
- .from(promotions)
64
- .where(eq(promotions.organizationId, orgId))
65
- .orderBy(desc(promotions.priority));
66
- }
67
-
68
- async findActive(orgId: string, ctx?: TxContext): Promise<Promotion[]> {
69
- const db = this.getDb(ctx);
70
- const now = new Date();
71
-
72
- return db
73
- .select()
74
- .from(promotions)
75
- .where(
76
- and(
77
- eq(promotions.organizationId, orgId),
78
- eq(promotions.isActive, true),
79
- or(isNull(promotions.validFrom), lte(promotions.validFrom, now)),
80
- or(isNull(promotions.validUntil), gte(promotions.validUntil, now)),
81
- ),
82
- )
83
- .orderBy(desc(promotions.priority));
84
- }
85
-
86
- async findAutomatic(orgId: string, ctx?: TxContext): Promise<Promotion[]> {
87
- const db = this.getDb(ctx);
88
- const now = new Date();
89
-
90
- return db
91
- .select()
92
- .from(promotions)
93
- .where(
94
- and(
95
- eq(promotions.organizationId, orgId),
96
- eq(promotions.isActive, true),
97
- eq(promotions.isAutomatic, true),
98
- or(isNull(promotions.validFrom), lte(promotions.validFrom, now)),
99
- or(isNull(promotions.validUntil), gte(promotions.validUntil, now)),
100
- ),
101
- )
102
- .orderBy(desc(promotions.priority));
103
- }
104
-
105
- async create(data: PromotionInsert, ctx?: TxContext): Promise<Promotion> {
106
- const db = this.getDb(ctx);
107
- const rows = await db.insert(promotions).values(data).returning();
108
- return rows[0]!;
109
- }
110
-
111
- async update(
112
- id: string,
113
- data: Partial<Omit<PromotionInsert, "id">>,
114
- ctx?: TxContext,
115
- ): Promise<Promotion | undefined> {
116
- const db = this.getDb(ctx);
117
- const rows = await db
118
- .update(promotions)
119
- .set({ ...data, updatedAt: new Date() })
120
- .where(eq(promotions.id, id))
121
- .returning();
122
- return rows[0];
123
- }
124
-
125
- async delete(id: string, ctx?: TxContext): Promise<boolean> {
126
- const db = this.getDb(ctx);
127
- const result = await db
128
- .delete(promotions)
129
- .where(eq(promotions.id, id))
130
- .returning();
131
- return result.length > 0;
132
- }
133
-
134
- async activate(id: string, ctx?: TxContext): Promise<Promotion | undefined> {
135
- return this.update(id, { isActive: true }, ctx);
136
- }
137
-
138
- async deactivate(
139
- id: string,
140
- ctx?: TxContext,
141
- ): Promise<Promotion | undefined> {
142
- return this.update(id, { isActive: false }, ctx);
143
- }
144
-
145
- // ─────────────────────────────────────────────────────────────────────────────
146
- // Promotion Usages
147
- // ─────────────────────────────────────────────────────────────────────────────
148
-
149
- async findUsageById(
150
- id: string,
151
- ctx?: TxContext,
152
- ): Promise<PromotionUsage | undefined> {
153
- const db = this.getDb(ctx);
154
- const rows = await db
155
- .select()
156
- .from(promotionUsages)
157
- .where(eq(promotionUsages.id, id));
158
- return rows[0];
159
- }
160
-
161
- async findUsagesByPromotionId(
162
- promotionId: string,
163
- ctx?: TxContext,
164
- ): Promise<PromotionUsage[]> {
165
- const db = this.getDb(ctx);
166
- return db
167
- .select()
168
- .from(promotionUsages)
169
- .where(eq(promotionUsages.promotionId, promotionId));
170
- }
171
-
172
- async findUsagesByCustomerId(
173
- customerId: string,
174
- ctx?: TxContext,
175
- ): Promise<PromotionUsage[]> {
176
- const db = this.getDb(ctx);
177
- return db
178
- .select()
179
- .from(promotionUsages)
180
- .where(eq(promotionUsages.customerId, customerId));
181
- }
182
-
183
- async createUsage(
184
- data: PromotionUsageInsert,
185
- ctx?: TxContext,
186
- ): Promise<PromotionUsage> {
187
- const db = this.getDb(ctx);
188
-
189
- // Atomic guard: use INSERT ... SELECT WHERE count < limit to prevent
190
- // race conditions between concurrent checkouts using the same coupon.
191
- // A plain count-then-insert has a TOCTOU gap; this single statement
192
- // ensures the insert only succeeds if the limit has not been reached.
193
- const promo = await db
194
- .select({ usageLimitTotal: promotions.usageLimitTotal })
195
- .from(promotions)
196
- .where(eq(promotions.id, data.promotionId));
197
-
198
- const limit = promo[0]?.usageLimitTotal;
199
-
200
- if (limit != null) {
201
- // Atomic guard: lock the promotion row and check usage count in the
202
- // same statement sequence. This prevents two concurrent checkouts
203
- // from both passing the count check (TOCTOU race).
204
- //
205
- // SELECT ... FOR UPDATE acquires a row-level lock that serializes
206
- // concurrent callers. If the caller is already inside a transaction
207
- // (ctx.tx), the lock is held until that transaction commits.
208
- await db.execute(
209
- sql`SELECT id FROM promotions WHERE id = ${data.promotionId} FOR UPDATE`,
210
- );
211
-
212
- const currentCount = await this.countUsages(data.promotionId, ctx);
213
- if (currentCount >= limit) {
214
- throw new Error(`Promotion usage limit reached (${currentCount}/${limit})`);
215
- }
216
-
217
- const rows = await db.insert(promotionUsages).values(data).returning();
218
- return rows[0]!;
219
- }
220
-
221
- const rows = await db.insert(promotionUsages).values(data).returning();
222
- return rows[0]!;
223
- }
224
-
225
- async countUsages(promotionId: string, ctx?: TxContext): Promise<number> {
226
- const db = this.getDb(ctx);
227
- const result = await db
228
- .select({ count: sql<number>`count(*)::int` })
229
- .from(promotionUsages)
230
- .where(eq(promotionUsages.promotionId, promotionId));
231
- return result[0]?.count ?? 0;
232
- }
233
-
234
- async countUsagesByCustomer(
235
- promotionId: string,
236
- customerId: string,
237
- ctx?: TxContext,
238
- ): Promise<number> {
239
- const db = this.getDb(ctx);
240
- const result = await db
241
- .select({ count: sql<number>`count(*)::int` })
242
- .from(promotionUsages)
243
- .where(
244
- and(
245
- eq(promotionUsages.promotionId, promotionId),
246
- eq(promotionUsages.customerId, customerId),
247
- ),
248
- );
249
- return result[0]?.count ?? 0;
250
- }
251
-
252
- // ─────────────────────────────────────────────────────────────────────────────
253
- // Validation Helpers
254
- // ─────────────────────────────────────────────────────────────────────────────
255
-
256
- async isUsageLimitReached(
257
- orgId: string,
258
- promotionId: string,
259
- ctx?: TxContext,
260
- ): Promise<boolean> {
261
- const promotion = await this.findById(orgId, promotionId, ctx);
262
- if (!promotion || promotion.usageLimitTotal === null) {
263
- return false;
264
- }
265
- const count = await this.countUsages(promotionId, ctx);
266
- return count >= promotion.usageLimitTotal;
267
- }
268
-
269
- async isCustomerUsageLimitReached(
270
- orgId: string,
271
- promotionId: string,
272
- customerId: string,
273
- ctx?: TxContext,
274
- ): Promise<boolean> {
275
- const promotion = await this.findById(orgId, promotionId, ctx);
276
- if (!promotion || promotion.usageLimitPerCustomer === null) {
277
- return false;
278
- }
279
- const count = await this.countUsagesByCustomer(
280
- promotionId,
281
- customerId,
282
- ctx,
283
- );
284
- return count >= promotion.usageLimitPerCustomer;
285
- }
286
-
287
- async isPromotionValid(
288
- orgId: string,
289
- promotionId: string,
290
- customerId?: string,
291
- ctx?: TxContext,
292
- ): Promise<{ valid: boolean; reason?: string }> {
293
- const promotion = await this.findById(orgId, promotionId, ctx);
294
-
295
- if (!promotion) {
296
- return { valid: false, reason: "Promotion not found" };
297
- }
298
-
299
- if (!promotion.isActive) {
300
- return { valid: false, reason: "Promotion is not active" };
301
- }
302
-
303
- const now = new Date();
304
- if (promotion.validFrom && promotion.validFrom > now) {
305
- return { valid: false, reason: "Promotion has not started yet" };
306
- }
307
-
308
- if (promotion.validUntil && promotion.validUntil < now) {
309
- return { valid: false, reason: "Promotion has expired" };
310
- }
311
-
312
- if (await this.isUsageLimitReached(orgId, promotionId, ctx)) {
313
- return { valid: false, reason: "Promotion usage limit reached" };
314
- }
315
-
316
- if (
317
- customerId &&
318
- (await this.isCustomerUsageLimitReached(orgId, promotionId, customerId, ctx))
319
- ) {
320
- return { valid: false, reason: "Customer usage limit reached" };
321
- }
322
-
323
- return { valid: true };
324
- }
325
- }
@@ -1,62 +0,0 @@
1
- import { boolean, index, integer, jsonb, pgTable, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core";
2
- import { organization } from "../../auth/auth-schema.js";
3
-
4
- export const promotions = pgTable(
5
- "promotions",
6
- {
7
- id: uuid("id").defaultRandom().primaryKey(),
8
- organizationId: text("organization_id")
9
- .notNull()
10
- .references(() => organization.id, { onDelete: "cascade" }),
11
- code: text("code"),
12
- name: text("name").notNull(),
13
- type: text("type", {
14
- enum: [
15
- "percentage_off_order",
16
- "fixed_off_order",
17
- "percentage_off_item",
18
- "fixed_off_item",
19
- "free_shipping",
20
- "buy_x_get_y",
21
- ],
22
- }).notNull(),
23
- value: integer("value").notNull().default(0),
24
- buyQuantity: integer("buy_quantity"),
25
- getQuantity: integer("get_quantity"),
26
- isAutomatic: boolean("is_automatic").notNull().default(false),
27
- isActive: boolean("is_active").notNull().default(true),
28
- priority: integer("priority").notNull().default(100),
29
- conditions: jsonb("conditions").$type<Record<string, unknown>>().default({}),
30
- usageLimitTotal: integer("usage_limit_total"),
31
- usageLimitPerCustomer: integer("usage_limit_per_customer"),
32
- validFrom: timestamp("valid_from", { withTimezone: true }),
33
- validUntil: timestamp("valid_until", { withTimezone: true }),
34
- metadata: jsonb("metadata").$type<Record<string, unknown>>().default({}),
35
- createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
36
- updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
37
- },
38
- (table) => ({
39
- codeIdx: index("idx_promotions_code").on(table.code),
40
- activePriorityIdx: index("idx_promotions_active_priority").on(table.isActive, table.priority),
41
- validityIdx: index("idx_promotions_validity").on(table.validFrom, table.validUntil),
42
- orgIdx: index("idx_promotions_org").on(table.organizationId),
43
- orgCodeUnique: uniqueIndex("promotions_org_code_unique").on(table.organizationId, table.code),
44
- }),
45
- );
46
-
47
- export const promotionUsages = pgTable(
48
- "promotion_usages",
49
- {
50
- id: uuid("id").defaultRandom().primaryKey(),
51
- promotionId: uuid("promotion_id")
52
- .references(() => promotions.id, { onDelete: "cascade" })
53
- .notNull(),
54
- customerId: uuid("customer_id"),
55
- orderId: uuid("order_id"),
56
- usedAt: timestamp("used_at", { withTimezone: true }).defaultNow().notNull(),
57
- },
58
- (table) => ({
59
- promotionIdx: index("idx_promotion_usage_promotion").on(table.promotionId),
60
- customerIdx: index("idx_promotion_usage_customer").on(table.customerId),
61
- }),
62
- );
@@ -1,38 +0,0 @@
1
- import { z } from "@hono/zod-openapi";
2
-
3
- // ─── Zod Body Schemas (single source of truth) ─────────────────────────────
4
-
5
- export const CreatePromotionBodySchema = z.object({
6
- name: z.string().openapi({ example: "Summer Sale" }),
7
- type: z.enum([
8
- "percentage_off_order",
9
- "fixed_off_order",
10
- "percentage_off_item",
11
- "fixed_off_item",
12
- "free_shipping",
13
- "buy_x_get_y",
14
- ]).openapi({ example: "percentage_off_order" }),
15
- value: z.number().openapi({ example: 10 }),
16
- code: z.string().optional().openapi({ example: "SUMMER10" }),
17
- buyQuantity: z.number().int().optional(),
18
- getQuantity: z.number().int().optional(),
19
- isAutomatic: z.boolean().optional(),
20
- isActive: z.boolean().optional(),
21
- priority: z.number().int().optional(),
22
- conditions: z.object({
23
- minimumOrderValue: z.number().optional(),
24
- minimumQuantity: z.number().int().optional(),
25
- entityTypes: z.array(z.string()).optional(),
26
- categories: z.array(z.string()).optional(),
27
- customerGroups: z.array(z.string()).optional(),
28
- }).optional(),
29
- usageLimitTotal: z.number().int().optional(),
30
- usageLimitPerCustomer: z.number().int().optional(),
31
- validFrom: z.coerce.date().optional(),
32
- validUntil: z.coerce.date().optional(),
33
- metadata: z.record(z.string(), z.unknown()).optional(),
34
- }).openapi("CreatePromotionRequest");
35
-
36
- // ─── Derived Input Types ────────────────────────────────────────────────────
37
-
38
- export type CreatePromotionInput = z.infer<typeof CreatePromotionBodySchema>;