@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,555 +0,0 @@
1
- import { createHmac } from "node:crypto";
2
- import { resolveOrgId } from "../../auth/org.js";
3
- import type { Actor } from "../../auth/types.js";
4
- import { CommerceNotFoundError } 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 { FulfillmentRepository } from "./repository/index.js";
8
- import type { OrdersRepository } from "../orders/repository/index.js";
9
- import type {
10
- FulfillmentLineItem,
11
- FulfillmentRecord,
12
- FulfillmentStrategy,
13
- FulfillmentStrategyContext,
14
- } from "./types.js";
15
- import { makeId } from "../../utils/id.js";
16
-
17
- interface InventoryServiceLike {
18
- adjust(input: {
19
- entityId: string;
20
- variantId?: string;
21
- warehouseId?: string;
22
- adjustment: number;
23
- reason: string;
24
- referenceType?: string;
25
- referenceId?: string;
26
- }, actor?: unknown): Promise<unknown>;
27
- release(input: {
28
- entityId: string;
29
- variantId?: string;
30
- quantity: number;
31
- orderId: string;
32
- performedBy?: string;
33
- }): Promise<unknown>;
34
- }
35
-
36
- interface FulfillmentServiceDeps {
37
- repository: FulfillmentRepository;
38
- ordersRepository: OrdersRepository;
39
- inventoryService?: InventoryServiceLike;
40
- }
41
-
42
- function toFulfillmentLineItem(lineItem: FulfillmentLineItem): {
43
- id: string;
44
- title: string;
45
- quantity: number;
46
- sku?: string;
47
- } {
48
- return {
49
- id: lineItem.id,
50
- title: lineItem.title,
51
- quantity: lineItem.quantity,
52
- ...(lineItem.sku != null ? { sku: lineItem.sku } : {}),
53
- };
54
- }
55
-
56
- class PhysicalFulfillmentStrategy implements FulfillmentStrategy {
57
- type = "physical";
58
-
59
- async canFulfill(
60
- _lineItem: FulfillmentLineItem,
61
- _context: FulfillmentStrategyContext,
62
- ): Promise<Result<boolean>> {
63
- return Ok(true);
64
- }
65
-
66
- async fulfill(
67
- lineItem: FulfillmentLineItem,
68
- _context: FulfillmentStrategyContext,
69
- ): Promise<Result<FulfillmentRecord>> {
70
- return Ok({
71
- id: makeId(),
72
- orderId: lineItem.orderId,
73
- type: this.type,
74
- status: "pending",
75
- lineItems: [toFulfillmentLineItem(lineItem)],
76
- });
77
- }
78
-
79
- async reverse(
80
- _fulfillmentId: string,
81
- _context: FulfillmentStrategyContext,
82
- ): Promise<Result<void>> {
83
- return Ok(undefined);
84
- }
85
- }
86
-
87
- class DigitalDownloadFulfillmentStrategy implements FulfillmentStrategy {
88
- type = "digital-download";
89
-
90
- async canFulfill(
91
- _lineItem: FulfillmentLineItem,
92
- _context: FulfillmentStrategyContext,
93
- ): Promise<Result<boolean>> {
94
- return Ok(true);
95
- }
96
-
97
- async fulfill(
98
- lineItem: FulfillmentLineItem,
99
- _context: FulfillmentStrategyContext,
100
- ): Promise<Result<FulfillmentRecord>> {
101
- const expiresAt = new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString();
102
- return Ok({
103
- id: makeId(),
104
- orderId: lineItem.orderId,
105
- type: this.type,
106
- status: "fulfilled",
107
- downloadUrl: `https://downloads.local/${lineItem.id}?token=${createHmac("sha256", "download").update(lineItem.id).digest("hex")}`,
108
- downloadExpiresAt: expiresAt,
109
- maxDownloads: 5,
110
- downloadCount: 0,
111
- lineItems: [toFulfillmentLineItem(lineItem)],
112
- entityType: "digitalDownload",
113
- entityId: lineItem.entityId,
114
- ...(lineItem.customerId !== undefined
115
- ? { customerId: lineItem.customerId }
116
- : {}),
117
- });
118
- }
119
-
120
- async reverse(
121
- _fulfillmentId: string,
122
- _context: FulfillmentStrategyContext,
123
- ): Promise<Result<void>> {
124
- return Ok(undefined);
125
- }
126
- }
127
-
128
- class DigitalAccessFulfillmentStrategy implements FulfillmentStrategy {
129
- type = "digital-access";
130
-
131
- async canFulfill(
132
- _lineItem: FulfillmentLineItem,
133
- _context: FulfillmentStrategyContext,
134
- ): Promise<Result<boolean>> {
135
- return Ok(true);
136
- }
137
-
138
- async fulfill(
139
- lineItem: FulfillmentLineItem,
140
- _context: FulfillmentStrategyContext,
141
- ): Promise<Result<FulfillmentRecord>> {
142
- return Ok({
143
- id: makeId(),
144
- orderId: lineItem.orderId,
145
- type: this.type,
146
- status: "fulfilled",
147
- lineItems: [toFulfillmentLineItem(lineItem)],
148
- entityType: "course",
149
- entityId: lineItem.entityId,
150
- ...(lineItem.customerId !== undefined
151
- ? { customerId: lineItem.customerId }
152
- : {}),
153
- isActive: true,
154
- grantedAt: new Date().toISOString(),
155
- });
156
- }
157
-
158
- async reverse(
159
- _fulfillmentId: string,
160
- _context: FulfillmentStrategyContext,
161
- ): Promise<Result<void>> {
162
- return Ok(undefined);
163
- }
164
- }
165
-
166
- class InternalTransferFulfillmentStrategy implements FulfillmentStrategy {
167
- type = "internal-transfer";
168
-
169
- async canFulfill(
170
- _lineItem: FulfillmentLineItem,
171
- _context: FulfillmentStrategyContext,
172
- ): Promise<Result<boolean>> {
173
- return Ok(true);
174
- }
175
-
176
- async fulfill(
177
- lineItem: FulfillmentLineItem,
178
- _context: FulfillmentStrategyContext,
179
- ): Promise<Result<FulfillmentRecord>> {
180
- return Ok({
181
- id: makeId(),
182
- orderId: lineItem.orderId,
183
- type: this.type,
184
- status: "processing",
185
- lineItems: [toFulfillmentLineItem(lineItem)],
186
- });
187
- }
188
-
189
- async reverse(
190
- _fulfillmentId: string,
191
- _context: FulfillmentStrategyContext,
192
- ): Promise<Result<void>> {
193
- return Ok(undefined);
194
- }
195
- }
196
-
197
- class AppointmentFulfillmentStrategy implements FulfillmentStrategy {
198
- type = "appointment";
199
-
200
- async canFulfill(
201
- _lineItem: FulfillmentLineItem,
202
- _context: FulfillmentStrategyContext,
203
- ): Promise<Result<boolean>> {
204
- return Ok(true);
205
- }
206
-
207
- async fulfill(
208
- lineItem: FulfillmentLineItem,
209
- _context: FulfillmentStrategyContext,
210
- ): Promise<Result<FulfillmentRecord>> {
211
- return Ok({
212
- id: makeId(),
213
- orderId: lineItem.orderId,
214
- type: this.type,
215
- status: "pending",
216
- lineItems: [toFulfillmentLineItem(lineItem)],
217
- });
218
- }
219
-
220
- async reverse(
221
- _fulfillmentId: string,
222
- _context: FulfillmentStrategyContext,
223
- ): Promise<Result<void>> {
224
- return Ok(undefined);
225
- }
226
- }
227
-
228
- export class FulfillmentService {
229
- private strategies = new Map<string, FulfillmentStrategy>();
230
-
231
- constructor(private deps: FulfillmentServiceDeps) {
232
- this.strategies.set("physical", new PhysicalFulfillmentStrategy());
233
- this.strategies.set(
234
- "digital-download",
235
- new DigitalDownloadFulfillmentStrategy(),
236
- );
237
- this.strategies.set(
238
- "digital-access",
239
- new DigitalAccessFulfillmentStrategy(),
240
- );
241
- this.strategies.set(
242
- "internal-transfer",
243
- new InternalTransferFulfillmentStrategy(),
244
- );
245
- this.strategies.set("appointment", new AppointmentFulfillmentStrategy());
246
- }
247
-
248
- async fulfillOrder(orderId: string, actor?: Actor | null, ctx?: TxContext): Promise<Result<void>> {
249
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
250
- const order = await this.deps.ordersRepository.findById(orgId, orderId, ctx);
251
- if (!order) return Err(new CommerceNotFoundError("Order not found."));
252
-
253
- const lineItems = await this.deps.ordersRepository.findLineItemsByOrderId(
254
- orderId,
255
- ctx,
256
- );
257
-
258
- for (const lineItem of lineItems) {
259
- const strategyId =
260
- lineItem.entityType === "digitalDownload"
261
- ? "digital-download"
262
- : lineItem.entityType === "course"
263
- ? "digital-access"
264
- : lineItem.entityType === "internalAsset"
265
- ? "internal-transfer"
266
- : "physical";
267
- const strategy = this.strategies.get(strategyId)!;
268
- const result = await strategy.fulfill(
269
- {
270
- ...lineItem,
271
- orderId,
272
- ...(order.customerId != null ? { customerId: order.customerId } : {}),
273
- },
274
- {},
275
- );
276
- if (!result.ok) return result;
277
-
278
- const record = result.value;
279
-
280
- // Persist the fulfillment record via repository
281
- const created = await this.deps.repository.create(
282
- {
283
- id: record.id,
284
- orderId: record.orderId,
285
- type: record.type,
286
- status: record.status,
287
- carrier: record.carrier ?? null,
288
- trackingNumber: record.trackingNumber ?? null,
289
- trackingUrl: record.trackingUrl ?? null,
290
- downloadUrl: record.downloadUrl ?? null,
291
- downloadExpiresAt: record.downloadExpiresAt
292
- ? new Date(record.downloadExpiresAt)
293
- : null,
294
- maxDownloads: record.maxDownloads ?? null,
295
- downloadCount: record.downloadCount ?? 0,
296
- entityType: record.entityType ?? null,
297
- entityId: record.entityId ?? null,
298
- grantedAt: record.grantedAt ? new Date(record.grantedAt) : null,
299
- expiresAt: record.expiresAt ? new Date(record.expiresAt) : null,
300
- isActive: record.isActive ?? true,
301
- customerId: record.customerId ?? null,
302
- },
303
- ctx,
304
- );
305
-
306
- // Create a fulfillment line item linking this fulfillment to the order line item
307
- for (const li of record.lineItems) {
308
- await this.deps.repository.createLineItem(
309
- {
310
- fulfillmentId: created.id,
311
- orderLineItemId: li.id,
312
- quantity: li.quantity,
313
- },
314
- ctx,
315
- );
316
- }
317
-
318
- }
319
-
320
- return Ok(undefined);
321
- }
322
-
323
- async getByOrderId(
324
- orderId: string,
325
- ctx?: TxContext,
326
- ): Promise<Result<FulfillmentRecord[]>> {
327
- const dbRecords = await this.deps.repository.findByOrderId(orderId, ctx);
328
-
329
- // Hydrate each record with its associated line items
330
- const records: FulfillmentRecord[] = [];
331
- for (const dbRecord of dbRecords) {
332
- const fulfillmentLineItems =
333
- await this.deps.repository.findLineItemsByFulfillmentId(
334
- dbRecord.id,
335
- ctx,
336
- );
337
- records.push(toServiceRecord(dbRecord, fulfillmentLineItems));
338
- }
339
-
340
- return Ok(records);
341
- }
342
-
343
- async updateTracking(
344
- input: {
345
- fulfillmentId: string;
346
- carrier?: string;
347
- trackingNumber?: string;
348
- trackingUrl?: string;
349
- status?: string;
350
- },
351
- ctx?: TxContext,
352
- ): Promise<Result<void>> {
353
- const existing = await this.deps.repository.findById(
354
- input.fulfillmentId,
355
- ctx,
356
- );
357
- if (!existing) {
358
- return Err(new CommerceNotFoundError("Fulfillment record not found."));
359
- }
360
-
361
- const updateData: Record<string, unknown> = {};
362
- if (input.carrier !== undefined) updateData.carrier = input.carrier;
363
- if (input.trackingNumber !== undefined)
364
- updateData.trackingNumber = input.trackingNumber;
365
- if (input.trackingUrl !== undefined)
366
- updateData.trackingUrl = input.trackingUrl;
367
- if (input.status !== undefined) updateData.status = input.status;
368
- if (input.status === "shipped") updateData.shippedAt = new Date();
369
- if (input.status === "delivered") updateData.deliveredAt = new Date();
370
-
371
- await this.deps.repository.update(input.fulfillmentId, updateData, ctx);
372
- return Ok(undefined);
373
- }
374
-
375
- async getDownloadUrl(
376
- orderId: string,
377
- lineItemId: string,
378
- userId: string,
379
- actor?: Actor | null,
380
- ctx?: TxContext,
381
- ): Promise<Result<{ url: string; remaining: number; expiresAt: string }>> {
382
- const dbRecords = await this.deps.repository.findByOrderId(orderId, ctx);
383
-
384
- let matchedRecord: (typeof dbRecords)[number] | undefined;
385
- for (const dbRecord of dbRecords) {
386
- if (dbRecord.type !== "digital-download" && dbRecord.type !== "digital")
387
- continue;
388
- const fliItems = await this.deps.repository.findLineItemsByFulfillmentId(
389
- dbRecord.id,
390
- ctx,
391
- );
392
- if (fliItems.some((item) => item.orderLineItemId === lineItemId)) {
393
- matchedRecord = dbRecord;
394
- break;
395
- }
396
- }
397
-
398
- if (!matchedRecord) {
399
- return Err(new CommerceNotFoundError("Digital download not found."));
400
- }
401
-
402
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
403
- const order = await this.deps.ordersRepository.findById(orgId, orderId, ctx);
404
- if (!order || order.customerId !== userId) {
405
- return Err(new CommerceNotFoundError("Order not found."));
406
- }
407
-
408
- if (!matchedRecord.downloadExpiresAt || !matchedRecord.downloadUrl) {
409
- return Err(new CommerceNotFoundError("Download metadata missing."));
410
- }
411
-
412
- if (new Date(matchedRecord.downloadExpiresAt).getTime() < Date.now()) {
413
- return Err(new CommerceNotFoundError("Download link expired."));
414
- }
415
-
416
- const updated = await this.deps.repository.incrementDownloadCount(
417
- matchedRecord.id,
418
- ctx,
419
- );
420
- const downloadCount =
421
- updated?.downloadCount ?? matchedRecord.downloadCount + 1;
422
- const remaining = Math.max(
423
- 0,
424
- (matchedRecord.maxDownloads ?? 5) - downloadCount,
425
- );
426
-
427
- return Ok({
428
- url: matchedRecord.downloadUrl,
429
- remaining,
430
- expiresAt: matchedRecord.downloadExpiresAt.toISOString(),
431
- });
432
- }
433
-
434
- async getDigitalAccess(
435
- userId: string,
436
- type = "course",
437
- ctx?: TxContext,
438
- ): Promise<
439
- Result<
440
- Array<{
441
- entityId: string;
442
- title: string;
443
- grantedAt: string;
444
- expiresAt: string | null;
445
- isActive: boolean;
446
- orderId: string;
447
- }>
448
- >
449
- > {
450
- const output: Array<{
451
- entityId: string;
452
- title: string;
453
- grantedAt: string;
454
- expiresAt: string | null;
455
- isActive: boolean;
456
- orderId: string;
457
- }> = [];
458
-
459
- // Find all fulfillments for this customer
460
- const dbRecords = await this.deps.repository.findByCustomerId(userId, ctx);
461
-
462
- for (const entry of dbRecords) {
463
- if (entry.type !== "digital-access" && entry.type !== "access_grant")
464
- continue;
465
- if (entry.entityType !== type) continue;
466
- if (!entry.entityId || !entry.grantedAt) continue;
467
-
468
- // Get the first fulfillment line item to extract the title
469
- const fliItems = await this.deps.repository.findLineItemsByFulfillmentId(
470
- entry.id,
471
- ctx,
472
- );
473
- let title = "Untitled";
474
- if (fliItems.length > 0) {
475
- const orderLineItem = await this.deps.ordersRepository.findLineItemById(
476
- fliItems[0]!.orderLineItemId,
477
- ctx,
478
- );
479
- if (orderLineItem) {
480
- title = orderLineItem.title;
481
- }
482
- }
483
-
484
- output.push({
485
- entityId: entry.entityId,
486
- title,
487
- grantedAt: entry.grantedAt.toISOString(),
488
- expiresAt: entry.expiresAt?.toISOString() ?? null,
489
- isActive: entry.isActive,
490
- orderId: entry.orderId,
491
- });
492
- }
493
-
494
- return Ok(output);
495
- }
496
- }
497
-
498
- /**
499
- * Maps a Drizzle FulfillmentRecord + its fulfillment line items back to
500
- * the service-layer FulfillmentRecord shape used by callers.
501
- */
502
- function toServiceRecord(
503
- dbRecord: Awaited<ReturnType<FulfillmentRepository["findById"]>> & {},
504
- fulfillmentLineItems: Awaited<
505
- ReturnType<FulfillmentRepository["findLineItemsByFulfillmentId"]>
506
- >,
507
- ): FulfillmentRecord {
508
- return {
509
- id: dbRecord.id,
510
- orderId: dbRecord.orderId,
511
- type: dbRecord.type,
512
- status: dbRecord.status,
513
- lineItems: fulfillmentLineItems.map((fli) => ({
514
- id: fli.orderLineItemId,
515
- title: "", // Title is on the order line item; callers should join if needed
516
- quantity: fli.quantity,
517
- })),
518
- ...(dbRecord.carrier != null ? { carrier: dbRecord.carrier } : {}),
519
- ...(dbRecord.trackingNumber != null
520
- ? { trackingNumber: dbRecord.trackingNumber }
521
- : {}),
522
- ...(dbRecord.trackingUrl != null
523
- ? { trackingUrl: dbRecord.trackingUrl }
524
- : {}),
525
- ...(dbRecord.estimatedDelivery != null
526
- ? { estimatedDelivery: dbRecord.estimatedDelivery.toISOString() }
527
- : {}),
528
- ...(dbRecord.shippedAt != null
529
- ? { shippedAt: dbRecord.shippedAt.toISOString() }
530
- : {}),
531
- ...(dbRecord.deliveredAt != null
532
- ? { deliveredAt: dbRecord.deliveredAt.toISOString() }
533
- : {}),
534
- ...(dbRecord.downloadUrl != null
535
- ? { downloadUrl: dbRecord.downloadUrl }
536
- : {}),
537
- ...(dbRecord.downloadExpiresAt != null
538
- ? { downloadExpiresAt: dbRecord.downloadExpiresAt.toISOString() }
539
- : {}),
540
- ...(dbRecord.maxDownloads != null
541
- ? { maxDownloads: dbRecord.maxDownloads }
542
- : {}),
543
- downloadCount: dbRecord.downloadCount,
544
- ...(dbRecord.customerId != null ? { customerId: dbRecord.customerId } : {}),
545
- ...(dbRecord.entityType != null ? { entityType: dbRecord.entityType } : {}),
546
- ...(dbRecord.entityId != null ? { entityId: dbRecord.entityId } : {}),
547
- ...(dbRecord.grantedAt != null
548
- ? { grantedAt: dbRecord.grantedAt.toISOString() }
549
- : {}),
550
- ...(dbRecord.expiresAt != null
551
- ? { expiresAt: dbRecord.expiresAt.toISOString() }
552
- : {}),
553
- isActive: dbRecord.isActive,
554
- };
555
- }
@@ -1,59 +0,0 @@
1
- import type { Result } from "../../kernel/result.js";
2
- import type { OrderLineItem } from "../orders/repository/index.js";
3
-
4
- export interface FulfillmentRecord {
5
- id: string;
6
- orderId: string;
7
- lineItems: Array<{
8
- id: string;
9
- title: string;
10
- quantity: number;
11
- sku?: string;
12
- }>;
13
- type: string;
14
- status: string;
15
- carrier?: string;
16
- trackingNumber?: string;
17
- trackingUrl?: string;
18
- estimatedDelivery?: string;
19
- shippedAt?: string;
20
- deliveredAt?: string;
21
- downloadUrl?: string;
22
- downloadExpiresAt?: string;
23
- maxDownloads?: number;
24
- downloadCount?: number;
25
- customerId?: string;
26
- entityType?: string;
27
- entityId?: string;
28
- grantedAt?: string;
29
- expiresAt?: string;
30
- isActive?: boolean;
31
- }
32
-
33
- export interface FulfillmentStrategyContext {
34
- actorId?: string;
35
- }
36
-
37
- export type FulfillmentLineItem = Pick<
38
- OrderLineItem,
39
- "id" | "entityId" | "entityType" | "sku" | "title" | "quantity"
40
- > & {
41
- orderId: string;
42
- customerId?: string;
43
- };
44
-
45
- export interface FulfillmentStrategy {
46
- type: string;
47
- canFulfill(
48
- lineItem: FulfillmentLineItem,
49
- context: FulfillmentStrategyContext,
50
- ): Promise<Result<boolean>>;
51
- fulfill(
52
- lineItem: FulfillmentLineItem,
53
- context: FulfillmentStrategyContext,
54
- ): Promise<Result<FulfillmentRecord>>;
55
- reverse(
56
- fulfillmentId: string,
57
- context: FulfillmentStrategyContext,
58
- ): Promise<Result<void>>;
59
- }