@porulle/core 0.6.0 → 0.8.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 (301) hide show
  1. package/dist/auth/setup.d.ts.map +1 -1
  2. package/dist/auth/setup.js +1 -0
  3. package/dist/config/defaults.d.ts.map +1 -1
  4. package/dist/config/defaults.js +5 -0
  5. package/dist/config/types.d.ts +17 -1
  6. package/dist/config/types.d.ts.map +1 -1
  7. package/dist/generated/plugin-manifest.d.ts +3 -0
  8. package/dist/generated/plugin-manifest.d.ts.map +1 -1
  9. package/dist/generated/plugin-manifest.js +1 -0
  10. package/dist/hooks/checkout.d.ts.map +1 -1
  11. package/dist/hooks/checkout.js +16 -1
  12. package/dist/index.d.ts +1 -0
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +1 -0
  15. package/dist/interfaces/rest/index.d.ts.map +1 -1
  16. package/dist/interfaces/rest/index.js +12 -0
  17. package/dist/interfaces/rest/routes/admin/staff.d.ts +12 -0
  18. package/dist/interfaces/rest/routes/admin/staff.d.ts.map +1 -0
  19. package/dist/interfaces/rest/routes/admin/staff.js +172 -0
  20. package/dist/interfaces/rest/routes/analytics.d.ts +5 -0
  21. package/dist/interfaces/rest/routes/analytics.d.ts.map +1 -0
  22. package/dist/interfaces/rest/routes/analytics.js +24 -0
  23. package/dist/interfaces/rest/routes/carts.d.ts.map +1 -1
  24. package/dist/interfaces/rest/routes/carts.js +39 -2
  25. package/dist/interfaces/rest/routes/catalog.d.ts.map +1 -1
  26. package/dist/interfaces/rest/routes/catalog.js +17 -1
  27. package/dist/interfaces/rest/routes/checkout.d.ts.map +1 -1
  28. package/dist/interfaces/rest/routes/checkout.js +17 -0
  29. package/dist/interfaces/rest/routes/documents.d.ts +10 -0
  30. package/dist/interfaces/rest/routes/documents.d.ts.map +1 -0
  31. package/dist/interfaces/rest/routes/documents.js +40 -0
  32. package/dist/interfaces/rest/routes/orders.d.ts.map +1 -1
  33. package/dist/interfaces/rest/routes/orders.js +122 -1
  34. package/dist/interfaces/rest/routes/pricing.d.ts.map +1 -1
  35. package/dist/interfaces/rest/routes/pricing.js +36 -1
  36. package/dist/interfaces/rest/routes/settings.d.ts +5 -0
  37. package/dist/interfaces/rest/routes/settings.d.ts.map +1 -0
  38. package/dist/interfaces/rest/routes/settings.js +50 -0
  39. package/dist/interfaces/rest/routes/shipping.d.ts +5 -0
  40. package/dist/interfaces/rest/routes/shipping.d.ts.map +1 -0
  41. package/dist/interfaces/rest/routes/shipping.js +68 -0
  42. package/dist/interfaces/rest/routes/tax.d.ts +5 -0
  43. package/dist/interfaces/rest/routes/tax.d.ts.map +1 -0
  44. package/dist/interfaces/rest/routes/tax.js +68 -0
  45. package/dist/interfaces/rest/schemas/admin-staff.d.ts +899 -0
  46. package/dist/interfaces/rest/schemas/admin-staff.d.ts.map +1 -0
  47. package/dist/interfaces/rest/schemas/admin-staff.js +98 -0
  48. package/dist/interfaces/rest/schemas/analytics.d.ts +252 -0
  49. package/dist/interfaces/rest/schemas/analytics.d.ts.map +1 -0
  50. package/dist/interfaces/rest/schemas/analytics.js +33 -0
  51. package/dist/interfaces/rest/schemas/carts.d.ts +656 -0
  52. package/dist/interfaces/rest/schemas/carts.d.ts.map +1 -1
  53. package/dist/interfaces/rest/schemas/carts.js +38 -0
  54. package/dist/interfaces/rest/schemas/catalog.d.ts +1230 -2
  55. package/dist/interfaces/rest/schemas/catalog.d.ts.map +1 -1
  56. package/dist/interfaces/rest/schemas/catalog.js +48 -0
  57. package/dist/interfaces/rest/schemas/checkout.d.ts +2 -0
  58. package/dist/interfaces/rest/schemas/checkout.d.ts.map +1 -1
  59. package/dist/interfaces/rest/schemas/checkout.js +4 -0
  60. package/dist/interfaces/rest/schemas/customer-portal.d.ts +35 -0
  61. package/dist/interfaces/rest/schemas/customer-portal.d.ts.map +1 -1
  62. package/dist/interfaces/rest/schemas/documents.d.ts +509 -0
  63. package/dist/interfaces/rest/schemas/documents.d.ts.map +1 -0
  64. package/dist/interfaces/rest/schemas/documents.js +65 -0
  65. package/dist/interfaces/rest/schemas/orders.d.ts +4467 -7
  66. package/dist/interfaces/rest/schemas/orders.d.ts.map +1 -1
  67. package/dist/interfaces/rest/schemas/orders.js +330 -0
  68. package/dist/interfaces/rest/schemas/pricing.d.ts +405 -0
  69. package/dist/interfaces/rest/schemas/pricing.d.ts.map +1 -1
  70. package/dist/interfaces/rest/schemas/pricing.js +70 -0
  71. package/dist/interfaces/rest/schemas/responses.d.ts +156 -0
  72. package/dist/interfaces/rest/schemas/responses.d.ts.map +1 -1
  73. package/dist/interfaces/rest/schemas/search.d.ts +17 -0
  74. package/dist/interfaces/rest/schemas/search.d.ts.map +1 -1
  75. package/dist/interfaces/rest/schemas/settings.d.ts +397 -0
  76. package/dist/interfaces/rest/schemas/settings.d.ts.map +1 -0
  77. package/dist/interfaces/rest/schemas/settings.js +79 -0
  78. package/dist/interfaces/rest/schemas/shipping.d.ts +1093 -0
  79. package/dist/interfaces/rest/schemas/shipping.d.ts.map +1 -0
  80. package/dist/interfaces/rest/schemas/shipping.js +146 -0
  81. package/dist/interfaces/rest/schemas/tax.d.ts +1074 -0
  82. package/dist/interfaces/rest/schemas/tax.d.ts.map +1 -0
  83. package/dist/interfaces/rest/schemas/tax.js +132 -0
  84. package/dist/kernel/database/schema.d.ts +4 -0
  85. package/dist/kernel/database/schema.d.ts.map +1 -1
  86. package/dist/kernel/database/schema.js +8 -0
  87. package/dist/kernel/error-mapper.d.ts.map +1 -1
  88. package/dist/kernel/error-mapper.js +1 -0
  89. package/dist/kernel/errors.d.ts +5 -0
  90. package/dist/kernel/errors.d.ts.map +1 -1
  91. package/dist/kernel/errors.js +9 -0
  92. package/dist/kernel/hooks/types.d.ts +1 -1
  93. package/dist/kernel/hooks/types.d.ts.map +1 -1
  94. package/dist/kernel/plugin/manifest.d.ts +23 -0
  95. package/dist/kernel/plugin/manifest.d.ts.map +1 -1
  96. package/dist/kernel/plugin/manifest.js +17 -2
  97. package/dist/modules/analytics/module.d.ts.map +1 -1
  98. package/dist/modules/analytics/module.js +6 -0
  99. package/dist/modules/analytics/reports.d.ts +58 -0
  100. package/dist/modules/analytics/reports.d.ts.map +1 -0
  101. package/dist/modules/analytics/reports.js +249 -0
  102. package/dist/modules/analytics/service.d.ts +10 -0
  103. package/dist/modules/analytics/service.d.ts.map +1 -1
  104. package/dist/modules/analytics/service.js +14 -0
  105. package/dist/modules/cart/repository/index.d.ts +7 -0
  106. package/dist/modules/cart/repository/index.d.ts.map +1 -1
  107. package/dist/modules/cart/repository/index.js +25 -1
  108. package/dist/modules/cart/schema.d.ts +17 -0
  109. package/dist/modules/cart/schema.d.ts.map +1 -1
  110. package/dist/modules/cart/schema.js +2 -0
  111. package/dist/modules/cart/schemas.d.ts +1 -0
  112. package/dist/modules/cart/schemas.d.ts.map +1 -1
  113. package/dist/modules/cart/schemas.js +1 -0
  114. package/dist/modules/cart/service.d.ts +32 -0
  115. package/dist/modules/cart/service.d.ts.map +1 -1
  116. package/dist/modules/cart/service.js +58 -0
  117. package/dist/modules/catalog/entity-service.d.ts +21 -0
  118. package/dist/modules/catalog/entity-service.d.ts.map +1 -1
  119. package/dist/modules/catalog/entity-service.js +151 -6
  120. package/dist/modules/catalog/schema.d.ts +34 -0
  121. package/dist/modules/catalog/schema.d.ts.map +1 -1
  122. package/dist/modules/catalog/schema.js +5 -0
  123. package/dist/modules/catalog/schemas.d.ts +15 -1
  124. package/dist/modules/catalog/schemas.d.ts.map +1 -1
  125. package/dist/modules/catalog/schemas.js +17 -1
  126. package/dist/modules/catalog/service.d.ts +43 -1
  127. package/dist/modules/catalog/service.d.ts.map +1 -1
  128. package/dist/modules/catalog/service.js +6 -0
  129. package/dist/modules/documents/module.d.ts +7 -0
  130. package/dist/modules/documents/module.d.ts.map +1 -0
  131. package/dist/modules/documents/module.js +12 -0
  132. package/dist/modules/documents/pdf.d.ts +25 -0
  133. package/dist/modules/documents/pdf.d.ts.map +1 -0
  134. package/dist/modules/documents/pdf.js +80 -0
  135. package/dist/modules/documents/render.d.ts +32 -0
  136. package/dist/modules/documents/render.d.ts.map +1 -0
  137. package/dist/modules/documents/render.js +190 -0
  138. package/dist/modules/documents/repository/index.d.ts +25 -0
  139. package/dist/modules/documents/repository/index.d.ts.map +1 -0
  140. package/dist/modules/documents/repository/index.js +47 -0
  141. package/dist/modules/documents/schema.d.ts +214 -0
  142. package/dist/modules/documents/schema.d.ts.map +1 -0
  143. package/dist/modules/documents/schema.js +42 -0
  144. package/dist/modules/documents/service.d.ts +51 -0
  145. package/dist/modules/documents/service.d.ts.map +1 -0
  146. package/dist/modules/documents/service.js +119 -0
  147. package/dist/modules/fulfillment/service.d.ts +18 -0
  148. package/dist/modules/fulfillment/service.d.ts.map +1 -1
  149. package/dist/modules/fulfillment/service.js +85 -1
  150. package/dist/modules/media/service.d.ts +18 -0
  151. package/dist/modules/media/service.d.ts.map +1 -1
  152. package/dist/modules/media/service.js +26 -0
  153. package/dist/modules/orders/repository/index.d.ts +18 -1
  154. package/dist/modules/orders/repository/index.d.ts.map +1 -1
  155. package/dist/modules/orders/repository/index.js +86 -1
  156. package/dist/modules/orders/schema.d.ts +374 -0
  157. package/dist/modules/orders/schema.d.ts.map +1 -1
  158. package/dist/modules/orders/schema.js +52 -1
  159. package/dist/modules/orders/service.d.ts +102 -2
  160. package/dist/modules/orders/service.d.ts.map +1 -1
  161. package/dist/modules/orders/service.js +494 -23
  162. package/dist/modules/pricing/repository/index.d.ts +5 -0
  163. package/dist/modules/pricing/repository/index.d.ts.map +1 -1
  164. package/dist/modules/pricing/repository/index.js +19 -0
  165. package/dist/modules/pricing/service.d.ts +18 -0
  166. package/dist/modules/pricing/service.d.ts.map +1 -1
  167. package/dist/modules/pricing/service.js +57 -1
  168. package/dist/modules/settings/module.d.ts +6 -0
  169. package/dist/modules/settings/module.d.ts.map +1 -0
  170. package/dist/modules/settings/module.js +11 -0
  171. package/dist/modules/settings/repository/index.d.ts +16 -0
  172. package/dist/modules/settings/repository/index.d.ts.map +1 -0
  173. package/dist/modules/settings/repository/index.js +41 -0
  174. package/dist/modules/settings/schema.d.ts +121 -0
  175. package/dist/modules/settings/schema.d.ts.map +1 -0
  176. package/dist/modules/settings/schema.js +24 -0
  177. package/dist/modules/settings/service.d.ts +26 -0
  178. package/dist/modules/settings/service.d.ts.map +1 -0
  179. package/dist/modules/settings/service.js +51 -0
  180. package/dist/modules/shipping/calculator.d.ts +5 -0
  181. package/dist/modules/shipping/calculator.d.ts.map +1 -1
  182. package/dist/modules/shipping/calculator.js +10 -0
  183. package/dist/modules/shipping/module.d.ts +5 -1
  184. package/dist/modules/shipping/module.d.ts.map +1 -1
  185. package/dist/modules/shipping/module.js +4 -1
  186. package/dist/modules/shipping/repository/index.d.ts +27 -2
  187. package/dist/modules/shipping/repository/index.d.ts.map +1 -1
  188. package/dist/modules/shipping/repository/index.js +105 -1
  189. package/dist/modules/shipping/schema.d.ts +419 -0
  190. package/dist/modules/shipping/schema.d.ts.map +1 -0
  191. package/dist/modules/shipping/schema.js +56 -0
  192. package/dist/modules/shipping/service.d.ts +54 -0
  193. package/dist/modules/shipping/service.d.ts.map +1 -1
  194. package/dist/modules/shipping/service.js +161 -2
  195. package/dist/modules/tax/adapter.d.ts +13 -0
  196. package/dist/modules/tax/adapter.d.ts.map +1 -1
  197. package/dist/modules/tax/module.d.ts +5 -1
  198. package/dist/modules/tax/module.d.ts.map +1 -1
  199. package/dist/modules/tax/module.js +4 -1
  200. package/dist/modules/tax/repository/index.d.ts +38 -2
  201. package/dist/modules/tax/repository/index.d.ts.map +1 -1
  202. package/dist/modules/tax/repository/index.js +138 -1
  203. package/dist/modules/tax/schema.d.ts +355 -0
  204. package/dist/modules/tax/schema.d.ts.map +1 -0
  205. package/dist/modules/tax/schema.js +55 -0
  206. package/dist/modules/tax/service.d.ts +60 -1
  207. package/dist/modules/tax/service.d.ts.map +1 -1
  208. package/dist/modules/tax/service.js +227 -2
  209. package/dist/runtime/kernel-modules.d.ts +15 -2
  210. package/dist/runtime/kernel-modules.d.ts.map +1 -1
  211. package/dist/runtime/kernel-modules.js +4 -0
  212. package/dist/runtime/kernel-types.d.ts +5 -1
  213. package/dist/runtime/kernel-types.d.ts.map +1 -1
  214. package/dist/runtime/kernel-types.js +2 -0
  215. package/dist/runtime/server.d.ts.map +1 -1
  216. package/dist/runtime/server.js +35 -3
  217. package/dist/test-utils/create-plugin-test-app.d.ts +3 -0
  218. package/dist/test-utils/create-plugin-test-app.d.ts.map +1 -1
  219. package/dist/test-utils/create-plugin-test-app.js +15 -3
  220. package/dist/test-utils/rest-api-test-utils.d.ts.map +1 -1
  221. package/dist/test-utils/rest-api-test-utils.js +6 -0
  222. package/package.json +3 -3
  223. package/src/auth/setup.ts +1 -0
  224. package/src/config/defaults.ts +5 -0
  225. package/src/config/types.ts +17 -1
  226. package/src/generated/plugin-capabilities.d.ts +1 -0
  227. package/src/generated/plugin-manifest.ts +1 -0
  228. package/src/generated/plugin-repositories.d.ts +1 -0
  229. package/src/hooks/checkout.ts +25 -3
  230. package/src/index.ts +1 -0
  231. package/src/interfaces/rest/index.ts +12 -0
  232. package/src/interfaces/rest/routes/admin/staff.ts +218 -0
  233. package/src/interfaces/rest/routes/analytics.ts +33 -0
  234. package/src/interfaces/rest/routes/carts.ts +52 -2
  235. package/src/interfaces/rest/routes/catalog.ts +34 -0
  236. package/src/interfaces/rest/routes/checkout.ts +22 -0
  237. package/src/interfaces/rest/routes/documents.ts +61 -0
  238. package/src/interfaces/rest/routes/orders.ts +172 -1
  239. package/src/interfaces/rest/routes/pricing.ts +44 -1
  240. package/src/interfaces/rest/routes/settings.ts +61 -0
  241. package/src/interfaces/rest/routes/shipping.ts +92 -0
  242. package/src/interfaces/rest/routes/tax.ts +90 -0
  243. package/src/interfaces/rest/schemas/admin-staff.ts +110 -0
  244. package/src/interfaces/rest/schemas/analytics.ts +39 -0
  245. package/src/interfaces/rest/schemas/carts.ts +40 -0
  246. package/src/interfaces/rest/schemas/catalog.ts +53 -0
  247. package/src/interfaces/rest/schemas/checkout.ts +4 -0
  248. package/src/interfaces/rest/schemas/documents.ts +72 -0
  249. package/src/interfaces/rest/schemas/orders.ts +361 -0
  250. package/src/interfaces/rest/schemas/pricing.ts +75 -0
  251. package/src/interfaces/rest/schemas/settings.ts +93 -0
  252. package/src/interfaces/rest/schemas/shipping.ts +164 -0
  253. package/src/interfaces/rest/schemas/tax.ts +148 -0
  254. package/src/kernel/database/schema.ts +12 -0
  255. package/src/kernel/error-mapper.ts +1 -0
  256. package/src/kernel/errors.ts +11 -0
  257. package/src/kernel/hooks/types.ts +1 -0
  258. package/src/kernel/plugin/manifest.ts +38 -2
  259. package/src/modules/analytics/module.ts +10 -0
  260. package/src/modules/analytics/reports.ts +311 -0
  261. package/src/modules/analytics/service.ts +23 -0
  262. package/src/modules/cart/repository/index.ts +30 -1
  263. package/src/modules/cart/schema.ts +2 -0
  264. package/src/modules/cart/schemas.ts +1 -0
  265. package/src/modules/cart/service.ts +101 -0
  266. package/src/modules/catalog/entity-service.ts +195 -6
  267. package/src/modules/catalog/schema.ts +5 -0
  268. package/src/modules/catalog/schemas.ts +18 -1
  269. package/src/modules/catalog/service.ts +32 -2
  270. package/src/modules/documents/module.ts +19 -0
  271. package/src/modules/documents/pdf.ts +101 -0
  272. package/src/modules/documents/render.ts +231 -0
  273. package/src/modules/documents/repository/index.ts +68 -0
  274. package/src/modules/documents/schema.ts +44 -0
  275. package/src/modules/documents/service.ts +176 -0
  276. package/src/modules/fulfillment/service.ts +139 -1
  277. package/src/modules/media/service.ts +40 -0
  278. package/src/modules/orders/repository/index.ts +127 -1
  279. package/src/modules/orders/schema.ts +54 -1
  280. package/src/modules/orders/service.ts +739 -24
  281. package/src/modules/pricing/repository/index.ts +34 -0
  282. package/src/modules/pricing/service.ts +100 -1
  283. package/src/modules/settings/module.ts +18 -0
  284. package/src/modules/settings/repository/index.ts +59 -0
  285. package/src/modules/settings/schema.ts +25 -0
  286. package/src/modules/settings/service.ts +82 -0
  287. package/src/modules/shipping/calculator.ts +27 -0
  288. package/src/modules/shipping/module.ts +6 -2
  289. package/src/modules/shipping/repository/index.ts +136 -2
  290. package/src/modules/shipping/schema.ts +58 -0
  291. package/src/modules/shipping/service.ts +260 -1
  292. package/src/modules/tax/adapter.ts +9 -0
  293. package/src/modules/tax/module.ts +6 -2
  294. package/src/modules/tax/repository/index.ts +169 -2
  295. package/src/modules/tax/schema.ts +57 -0
  296. package/src/modules/tax/service.ts +313 -2
  297. package/src/runtime/kernel-modules.ts +4 -0
  298. package/src/runtime/kernel-types.ts +6 -0
  299. package/src/runtime/server.ts +36 -3
  300. package/src/test-utils/create-plugin-test-app.ts +17 -4
  301. package/src/test-utils/rest-api-test-utils.ts +6 -0
