@zonastery/bursar 2.0.6 → 2.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 (379) hide show
  1. package/README.md +33 -4
  2. package/dist/billing/auto-recharge-port.d.ts +11 -0
  3. package/dist/billing/auto-recharge-port.d.ts.map +1 -1
  4. package/dist/billing/auto-recharge-port.js.map +1 -1
  5. package/dist/billing/auto-recharge-service.d.ts.map +1 -1
  6. package/dist/billing/auto-recharge-service.js +53 -26
  7. package/dist/billing/auto-recharge-service.js.map +1 -1
  8. package/dist/billing/billing-service.d.ts +38 -3
  9. package/dist/billing/billing-service.d.ts.map +1 -1
  10. package/dist/billing/billing-service.js +51 -7
  11. package/dist/billing/billing-service.js.map +1 -1
  12. package/dist/billing/billing-store.d.ts +34 -9
  13. package/dist/billing/billing-store.d.ts.map +1 -1
  14. package/dist/billing/billing-store.js +2 -0
  15. package/dist/billing/billing-store.js.map +1 -1
  16. package/dist/billing/contracts.d.ts +35 -3
  17. package/dist/billing/contracts.d.ts.map +1 -1
  18. package/dist/billing/contracts.js.map +1 -1
  19. package/dist/billing/event-handlers.d.ts +1 -2
  20. package/dist/billing/event-handlers.d.ts.map +1 -1
  21. package/dist/billing/event-handlers.js +69 -29
  22. package/dist/billing/event-handlers.js.map +1 -1
  23. package/dist/billing/event-processor.d.ts +2 -2
  24. package/dist/billing/event-processor.d.ts.map +1 -1
  25. package/dist/billing/event-processor.js +82 -19
  26. package/dist/billing/event-processor.js.map +1 -1
  27. package/dist/billing/financial-event-handlers.d.ts +2 -0
  28. package/dist/billing/financial-event-handlers.d.ts.map +1 -1
  29. package/dist/billing/financial-event-handlers.js +168 -44
  30. package/dist/billing/financial-event-handlers.js.map +1 -1
  31. package/dist/billing/index.d.ts +1 -1
  32. package/dist/billing/index.d.ts.map +1 -1
  33. package/dist/billing/index.js.map +1 -1
  34. package/dist/billing/management.d.ts +16 -3
  35. package/dist/billing/management.d.ts.map +1 -1
  36. package/dist/billing/management.js +11 -2
  37. package/dist/billing/management.js.map +1 -1
  38. package/dist/billing/postgres/repositories/auto-recharge.d.ts +13 -1
  39. package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -1
  40. package/dist/billing/postgres/repositories/auto-recharge.js +71 -12
  41. package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -1
  42. package/dist/billing/postgres/repositories/invoice.d.ts +3 -2
  43. package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -1
  44. package/dist/billing/postgres/repositories/invoice.js +33 -3
  45. package/dist/billing/postgres/repositories/invoice.js.map +1 -1
  46. package/dist/billing/postgres/repositories/offer.d.ts +5 -1
  47. package/dist/billing/postgres/repositories/offer.d.ts.map +1 -1
  48. package/dist/billing/postgres/repositories/offer.js +7 -10
  49. package/dist/billing/postgres/repositories/offer.js.map +1 -1
  50. package/dist/billing/postgres/repositories/payment.d.ts +6 -1
  51. package/dist/billing/postgres/repositories/payment.d.ts.map +1 -1
  52. package/dist/billing/postgres/repositories/payment.js +25 -16
  53. package/dist/billing/postgres/repositories/payment.js.map +1 -1
  54. package/dist/billing/postgres/repositories/subscription-change.d.ts +1 -0
  55. package/dist/billing/postgres/repositories/subscription-change.d.ts.map +1 -1
  56. package/dist/billing/postgres/repositories/subscription-change.js +25 -4
  57. package/dist/billing/postgres/repositories/subscription-change.js.map +1 -1
  58. package/dist/billing/postgres/repositories/subscription.d.ts +3 -2
  59. package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -1
  60. package/dist/billing/postgres/repositories/subscription.js +44 -48
  61. package/dist/billing/postgres/repositories/subscription.js.map +1 -1
  62. package/dist/billing/postgres/repositories/topup.d.ts +6 -1
  63. package/dist/billing/postgres/repositories/topup.d.ts.map +1 -1
  64. package/dist/billing/postgres/repositories/topup.js +27 -10
  65. package/dist/billing/postgres/repositories/topup.js.map +1 -1
  66. package/dist/billing/postgres/store.d.ts +46 -34
  67. package/dist/billing/postgres/store.d.ts.map +1 -1
  68. package/dist/billing/postgres/store.js +152 -126
  69. package/dist/billing/postgres/store.js.map +1 -1
  70. package/dist/billing/service-types.d.ts +0 -1
  71. package/dist/billing/service-types.d.ts.map +1 -1
  72. package/dist/billing/service-types.js.map +1 -1
  73. package/dist/billing/types/auto-recharge.d.ts +2 -0
  74. package/dist/billing/types/auto-recharge.d.ts.map +1 -1
  75. package/dist/billing/types/auto-recharge.js.map +1 -1
  76. package/dist/billing/types/catalog.d.ts +1 -0
  77. package/dist/billing/types/catalog.d.ts.map +1 -1
  78. package/dist/billing/types/catalog.js.map +1 -1
  79. package/dist/billing/types/documents.d.ts +15 -1
  80. package/dist/billing/types/documents.d.ts.map +1 -1
  81. package/dist/billing/types/documents.js.map +1 -1
  82. package/dist/billing/types/events.d.ts +2 -0
  83. package/dist/billing/types/events.d.ts.map +1 -1
  84. package/dist/billing/types/events.js +15 -0
  85. package/dist/billing/types/events.js.map +1 -1
  86. package/dist/billing/types/subscriptions.d.ts +12 -0
  87. package/dist/billing/types/subscriptions.d.ts.map +1 -1
  88. package/dist/billing/types/subscriptions.js.map +1 -1
  89. package/dist/breakdown.d.ts +2 -24
  90. package/dist/breakdown.d.ts.map +1 -1
  91. package/dist/breakdown.js +1 -29
  92. package/dist/breakdown.js.map +1 -1
  93. package/dist/bursar.d.ts +13 -21
  94. package/dist/bursar.d.ts.map +1 -1
  95. package/dist/bursar.js +44 -50
  96. package/dist/bursar.js.map +1 -1
  97. package/dist/catalog.d.ts +21 -2
  98. package/dist/catalog.d.ts.map +1 -1
  99. package/dist/catalog.js +27 -4
  100. package/dist/catalog.js.map +1 -1
  101. package/dist/cli.d.ts +3 -0
  102. package/dist/cli.d.ts.map +1 -0
  103. package/dist/cli.js +147 -0
  104. package/dist/cli.js.map +1 -0
  105. package/dist/commerce/plan-change.d.ts +3 -3
  106. package/dist/commerce/plan-change.d.ts.map +1 -1
  107. package/dist/commerce/plan-change.js +14 -7
  108. package/dist/commerce/plan-change.js.map +1 -1
  109. package/dist/commerce/provider-registry.d.ts.map +1 -1
  110. package/dist/commerce/provider-registry.js +8 -2
  111. package/dist/commerce/provider-registry.js.map +1 -1
  112. package/dist/commerce/service.d.ts +2 -0
  113. package/dist/commerce/service.d.ts.map +1 -1
  114. package/dist/commerce/service.js +330 -76
  115. package/dist/commerce/service.js.map +1 -1
  116. package/dist/commerce/types.d.ts +12 -3
  117. package/dist/commerce/types.d.ts.map +1 -1
  118. package/dist/commerce/types.js.map +1 -1
  119. package/dist/config/availability.d.ts +4 -0
  120. package/dist/config/availability.d.ts.map +1 -0
  121. package/dist/config/availability.js +10 -0
  122. package/dist/config/availability.js.map +1 -0
  123. package/dist/config/normalize.d.ts +7 -0
  124. package/dist/config/normalize.d.ts.map +1 -0
  125. package/dist/config/normalize.js +119 -0
  126. package/dist/config/normalize.js.map +1 -0
  127. package/dist/config/parse-access.d.ts.map +1 -1
  128. package/dist/config/parse-access.js +4 -1
  129. package/dist/config/parse-access.js.map +1 -1
  130. package/dist/config/parse-commerce.d.ts +5 -1
  131. package/dist/config/parse-commerce.d.ts.map +1 -1
  132. package/dist/config/parse-commerce.js +79 -22
  133. package/dist/config/parse-commerce.js.map +1 -1
  134. package/dist/config/parse-credits.d.ts.map +1 -1
  135. package/dist/config/parse-credits.js +10 -7
  136. package/dist/config/parse-credits.js.map +1 -1
  137. package/dist/config/parse-plans.d.ts.map +1 -1
  138. package/dist/config/parse-plans.js +31 -15
  139. package/dist/config/parse-plans.js.map +1 -1
  140. package/dist/config/parse-pricing.d.ts.map +1 -1
  141. package/dist/config/parse-pricing.js +42 -27
  142. package/dist/config/parse-pricing.js.map +1 -1
  143. package/dist/config/parse-utils.d.ts +4 -0
  144. package/dist/config/parse-utils.d.ts.map +1 -1
  145. package/dist/config/parse-utils.js +26 -1
  146. package/dist/config/parse-utils.js.map +1 -1
  147. package/dist/config/types.d.ts +8 -4
  148. package/dist/config/types.d.ts.map +1 -1
  149. package/dist/config/types.js.map +1 -1
  150. package/dist/config.d.ts +5 -4
  151. package/dist/config.d.ts.map +1 -1
  152. package/dist/config.js +118 -70
  153. package/dist/config.js.map +1 -1
  154. package/dist/credits/amount.d.ts.map +1 -1
  155. package/dist/credits/amount.js +5 -4
  156. package/dist/credits/amount.js.map +1 -1
  157. package/dist/credits/catalog-runtime.d.ts +15 -2
  158. package/dist/credits/catalog-runtime.d.ts.map +1 -1
  159. package/dist/credits/catalog-runtime.js +77 -21
  160. package/dist/credits/catalog-runtime.js.map +1 -1
  161. package/dist/credits/lease-workflow.d.ts +3 -1
  162. package/dist/credits/lease-workflow.d.ts.map +1 -1
  163. package/dist/credits/lease-workflow.js +47 -38
  164. package/dist/credits/lease-workflow.js.map +1 -1
  165. package/dist/credits/low-balance-monitor.d.ts +2 -7
  166. package/dist/credits/low-balance-monitor.d.ts.map +1 -1
  167. package/dist/credits/low-balance-monitor.js +6 -57
  168. package/dist/credits/low-balance-monitor.js.map +1 -1
  169. package/dist/credits/postgres/repositories/analytics.d.ts +3 -3
  170. package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -1
  171. package/dist/credits/postgres/repositories/analytics.js +25 -7
  172. package/dist/credits/postgres/repositories/analytics.js.map +1 -1
  173. package/dist/credits/postgres/repositories/balance.d.ts +28 -0
  174. package/dist/credits/postgres/repositories/balance.d.ts.map +1 -1
  175. package/dist/credits/postgres/repositories/balance.js +33 -14
  176. package/dist/credits/postgres/repositories/balance.js.map +1 -1
  177. package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -1
  178. package/dist/credits/postgres/repositories/bucket.js +2 -2
  179. package/dist/credits/postgres/repositories/bucket.js.map +1 -1
  180. package/dist/credits/postgres/repositories/catalog.d.ts +2 -2
  181. package/dist/credits/postgres/repositories/catalog.d.ts.map +1 -1
  182. package/dist/credits/postgres/repositories/catalog.js +2 -12
  183. package/dist/credits/postgres/repositories/catalog.js.map +1 -1
  184. package/dist/credits/postgres/repositories/deduction.d.ts +2 -0
  185. package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -1
  186. package/dist/credits/postgres/repositories/deduction.js +9 -2
  187. package/dist/credits/postgres/repositories/deduction.js.map +1 -1
  188. package/dist/credits/postgres/repositories/lease.d.ts +3 -0
  189. package/dist/credits/postgres/repositories/lease.d.ts.map +1 -1
  190. package/dist/credits/postgres/repositories/lease.js +46 -12
  191. package/dist/credits/postgres/repositories/lease.js.map +1 -1
  192. package/dist/credits/postgres/repositories/plan.d.ts +6 -1
  193. package/dist/credits/postgres/repositories/plan.d.ts.map +1 -1
  194. package/dist/credits/postgres/repositories/plan.js +13 -3
  195. package/dist/credits/postgres/repositories/plan.js.map +1 -1
  196. package/dist/credits/postgres/repositories/team.d.ts +1 -1
  197. package/dist/credits/postgres/repositories/team.d.ts.map +1 -1
  198. package/dist/credits/postgres/repositories/team.js +9 -5
  199. package/dist/credits/postgres/repositories/team.js.map +1 -1
  200. package/dist/credits/postgres/store.d.ts +30 -20
  201. package/dist/credits/postgres/store.d.ts.map +1 -1
  202. package/dist/credits/postgres/store.js +335 -251
  203. package/dist/credits/postgres/store.js.map +1 -1
  204. package/dist/credits/postgres/value-mappers.d.ts.map +1 -1
  205. package/dist/credits/postgres/value-mappers.js +4 -4
  206. package/dist/credits/postgres/value-mappers.js.map +1 -1
  207. package/dist/credits/rated-wire.d.ts +7 -0
  208. package/dist/credits/rated-wire.d.ts.map +1 -0
  209. package/dist/credits/rated-wire.js +70 -0
  210. package/dist/credits/rated-wire.js.map +1 -0
  211. package/dist/credits/service-errors.d.ts.map +1 -1
  212. package/dist/credits/service-errors.js +8 -1
  213. package/dist/credits/service-errors.js.map +1 -1
  214. package/dist/credits/service-types.d.ts +5 -7
  215. package/dist/credits/service-types.d.ts.map +1 -1
  216. package/dist/credits/service-types.js.map +1 -1
  217. package/dist/credits/service.d.ts +12 -9
  218. package/dist/credits/service.d.ts.map +1 -1
  219. package/dist/credits/service.js +161 -138
  220. package/dist/credits/service.js.map +1 -1
  221. package/dist/credits/store.d.ts +11 -3
  222. package/dist/credits/store.d.ts.map +1 -1
  223. package/dist/credits/store.js +9 -0
  224. package/dist/credits/store.js.map +1 -1
  225. package/dist/credits/types/account.d.ts +9 -1
  226. package/dist/credits/types/account.d.ts.map +1 -1
  227. package/dist/credits/types/account.js.map +1 -1
  228. package/dist/credits/types/admission.d.ts +1 -0
  229. package/dist/credits/types/admission.d.ts.map +1 -1
  230. package/dist/credits/types/admission.js.map +1 -1
  231. package/dist/credits/types/catalog.d.ts +1 -0
  232. package/dist/credits/types/catalog.d.ts.map +1 -1
  233. package/dist/credits/types/catalog.js.map +1 -1
  234. package/dist/credits/types/grants.d.ts +12 -0
  235. package/dist/credits/types/grants.d.ts.map +1 -1
  236. package/dist/credits/types/grants.js.map +1 -1
  237. package/dist/credits/types/ledger.d.ts +13 -0
  238. package/dist/credits/types/ledger.d.ts.map +1 -1
  239. package/dist/credits/types/ledger.js.map +1 -1
  240. package/dist/credits/types/quotas.d.ts +2 -1
  241. package/dist/credits/types/quotas.d.ts.map +1 -1
  242. package/dist/credits/types/quotas.js.map +1 -1
  243. package/dist/engine.d.ts +4 -1
  244. package/dist/engine.d.ts.map +1 -1
  245. package/dist/engine.js +59 -36
  246. package/dist/engine.js.map +1 -1
  247. package/dist/errors.d.ts +12 -1
  248. package/dist/errors.d.ts.map +1 -1
  249. package/dist/errors.js +13 -0
  250. package/dist/errors.js.map +1 -1
  251. package/dist/expr/evaluator.d.ts.map +1 -1
  252. package/dist/expr/evaluator.js +29 -9
  253. package/dist/expr/evaluator.js.map +1 -1
  254. package/dist/expr/language.js +1 -1
  255. package/dist/expr/language.js.map +1 -1
  256. package/dist/expr.d.ts.map +1 -1
  257. package/dist/expr.js +5 -6
  258. package/dist/expr.js.map +1 -1
  259. package/dist/generated/pricing-config.schema.json +272 -228
  260. package/dist/generated/pricing-config.types.d.ts +128 -79
  261. package/dist/generated/pricing-config.types.d.ts.map +1 -1
  262. package/dist/generated/pricing-config.types.js.map +1 -1
  263. package/dist/index.d.ts +8 -6
  264. package/dist/index.d.ts.map +1 -1
  265. package/dist/index.js +4 -2
  266. package/dist/index.js.map +1 -1
  267. package/dist/load-config-file.d.ts +5 -1
  268. package/dist/load-config-file.d.ts.map +1 -1
  269. package/dist/load-config-file.js +56 -17
  270. package/dist/load-config-file.js.map +1 -1
  271. package/dist/metrics.d.ts +1 -1
  272. package/dist/metrics.d.ts.map +1 -1
  273. package/dist/metrics.js.map +1 -1
  274. package/dist/node.d.ts +2 -2
  275. package/dist/node.d.ts.map +1 -1
  276. package/dist/node.js +1 -1
  277. package/dist/node.js.map +1 -1
  278. package/dist/pricing-decimal.d.ts +7 -0
  279. package/dist/pricing-decimal.d.ts.map +1 -0
  280. package/dist/pricing-decimal.js +43 -0
  281. package/dist/pricing-decimal.js.map +1 -0
  282. package/dist/providers/dodo/better-auth-core.d.ts +1 -1
  283. package/dist/providers/dodo/better-auth-core.d.ts.map +1 -1
  284. package/dist/providers/dodo/better-auth-core.js +2 -2
  285. package/dist/providers/dodo/better-auth-core.js.map +1 -1
  286. package/dist/providers/dodo/better-auth.d.ts.map +1 -1
  287. package/dist/providers/dodo/better-auth.js +16 -2
  288. package/dist/providers/dodo/better-auth.js.map +1 -1
  289. package/dist/providers/dodo/event-mapper.d.ts +1 -1
  290. package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
  291. package/dist/providers/dodo/event-mapper.js +124 -18
  292. package/dist/providers/dodo/event-mapper.js.map +1 -1
  293. package/dist/providers/dodo/nextjs-core.d.ts +1 -1
  294. package/dist/providers/dodo/nextjs-core.d.ts.map +1 -1
  295. package/dist/providers/dodo/nextjs-core.js +2 -2
  296. package/dist/providers/dodo/nextjs-core.js.map +1 -1
  297. package/dist/providers/dodo/nextjs.d.ts.map +1 -1
  298. package/dist/providers/dodo/nextjs.js +16 -6
  299. package/dist/providers/dodo/nextjs.js.map +1 -1
  300. package/dist/providers/dodo/provider.d.ts +5 -2
  301. package/dist/providers/dodo/provider.d.ts.map +1 -1
  302. package/dist/providers/dodo/provider.js +46 -7
  303. package/dist/providers/dodo/provider.js.map +1 -1
  304. package/dist/providers/dodo/verified-payment-payload.d.ts +6 -0
  305. package/dist/providers/dodo/verified-payment-payload.d.ts.map +1 -0
  306. package/dist/providers/dodo/verified-payment-payload.js +42 -0
  307. package/dist/providers/dodo/verified-payment-payload.js.map +1 -0
  308. package/dist/providers/index.d.ts +1 -1
  309. package/dist/providers/index.d.ts.map +1 -1
  310. package/dist/providers/index.js.map +1 -1
  311. package/dist/providers/mock/provider.d.ts +4 -1
  312. package/dist/providers/mock/provider.d.ts.map +1 -1
  313. package/dist/providers/mock/provider.js +3 -1
  314. package/dist/providers/mock/provider.js.map +1 -1
  315. package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
  316. package/dist/providers/stripe/event-mapper.js +51 -24
  317. package/dist/providers/stripe/event-mapper.js.map +1 -1
  318. package/dist/providers/stripe/provider.d.ts +4 -1
  319. package/dist/providers/stripe/provider.d.ts.map +1 -1
  320. package/dist/providers/stripe/provider.js +118 -34
  321. package/dist/providers/stripe/provider.js.map +1 -1
  322. package/dist/providers/types.d.ts +16 -2
  323. package/dist/providers/types.d.ts.map +1 -1
  324. package/dist/providers/types.js +9 -0
  325. package/dist/providers/types.js.map +1 -1
  326. package/dist/shared/postgres-client.d.ts +5 -0
  327. package/dist/shared/postgres-client.d.ts.map +1 -1
  328. package/dist/shared/postgres-client.js +72 -11
  329. package/dist/shared/postgres-client.js.map +1 -1
  330. package/dist/shared/postgres-errors.d.ts.map +1 -1
  331. package/dist/shared/postgres-errors.js +6 -0
  332. package/dist/shared/postgres-errors.js.map +1 -1
  333. package/dist/shared/provider-rejection.d.ts +3 -0
  334. package/dist/shared/provider-rejection.d.ts.map +1 -0
  335. package/dist/shared/provider-rejection.js +17 -0
  336. package/dist/shared/provider-rejection.js.map +1 -0
  337. package/dist/shared/timestamp.d.ts +5 -0
  338. package/dist/shared/timestamp.d.ts.map +1 -0
  339. package/dist/shared/timestamp.js +21 -0
  340. package/dist/shared/timestamp.js.map +1 -0
  341. package/dist/starter.yaml +17 -0
  342. package/dist/storage/adapters/clickhouse.d.ts +1 -1
  343. package/dist/storage/adapters/clickhouse.d.ts.map +1 -1
  344. package/dist/storage/adapters/clickhouse.js +42 -37
  345. package/dist/storage/adapters/clickhouse.js.map +1 -1
  346. package/dist/storage/adapters/s3.d.ts +5 -0
  347. package/dist/storage/adapters/s3.d.ts.map +1 -1
  348. package/dist/storage/adapters/s3.js +46 -14
  349. package/dist/storage/adapters/s3.js.map +1 -1
  350. package/dist/storage/diagnostics.d.ts +3 -7
  351. package/dist/storage/diagnostics.d.ts.map +1 -1
  352. package/dist/storage/diagnostics.js +6 -15
  353. package/dist/storage/diagnostics.js.map +1 -1
  354. package/dist/storage/outbox-worker.d.ts.map +1 -1
  355. package/dist/storage/outbox-worker.js +4 -3
  356. package/dist/storage/outbox-worker.js.map +1 -1
  357. package/dist/storage/ports.d.ts +8 -0
  358. package/dist/storage/ports.d.ts.map +1 -1
  359. package/dist/storage/ports.js.map +1 -1
  360. package/dist/storage/postgres-repository.d.ts +1 -3
  361. package/dist/storage/postgres-repository.d.ts.map +1 -1
  362. package/dist/storage/postgres-repository.js +34 -12
  363. package/dist/storage/postgres-repository.js.map +1 -1
  364. package/dist/storage/raw-json.d.ts +10 -0
  365. package/dist/storage/raw-json.d.ts.map +1 -0
  366. package/dist/storage/raw-json.js +19 -0
  367. package/dist/storage/raw-json.js.map +1 -0
  368. package/dist/storage/runtime.d.ts +15 -10
  369. package/dist/storage/runtime.d.ts.map +1 -1
  370. package/dist/storage/runtime.js +97 -68
  371. package/dist/storage/runtime.js.map +1 -1
  372. package/dist/telemetry/index.d.ts +1 -1
  373. package/dist/telemetry/index.js +1 -1
  374. package/dist/telemetry/index.js.map +1 -1
  375. package/package.json +78 -71
  376. package/dist/credits/queries.d.ts +0 -33
  377. package/dist/credits/queries.d.ts.map +0 -1
  378. package/dist/credits/queries.js +0 -71
  379. package/dist/credits/queries.js.map +0 -1
