@unifiedcommerce/core 0.1.0 → 0.2.0

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 (245) hide show
  1. package/dist/auth/setup.d.ts.map +1 -1
  2. package/dist/auth/setup.js +8 -3
  3. package/dist/config/types.d.ts +3 -1
  4. package/dist/config/types.d.ts.map +1 -1
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -0
  8. package/dist/interfaces/mcp/server.d.ts +3 -5
  9. package/dist/interfaces/mcp/server.d.ts.map +1 -1
  10. package/dist/interfaces/mcp/server.js +25 -510
  11. package/dist/interfaces/mcp/tool-builder.d.ts +120 -0
  12. package/dist/interfaces/mcp/tool-builder.d.ts.map +1 -0
  13. package/dist/interfaces/mcp/tool-builder.js +224 -0
  14. package/dist/interfaces/mcp/tools/analytics.d.ts +42 -0
  15. package/dist/interfaces/mcp/tools/analytics.d.ts.map +1 -0
  16. package/dist/interfaces/mcp/tools/analytics.js +70 -0
  17. package/dist/interfaces/mcp/tools/cart.d.ts +14 -0
  18. package/dist/interfaces/mcp/tools/cart.d.ts.map +1 -0
  19. package/dist/interfaces/mcp/tools/cart.js +47 -0
  20. package/dist/interfaces/mcp/tools/catalog.d.ts +53 -0
  21. package/dist/interfaces/mcp/tools/catalog.d.ts.map +1 -0
  22. package/dist/interfaces/mcp/tools/catalog.js +284 -0
  23. package/dist/interfaces/mcp/tools/index.d.ts +3 -0
  24. package/dist/interfaces/mcp/tools/index.d.ts.map +1 -0
  25. package/dist/interfaces/mcp/tools/index.js +20 -0
  26. package/dist/interfaces/mcp/tools/inventory.d.ts +27 -0
  27. package/dist/interfaces/mcp/tools/inventory.d.ts.map +1 -0
  28. package/dist/interfaces/mcp/tools/inventory.js +143 -0
  29. package/dist/interfaces/mcp/tools/orders.d.ts +18 -0
  30. package/dist/interfaces/mcp/tools/orders.d.ts.map +1 -0
  31. package/dist/interfaces/mcp/tools/orders.js +82 -0
  32. package/dist/interfaces/mcp/tools/pricing.d.ts +29 -0
  33. package/dist/interfaces/mcp/tools/pricing.d.ts.map +1 -0
  34. package/dist/interfaces/mcp/tools/pricing.js +90 -0
  35. package/dist/interfaces/mcp/tools/promotions.d.ts +44 -0
  36. package/dist/interfaces/mcp/tools/promotions.d.ts.map +1 -0
  37. package/dist/interfaces/mcp/tools/promotions.js +109 -0
  38. package/dist/interfaces/mcp/tools/registry.d.ts +32 -0
  39. package/dist/interfaces/mcp/tools/registry.d.ts.map +1 -0
  40. package/dist/interfaces/mcp/tools/registry.js +55 -0
  41. package/dist/interfaces/mcp/tools/search.d.ts +14 -0
  42. package/dist/interfaces/mcp/tools/search.d.ts.map +1 -0
  43. package/dist/interfaces/mcp/tools/search.js +39 -0
  44. package/dist/interfaces/mcp/tools/webhooks.d.ts +15 -0
  45. package/dist/interfaces/mcp/tools/webhooks.d.ts.map +1 -0
  46. package/dist/interfaces/mcp/tools/webhooks.js +48 -0
  47. package/dist/interfaces/mcp/transport.d.ts +17 -2
  48. package/dist/interfaces/mcp/transport.d.ts.map +1 -1
  49. package/dist/interfaces/mcp/transport.js +91 -44
  50. package/dist/interfaces/rest/router.d.ts.map +1 -1
  51. package/dist/interfaces/rest/routes/checkout.d.ts.map +1 -1
  52. package/dist/interfaces/rest/routes/checkout.js +1 -1
  53. package/dist/interfaces/rest/routes/promotions.d.ts.map +1 -1
  54. package/dist/interfaces/rest/routes/promotions.js +3 -2
  55. package/dist/kernel/database/adapter.d.ts +8 -0
  56. package/dist/kernel/database/adapter.d.ts.map +1 -1
  57. package/dist/kernel/factory/repository-factory.d.ts.map +1 -1
  58. package/dist/kernel/factory/repository-factory.js +3 -1
  59. package/dist/kernel/local-api.d.ts.map +1 -1
  60. package/dist/kernel/local-api.js +2 -0
  61. package/dist/kernel/plugin/manifest.d.ts +3 -3
  62. package/dist/kernel/plugin/manifest.d.ts.map +1 -1
  63. package/dist/kernel/plugin/manifest.js +36 -7
  64. package/dist/runtime/kernel.d.ts +1 -2
  65. package/dist/runtime/kernel.d.ts.map +1 -1
  66. package/dist/runtime/kernel.js +16 -8
  67. package/dist/runtime/server.d.ts.map +1 -1
  68. package/dist/runtime/server.js +8 -3
  69. package/dist/test-utils/create-pglite-adapter.d.ts.map +1 -1
  70. package/dist/test-utils/create-pglite-adapter.js +7 -6
  71. package/dist/tsconfig.tsbuildinfo +1 -0
  72. package/package.json +2 -2
  73. package/src/adapters/console-email.ts +0 -43
  74. package/src/auth/access.ts +0 -187
  75. package/src/auth/auth-schema.ts +0 -139
  76. package/src/auth/middleware.ts +0 -161
  77. package/src/auth/org.ts +0 -41
  78. package/src/auth/permissions.ts +0 -28
  79. package/src/auth/setup.ts +0 -169
  80. package/src/auth/system-actor.ts +0 -19
  81. package/src/auth/types.ts +0 -10
  82. package/src/config/defaults.ts +0 -82
  83. package/src/config/define-config.ts +0 -53
  84. package/src/config/types.ts +0 -299
  85. package/src/generated/plugin-capabilities.d.ts +0 -20
  86. package/src/generated/plugin-manifest.ts +0 -23
  87. package/src/generated/plugin-repositories.d.ts +0 -20
  88. package/src/hooks/checkout-completion.ts +0 -262
  89. package/src/hooks/checkout.ts +0 -677
  90. package/src/hooks/order-emails.ts +0 -62
  91. package/src/index.ts +0 -214
  92. package/src/interfaces/mcp/agent-prompt.ts +0 -174
  93. package/src/interfaces/mcp/context-enrichment.ts +0 -177
  94. package/src/interfaces/mcp/server.ts +0 -617
  95. package/src/interfaces/mcp/transport.ts +0 -68
  96. package/src/interfaces/rest/customer-portal.ts +0 -299
  97. package/src/interfaces/rest/index.ts +0 -74
  98. package/src/interfaces/rest/router.ts +0 -334
  99. package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
  100. package/src/interfaces/rest/routes/audit.ts +0 -50
  101. package/src/interfaces/rest/routes/carts.ts +0 -89
  102. package/src/interfaces/rest/routes/catalog.ts +0 -493
  103. package/src/interfaces/rest/routes/checkout.ts +0 -283
  104. package/src/interfaces/rest/routes/inventory.ts +0 -70
  105. package/src/interfaces/rest/routes/media.ts +0 -86
  106. package/src/interfaces/rest/routes/orders.ts +0 -78
  107. package/src/interfaces/rest/routes/payments.ts +0 -60
  108. package/src/interfaces/rest/routes/pricing.ts +0 -57
  109. package/src/interfaces/rest/routes/promotions.ts +0 -92
  110. package/src/interfaces/rest/routes/search.ts +0 -71
  111. package/src/interfaces/rest/routes/webhooks.ts +0 -46
  112. package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
  113. package/src/interfaces/rest/schemas/audit.ts +0 -46
  114. package/src/interfaces/rest/schemas/carts.ts +0 -125
  115. package/src/interfaces/rest/schemas/catalog.ts +0 -450
  116. package/src/interfaces/rest/schemas/checkout.ts +0 -66
  117. package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
  118. package/src/interfaces/rest/schemas/inventory.ts +0 -138
  119. package/src/interfaces/rest/schemas/media.ts +0 -75
  120. package/src/interfaces/rest/schemas/orders.ts +0 -104
  121. package/src/interfaces/rest/schemas/pricing.ts +0 -80
  122. package/src/interfaces/rest/schemas/promotions.ts +0 -110
  123. package/src/interfaces/rest/schemas/responses.ts +0 -85
  124. package/src/interfaces/rest/schemas/search.ts +0 -58
  125. package/src/interfaces/rest/schemas/shared.ts +0 -62
  126. package/src/interfaces/rest/schemas/webhooks.ts +0 -68
  127. package/src/interfaces/rest/utils.ts +0 -104
  128. package/src/interfaces/rest/webhook-router.ts +0 -50
  129. package/src/kernel/compensation/executor.ts +0 -61
  130. package/src/kernel/compensation/types.ts +0 -26
  131. package/src/kernel/database/adapter.ts +0 -13
  132. package/src/kernel/database/drizzle-db.ts +0 -56
  133. package/src/kernel/database/migrate.ts +0 -76
  134. package/src/kernel/database/plugin-types.ts +0 -34
  135. package/src/kernel/database/schema.ts +0 -49
  136. package/src/kernel/database/scoped-db.ts +0 -68
  137. package/src/kernel/database/tx-context.ts +0 -46
  138. package/src/kernel/error-mapper.ts +0 -15
  139. package/src/kernel/errors.ts +0 -89
  140. package/src/kernel/factory/repository-factory.ts +0 -242
  141. package/src/kernel/hooks/create-context.ts +0 -43
  142. package/src/kernel/hooks/executor.ts +0 -88
  143. package/src/kernel/hooks/registry.ts +0 -74
  144. package/src/kernel/hooks/types.ts +0 -52
  145. package/src/kernel/http-error.ts +0 -44
  146. package/src/kernel/jobs/adapter.ts +0 -36
  147. package/src/kernel/jobs/drizzle-adapter.ts +0 -58
  148. package/src/kernel/jobs/runner.ts +0 -153
  149. package/src/kernel/jobs/schema.ts +0 -46
  150. package/src/kernel/jobs/types.ts +0 -30
  151. package/src/kernel/local-api.ts +0 -185
  152. package/src/kernel/plugin/manifest.ts +0 -253
  153. package/src/kernel/query/executor.ts +0 -184
  154. package/src/kernel/query/registry.ts +0 -46
  155. package/src/kernel/result.ts +0 -33
  156. package/src/kernel/schema/extra-columns.ts +0 -37
  157. package/src/kernel/service-registry.ts +0 -76
  158. package/src/kernel/service-timing.ts +0 -89
  159. package/src/kernel/state-machine/machine.ts +0 -101
  160. package/src/modules/analytics/drizzle-adapter.ts +0 -426
  161. package/src/modules/analytics/hooks.ts +0 -11
  162. package/src/modules/analytics/models.ts +0 -125
  163. package/src/modules/analytics/repository/index.ts +0 -6
  164. package/src/modules/analytics/service.ts +0 -245
  165. package/src/modules/analytics/types.ts +0 -180
  166. package/src/modules/audit/hooks.ts +0 -78
  167. package/src/modules/audit/schema.ts +0 -33
  168. package/src/modules/audit/service.ts +0 -151
  169. package/src/modules/cart/access.ts +0 -27
  170. package/src/modules/cart/matcher.ts +0 -26
  171. package/src/modules/cart/repository/index.ts +0 -234
  172. package/src/modules/cart/schema.ts +0 -42
  173. package/src/modules/cart/schemas.ts +0 -38
  174. package/src/modules/cart/service.ts +0 -541
  175. package/src/modules/catalog/repository/index.ts +0 -772
  176. package/src/modules/catalog/schema.ts +0 -203
  177. package/src/modules/catalog/schemas.ts +0 -104
  178. package/src/modules/catalog/service.ts +0 -1544
  179. package/src/modules/customers/repository/index.ts +0 -327
  180. package/src/modules/customers/schema.ts +0 -64
  181. package/src/modules/customers/service.ts +0 -171
  182. package/src/modules/fulfillment/repository/index.ts +0 -426
  183. package/src/modules/fulfillment/schema.ts +0 -101
  184. package/src/modules/fulfillment/service.ts +0 -555
  185. package/src/modules/fulfillment/types.ts +0 -59
  186. package/src/modules/inventory/repository/index.ts +0 -509
  187. package/src/modules/inventory/schema.ts +0 -94
  188. package/src/modules/inventory/schemas.ts +0 -38
  189. package/src/modules/inventory/service.ts +0 -490
  190. package/src/modules/media/adapter.ts +0 -17
  191. package/src/modules/media/repository/index.ts +0 -274
  192. package/src/modules/media/schema.ts +0 -41
  193. package/src/modules/media/service.ts +0 -151
  194. package/src/modules/orders/repository/index.ts +0 -287
  195. package/src/modules/orders/schema.ts +0 -66
  196. package/src/modules/orders/service.ts +0 -619
  197. package/src/modules/orders/stale-order-cleanup.ts +0 -76
  198. package/src/modules/organization/service.ts +0 -191
  199. package/src/modules/payments/adapter.ts +0 -47
  200. package/src/modules/payments/repository/index.ts +0 -6
  201. package/src/modules/payments/service.ts +0 -107
  202. package/src/modules/pricing/repository/index.ts +0 -291
  203. package/src/modules/pricing/schema.ts +0 -71
  204. package/src/modules/pricing/schemas.ts +0 -38
  205. package/src/modules/pricing/service.ts +0 -494
  206. package/src/modules/promotions/repository/index.ts +0 -325
  207. package/src/modules/promotions/schema.ts +0 -62
  208. package/src/modules/promotions/schemas.ts +0 -38
  209. package/src/modules/promotions/service.ts +0 -598
  210. package/src/modules/search/adapter.ts +0 -57
  211. package/src/modules/search/hooks.ts +0 -12
  212. package/src/modules/search/repository/index.ts +0 -6
  213. package/src/modules/search/service.ts +0 -315
  214. package/src/modules/shipping/calculator.ts +0 -188
  215. package/src/modules/shipping/repository/index.ts +0 -6
  216. package/src/modules/shipping/service.ts +0 -51
  217. package/src/modules/tax/adapter.ts +0 -60
  218. package/src/modules/tax/repository/index.ts +0 -6
  219. package/src/modules/tax/service.ts +0 -53
  220. package/src/modules/webhooks/hook.ts +0 -34
  221. package/src/modules/webhooks/repository/index.ts +0 -278
  222. package/src/modules/webhooks/schema.ts +0 -56
  223. package/src/modules/webhooks/service.ts +0 -117
  224. package/src/modules/webhooks/signing.ts +0 -6
  225. package/src/modules/webhooks/ssrf-guard.ts +0 -71
  226. package/src/modules/webhooks/tasks.ts +0 -52
  227. package/src/modules/webhooks/worker.ts +0 -134
  228. package/src/runtime/commerce.ts +0 -145
  229. package/src/runtime/kernel.ts +0 -419
  230. package/src/runtime/logger.ts +0 -36
  231. package/src/runtime/server.ts +0 -349
  232. package/src/runtime/shutdown.ts +0 -43
  233. package/src/test-utils/create-pglite-adapter.ts +0 -129
  234. package/src/test-utils/create-plugin-test-app.ts +0 -128
  235. package/src/test-utils/create-repository-test-harness.ts +0 -16
  236. package/src/test-utils/create-test-config.ts +0 -190
  237. package/src/test-utils/create-test-kernel.ts +0 -7
  238. package/src/test-utils/create-test-plugin-context.ts +0 -75
  239. package/src/test-utils/rest-api-test-utils.ts +0 -265
  240. package/src/test-utils/test-actors.ts +0 -62
  241. package/src/test-utils/typed-hooks.ts +0 -54
  242. package/src/types/commerce-types.ts +0 -34
  243. package/src/utils/id.ts +0 -3
  244. package/src/utils/logger.ts +0 -18
  245. package/src/utils/pagination.ts +0 -22