@@ -192,6 +192,40 @@ export class PricingRepository {
192
192
  );
193
193
  }
194
194
 
195
+ async findModifiers(
196
+ orgId: string,
197
+ filter?: { entityId?: string; currency?: string; activeAt?: Date },
198
+ ctx?: TxContext,
199
+ ): Promise<PriceModifier[]> {
200
+ const db = this.getDb(ctx);
201
+ const conditions = [eq(priceModifiers.organizationId, orgId)];
202
+ if (filter?.entityId) {
203
+ conditions.push(eq(priceModifiers.entityId, filter.entityId));
204
+ }
205
+ if (filter?.currency) {
206
+ conditions.push(eq(priceModifiers.currency, filter.currency));
207
+ }
208
+ if (filter?.activeAt) {
209
+ conditions.push(
210
+ or(
211
+ isNull(priceModifiers.validFrom),
212
+ lte(priceModifiers.validFrom, filter.activeAt),
213
+ )!,
214
+ );
215
+ conditions.push(
216
+ or(
217
+ isNull(priceModifiers.validUntil),
218
+ gte(priceModifiers.validUntil, filter.activeAt),
219
+ )!,
220
+ );
221
+ }
222
+ const rows = await db
223
+ .select()
224
+ .from(priceModifiers)
225
+ .where(and(...conditions));
226
+ return rows.sort((a, b) => a.priority - b.priority);
227
+ }
228
+
195
229
  async findActiveModifiers(
196
230
  orgId: string,
197
231
  entityId: string,
@@ -129,6 +129,11 @@ function quantityRangeWidth(
129
129
  return Number.MAX_SAFE_INTEGER;
130
130
  }
131
131
 
132
+ function sameNaturalKeyDate(a: Date | null | undefined, b: Date | null | undefined): boolean {
133
+ if (a == null || b == null) return (a ?? null) === (b ?? null);
134
+ return a.getTime() === b.getTime();
135
+ }
136
+
132
137
  function compareBasePriceSpecificity(
133
138
  a: Price,
134
139
  b: Price,
@@ -229,7 +234,30 @@ export class PricingService {
229
234
  validUntil: input.validUntil ?? null,
230
235
  };
231
236
 
232
- const record = await this.repo.createPrice(priceData, ctx);
237
+ // Upsert on the natural key. "Set base price" is idempotent per
238
+ // (org, entity, variant, currency, customerGroup, qty range, validity):
239
+ // a repeat call replaces the existing row's amount instead of appending a
240
+ // shadow row that the resolver would then tie-break on createdAt.
241
+ const existing = (
242
+ await this.repo.findPricesByEntityId(orgId, input.entityId, ctx)
243
+ ).find(
244
+ (p) =>
245
+ p.currency === priceData.currency &&
246
+ p.variantId === (priceData.variantId ?? null) &&
247
+ p.customerGroupId === (priceData.customerGroupId ?? null) &&
248
+ p.minQuantity === (priceData.minQuantity ?? null) &&
249
+ p.maxQuantity === (priceData.maxQuantity ?? null) &&
250
+ sameNaturalKeyDate(p.validFrom, priceData.validFrom ?? null) &&
251
+ sameNaturalKeyDate(p.validUntil, priceData.validUntil ?? null),
252
+ );
253
+
254
+ const record = existing
255
+ ? (await this.repo.updatePrice(
256
+ existing.id,
257
+ { amount: priceData.amount, metadata: priceData.metadata },
258
+ ctx,
259
+ )) ?? existing
260
+ : await this.repo.createPrice(priceData, ctx);
233
261
 
234
262
  const afterHooks = this.deps.hooks.resolve(
235
263
  "pricing.afterCreate",
@@ -305,6 +333,77 @@ export class PricingService {
305
333
  return Ok(modifier);
306
334
  }
307
335
 
336
+ async listModifiers(
337
+ filter?: { entityId?: string; active?: boolean; currency?: string },
338
+ actor?: Actor | null,
339
+ ctx?: TxContext,
340
+ ): Promise<Result<PriceModifier[]>> {
341
+ const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
342
+ const rows = await this.repo.findModifiers(
343
+ orgId,
344
+ {
345
+ ...(filter?.entityId !== undefined ? { entityId: filter.entityId } : {}),
346
+ ...(filter?.currency !== undefined
347
+ ? { currency: normalizeCurrency(filter.currency) }
348
+ : {}),
349
+ ...(filter?.active ? { activeAt: new Date() } : {}),
350
+ },
351
+ ctx,
352
+ );
353
+ return Ok(rows);
354
+ }
355
+
356
+ async updateModifier(
357
+ id: string,
358
+ patch: {
359
+ name?: string | undefined;
360
+ value?: number | undefined;
361
+ priority?: number | undefined;
362
+ validFrom?: Date | null | undefined;
363
+ validUntil?: Date | null | undefined;
364
+ minQuantity?: number | null | undefined;
365
+ maxQuantity?: number | null | undefined;
366
+ metadata?: Record<string, unknown> | undefined;
367
+ },
368
+ actor?: Actor | null,
369
+ ctx?: TxContext,
370
+ ): Promise<Result<PriceModifier>> {
371
+ const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
372
+ const existing = await this.repo.findModifierById(orgId, id, ctx);
373
+ if (!existing) {
374
+ return Err(new CommerceNotFoundError("Price modifier not found."));
375
+ }
376
+ const updated = await this.repo.updateModifier(
377
+ id,
378
+ {
379
+ ...(patch.name !== undefined ? { name: patch.name } : {}),
380
+ ...(patch.value !== undefined ? { value: patch.value } : {}),
381
+ ...(patch.priority !== undefined ? { priority: patch.priority } : {}),
382
+ ...(patch.validFrom !== undefined ? { validFrom: patch.validFrom } : {}),
383
+ ...(patch.validUntil !== undefined ? { validUntil: patch.validUntil } : {}),
384
+ ...(patch.minQuantity !== undefined ? { minQuantity: patch.minQuantity } : {}),
385
+ ...(patch.maxQuantity !== undefined ? { maxQuantity: patch.maxQuantity } : {}),
386
+ ...(patch.metadata !== undefined ? { metadata: patch.metadata } : {}),
387
+ },
388
+ ctx,
389
+ );
390
+ return Ok(updated!);
391
+ }
392
+
393
+ async deleteModifier(
394
+ id: string,
395
+ actor?: Actor | null,
396
+ ctx?: TxContext,
397
+ ): Promise<Result<{ deleted: true }>> {
398
+ const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
399
+ const existing = await this.repo.findModifierById(orgId, id, ctx);
400
+ if (!existing) {
401
+ return Err(new CommerceNotFoundError("Price modifier not found."));
402
+ }
403
+ await this.repo.deleteModifier(id, ctx);
404
+ return Ok({ deleted: true });
405
+ }
406
+
308
407
  async listPrices(
309
408
  filter?: {
310
409
  entityId?: string;
@@ -0,0 +1,18 @@
1
+ import type { DrizzleDatabase } from "../../kernel/database/drizzle-db.js";
2
+ import { defineModule } from "../../kernel/module/index.js";
3
+ import { SettingsRepository } from "./repository/index.js";
4
+ import { storeSettings } from "./schema.js";
5
+ import { SettingsService } from "./service.js";
6
+
7
+ export const settingsModule = defineModule<
8
+ { storeSettings: typeof storeSettings },
9
+ SettingsService,
10
+ Record<string, never>
11
+ >({
12
+ id: "settings",
13
+ schema: () => ({ storeSettings }),
14
+ service: (deps) =>
15
+ new SettingsService({
16
+ repository: new SettingsRepository(deps.db.db as DrizzleDatabase),
17
+ }),
18
+ });
@@ -0,0 +1,59 @@
1
+ import { and, eq } 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 { storeSettings } from "../schema.js";
8
+
9
+ export type StoreSetting = typeof storeSettings.$inferSelect;
10
+
11
+ /**
12
+ * Persistence for org-scoped runtime settings (issue #49).
13
+ */
14
+ export class SettingsRepository {
15
+ constructor(private readonly db: DrizzleDatabase) {}
16
+
17
+ private getDb(ctx?: TxContext): DbOrTx {
18
+ return (ctx?.tx as DbOrTx | undefined) ?? this.db;
19
+ }
20
+
21
+ async findByGroup(
22
+ orgId: string,
23
+ group: string,
24
+ ctx?: TxContext,
25
+ ): Promise<StoreSetting | undefined> {
26
+ const db = this.getDb(ctx);
27
+ const rows = await db
28
+ .select()
29
+ .from(storeSettings)
30
+ .where(and(eq(storeSettings.organizationId, orgId), eq(storeSettings.group, group)));
31
+ return rows[0];
32
+ }
33
+
34
+ async findAll(orgId: string, ctx?: TxContext): Promise<StoreSetting[]> {
35
+ const db = this.getDb(ctx);
36
+ return db
37
+ .select()
38
+ .from(storeSettings)
39
+ .where(eq(storeSettings.organizationId, orgId));
40
+ }
41
+
42
+ async upsert(
43
+ orgId: string,
44
+ group: string,
45
+ value: Record<string, unknown>,
46
+ ctx?: TxContext,
47
+ ): Promise<StoreSetting> {
48
+ const db = this.getDb(ctx);
49
+ const rows = await db
50
+ .insert(storeSettings)
51
+ .values({ organizationId: orgId, group, value })
52
+ .onConflictDoUpdate({
53
+ target: [storeSettings.organizationId, storeSettings.group],
54
+ set: { value, updatedAt: new Date() },
55
+ })
56
+ .returning();
57
+ return rows[0]!;
58
+ }
59
+ }
@@ -0,0 +1,25 @@
1
+ import { index, jsonb, pgTable, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core";
2
+ import { organization } from "../../auth/auth-schema.js";
3
+
4
+ /**
5
+ * Org-scoped runtime settings (issue #49).
6
+ *
7
+ * One row per (organization, group). Groups are shallow JSON objects —
8
+ * `general` (currency, timezone, locale), `branding` (receipt header/footer,
9
+ * store name, logo), `policies` (free-form policy knobs), plus any custom
10
+ * group a plugin namespaces for itself. PATCH semantics are shallow merge
11
+ * with `null` deleting a key.
12
+ */
13
+ export const storeSettings = pgTable("store_settings", {
14
+ id: uuid("id").defaultRandom().primaryKey(),
15
+ organizationId: text("organization_id")
16
+ .notNull()
17
+ .references(() => organization.id, { onDelete: "cascade" }),
18
+ group: text("group").notNull(),
19
+ value: jsonb("value").$type<Record<string, unknown>>().notNull().default({}),
20
+ createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
21
+ updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
22
+ }, (table) => ({
23
+ orgGroupUnique: uniqueIndex("store_settings_org_group_unique").on(table.organizationId, table.group),
24
+ orgIdx: index("idx_store_settings_org").on(table.organizationId),
25
+ }));
@@ -0,0 +1,82 @@
1
+ import { resolveOrgId } from "../../auth/org.js";
2
+ import type { Actor } from "../../auth/types.js";
3
+ import type { TxContext } from "../../kernel/database/tx-context.js";
4
+ import { CommerceValidationError } from "../../kernel/errors.js";
5
+ import { Err, Ok, type Result } from "../../kernel/result.js";
6
+ import type { SettingsRepository } from "./repository/index.js";
7
+
8
+ const GROUP_NAME_RE = /^[a-z][a-z0-9_-]*$/;
9
+
10
+ interface SettingsServiceDeps {
11
+ repository: SettingsRepository;
12
+ }
13
+
14
+ /**
15
+ * Org-scoped runtime settings (issue #49).
16
+ *
17
+ * REST-facing methods resolve the org from the actor; `read()` is the
18
+ * plugin/hook-facing API — an orgId-keyed lookup with no actor requirement so
19
+ * policy values can be consumed at request time inside any service or hook.
20
+ */
21
+ export class SettingsService {
22
+ private repository: SettingsRepository;
23
+
24
+ constructor(deps: SettingsServiceDeps) {
25
+ this.repository = deps.repository;
26
+ }
27
+
28
+ /** Runtime read for plugins/hooks. Unset groups read as `{}`. */
29
+ async read(
30
+ orgId: string,
31
+ group: string,
32
+ ctx?: TxContext,
33
+ ): Promise<Record<string, unknown>> {
34
+ const row = await this.repository.findByGroup(orgId, group, ctx);
35
+ return row?.value ?? {};
36
+ }
37
+
38
+ async getGroup(
39
+ group: string,
40
+ actor?: Actor | null,
41
+ ctx?: TxContext,
42
+ ): Promise<Result<Record<string, unknown>>> {
43
+ const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
44
+ return Ok(await this.read(orgId, group, ctx));
45
+ }
46
+
47
+ async getAll(
48
+ actor?: Actor | null,
49
+ ctx?: TxContext,
50
+ ): Promise<Result<Record<string, Record<string, unknown>>>> {
51
+ const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
52
+ const rows = await this.repository.findAll(orgId, ctx);
53
+ const all: Record<string, Record<string, unknown>> = {};
54
+ for (const row of rows) all[row.group] = row.value;
55
+ return Ok(all);
56
+ }
57
+
58
+ /** Shallow merge: keys in `patch` overwrite, `null` deletes the key. */
59
+ async updateGroup(
60
+ group: string,
61
+ patch: Record<string, unknown>,
62
+ actor?: Actor | null,
63
+ ctx?: TxContext,
64
+ ): Promise<Result<Record<string, unknown>>> {
65
+ if (!GROUP_NAME_RE.test(group)) {
66
+ return Err(
67
+ new CommerceValidationError(
68
+ "Settings group must match [a-z][a-z0-9_-]* (e.g. general, branding, policies).",
69
+ ),
70
+ );
71
+ }
72
+ const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
73
+ const current = await this.read(orgId, group, ctx);
74
+ const next: Record<string, unknown> = { ...current };
75
+ for (const [key, value] of Object.entries(patch)) {
76
+ if (value === null) delete next[key];
77
+ else next[key] = value;
78
+ }
79
+ const row = await this.repository.upsert(orgId, group, next, ctx);
80
+ return Ok(row.value);
81
+ }
82
+ }
@@ -117,6 +117,33 @@ function resolveStrategy(config: CommerceConfig): ShippingStrategy {
117
117
  };
118
118
  }
119
119
 
120
+ /**
121
+ * Total weight (grams) of the shippable line items — used by both the
122
+ * code-config strategy and runtime zone rates with weight bands.
123
+ */
124
+ export async function computeShippableWeightGrams(
125
+ config: CommerceConfig,
126
+ catalogRepo: CatalogRepository,
127
+ lineItems: ShippingLineItem[],
128
+ ctx?: TxContext,
129
+ ): Promise<number> {
130
+ const flags = await Promise.all(
131
+ lineItems.map((lineItem) =>
132
+ isShippableEntity(config, catalogRepo, lineItem.entityId, ctx),
133
+ ),
134
+ );
135
+ const shippable = lineItems.filter((_, i) => flags[i]);
136
+ const weights = await Promise.all(
137
+ shippable.map((lineItem) =>
138
+ resolveWeightGrams(catalogRepo, lineItem.entityId, lineItem.variantId, ctx),
139
+ ),
140
+ );
141
+ return shippable.reduce(
142
+ (sum, lineItem, i) => sum + (weights[i] ?? 0) * lineItem.quantity,
143
+ 0,
144
+ );
145
+ }
146
+
120
147
  export async function calculateShippingCost(
121
148
  config: CommerceConfig,
122
149
  catalogRepo: CatalogRepository,
@@ -1,6 +1,9 @@
1
1
  import type { CommerceConfig } from "../../config/types.js";
2
+ import type { DrizzleDatabase } from "../../kernel/database/drizzle-db.js";
2
3
  import { defineModule } from "../../kernel/module/index.js";
3
4
  import type { CatalogServiceImpl } from "../catalog/service.js";
5
+ import { ShippingConfigRepository } from "./repository/index.js";
6
+ import { shippingZones, shippingRates } from "./schema.js";
4
7
  import { ShippingService } from "./service.js";
5
8
 
6
9
  type ShippingModuleDeps = {
@@ -8,16 +11,17 @@ type ShippingModuleDeps = {
8
11
  };
9
12
 
10
13
  export const shippingModule = defineModule<
11
- Record<string, never>,
14
+ { shippingZones: typeof shippingZones; shippingRates: typeof shippingRates },
12
15
  ShippingService,
13
16
  ShippingModuleDeps
14
17
  >({
15
18
  id: "shipping",
16
19
  dependencies: ["catalog"],
17
- schema: () => ({}),
20
+ schema: () => ({ shippingZones, shippingRates }),
18
21
  service: (deps) =>
19
22
  new ShippingService({
20
23
  config: deps.config as CommerceConfig,
21
24
  catalogRepository: deps.services.catalog.repository,
25
+ repository: new ShippingConfigRepository(deps.db.db as DrizzleDatabase),
22
26
  }),
23
27
  });
@@ -1,5 +1,139 @@
1
+ import { eq, and, asc } from "drizzle-orm";
1
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 { shippingZones, shippingRates } from "../schema.js";
2
8
 
3
- export interface ShippingRepository {
4
- ping(ctx: TxContext): Promise<void>;
9
+ export type ShippingZone = typeof shippingZones.$inferSelect;
10
+ export type ShippingZoneInsert = typeof shippingZones.$inferInsert;
11
+ export type ShippingRate = typeof shippingRates.$inferSelect;
12
+ export type ShippingRateInsert = typeof shippingRates.$inferInsert;
13
+
14
+ /**
15
+ * Persistence for runtime shipping zones & rates (issue #45).
16
+ */
17
+ export class ShippingConfigRepository {
18
+ constructor(private readonly db: DrizzleDatabase) {}
19
+
20
+ private getDb(ctx?: TxContext): DbOrTx {
21
+ return (ctx?.tx as DbOrTx | undefined) ?? this.db;
22
+ }
23
+
24
+ // ── Zones ──────────────────────────────────────────────────────────────
25
+
26
+ async findZoneById(orgId: string, id: string, ctx?: TxContext): Promise<ShippingZone | undefined> {
27
+ const db = this.getDb(ctx);
28
+ const rows = await db
29
+ .select()
30
+ .from(shippingZones)
31
+ .where(and(eq(shippingZones.organizationId, orgId), eq(shippingZones.id, id)));
32
+ return rows[0];
33
+ }
34
+
35
+ async findZones(orgId: string, ctx?: TxContext): Promise<ShippingZone[]> {
36
+ const db = this.getDb(ctx);
37
+ return db
38
+ .select()
39
+ .from(shippingZones)
40
+ .where(eq(shippingZones.organizationId, orgId))
41
+ .orderBy(asc(shippingZones.priority));
42
+ }
43
+
44
+ async findActiveZones(orgId: string, ctx?: TxContext): Promise<ShippingZone[]> {
45
+ const db = this.getDb(ctx);
46
+ return db
47
+ .select()
48
+ .from(shippingZones)
49
+ .where(and(eq(shippingZones.organizationId, orgId), eq(shippingZones.isActive, true)))
50
+ .orderBy(asc(shippingZones.priority));
51
+ }
52
+
53
+ async createZone(data: ShippingZoneInsert, ctx?: TxContext): Promise<ShippingZone> {
54
+ const db = this.getDb(ctx);
55
+ const rows = await db.insert(shippingZones).values(data).returning();
56
+ return rows[0]!;
57
+ }
58
+
59
+ async updateZone(
60
+ id: string,
61
+ data: Partial<Omit<ShippingZoneInsert, "id" | "organizationId">>,
62
+ ctx?: TxContext,
63
+ ): Promise<ShippingZone | undefined> {
64
+ const db = this.getDb(ctx);
65
+ const rows = await db
66
+ .update(shippingZones)
67
+ .set({ ...data, updatedAt: new Date() })
68
+ .where(eq(shippingZones.id, id))
69
+ .returning();
70
+ return rows[0];
71
+ }
72
+
73
+ async deleteZone(id: string, ctx?: TxContext): Promise<boolean> {
74
+ const db = this.getDb(ctx);
75
+ const rows = await db.delete(shippingZones).where(eq(shippingZones.id, id)).returning();
76
+ return rows.length > 0;
77
+ }
78
+
79
+ // ── Rates ──────────────────────────────────────────────────────────────
80
+
81
+ async findRateById(orgId: string, id: string, ctx?: TxContext): Promise<ShippingRate | undefined> {
82
+ const db = this.getDb(ctx);
83
+ const rows = await db
84
+ .select()
85
+ .from(shippingRates)
86
+ .where(and(eq(shippingRates.organizationId, orgId), eq(shippingRates.id, id)));
87
+ return rows[0];
88
+ }
89
+
90
+ async findRates(
91
+ orgId: string,
92
+ filter?: { zoneId?: string },
93
+ ctx?: TxContext,
94
+ ): Promise<ShippingRate[]> {
95
+ const db = this.getDb(ctx);
96
+ const conditions = [eq(shippingRates.organizationId, orgId)];
97
+ if (filter?.zoneId) conditions.push(eq(shippingRates.zoneId, filter.zoneId));
98
+ return db
99
+ .select()
100
+ .from(shippingRates)
101
+ .where(and(...conditions))
102
+ .orderBy(asc(shippingRates.amount));
103
+ }
104
+
105
+ async findActiveRatesByZoneId(zoneId: string, ctx?: TxContext): Promise<ShippingRate[]> {
106
+ const db = this.getDb(ctx);
107
+ return db
108
+ .select()
109
+ .from(shippingRates)
110
+ .where(and(eq(shippingRates.zoneId, zoneId), eq(shippingRates.isActive, true)))
111
+ .orderBy(asc(shippingRates.amount));
112
+ }
113
+
114
+ async createRate(data: ShippingRateInsert, ctx?: TxContext): Promise<ShippingRate> {
115
+ const db = this.getDb(ctx);
116
+ const rows = await db.insert(shippingRates).values(data).returning();
117
+ return rows[0]!;
118
+ }
119
+
120
+ async updateRate(
121
+ id: string,
122
+ data: Partial<Omit<ShippingRateInsert, "id" | "organizationId">>,
123
+ ctx?: TxContext,
124
+ ): Promise<ShippingRate | undefined> {
125
+ const db = this.getDb(ctx);
126
+ const rows = await db
127
+ .update(shippingRates)
128
+ .set({ ...data, updatedAt: new Date() })
129
+ .where(eq(shippingRates.id, id))
130
+ .returning();
131
+ return rows[0];
132
+ }
133
+
134
+ async deleteRate(id: string, ctx?: TxContext): Promise<boolean> {
135
+ const db = this.getDb(ctx);
136
+ const rows = await db.delete(shippingRates).where(eq(shippingRates.id, id)).returning();
137
+ return rows.length > 0;
138
+ }
5
139
  }
@@ -0,0 +1,58 @@
1
+ import { boolean, index, integer, jsonb, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
2
+ import { organization } from "../../auth/auth-schema.js";
3
+
4
+ /**
5
+ * Runtime shipping configuration (issue #45).
6
+ *
7
+ * Zones match a destination (country + optional state); rates belong to a
8
+ * zone and carry a flat amount with optional subtotal/weight bands and a
9
+ * free-shipping threshold. When zones exist for an organization they take
10
+ * precedence over the code-config strategy in `defineConfig({ shipping })`,
11
+ * which remains the fallback.
12
+ */
13
+ export const shippingZones = pgTable("shipping_zones", {
14
+ id: uuid("id").defaultRandom().primaryKey(),
15
+ organizationId: text("organization_id")
16
+ .notNull()
17
+ .references(() => organization.id, { onDelete: "cascade" }),
18
+ name: text("name").notNull(),
19
+ // ISO 3166-1 alpha-2 codes, uppercase. "*" matches any country.
20
+ countries: jsonb("countries").$type<string[]>().notNull().default([]),
21
+ // Optional state/province codes; empty array = the whole country.
22
+ states: jsonb("states").$type<string[]>().notNull().default([]),
23
+ priority: integer("priority").notNull().default(100),
24
+ isActive: boolean("is_active").notNull().default(true),
25
+ createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
26
+ updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
27
+ }, (table) => ({
28
+ orgIdx: index("idx_shipping_zones_org").on(table.organizationId),
29
+ priorityIdx: index("idx_shipping_zones_priority").on(table.priority),
30
+ }));
31
+
32
+ export const shippingRates = pgTable("shipping_rates", {
33
+ id: uuid("id").defaultRandom().primaryKey(),
34
+ organizationId: text("organization_id")
35
+ .notNull()
36
+ .references(() => organization.id, { onDelete: "cascade" }),
37
+ zoneId: uuid("zone_id")
38
+ .notNull()
39
+ .references(() => shippingZones.id, { onDelete: "cascade" }),
40
+ name: text("name").notNull(),
41
+ currency: text("currency").notNull().default("USD"),
42
+ // Flat cost in minor units
43
+ amount: integer("amount").notNull(),
44
+ // Price bands (subtotal after discount, minor units) — nulls are open-ended
45
+ minSubtotal: integer("min_subtotal"),
46
+ maxSubtotal: integer("max_subtotal"),
47
+ // Weight bands (grams) — nulls are open-ended
48
+ minWeightGrams: integer("min_weight_grams"),
49
+ maxWeightGrams: integer("max_weight_grams"),
50
+ // Subtotal at or above which shipping is free for this rate
51
+ freeShippingThreshold: integer("free_shipping_threshold"),
52
+ isActive: boolean("is_active").notNull().default(true),
53
+ createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
54
+ updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
55
+ }, (table) => ({
56
+ zoneIdx: index("idx_shipping_rates_zone").on(table.zoneId),
57
+ orgIdx: index("idx_shipping_rates_org").on(table.organizationId),
58
+ }));