@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,327 +0,0 @@
1
- import { eq, and } 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 {
8
- customers,
9
- customerAddresses,
10
- customerGroups,
11
- customerGroupMembers,
12
- } from "../schema.js";
13
-
14
- // Infer types from Drizzle schema
15
- export type Customer = typeof customers.$inferSelect;
16
- export type CustomerInsert = typeof customers.$inferInsert;
17
- export type CustomerAddress = typeof customerAddresses.$inferSelect;
18
- export type CustomerAddressInsert = typeof customerAddresses.$inferInsert;
19
- export type CustomerGroup = typeof customerGroups.$inferSelect;
20
- export type CustomerGroupInsert = typeof customerGroups.$inferInsert;
21
- export type CustomerGroupMember = typeof customerGroupMembers.$inferSelect;
22
- export type CustomerGroupMemberInsert =
23
- typeof customerGroupMembers.$inferInsert;
24
-
25
- /**
26
- * CustomersRepository provides type-safe database operations for customers.
27
- *
28
- * This repository manages customers, addresses, groups, and group memberships.
29
- * All methods support an optional TxContext parameter for transaction participation.
30
- */
31
- export class CustomersRepository {
32
- constructor(private readonly db: DrizzleDatabase) {}
33
-
34
- private getDb(ctx?: TxContext): DbOrTx {
35
- return (ctx?.tx as DbOrTx | undefined) ?? this.db;
36
- }
37
-
38
- // ─────────────────────────────────────────────────────────────────────────────
39
- // Customers
40
- // ─────────────────────────────────────────────────────────────────────────────
41
-
42
- async findById(orgId: string, id: string, ctx?: TxContext): Promise<Customer | undefined> {
43
- const db = this.getDb(ctx);
44
- const rows = await db
45
- .select()
46
- .from(customers)
47
- .where(and(eq(customers.organizationId, orgId), eq(customers.id, id)));
48
- return rows[0];
49
- }
50
-
51
- async findByUserId(
52
- orgId: string,
53
- userId: string,
54
- ctx?: TxContext,
55
- ): Promise<Customer | undefined> {
56
- const db = this.getDb(ctx);
57
- const rows = await db
58
- .select()
59
- .from(customers)
60
- .where(and(eq(customers.organizationId, orgId), eq(customers.userId, userId)));
61
- return rows[0];
62
- }
63
-
64
- async findByEmail(
65
- orgId: string,
66
- email: string,
67
- ctx?: TxContext,
68
- ): Promise<Customer | undefined> {
69
- const db = this.getDb(ctx);
70
- const rows = await db
71
- .select()
72
- .from(customers)
73
- .where(and(eq(customers.organizationId, orgId), eq(customers.email, email)));
74
- return rows[0];
75
- }
76
-
77
- async findByPosPin(
78
- orgId: string,
79
- hashedPin: string,
80
- ctx?: TxContext,
81
- ): Promise<Customer | undefined> {
82
- const db = this.getDb(ctx);
83
- const rows = await db
84
- .select()
85
- .from(customers)
86
- .where(and(eq(customers.organizationId, orgId), eq(customers.posOperatorPin, hashedPin)));
87
- return rows[0];
88
- }
89
-
90
- async findAll(orgId: string, ctx?: TxContext): Promise<Customer[]> {
91
- const db = this.getDb(ctx);
92
- return db
93
- .select()
94
- .from(customers)
95
- .where(eq(customers.organizationId, orgId));
96
- }
97
-
98
- async create(data: CustomerInsert, ctx?: TxContext): Promise<Customer> {
99
- const db = this.getDb(ctx);
100
- const rows = await db.insert(customers).values(data).returning();
101
- return rows[0]!;
102
- }
103
-
104
- async update(
105
- id: string,
106
- data: Partial<Omit<CustomerInsert, "id">>,
107
- ctx?: TxContext,
108
- ): Promise<Customer | undefined> {
109
- const db = this.getDb(ctx);
110
- const rows = await db
111
- .update(customers)
112
- .set({ ...data, updatedAt: new Date() })
113
- .where(eq(customers.id, id))
114
- .returning();
115
- return rows[0];
116
- }
117
-
118
- async delete(id: string, ctx?: TxContext): Promise<boolean> {
119
- const db = this.getDb(ctx);
120
- const result = await db
121
- .delete(customers)
122
- .where(eq(customers.id, id))
123
- .returning();
124
- return result.length > 0;
125
- }
126
-
127
- // ─────────────────────────────────────────────────────────────────────────────
128
- // Customer Addresses
129
- // ─────────────────────────────────────────────────────────────────────────────
130
-
131
- async findAddressById(
132
- id: string,
133
- ctx?: TxContext,
134
- ): Promise<CustomerAddress | undefined> {
135
- const db = this.getDb(ctx);
136
- const rows = await db
137
- .select()
138
- .from(customerAddresses)
139
- .where(eq(customerAddresses.id, id));
140
- return rows[0];
141
- }
142
-
143
- async findAddressesByCustomerId(
144
- customerId: string,
145
- ctx?: TxContext,
146
- ): Promise<CustomerAddress[]> {
147
- const db = this.getDb(ctx);
148
- return db
149
- .select()
150
- .from(customerAddresses)
151
- .where(eq(customerAddresses.customerId, customerId));
152
- }
153
-
154
- async findDefaultAddress(
155
- customerId: string,
156
- type: "shipping" | "billing",
157
- ctx?: TxContext,
158
- ): Promise<CustomerAddress | undefined> {
159
- const db = this.getDb(ctx);
160
- const rows = await db
161
- .select()
162
- .from(customerAddresses)
163
- .where(
164
- and(
165
- eq(customerAddresses.customerId, customerId),
166
- eq(customerAddresses.type, type),
167
- eq(customerAddresses.isDefault, true),
168
- ),
169
- );
170
- return rows[0];
171
- }
172
-
173
- async createAddress(
174
- data: CustomerAddressInsert,
175
- ctx?: TxContext,
176
- ): Promise<CustomerAddress> {
177
- const db = this.getDb(ctx);
178
- const rows = await db.insert(customerAddresses).values(data).returning();
179
- return rows[0]!;
180
- }
181
-
182
- async updateAddress(
183
- id: string,
184
- data: Partial<Omit<CustomerAddressInsert, "id">>,
185
- ctx?: TxContext,
186
- ): Promise<CustomerAddress | undefined> {
187
- const db = this.getDb(ctx);
188
- const rows = await db
189
- .update(customerAddresses)
190
- .set(data)
191
- .where(eq(customerAddresses.id, id))
192
- .returning();
193
- return rows[0];
194
- }
195
-
196
- async deleteAddress(id: string, ctx?: TxContext): Promise<boolean> {
197
- const db = this.getDb(ctx);
198
- const result = await db
199
- .delete(customerAddresses)
200
- .where(eq(customerAddresses.id, id))
201
- .returning();
202
- return result.length > 0;
203
- }
204
-
205
- async deleteAddressesByCustomerId(
206
- customerId: string,
207
- ctx?: TxContext,
208
- ): Promise<void> {
209
- const db = this.getDb(ctx);
210
- await db
211
- .delete(customerAddresses)
212
- .where(eq(customerAddresses.customerId, customerId));
213
- }
214
-
215
- // ─────────────────────────────────────────────────────────────────────────────
216
- // Customer Groups
217
- // ─────────────────────────────────────────────────────────────────────────────
218
-
219
- async findGroupById(
220
- orgId: string,
221
- id: string,
222
- ctx?: TxContext,
223
- ): Promise<CustomerGroup | undefined> {
224
- const db = this.getDb(ctx);
225
- const rows = await db
226
- .select()
227
- .from(customerGroups)
228
- .where(and(eq(customerGroups.organizationId, orgId), eq(customerGroups.id, id)));
229
- return rows[0];
230
- }
231
-
232
- async findGroupByName(
233
- orgId: string,
234
- name: string,
235
- ctx?: TxContext,
236
- ): Promise<CustomerGroup | undefined> {
237
- const db = this.getDb(ctx);
238
- const rows = await db
239
- .select()
240
- .from(customerGroups)
241
- .where(and(eq(customerGroups.organizationId, orgId), eq(customerGroups.name, name)));
242
- return rows[0];
243
- }
244
-
245
- async findAllGroups(orgId: string, ctx?: TxContext): Promise<CustomerGroup[]> {
246
- const db = this.getDb(ctx);
247
- return db.select().from(customerGroups).where(eq(customerGroups.organizationId, orgId));
248
- }
249
-
250
- async createGroup(
251
- data: CustomerGroupInsert,
252
- ctx?: TxContext,
253
- ): Promise<CustomerGroup> {
254
- const db = this.getDb(ctx);
255
- const rows = await db.insert(customerGroups).values(data).returning();
256
- return rows[0]!;
257
- }
258
-
259
- async updateGroup(
260
- id: string,
261
- data: Partial<Omit<CustomerGroupInsert, "id">>,
262
- ctx?: TxContext,
263
- ): Promise<CustomerGroup | undefined> {
264
- const db = this.getDb(ctx);
265
- const rows = await db
266
- .update(customerGroups)
267
- .set(data)
268
- .where(eq(customerGroups.id, id))
269
- .returning();
270
- return rows[0];
271
- }
272
-
273
- async deleteGroup(id: string, ctx?: TxContext): Promise<boolean> {
274
- const db = this.getDb(ctx);
275
- const result = await db
276
- .delete(customerGroups)
277
- .where(eq(customerGroups.id, id))
278
- .returning();
279
- return result.length > 0;
280
- }
281
-
282
- // ─────────────────────────────────────────────────────────────────────────────
283
- // Customer Group Members
284
- // ─────────────────────────────────────────────────────────────────────────────
285
-
286
- async findGroupsByCustomerId(
287
- customerId: string,
288
- ctx?: TxContext,
289
- ): Promise<string[]> {
290
- const db = this.getDb(ctx);
291
- const rows = await db
292
- .select({ groupId: customerGroupMembers.groupId })
293
- .from(customerGroupMembers)
294
- .where(eq(customerGroupMembers.customerId, customerId));
295
- return rows.map((r) => r.groupId);
296
- }
297
-
298
- async addToGroup(
299
- customerId: string,
300
- groupId: string,
301
- ctx?: TxContext,
302
- ): Promise<void> {
303
- const db = this.getDb(ctx);
304
- await db
305
- .insert(customerGroupMembers)
306
- .values({ customerId, groupId })
307
- .onConflictDoNothing();
308
- }
309
-
310
- async removeFromGroup(
311
- customerId: string,
312
- groupId: string,
313
- ctx?: TxContext,
314
- ): Promise<boolean> {
315
- const db = this.getDb(ctx);
316
- const result = await db
317
- .delete(customerGroupMembers)
318
- .where(
319
- and(
320
- eq(customerGroupMembers.customerId, customerId),
321
- eq(customerGroupMembers.groupId, groupId),
322
- ),
323
- )
324
- .returning();
325
- return result.length > 0;
326
- }
327
- }
@@ -1,64 +0,0 @@
1
- import { boolean, index, jsonb, pgTable, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core";
2
- import { organization } from "../../auth/auth-schema.js";
3
-
4
- export const customers = pgTable("customers", {
5
- id: uuid("id").defaultRandom().primaryKey(),
6
- organizationId: text("organization_id").notNull().references(() => organization.id, { onDelete: "cascade" }),
7
- userId: text("user_id").notNull(),
8
- email: text("email"),
9
- phone: text("phone"),
10
- firstName: text("first_name"),
11
- lastName: text("last_name"),
12
- metadata: jsonb("metadata").$type<Record<string, unknown>>().default({}),
13
- createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
14
- updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
15
- posOperatorPin: text("pos_operator_pin"),
16
- }, (table) => ({
17
- orgIdx: index("idx_customers_org").on(table.organizationId),
18
- orgUserIdUnique: uniqueIndex("customers_org_user_id_unique").on(table.organizationId, table.userId),
19
- orgEmailUnique: uniqueIndex("customers_org_email_unique").on(table.organizationId, table.email),
20
- }));
21
-
22
- export const customerAddresses = pgTable("customer_addresses", {
23
- id: uuid("id").defaultRandom().primaryKey(),
24
- customerId: uuid("customer_id")
25
- .references(() => customers.id, { onDelete: "cascade" })
26
- .notNull(),
27
- type: text("type", { enum: ["shipping", "billing"] }).notNull(),
28
- isDefault: boolean("is_default").notNull().default(false),
29
- firstName: text("first_name").notNull(),
30
- lastName: text("last_name").notNull(),
31
- line1: text("line1").notNull(),
32
- line2: text("line2"),
33
- city: text("city").notNull(),
34
- state: text("state"),
35
- postalCode: text("postal_code"),
36
- country: text("country").notNull(),
37
- phone: text("phone"),
38
- }, (table) => [
39
- index("idx_customer_addresses_customer_id").on(table.customerId),
40
- ]);
41
-
42
- export const customerGroups = pgTable("customer_groups", {
43
- id: uuid("id").defaultRandom().primaryKey(),
44
- organizationId: text("organization_id").notNull().references(() => organization.id, { onDelete: "cascade" }),
45
- name: text("name").notNull(),
46
- description: text("description"),
47
- metadata: jsonb("metadata").$type<Record<string, unknown>>().default({}),
48
- }, (table) => ({
49
- orgIdx: index("idx_customer_groups_org").on(table.organizationId),
50
- orgNameUnique: uniqueIndex("customer_groups_org_name_unique").on(table.organizationId, table.name),
51
- }));
52
-
53
- export const customerGroupMembers = pgTable("customer_group_members", {
54
- customerId: uuid("customer_id")
55
- .references(() => customers.id, { onDelete: "cascade" })
56
- .notNull(),
57
- groupId: uuid("group_id")
58
- .references(() => customerGroups.id, { onDelete: "cascade" })
59
- .notNull(),
60
- }, (table) => [
61
- index("idx_group_members_customer_id").on(table.customerId),
62
- index("idx_group_members_group_id").on(table.groupId),
63
- uniqueIndex("customer_group_members_customer_group_unique").on(table.customerId, table.groupId),
64
- ]);
@@ -1,171 +0,0 @@
1
- import { resolveOrgId } from "../../auth/org.js";
2
- import type { Actor } from "../../auth/types.js";
3
- import { CommerceNotFoundError } from "../../kernel/errors.js";
4
- import { Err, Ok, type Result } from "../../kernel/result.js";
5
- import type { TxContext } from "../../kernel/database/tx-context.js";
6
- import type {
7
- CustomersRepository,
8
- Customer,
9
- CustomerAddress,
10
- CustomerAddressInsert,
11
- } from "./repository/index.js";
12
-
13
- interface CustomerServiceDeps {
14
- repository: CustomersRepository;
15
- }
16
-
17
- export class CustomerService {
18
- private readonly repo: CustomersRepository;
19
-
20
- constructor(private deps: CustomerServiceDeps) {
21
- this.repo = deps.repository;
22
- }
23
-
24
- private async getOrCreateByUserId(
25
- orgId: string,
26
- userId: string,
27
- ctx?: TxContext,
28
- ): Promise<Customer> {
29
- const existing = await this.repo.findByUserId(orgId, userId, ctx);
30
- if (existing) {
31
- return existing;
32
- }
33
-
34
- const customer = await this.repo.create(
35
- {
36
- organizationId: orgId,
37
- userId,
38
- metadata: {},
39
- },
40
- ctx,
41
- );
42
-
43
- return customer;
44
- }
45
-
46
- async getById(
47
- id: string,
48
- actor?: Actor | null,
49
- ctx?: TxContext,
50
- ): Promise<Result<Customer>> {
51
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
52
- const customer = await this.repo.findById(orgId, id, ctx);
53
- if (!customer) return Err(new CommerceNotFoundError("Customer not found."));
54
- return Ok(customer);
55
- }
56
-
57
- async getByUserId(
58
- userId: string,
59
- actor?: Actor | null,
60
- ctx?: TxContext,
61
- ): Promise<Result<Customer>> {
62
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
63
- const customer = await this.getOrCreateByUserId(orgId, userId, ctx);
64
- return Ok(customer);
65
- }
66
-
67
- async updateByUserId(
68
- userId: string,
69
- updates: Partial<
70
- Omit<Customer, "id" | "userId" | "createdAt" | "updatedAt">
71
- >,
72
- actor?: Actor | null,
73
- ctx?: TxContext,
74
- ): Promise<Result<Customer>> {
75
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
76
- const customer = await this.getOrCreateByUserId(orgId, userId, ctx);
77
-
78
- const updated = await this.repo.update(customer.id, updates, ctx);
79
- if (!updated) {
80
- return Err(new CommerceNotFoundError("Customer not found."));
81
- }
82
-
83
- return Ok(updated);
84
- }
85
-
86
- async getAddresses(
87
- userId: string,
88
- actor?: Actor | null,
89
- ctx?: TxContext,
90
- ): Promise<Result<CustomerAddress[]>> {
91
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
92
- const customer = await this.getOrCreateByUserId(orgId, userId, ctx);
93
- const addresses = await this.repo.findAddressesByCustomerId(
94
- customer.id,
95
- ctx,
96
- );
97
- return Ok(addresses);
98
- }
99
-
100
- async addAddress(
101
- userId: string,
102
- input: Omit<CustomerAddressInsert, "id" | "customerId">,
103
- actor?: Actor | null,
104
- ctx?: TxContext,
105
- ): Promise<Result<CustomerAddress>> {
106
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
107
- const customer = await this.getOrCreateByUserId(orgId, userId, ctx);
108
-
109
- // If new address is default, unset other defaults of same type
110
- if (input.isDefault) {
111
- const addresses = await this.repo.findAddressesByCustomerId(
112
- customer.id,
113
- ctx,
114
- );
115
- for (const addr of addresses) {
116
- if (addr.type === input.type && addr.isDefault) {
117
- await this.repo.updateAddress(addr.id, { isDefault: false }, ctx);
118
- }
119
- }
120
- }
121
-
122
- const address = await this.repo.createAddress(
123
- {
124
- ...input,
125
- customerId: customer.id,
126
- },
127
- ctx,
128
- );
129
-
130
- return Ok(address);
131
- }
132
-
133
- async deleteAddress(
134
- userId: string,
135
- addressId: string,
136
- actor?: Actor | null,
137
- ctx?: TxContext,
138
- ): Promise<Result<void>> {
139
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
140
- const customer = await this.getOrCreateByUserId(orgId, userId, ctx);
141
- const addresses = await this.repo.findAddressesByCustomerId(
142
- customer.id,
143
- ctx,
144
- );
145
-
146
- const addressExists = addresses.some((addr) => addr.id === addressId);
147
- if (!addressExists) {
148
- return Err(new CommerceNotFoundError("Address not found."));
149
- }
150
-
151
- await this.repo.deleteAddress(addressId, ctx);
152
- return Ok(undefined);
153
- }
154
-
155
- async findPOSOperatorByPin(
156
- hashedPin: string,
157
- actor?: Actor | null,
158
- ctx?: TxContext,
159
- ): Promise<{ id: string; name: string } | null> {
160
- const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
161
- const user = await this.repo.findByPosPin(orgId, hashedPin, ctx);
162
- if (!user) return null;
163
- return {
164
- id: user.userId,
165
- name:
166
- [user.firstName, user.lastName].filter(Boolean).join(" ") ||
167
- user.email ||
168
- "POS Operator",
169
- };
170
- }
171
- }