@@ -1,7 +1,8 @@
1
+ import { PricingDecimal } from "../../pricing-decimal.js";
1
2
  import { Decimal } from "decimal.js";
2
3
  import { z } from "zod";
3
4
  import { BillingStore } from "../billing-store.js";
4
- import { PostgresClient, } from "../../shared/postgres-client.js";
5
+ import { PostgresClient, normalizeTenantId, } from "../../shared/postgres-client.js";
5
6
  import { normalizeProviderEnvironment, } from "../../providers/environment.js";
6
7
  import { StoreClosedError, StoreError } from "../../errors.js";
7
8
  import { optionalBoundedDiagnosticMessage } from "../../shared/diagnostics.js";
@@ -29,7 +30,7 @@ const BillingCreditPostingRowSchema = z
29
30
  balance_after: z
30
31
  .union([z.string(), z.number(), z.instanceof(Decimal)])
31
32
  .nullable()
32
- .transform((value) => (value === null ? null : new Decimal(value))),
33
+ .transform((value) => (value === null ? null : new PricingDecimal(value))),
33
34
  replayed: pgBoolean,
34
35
  error_code: z.string().min(1).nullable(),
35
36
  })
@@ -52,6 +53,7 @@ const CheckoutIntentRowSchema = z
52
53
  request_digest: z.instanceof(Uint8Array).refine((value) => value.byteLength === 32),