@@ -1,191 +0,0 @@
1
- /**
2
- * OrganizationService — wraps Better Auth's server-side organization API.
3
- *
4
- * When auth.api is available (server context), uses Better Auth's
5
- * createOrganization which properly creates org + member records.
6
- *
7
- * When auth is not available (kernel-only scripts), falls back to
8
- * direct Drizzle insert into the organization table.
9
- */
10
-
11
- import { eq } from "drizzle-orm";
12
- import type { PgDatabase, PgQueryResultHKT } from "drizzle-orm/pg-core";
13
- import { organization, member } from "../../auth/auth-schema.js";
14
- import type { AuthInstance } from "../../auth/setup.js";
15
-
16
- type DrizzleDb = PgDatabase<PgQueryResultHKT, Record<string, unknown>>;
17
-
18
- interface Result<T> { ok: true; value: T }
19
- interface ResultErr { ok: false; error: string }
20
- function Ok<T>(value: T): Result<T> { return { ok: true, value }; }
21
- function Err(error: string): ResultErr { return { ok: false, error }; }
22
-
23
- export interface Organization {
24
- id: string;
25
- name: string;
26
- slug: string;
27
- logo?: string | null;
28
- metadata?: string | null;
29
- createdAt: Date;
30
- }
31
-
32
- export interface OrganizationCreateInput {
33
- id?: string;
34
- name: string;
35
- slug: string;
36
- userId?: string;
37
- logo?: string;
38
- metadata?: Record<string, unknown>;
39
- }
40
-
41
- export class OrganizationService {
42
- private auth: AuthInstance | null;
43
- private db: DrizzleDb;
44
-
45
- constructor(db: unknown, auth?: AuthInstance | null) {
46
- this.db = db as DrizzleDb;
47
- this.auth = auth ?? null;
48
- }
49
-
50
- /**
51
- * Create an organization.
52
- *
53
- * If auth.api is available, uses Better Auth's createOrganization
54
- * which creates both the org and a member record for the creator.
55
- *
56
- * If auth is not available, falls back to direct Drizzle insert
57
- * and manually creates a member record if userId is provided.
58
- */
59
- async create(input: OrganizationCreateInput): Promise<Result<Organization> | ResultErr> {
60
- // Try Better Auth server-side API first
61
- if (this.auth) {
62
- try {
63
- const api = this.auth.api as Record<string, unknown>;
64
- if (typeof api.createOrganization === "function") {
65
- const result = await (api.createOrganization as (opts: unknown) => Promise<unknown>)({
66
- body: {
67
- name: input.name,
68
- slug: input.slug,
69
- logo: input.logo,
70
- metadata: input.metadata ? JSON.stringify(input.metadata) : undefined,
71
- ...(input.userId ? { userId: input.userId } : {}),
72
- },
73
- });
74
-
75
- const org = result as Record<string, unknown>;
76
- return Ok({
77
- id: String(org.id ?? ""),
78
- name: String(org.name ?? ""),
79
- slug: String(org.slug ?? ""),
80
- logo: org.logo as string | null,
81
- metadata: org.metadata as string | null,
82
- createdAt: org.createdAt instanceof Date ? org.createdAt : new Date(),
83
- });
84
- }
85
- } catch (err) {
86
- // If Better Auth throws (e.g., slug conflict), handle gracefully
87
- const message = err instanceof Error ? err.message : String(err);
88
- if (message.includes("already exists") || message.includes("UNIQUE")) {
89
- return Err(`Organization with slug "${input.slug}" already exists`);
90
- }
91
- // Fall through to Drizzle fallback
92
- }
93
- }
94
-
95
- // Fallback: direct Drizzle insert
96
- const orgId = input.id ?? crypto.randomUUID().replace(/-/g, "").slice(0, 32);
97
-
98
- // Check if already exists
99
- const existing = await this.db
100
- .select({ id: organization.id })
101
- .from(organization)
102
- .where(eq(organization.id, orgId));
103
-
104
- if (existing.length > 0) {
105
- return Ok({
106
- id: existing[0]!.id,
107
- name: input.name,
108
- slug: input.slug,
109
- logo: null,
110
- metadata: null,
111
- createdAt: new Date(),
112
- });
113
- }
114
-
115
- const existingSlug = await this.db
116
- .select({ id: organization.id })
117
- .from(organization)
118
- .where(eq(organization.slug, input.slug));
119
-
120
- if (existingSlug.length > 0) {
121
- return Ok({
122
- id: existingSlug[0]!.id,
123
- name: input.name,
124
- slug: input.slug,
125
- logo: null,
126
- metadata: null,
127
- createdAt: new Date(),
128
- });
129
- }
130
-
131
- await this.db.insert(organization).values({
132
- id: orgId,
133
- name: input.name,
134
- slug: input.slug,
135
- logo: input.logo,
136
- metadata: input.metadata ? JSON.stringify(input.metadata) : undefined,
137
- createdAt: new Date(),
138
- });
139
-
140
- // Create member record for the creator
141
- if (input.userId) {
142
- await this.db.insert(member).values({
143
- id: crypto.randomUUID().replace(/-/g, "").slice(0, 32),
144
- organizationId: orgId,
145
- userId: input.userId,
146
- role: "owner",
147
- createdAt: new Date(),
148
- });
149
- }
150
-
151
- return Ok({
152
- id: orgId,
153
- name: input.name,
154
- slug: input.slug,
155
- logo: input.logo ?? null,
156
- metadata: input.metadata ? JSON.stringify(input.metadata) : null,
157
- createdAt: new Date(),
158
- });
159
- }
160
-
161
- async getById(id: string): Promise<Result<Organization> | ResultErr> {
162
- const rows = await this.db
163
- .select()
164
- .from(organization)
165
- .where(eq(organization.id, id));
166
-
167
- if (rows.length === 0) return Err("Organization not found");
168
-
169
- const org = rows[0]!;
170
- return Ok({
171
- id: org.id,
172
- name: org.name,
173
- slug: org.slug,
174
- logo: org.logo,
175
- metadata: org.metadata,
176
- createdAt: org.createdAt,
177
- });
178
- }
179
-
180
- async list(): Promise<Result<Organization[]>> {
181
- const rows = await this.db.select().from(organization);
182
- return Ok(rows.map(org => ({
183
- id: org.id,
184
- name: org.name,
185
- slug: org.slug,
186
- logo: org.logo,
187
- metadata: org.metadata,
188
- createdAt: org.createdAt,
189
- })));
190
- }
191
- }
@@ -1,47 +0,0 @@
1
- import type { Result } from "../../kernel/result.js";
2
-
3
- export interface PaymentIntent {
4
- id: string;
5
- status: string;
6
- amount: number;
7
- currency: string;
8
- clientSecret?: string | null;
9
- }
10
-
11
- export interface PaymentCapture {
12
- id: string;
13
- status: string;
14
- amountCaptured: number;
15
- }
16
-
17
- export interface PaymentRefund {
18
- id: string;
19
- status: string;
20
- amountRefunded: number;
21
- }
22
-
23
- export interface PaymentWebhookEvent {
24
- id: string;
25
- type: string;
26
- data: unknown;
27
- }
28
-
29
- export interface CreatePaymentIntentParams {
30
- amount: number;
31
- currency: string;
32
- orderId: string;
33
- customerId?: string;
34
- metadata?: Record<string, string>;
35
- terminalId?: string;
36
- }
37
-
38
- export interface PaymentAdapter {
39
- readonly providerId: string;
40
- createPaymentIntent(params: CreatePaymentIntentParams): Promise<Result<PaymentIntent>>;
41
- capturePayment(paymentIntentId: string, amount?: number): Promise<Result<PaymentCapture>>;
42
- refundPayment(paymentId: string, amount: number, reason?: string): Promise<Result<PaymentRefund>>;
43
- cancelPaymentIntent(paymentIntentId: string): Promise<Result<void>>;
44
- verifyWebhook(request: Request): Promise<Result<PaymentWebhookEvent>>;
45
- /** Optional schema extension: adapter-owned columns prefixed with `providerId_` */
46
- extraColumns?(): Record<string, unknown>;
47
- }
@@ -1,6 +0,0 @@
1
- import type { TxContext } from "../../../kernel/database/tx-context.js";
2
-
3
- export interface PaymentsRepository {
4
- // RFC-002 scaffold: module repositories become the only persistence boundary.
5
- ping(ctx: TxContext): Promise<void>;
6
- }
@@ -1,107 +0,0 @@
1
- import { CommerceValidationError } from "../../kernel/errors.js";
2
- import { Err, Ok, type Result } from "../../kernel/result.js";
3
- import type {
4
- CreatePaymentIntentParams,
5
- PaymentAdapter,
6
- PaymentCapture,
7
- PaymentIntent,
8
- PaymentRefund,
9
- } from "./adapter.js";
10
-
11
- export class PaymentsService {
12
- private readonly adapterMap: Map<string, PaymentAdapter>;
13
- private readonly defaultAdapter: PaymentAdapter | undefined;
14
-
15
- constructor(adapters: PaymentAdapter[] | undefined) {
16
- this.adapterMap = new Map();
17
- for (const adapter of adapters ?? []) {
18
- this.adapterMap.set(adapter.providerId, adapter);
19
- }
20
- this.defaultAdapter = adapters?.[0];
21
- }
22
-
23
- /**
24
- * Resolve a specific adapter by its providerId.
25
- * Falls back to the default (first) adapter only when paymentMethodId is omitted.
26
- */
27
- private resolveAdapter(paymentMethodId?: string): Result<PaymentAdapter> {
28
- if (paymentMethodId) {
29
- const adapter = this.adapterMap.get(paymentMethodId);
30
- if (!adapter) {
31
- return Err(
32
- new CommerceValidationError(
33
- `No payment adapter registered for provider "${paymentMethodId}". ` +
34
- `Available: [${[...this.adapterMap.keys()].join(", ")}]`,
35
- ),
36
- );
37
- }
38
- return Ok(adapter);
39
- }
40
- if (!this.defaultAdapter) {
41
- return Err(new CommerceValidationError("No payment adapter configured."));
42
- }
43
- return Ok(this.defaultAdapter);
44
- }
45
-
46
- /** Expose registered provider IDs for validation hooks. */
47
- get registeredProviderIds(): string[] {
48
- return [...this.adapterMap.keys()];
49
- }
50
-
51
- async authorize(
52
- params: Omit<CreatePaymentIntentParams, "orderId"> & {
53
- paymentMethodId?: string;
54
- metadata?: Record<string, unknown>;
55
- },
56
- ): Promise<Result<PaymentIntent>> {
57
- const adapter = this.resolveAdapter(params.paymentMethodId);
58
- if (!adapter.ok) return adapter;
59
-
60
- return adapter.value.createPaymentIntent({
61
- ...params,
62
- orderId: String(params.metadata?.orderId ?? "pending-order"),
63
- metadata: Object.fromEntries(
64
- Object.entries(params.metadata ?? {}).map(([key, value]) => [
65
- key,
66
- String(value),
67
- ]),
68
- ),
69
- });
70
- }
71
-
72
- async capture(
73
- paymentIntentId: string,
74
- amount?: number,
75
- paymentMethodId?: string,
76
- ): Promise<Result<PaymentCapture>> {
77
- const adapter = this.resolveAdapter(paymentMethodId);
78
- if (!adapter.ok) return adapter;
79
- return adapter.value.capturePayment(paymentIntentId, amount);
80
- }
81
-
82
- async refund(
83
- paymentId: string,
84
- amount: number,
85
- reason?: string,
86
- paymentMethodId?: string,
87
- ): Promise<Result<PaymentRefund>> {
88
- const adapter = this.resolveAdapter(paymentMethodId);
89
- if (!adapter.ok) return adapter;
90
- return adapter.value.refundPayment(paymentId, amount, reason);
91
- }
92
-
93
- async cancel(
94
- paymentIntentId: string,
95
- paymentMethodId?: string,
96
- ): Promise<Result<void>> {
97
- const adapter = this.resolveAdapter(paymentMethodId);
98
- if (!adapter.ok) return adapter;
99
- return adapter.value.cancelPaymentIntent(paymentIntentId);
100
- }
101
-
102
- async verifyWebhook(request: Request) {
103
- const adapter = this.resolveAdapter();
104
- if (!adapter.ok) return adapter;
105
- return adapter.value.verifyWebhook(request);
106
- }
107
- }
@@ -1,291 +0,0 @@
1
- import { eq, and, lte, gte, or, isNull } 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 { prices, priceModifiers } from "../schema.js";
8
-
9
- // Infer types from Drizzle schema
10
- export type Price = typeof prices.$inferSelect;
11
- export type PriceInsert = typeof prices.$inferInsert;
12
- export type PriceModifier = typeof priceModifiers.$inferSelect;
13
- export type PriceModifierInsert = typeof priceModifiers.$inferInsert;
14
-
15
- /**
16
- * PricingRepository provides type-safe database operations for pricing.
17
- *
18
- * This repository manages prices and price modifiers.
19
- * All methods support an optional TxContext parameter for transaction participation.
20
- */
21
- export class PricingRepository {
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
- // Prices
30
- // ─────────────────────────────────────────────────────────────────────────────
31
-
32
- async findPriceById(orgId: string, id: string, ctx?: TxContext): Promise<Price | undefined> {
33
- const db = this.getDb(ctx);
34
- const rows = await db.select().from(prices).where(
35
- and(eq(prices.organizationId, orgId), eq(prices.id, id)),
36
- );
37
- return rows[0];
38
- }
39
-
40
- async findPricesByEntityId(
41
- entityId: string,
42
- ctx?: TxContext,
43
- ): Promise<Price[]> {
44
- const db = this.getDb(ctx);
45
- return db.select().from(prices).where(eq(prices.entityId, entityId));
46
- }
47
-
48
- async findPricesByEntityAndVariant(
49
- entityId: string,
50
- variantId: string | null,
51
- ctx?: TxContext,
52
- ): Promise<Price[]> {
53
- const db = this.getDb(ctx);
54
-
55
- if (variantId === null) {
56
- return db
57
- .select()
58
- .from(prices)
59
- .where(and(eq(prices.entityId, entityId), isNull(prices.variantId)));
60
- }
61
-
62
- return db
63
- .select()
64
- .from(prices)
65
- .where(
66
- and(eq(prices.entityId, entityId), eq(prices.variantId, variantId)),
67
- );
68
- }
69
-
70
- async findActivePrices(
71
- entityId: string,
72
- currency: string,
73
- variantId?: string,
74
- customerGroupId?: string,
75
- quantity?: number,
76
- ctx?: TxContext,
77
- ): Promise<Price[]> {
78
- const db = this.getDb(ctx);
79
- const now = new Date();
80
-
81
- const rows = await db
82
- .select()
83
- .from(prices)
84
- .where(
85
- and(
86
- eq(prices.entityId, entityId),
87
- eq(prices.currency, currency),
88
- or(isNull(prices.validFrom), lte(prices.validFrom, now)),
89
- or(isNull(prices.validUntil), gte(prices.validUntil, now)),
90
- ),
91
- );
92
-
93
- // Filter by variantId
94
- let filtered = rows.filter((p) => {
95
- if (variantId === undefined) return p.variantId === null;
96
- return p.variantId === variantId || p.variantId === null;
97
- });
98
-
99
- // Filter by customerGroupId
100
- if (customerGroupId) {
101
- filtered = filtered.filter(
102
- (p) =>
103
- p.customerGroupId === null || p.customerGroupId === customerGroupId,
104
- );
105
- } else {
106
- filtered = filtered.filter((p) => p.customerGroupId === null);
107
- }
108
-
109
- // Filter by quantity
110
- if (quantity !== undefined) {
111
- filtered = filtered.filter((p) => {
112
- const minOk = p.minQuantity === null || quantity >= p.minQuantity;
113
- const maxOk = p.maxQuantity === null || quantity <= p.maxQuantity;
114
- return minOk && maxOk;
115
- });
116
- }
117
-
118
- return filtered;
119
- }
120
-
121
- async createPrice(data: PriceInsert, ctx?: TxContext): Promise<Price> {
122
- const db = this.getDb(ctx);
123
- const rows = await db.insert(prices).values(data).returning();
124
- return rows[0]!;
125
- }
126
-
127
- async updatePrice(
128
- id: string,
129
- data: Partial<Omit<PriceInsert, "id">>,
130
- ctx?: TxContext,
131
- ): Promise<Price | undefined> {
132
- const db = this.getDb(ctx);
133
- const rows = await db
134
- .update(prices)
135
- .set({ ...data, updatedAt: new Date() })
136
- .where(eq(prices.id, id))
137
- .returning();
138
- return rows[0];
139
- }
140
-
141
- async deletePrice(id: string, ctx?: TxContext): Promise<boolean> {
142
- const db = this.getDb(ctx);
143
- const result = await db.delete(prices).where(eq(prices.id, id)).returning();
144
- return result.length > 0;
145
- }
146
-
147
- async deletePricesByEntityId(
148
- entityId: string,
149
- ctx?: TxContext,
150
- ): Promise<void> {
151
- const db = this.getDb(ctx);
152
- await db.delete(prices).where(eq(prices.entityId, entityId));
153
- }
154
-
155
- // ─────────────────────────────────────────────────────────────────────────────
156
- // Price Modifiers
157
- // ─────────────────────────────────────────────────────────────────────────────
158
-
159
- async findModifierById(
160
- orgId: string,
161
- id: string,
162
- ctx?: TxContext,
163
- ): Promise<PriceModifier | undefined> {
164
- const db = this.getDb(ctx);
165
- const rows = await db
166
- .select()
167
- .from(priceModifiers)
168
- .where(and(eq(priceModifiers.organizationId, orgId), eq(priceModifiers.id, id)));
169
- return rows[0];
170
- }
171
-
172
- async findModifiersByEntityId(
173
- entityId: string,
174
- ctx?: TxContext,
175
- ): Promise<PriceModifier[]> {
176
- const db = this.getDb(ctx);
177
- return db
178
- .select()
179
- .from(priceModifiers)
180
- .where(eq(priceModifiers.entityId, entityId));
181
- }
182
-
183
- async findActiveModifiers(
184
- entityId: string,
185
- variantId?: string,
186
- customerGroupId?: string,
187
- currency?: string,
188
- quantity?: number,
189
- ctx?: TxContext,
190
- ): Promise<PriceModifier[]> {
191
- const db = this.getDb(ctx);
192
- const now = new Date();
193
-
194
- const rows = await db
195
- .select()
196
- .from(priceModifiers)
197
- .where(
198
- and(
199
- or(
200
- isNull(priceModifiers.entityId),
201
- eq(priceModifiers.entityId, entityId),
202
- ),
203
- or(
204
- isNull(priceModifiers.validFrom),
205
- lte(priceModifiers.validFrom, now),
206
- ),
207
- or(
208
- isNull(priceModifiers.validUntil),
209
- gte(priceModifiers.validUntil, now),
210
- ),
211
- ),
212
- );
213
-
214
- // Filter by variantId
215
- let filtered = rows.filter((m) => {
216
- if (m.variantId === null) return true;
217
- return m.variantId === variantId;
218
- });
219
-
220
- // Filter by customerGroupId
221
- if (customerGroupId) {
222
- filtered = filtered.filter(
223
- (m) =>
224
- m.customerGroupId === null || m.customerGroupId === customerGroupId,
225
- );
226
- } else {
227
- filtered = filtered.filter((m) => m.customerGroupId === null);
228
- }
229
-
230
- // Filter by currency
231
- if (currency) {
232
- filtered = filtered.filter(
233
- (m) => m.currency === null || m.currency === currency,
234
- );
235
- }
236
-
237
- // Filter by quantity
238
- if (quantity !== undefined) {
239
- filtered = filtered.filter((m) => {
240
- const minOk = m.minQuantity === null || quantity >= m.minQuantity;
241
- const maxOk = m.maxQuantity === null || quantity <= m.maxQuantity;
242
- return minOk && maxOk;
243
- });
244
- }
245
-
246
- // Sort by priority (lower number = higher priority)
247
- return filtered.sort((a, b) => a.priority - b.priority);
248
- }
249
-
250
- async createModifier(
251
- data: PriceModifierInsert,
252
- ctx?: TxContext,
253
- ): Promise<PriceModifier> {
254
- const db = this.getDb(ctx);
255
- const rows = await db.insert(priceModifiers).values(data).returning();
256
- return rows[0]!;
257
- }
258
-
259
- async updateModifier(
260
- id: string,
261
- data: Partial<Omit<PriceModifierInsert, "id">>,
262
- ctx?: TxContext,
263
- ): Promise<PriceModifier | undefined> {
264
- const db = this.getDb(ctx);
265
- const rows = await db
266
- .update(priceModifiers)
267
- .set({ ...data, updatedAt: new Date() })
268
- .where(eq(priceModifiers.id, id))
269
- .returning();
270
- return rows[0];
271
- }
272
-
273
- async deleteModifier(id: string, ctx?: TxContext): Promise<boolean> {
274
- const db = this.getDb(ctx);
275
- const result = await db
276
- .delete(priceModifiers)
277
- .where(eq(priceModifiers.id, id))
278
- .returning();
279
- return result.length > 0;
280
- }
281
-
282
- async deleteModifiersByEntityId(
283
- entityId: string,
284
- ctx?: TxContext,
285
- ): Promise<void> {
286
- const db = this.getDb(ctx);
287
- await db
288
- .delete(priceModifiers)
289
- .where(eq(priceModifiers.entityId, entityId));
290
- }
291
- }