53
54
  status: z.enum(["open", "completed", "failed", "expired"]),
54
55
  provider_session_id: z.string().min(1).nullable(),
56
+ provider_customer_id: z.string().min(1).nullable(),
55
57
  checkout_url: z.string().min(1).nullable(),
56
58
  expires_at: z.union([z.string().datetime({ offset: true }), z.date()]),
57
59
  })
@@ -71,120 +73,110 @@ function billingCreditPostingResult(rows, context) {
71
73
  errorCode: row.error_code,
72
74
  };
73
75
  }
76
+ const transactionScope = Symbol("billing transaction scope");
74
77
  export class PostgresBillingStore extends BillingStore {
78
+ tenantId;
75
79
  postgres;
80
+ scoped;
81
+ options;
76
82
  providerEnvironment;
77
- _offer = null;
78
- _topup = null;
79
- _customer = null;
80
- _subscription = null;
81
- _event = null;
82
- _payment = null;
83
- _refund = null;
84
- _invoice = null;
85
- _dispute = null;
86
- _preferences = null;
87
- _autoRecharge = null;
88
- _subscriptionChange = null;
83
+ queryFn;
84
+ billingOffer;
85
+ billingTopup;
86
+ billingCustomer;
87
+ billingSubscription;
88
+ billingEvent;
89
+ billingPayment;
90
+ billingRefund;
91
+ billingInvoice;
92
+ billingDispute;
93
+ billingPreferences;
94
+ billingAutoRecharge;
95
+ billingSubscriptionChange;
89
96
  constructor(options) {
90
97
  super();
98
+ // SAFETY: Only this module can attach the private transaction scope symbol.
99
+ const scope = options[transactionScope];
100
+ this.options = { ...options };
101
+ this.scoped = scope !== undefined;
91
102
  if (!z.object({}).safeParse(options).success) {
92
103
  throw new TypeError("PostgresBillingStore options are required");
93
104
  }
94
105
  if (!z.string().safeParse(options.postgres).success && options.poolConstructor !== undefined) {
95
106
  throw new TypeError("poolConstructor cannot be used with an existing PostgreSQL pool");
96
107
  }
97
- const providerEnvironment = normalizeProviderEnvironment(options.providerEnvironment);
98
- this.providerEnvironment = providerEnvironment;
99
- this.postgres = new PostgresClient(options.postgres, {
100
- tenantId: options.tenantId,
101
- providerEnvironment,
102
- poolConstructor: options.poolConstructor,
103
- billingPayloadBackend: options.billingPayloadBackend,
104
- connectionTimeoutMs: options.connectionTimeoutMs,
105
- statementTimeoutMs: options.statementTimeoutMs,
106
- idleTransactionTimeoutMs: options.idleTransactionTimeoutMs,
107
- idleTimeoutMs: options.idleTimeoutMs,
108
- maxConnections: options.maxConnections,
109
- applicationName: options.applicationName,
110
- onPoolError: options.onPoolError,
111
- closedError: () => new StoreClosedError("Billing store has been closed"),
108
+ this.tenantId = normalizeTenantId(options.tenantId);
109
+ this.providerEnvironment = normalizeProviderEnvironment(options.providerEnvironment);
110
+ this.postgres =
111
+ scope?.postgres ??
112
+ new PostgresClient(options.postgres, {
113
+ tenantId: this.tenantId,
114
+ providerEnvironment: this.providerEnvironment,
115
+ poolConstructor: options.poolConstructor,
116
+ billingPayloadBackend: options.billingPayloadBackend,
117
+ connectionTimeoutMs: options.connectionTimeoutMs,
118
+ statementTimeoutMs: options.statementTimeoutMs,
119
+ idleTransactionTimeoutMs: options.idleTransactionTimeoutMs,
120
+ idleTimeoutMs: options.idleTimeoutMs,
121
+ maxConnections: options.maxConnections,
122
+ applicationName: options.applicationName,
123
+ onPoolError: options.onPoolError,
124
+ instrumentation: options.instrumentation,
125
+ closedError: () => new StoreClosedError("Billing store has been closed"),
126
+ });
127
+ this.queryFn = scope?.query ?? this.postgres.query;
128
+ this.billingOffer = new BillingOfferRepository(this.queryFn);
129
+ this.billingTopup = new BillingTopupRepository(this.queryFn);
130
+ this.billingCustomer = new BillingCustomerRepository(this.queryFn);
131
+ this.billingSubscription = new BillingSubscriptionRepository(this.queryFn);
132
+ this.billingEvent = new BillingEventRepository(this.queryFn);
133
+ this.billingPayment = new BillingPaymentRepository(this.queryFn);
134
+ this.billingRefund = new BillingRefundRepository(this.queryFn);
135
+ this.billingInvoice = new BillingInvoiceRepository(this.queryFn);
136
+ this.billingDispute = new BillingDisputeRepository(this.queryFn);
137
+ this.billingPreferences = new BillingPreferencesRepository(this.queryFn);
138
+ this.billingAutoRecharge = new BillingAutoRechargeRepository(this.queryFn);
139
+ this.billingSubscriptionChange = new BillingSubscriptionChangeRepository(this.queryFn);
140
+ }
141
+ async withBillingEventTransaction(action) {
142
+ if (this.scoped)
143
+ throw new TypeError("Nested billing event transactions are not supported");
144
+ return this.postgres.withTransaction(async (transactionQuery) => {
145
+ let active = true;
146
+ const queryFn = (text, params) => {
147
+ if (!active)
148
+ throw new StoreClosedError("Billing transaction has ended");
149
+ return transactionQuery(text, params);
150
+ };
151
+ const options = {
152
+ ...this.options,
153
+ [transactionScope]: { postgres: this.postgres, query: queryFn },
154
+ };
155
+ const scoped = new PostgresBillingStore(options);
156
+ try {
157
+ return await action(scoped);
158
+ }
159
+ finally {
160
+ active = false;
161
+ }
112
162
  });
113
163
  }
164
+ async observeBillingInvoiceOutcome(provider, invoiceId, outcome) {
165
+ const rows = await this.queryFn("SELECT bursar.observe_billing_invoice_outcome($1, $2, $3) AS accepted", [provider, invoiceId, outcome]);
166
+ return requireResultField(rows, "accepted", pgBoolean, "observeBillingInvoiceOutcome");
167
+ }
114
168
  async close() {
169
+ if (this.scoped)
170
+ throw new TypeError("Cannot close a scoped billing store");
115
171
  await this.postgres.close();
116
172
  }
117
- get queryFn() {
118
- return this.postgres.query;
119
- }
120
- get billingOffer() {
121
- if (!this._offer)
122
- this._offer = new BillingOfferRepository(this.queryFn);
123
- return this._offer;
124
- }
125
- get billingTopup() {
126
- if (!this._topup)
127
- this._topup = new BillingTopupRepository(this.queryFn);
128
- return this._topup;
129
- }
130
- get billingCustomer() {
131
- if (!this._customer)
132
- this._customer = new BillingCustomerRepository(this.queryFn);
133
- return this._customer;
134
- }
135
- get billingSubscription() {
136
- if (!this._subscription)
137
- this._subscription = new BillingSubscriptionRepository(this.queryFn);
138
- return this._subscription;
139
- }
140
- get billingEvent() {
141
- if (!this._event)
142
- this._event = new BillingEventRepository(this.queryFn);
143
- return this._event;
144
- }
145
- get billingPayment() {
146
- if (!this._payment)
147
- this._payment = new BillingPaymentRepository(this.queryFn);
148
- return this._payment;
149
- }
150
- get billingRefund() {
151
- if (!this._refund)
152
- this._refund = new BillingRefundRepository(this.queryFn);
153
- return this._refund;
154
- }
155
- get billingInvoice() {
156
- if (!this._invoice)
157
- this._invoice = new BillingInvoiceRepository(this.queryFn);
158
- return this._invoice;
159
- }
160
- get billingDispute() {
161
- if (!this._dispute)
162
- this._dispute = new BillingDisputeRepository(this.queryFn);
163
- return this._dispute;
164
- }
165
- get billingPreferences() {
166
- if (!this._preferences)
167
- this._preferences = new BillingPreferencesRepository(this.queryFn);
168
- return this._preferences;
169
- }
170
- get billingAutoRecharge() {
171
- if (!this._autoRecharge)
172
- this._autoRecharge = new BillingAutoRechargeRepository(this.queryFn);
173
- return this._autoRecharge;
174
- }
175
- get billingSubscriptionChange() {
176
- if (!this._subscriptionChange) {
177
- this._subscriptionChange = new BillingSubscriptionChangeRepository(this.queryFn);
178
- }
179
- return this._subscriptionChange;
180
- }
181
173
  async createOrGetCheckoutIntent(input) {
182
174
  const operationKey = requireStableKey(input.operationKey, "operationKey");
183
175
  if (!/^[0-9a-fA-F]{64}$/.test(input.requestDigest)) {
184
176
  throw new TypeError("requestDigest must be a 32-byte hex string");
185
177
  }
186
178
  const rows = await this.queryFn(`SELECT bursar.create_checkout_intent(
187
- $1::uuid, $2, $3, $4, $5, decode($6, 'hex'), $7::timestamptz
179
+ $1::uuid, $2, $3, $4, $5, decode($6, 'hex'), $7::timestamptz, NULL, NULL, $8, $9::uuid, $10::uuid, $11
188
180
  ) AS id`, [
189
181
  input.subjectId,
190
182
  input.provider,
@@ -193,6 +185,10 @@ export class PostgresBillingStore extends BillingStore {
193
185
  input.productKey,
194
186
  input.requestDigest,
195
187
  input.expiresAt,
188
+ input.region ?? null,
189
+ input.expectedRevisionId ?? null,
190
+ input.accountId ?? input.subjectId,
191
+ input.providerCustomerId ?? null,
196
192
  ]);
197
193
  const id = requireResultField(rows, "id", postgresUuid, "PostgresBillingStore.createOrGetCheckoutIntent");
198
194
  const intent = await this.getCheckoutIntent(id, input.subjectId);
@@ -205,13 +201,38 @@ export class PostgresBillingStore extends BillingStore {
205
201
  return intent;
206
202
  }
207
203
  async updateCheckoutIntent(id, update) {
208
- const rows = await this.queryFn(`SELECT bursar.advance_checkout_intent($1::uuid, $2, $3, $4) AS advanced`, [id, update.status ?? null, update.providerSessionId ?? null, update.checkoutUrl ?? null]);
204
+ const rows = await this.queryFn(`SELECT bursar.advance_checkout_intent($1::uuid, $2, $3, $4, $5, $6::uuid) AS advanced`, [
205
+ id,
206
+ update.status ?? null,
207
+ update.providerSessionId ?? null,
208
+ update.checkoutUrl ?? null,
209
+ update.expectedOwner?.provider ?? null,
210
+ update.expectedOwner?.accountId ?? null,
211
+ ]);
209
212
  if (!requireResultField(rows, "advanced", pgBoolean, "PostgresBillingStore.updateCheckoutIntent")) {
210
213
  throw new StoreError(`checkout intent transition rejected: ${id}`, {
211
214
  details: { checkoutIntentId: id },
212
215
  });
213
216
  }
214
217
  }
218
+ async findCheckoutIntent(subjectId, operationKey, provider) {
219
+ const rows = await this.queryFn("SELECT * FROM bursar.find_checkout_intent($1::uuid, $2, $3)", [
220
+ subjectId,
221
+ operationKey,
222
+ provider ?? null,
223
+ ]);
224
+ if (rows.length === 0)
225
+ return null;
226
+ if (rows.length !== 1)
227
+ throw new StoreError("checkout operation key matches multiple providers");
228
+ const row = rows[0];
229
+ const id = safeParse(postgresUuid, row.intent_id, "findCheckoutIntent.id");
230
+ const catalog = safeParse(z.record(z.string(), z.json()), row.source_document, "findCheckoutIntent.catalog");
231
+ const intent = await this.getCheckoutIntent(id, subjectId);
232
+ if (!intent)
233
+ throw new StoreError("checkout intent disappeared");
234
+ return { intent, catalog };
235
+ }
215
236
  async getCheckoutIntent(id, subjectId) {
216
237
  const rows = await this.queryFn(`SELECT * FROM bursar.get_checkout_intent($1::uuid, $2::uuid)`, [id, subjectId]);
217
238
  if (rows.length === 0)
@@ -233,6 +254,7 @@ export class PostgresBillingStore extends BillingStore {
233
254
  request_digest: row.request_digest,
234
255
  status: row.status,
235
256
  provider_session_id: row.provider_session_id,
257
+ provider_customer_id: row.provider_customer_id,
236
258
  checkout_url: row.checkout_url,
237
259
  expires_at: row.expires_at,
238
260
  }, "PostgresBillingStore.getCheckoutIntent"));
@@ -247,6 +269,7 @@ export class PostgresBillingStore extends BillingStore {
247
269
  requestDigest: Buffer.from(row.request_digest).toString("hex"),
248
270
  status: row.status,
249
271
  providerSessionId: row.provider_session_id,
272
+ providerCustomerId: row.provider_customer_id,
250
273
  checkoutUrl: row.checkout_url,
251
274
  expiresAt: row.expires_at instanceof Date ? row.expires_at.toISOString() : row.expires_at,
252
275
  };
@@ -271,8 +294,8 @@ export class PostgresBillingStore extends BillingStore {
271
294
  },
272
295
  };
273
296
  }
274
- async resolveBillingOffer(provider, productId, priceId) {
275
- const r = await this.billingOffer.resolveByPrice(provider, priceId ?? null, productId ?? null);
297
+ async resolveBillingOffer(provider, productId, priceId, checkout) {
298
+ const r = await this.billingOffer.resolveByPrice(provider, priceId ?? null, productId ?? null, checkout);
276
299
  return this.rowToOffer(r);
277
300
  }
278
301
  async resolveBillingOfferByLookup(provider, lookupKey) {
@@ -345,6 +368,9 @@ export class PostgresBillingStore extends BillingStore {
345
368
  }
346
369
  return this.billingSubscriptionChange.create(subscription.id, input);
347
370
  }
371
+ async findBillingSubscriptionChange(provider, subscriptionId, operationKey) {
372
+ return this.billingSubscriptionChange.find(provider, subscriptionId, operationKey);
373
+ }
348
374
  async getOpenBillingSubscriptionChange(provider, providerSubscriptionId) {
349
375
  return this.billingSubscriptionChange.getOpen(provider, providerSubscriptionId);
350
376
  }
@@ -352,11 +378,11 @@ export class PostgresBillingStore extends BillingStore {
352
378
  const rows = await this.billingSubscription.listExpiredGraceSubscriptions(now, limit);
353
379
  return rows.map((row) => this.rowToSubscriptionState(row));
354
380
  }
355
- async reconcileSubscriptionEntitlement(subjectId, subscriptionId, billingEventId, expectedStatus, expectedProviderUpdatedAt, planAssignedAt, applyEntitlement, terminalPlanKey, reason) {
356
- return this.billingSubscription.reconcileEntitlement(subjectId, subscriptionId, billingEventId, expectedStatus, expectedProviderUpdatedAt, planAssignedAt, applyEntitlement, terminalPlanKey, reason);
381
+ async reconcileSubscriptionEntitlement(subjectId, subscriptionId, billingEventId, expectedStatus, expectedProviderUpdatedAt, planAssignedAt, applyEntitlement, reason) {
382
+ return this.billingSubscription.reconcileEntitlement(subjectId, subscriptionId, billingEventId, expectedStatus, expectedProviderUpdatedAt, planAssignedAt, applyEntitlement, reason);
357
383
  }
358
- async expireSubscriptionGracePeriod(subjectId, subscriptionId, expectedGraceEndsAt, expiredAt, terminalPlanKey) {
359
- return this.billingSubscription.expireGracePeriod(subjectId, subscriptionId, expectedGraceEndsAt, expiredAt, terminalPlanKey);
384
+ async expireSubscriptionGracePeriod(subjectId, subscriptionId, expectedGraceEndsAt, expiredAt) {
385
+ return this.billingSubscription.expireGracePeriod(subjectId, subscriptionId, expectedGraceEndsAt, expiredAt);
360
386
  }
361
387
  async updateBillingSubscriptionChange(id, update) {
362
388
  await this.billingSubscriptionChange.update(id, update);
@@ -385,6 +411,7 @@ export class PostgresBillingStore extends BillingStore {
385
411
  creditsPerUnit: r.credits_per_unit,
386
412
  depositTo: r.bucket_key,
387
413
  amountMinor: r.amount_minor,
414
+ taxBehavior: r.tax_behavior,
388
415
  currency: r.currency,
389
416
  minQuantity: r.min_quantity,
390
417
  maxQuantity: r.max_quantity,
@@ -393,29 +420,16 @@ export class PostgresBillingStore extends BillingStore {
393
420
  maxAmountMinor,
394
421
  };
395
422
  }
396
- async resolveCreditTopup(provider, productId, priceId) {
397
- const r = await this.billingTopup.resolveByPrice(provider, priceId ?? null, productId ?? null);
423
+ async resolveCreditTopup(provider, productId, priceId, checkoutIntentId, subjectId, providerPaymentId, rechargeAttemptId, billingEventId) {
424
+ const r = await this.billingTopup.resolveByPrice(provider, priceId ?? null, productId ?? null, checkoutIntentId, subjectId, providerPaymentId, rechargeAttemptId, billingEventId);
398
425
  return this.rowToTopup(r);
399
426
  }
400
427
  async resolveCreditTopupByLookup(provider, lookupKey) {
401
428
  const r = await this.billingTopup.resolveByLookup(provider, lookupKey);
402
429
  return this.rowToTopup(r);
403
430
  }
404
- async computeTopupCredits(amountMinor, topupConfig) {
405
- if (!Number.isSafeInteger(amountMinor) || amountMinor < 0) {
406
- throw new TypeError("amountMinor must be a non-negative safe integer");
407
- }
408
- const unitAmount = topupConfig.amountMinor;
409
- if (unitAmount <= 0 || amountMinor % unitAmount !== 0)
410
- return new Decimal(0);
411
- const quantity = amountMinor / unitAmount;
412
- if (quantity < topupConfig.minQuantity || quantity > topupConfig.maxQuantity) {
413
- return new Decimal(0);
414
- }
415
- return topupConfig.creditsPerUnit.mul(quantity);
416
- }
417
431
  async upsertBillingPayment(options) {
418
- return this.billingPayment.upsert(options.provider, options.providerPaymentId, options.providerInvoiceId ?? null, options.userId, options.amountMinor, options.taxMinor, options.currency, options.purpose, options.metadata ? JSON.stringify(options.metadata) : null, options.status, options.providerUpdatedAt);
432
+ return this.billingPayment.upsert(options);
419
433
  }
420
434
  async createBillingCreditGrant(input) {
421
435
  const rows = await this.queryFn("SELECT bursar.create_billing_credit_grant($1::uuid, $2::uuid, $3::uuid, $4, $5, $6::uuid) AS id", [
@@ -465,8 +479,11 @@ export class PostgresBillingStore extends BillingStore {
465
479
  }
466
480
  await this.billingInvoice.upsert(options.userId, options.provider, options.providerInvoiceId, subscription?.id ? String(subscription.id) : null, options.status, options.amountDueMinor, options.amountPaidMinor, options.currency, options.periodStart ?? null, options.periodEnd ?? null, options.metadata ?? {}, options.providerUpdatedAt);
467
481
  }
468
- async listBillingInvoices(userId) {
469
- return this.billingInvoice.listForUser(userId);
482
+ async listBillingInvoices(userId, options) {
483
+ return this.billingInvoice.listForUser(userId, options);
484
+ }
485
+ async hasBillingInvoice(userId, provider, providerInvoiceId) {
486
+ return this.billingInvoice.hasForUser(userId, provider, providerInvoiceId);
470
487
  }
471
488
  async upsertBillingDispute(options) {
472
489
  const payment = await this.billingPayment.getForRefund(options.provider, options.providerPaymentId);
@@ -505,6 +522,7 @@ export class PostgresBillingStore extends BillingStore {
505
522
  }
506
523
  rowToSubscriptionState(r) {
507
524
  return {
525
+ cycleGrantCredits: r.cycle_grant_amount == null ? null : new PricingDecimal(r.cycle_grant_amount),
508
526
  subscriptionId: r.id,
509
527
  userId: r.user_id,
510
528
  provider: r.provider,
@@ -529,16 +547,18 @@ export class PostgresBillingStore extends BillingStore {
529
547
  metadata: r.metadata,
530
548
  };
531
549
  }
532
- async getActiveCatalogDocument() {
550
+ async getActiveCatalogRevision() {
533
551
  const rows = await this.queryFn("SELECT * FROM bursar.active_catalog_revision()", []);
534
- const row = optionalRecordRow(rows, "PostgresBillingStore.getActiveCatalogDocument");
552
+ const row = optionalRecordRow(rows, "PostgresBillingStore.getActiveCatalogRevision");
535
553
  if (row === null)
536
554
  return null;
537
- const sourceDocument = row.source_document;
538
- if (sourceDocument === undefined) {
539
- throw new StoreError("active catalog revision returned no source document");
540
- }
541
- return safeParse(z.record(z.string(), z.json()), sourceDocument, "PostgresBillingStore.getActiveCatalogDocument");
555
+ return {
556
+ id: safeParse(postgresUuid, row.id, "PostgresBillingStore.getActiveCatalogRevision.id"),
557
+ config: safeParse(z.record(z.string(), z.json()), row.source_document, "PostgresBillingStore.getActiveCatalogRevision.config"),
558
+ };
559
+ }
560
+ async getActiveCatalogDocument() {
561
+ return (await this.getActiveCatalogRevision())?.config ?? null;
542
562
  }
543
563
  async pseudonymizeFinancialSubject(userId) {
544
564
  const rows = await this.queryFn(`SELECT bursar.pseudonymize_financial_subject($1::uuid) AS pseudonymized`, [userId]);
@@ -556,6 +576,9 @@ export class PostgresBillingStore extends BillingStore {
556
576
  async upsertAutoRechargeProfile(profile, options) {
557
577
  return this.billingAutoRecharge.upsertProfile(profile, options);
558
578
  }
579
+ async resumeAutoRechargeProfile(profile) {
580
+ return this.billingAutoRecharge.resumeProfile(profile);
581
+ }
559
582
  async claimAutoRechargeAttempt(input) {
560
583
  return this.billingAutoRecharge.claimAttempt(input);
561
584
  }
@@ -565,6 +588,9 @@ export class PostgresBillingStore extends BillingStore {
565
588
  async updateAutoRechargeAttemptByProviderPayment(input) {
566
589
  return this.billingAutoRecharge.updateAttemptByProviderPayment(input);
567
590
  }
591
+ async getAutoRechargeOperationalWindow(userId) {
592
+ return this.billingAutoRecharge.operationalWindow(userId);
593
+ }
568
594
  async countAutoRechargeAttempts(userId, since) {
569
595
  return this.billingAutoRecharge.countAttempts(userId, since);
570
596
  }