@zonastery/bursar 0.0.2 → 1.0.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 (493) hide show
  1. package/README.md +85 -344
  2. package/dist/billing/auto-recharge-port.d.ts +27 -0
  3. package/dist/billing/auto-recharge-port.d.ts.map +1 -0
  4. package/dist/billing/auto-recharge-port.js +2 -0
  5. package/dist/billing/auto-recharge-port.js.map +1 -0
  6. package/dist/billing/auto-recharge-service.d.ts +43 -0
  7. package/dist/billing/auto-recharge-service.d.ts.map +1 -0
  8. package/dist/billing/auto-recharge-service.js +218 -0
  9. package/dist/billing/auto-recharge-service.js.map +1 -0
  10. package/dist/billing/billing-service.d.ts +71 -0
  11. package/dist/billing/billing-service.d.ts.map +1 -0
  12. package/dist/billing/billing-service.js +172 -0
  13. package/dist/billing/billing-service.js.map +1 -0
  14. package/dist/billing/billing-store.d.ts +45 -13
  15. package/dist/billing/billing-store.d.ts.map +1 -1
  16. package/dist/billing/billing-store.js.map +1 -1
  17. package/dist/billing/contracts.d.ts +149 -0
  18. package/dist/billing/contracts.d.ts.map +1 -0
  19. package/dist/billing/contracts.js +2 -0
  20. package/dist/billing/contracts.js.map +1 -0
  21. package/dist/billing/event-handlers.d.ts +57 -0
  22. package/dist/billing/event-handlers.d.ts.map +1 -0
  23. package/dist/billing/event-handlers.js +667 -0
  24. package/dist/billing/event-handlers.js.map +1 -0
  25. package/dist/billing/event-processor.d.ts +19 -0
  26. package/dist/billing/event-processor.d.ts.map +1 -0
  27. package/dist/billing/event-processor.js +105 -0
  28. package/dist/billing/event-processor.js.map +1 -0
  29. package/dist/billing/financial-event-handlers.d.ts +26 -0
  30. package/dist/billing/financial-event-handlers.d.ts.map +1 -0
  31. package/dist/billing/financial-event-handlers.js +241 -0
  32. package/dist/billing/financial-event-handlers.js.map +1 -0
  33. package/dist/billing/index.d.ts +8 -5
  34. package/dist/billing/index.d.ts.map +1 -1
  35. package/dist/billing/index.js +4 -4
  36. package/dist/billing/index.js.map +1 -1
  37. package/dist/billing/management.d.ts +59 -0
  38. package/dist/billing/management.d.ts.map +1 -0
  39. package/dist/billing/management.js +113 -0
  40. package/dist/billing/management.js.map +1 -0
  41. package/dist/billing/policy-window.d.ts +24 -0
  42. package/dist/billing/policy-window.d.ts.map +1 -0
  43. package/dist/billing/policy-window.js +88 -0
  44. package/dist/billing/policy-window.js.map +1 -0
  45. package/dist/billing/postgres/repositories/auto-recharge.d.ts +32 -0
  46. package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -0
  47. package/dist/billing/postgres/repositories/auto-recharge.js +178 -0
  48. package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -0
  49. package/dist/billing/postgres/repositories/customer.d.ts +12 -0
  50. package/dist/billing/postgres/repositories/customer.d.ts.map +1 -0
  51. package/dist/billing/postgres/repositories/customer.js +33 -0
  52. package/dist/billing/postgres/repositories/customer.js.map +1 -0
  53. package/dist/billing/postgres/repositories/dispute.d.ts +7 -0
  54. package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -0
  55. package/dist/billing/postgres/repositories/dispute.js +18 -0
  56. package/dist/billing/postgres/repositories/dispute.js.map +1 -0
  57. package/dist/billing/postgres/repositories/event.d.ts +21 -0
  58. package/dist/billing/postgres/repositories/event.d.ts.map +1 -0
  59. package/dist/billing/postgres/repositories/event.js +47 -0
  60. package/dist/billing/postgres/repositories/event.js.map +1 -0
  61. package/dist/billing/postgres/repositories/invoice.d.ts +9 -0
  62. package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -0
  63. package/dist/billing/postgres/repositories/invoice.js +36 -0
  64. package/dist/billing/postgres/repositories/invoice.js.map +1 -0
  65. package/dist/billing/postgres/repositories/offer.d.ts +26 -0
  66. package/dist/billing/postgres/repositories/offer.d.ts.map +1 -0
  67. package/dist/billing/postgres/repositories/offer.js +73 -0
  68. package/dist/billing/postgres/repositories/offer.js.map +1 -0
  69. package/dist/billing/postgres/repositories/payment.d.ts +32 -0
  70. package/dist/billing/postgres/repositories/payment.d.ts.map +1 -0
  71. package/dist/billing/postgres/repositories/payment.js +78 -0
  72. package/dist/billing/postgres/repositories/payment.js.map +1 -0
  73. package/dist/billing/postgres/repositories/preferences.d.ts +15 -0
  74. package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -0
  75. package/dist/billing/postgres/repositories/preferences.js +23 -0
  76. package/dist/billing/postgres/repositories/preferences.js.map +1 -0
  77. package/dist/billing/postgres/repositories/refund.d.ts +7 -0
  78. package/dist/billing/postgres/repositories/refund.d.ts.map +1 -0
  79. package/dist/billing/postgres/repositories/refund.js +32 -0
  80. package/dist/billing/postgres/repositories/refund.js.map +1 -0
  81. package/dist/billing/postgres/repositories/subscription.d.ts +46 -0
  82. package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -0
  83. package/dist/billing/postgres/repositories/subscription.js +187 -0
  84. package/dist/billing/postgres/repositories/subscription.js.map +1 -0
  85. package/dist/billing/postgres/repositories/topup.d.ts +27 -0
  86. package/dist/billing/postgres/repositories/topup.d.ts.map +1 -0
  87. package/dist/billing/postgres/repositories/topup.js +51 -0
  88. package/dist/billing/postgres/repositories/topup.js.map +1 -0
  89. package/dist/billing/postgres/store.d.ts +87 -0
  90. package/dist/billing/postgres/store.d.ts.map +1 -0
  91. package/dist/billing/postgres/store.js +527 -0
  92. package/dist/billing/postgres/store.js.map +1 -0
  93. package/dist/billing/service-types.d.ts +37 -0
  94. package/dist/billing/service-types.d.ts.map +1 -0
  95. package/dist/billing/service-types.js +2 -0
  96. package/dist/billing/service-types.js.map +1 -0
  97. package/dist/billing/types/auto-recharge.d.ts +56 -0
  98. package/dist/billing/types/auto-recharge.d.ts.map +1 -0
  99. package/dist/billing/types/auto-recharge.js +2 -0
  100. package/dist/billing/types/auto-recharge.js.map +1 -0
  101. package/dist/billing/types/catalog.d.ts +47 -0
  102. package/dist/billing/types/catalog.d.ts.map +1 -0
  103. package/dist/billing/types/catalog.js +2 -0
  104. package/dist/billing/types/catalog.js.map +1 -0
  105. package/dist/billing/types/common.d.ts +16 -0
  106. package/dist/billing/types/common.d.ts.map +1 -0
  107. package/dist/billing/types/common.js +2 -0
  108. package/dist/billing/types/common.js.map +1 -0
  109. package/dist/billing/types/documents.d.ts +39 -0
  110. package/dist/billing/types/documents.d.ts.map +1 -0
  111. package/dist/billing/types/documents.js +2 -0
  112. package/dist/billing/types/documents.js.map +1 -0
  113. package/dist/billing/types/events.d.ts +75 -0
  114. package/dist/billing/types/events.d.ts.map +1 -0
  115. package/dist/billing/types/events.js +38 -0
  116. package/dist/billing/types/events.js.map +1 -0
  117. package/dist/billing/types/index.d.ts +8 -0
  118. package/dist/billing/types/index.d.ts.map +1 -0
  119. package/dist/billing/types/index.js +8 -0
  120. package/dist/billing/types/index.js.map +1 -0
  121. package/dist/billing/types/preferences.d.ts +13 -0
  122. package/dist/billing/types/preferences.d.ts.map +1 -0
  123. package/dist/billing/types/preferences.js +2 -0
  124. package/dist/billing/types/preferences.js.map +1 -0
  125. package/dist/billing/types/subscriptions.d.ts +97 -0
  126. package/dist/billing/types/subscriptions.d.ts.map +1 -0
  127. package/dist/billing/types/subscriptions.js +12 -0
  128. package/dist/billing/types/subscriptions.js.map +1 -0
  129. package/dist/breakdown.d.ts +3 -1
  130. package/dist/breakdown.d.ts.map +1 -1
  131. package/dist/breakdown.js +4 -1
  132. package/dist/breakdown.js.map +1 -1
  133. package/dist/browser.d.ts +8 -0
  134. package/dist/browser.d.ts.map +1 -0
  135. package/dist/browser.js +7 -0
  136. package/dist/browser.js.map +1 -0
  137. package/dist/bursar.d.ts +80 -0
  138. package/dist/bursar.d.ts.map +1 -0
  139. package/dist/bursar.js +129 -0
  140. package/dist/bursar.js.map +1 -0
  141. package/dist/catalog.d.ts +61 -0
  142. package/dist/catalog.d.ts.map +1 -0
  143. package/dist/catalog.js +95 -0
  144. package/dist/catalog.js.map +1 -0
  145. package/dist/commerce/errors.d.ts +69 -0
  146. package/dist/commerce/errors.d.ts.map +1 -0
  147. package/dist/commerce/errors.js +107 -0
  148. package/dist/commerce/errors.js.map +1 -0
  149. package/dist/commerce/index.d.ts +6 -0
  150. package/dist/commerce/index.d.ts.map +1 -0
  151. package/dist/commerce/index.js +6 -0
  152. package/dist/commerce/index.js.map +1 -0
  153. package/dist/commerce/plan-change.d.ts +10 -0
  154. package/dist/commerce/plan-change.d.ts.map +1 -0
  155. package/dist/commerce/plan-change.js +42 -0
  156. package/dist/commerce/plan-change.js.map +1 -0
  157. package/dist/commerce/provider-registry.d.ts +18 -0
  158. package/dist/commerce/provider-registry.d.ts.map +1 -0
  159. package/dist/commerce/provider-registry.js +70 -0
  160. package/dist/commerce/provider-registry.js.map +1 -0
  161. package/dist/commerce/service.d.ts +73 -0
  162. package/dist/commerce/service.d.ts.map +1 -0
  163. package/dist/commerce/service.js +981 -0
  164. package/dist/commerce/service.js.map +1 -0
  165. package/dist/commerce/types.d.ts +217 -0
  166. package/dist/commerce/types.d.ts.map +1 -0
  167. package/dist/commerce/types.js.map +1 -0
  168. package/dist/config/parse-access.d.ts +6 -0
  169. package/dist/config/parse-access.d.ts.map +1 -0
  170. package/dist/config/parse-access.js +72 -0
  171. package/dist/config/parse-access.js.map +1 -0
  172. package/dist/config/parse-commerce.d.ts +3 -0
  173. package/dist/config/parse-commerce.d.ts.map +1 -0
  174. package/dist/config/parse-commerce.js +236 -0
  175. package/dist/config/parse-commerce.js.map +1 -0
  176. package/dist/config/parse-credits.d.ts +3 -0
  177. package/dist/config/parse-credits.d.ts.map +1 -0
  178. package/dist/config/parse-credits.js +99 -0
  179. package/dist/config/parse-credits.js.map +1 -0
  180. package/dist/config/parse-plans.d.ts +5 -0
  181. package/dist/config/parse-plans.d.ts.map +1 -0
  182. package/dist/config/parse-plans.js +126 -0
  183. package/dist/config/parse-plans.js.map +1 -0
  184. package/dist/config/parse-pricing.d.ts +4 -0
  185. package/dist/config/parse-pricing.d.ts.map +1 -0
  186. package/dist/config/parse-pricing.js +252 -0
  187. package/dist/config/parse-pricing.js.map +1 -0
  188. package/dist/config/parse-utils.d.ts +18 -0
  189. package/dist/config/parse-utils.d.ts.map +1 -0
  190. package/dist/config/parse-utils.js +118 -0
  191. package/dist/config/parse-utils.js.map +1 -0
  192. package/dist/config/types.d.ts +327 -0
  193. package/dist/config/types.d.ts.map +1 -0
  194. package/dist/config/types.js +2 -0
  195. package/dist/config/types.js.map +1 -0
  196. package/dist/config.d.ts +6 -23
  197. package/dist/config.d.ts.map +1 -1
  198. package/dist/config.js +86 -385
  199. package/dist/config.js.map +1 -1
  200. package/dist/{stores → credits}/events.d.ts +6 -6
  201. package/dist/credits/events.d.ts.map +1 -0
  202. package/dist/{stores → credits}/events.js +5 -5
  203. package/dist/credits/events.js.map +1 -0
  204. package/dist/credits/lease-workflow.d.ts +90 -0
  205. package/dist/credits/lease-workflow.d.ts.map +1 -0
  206. package/dist/credits/lease-workflow.js +378 -0
  207. package/dist/credits/lease-workflow.js.map +1 -0
  208. package/dist/credits/low-balance-monitor.d.ts +26 -0
  209. package/dist/credits/low-balance-monitor.d.ts.map +1 -0
  210. package/dist/credits/low-balance-monitor.js +110 -0
  211. package/dist/credits/low-balance-monitor.js.map +1 -0
  212. package/dist/credits/postgres/repositories/analytics.d.ts +77 -0
  213. package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -0
  214. package/dist/credits/postgres/repositories/analytics.js +151 -0
  215. package/dist/credits/postgres/repositories/analytics.js.map +1 -0
  216. package/dist/credits/postgres/repositories/balance.d.ts +66 -0
  217. package/dist/credits/postgres/repositories/balance.d.ts.map +1 -0
  218. package/dist/credits/postgres/repositories/balance.js +139 -0
  219. package/dist/credits/postgres/repositories/balance.js.map +1 -0
  220. package/dist/credits/postgres/repositories/bucket.d.ts +25 -0
  221. package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -0
  222. package/dist/credits/postgres/repositories/bucket.js +52 -0
  223. package/dist/credits/postgres/repositories/bucket.js.map +1 -0
  224. package/dist/credits/postgres/repositories/deduction.d.ts +55 -0
  225. package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -0
  226. package/dist/credits/postgres/repositories/deduction.js +130 -0
  227. package/dist/credits/postgres/repositories/deduction.js.map +1 -0
  228. package/dist/credits/postgres/repositories/lease.d.ts +69 -0
  229. package/dist/credits/postgres/repositories/lease.d.ts.map +1 -0
  230. package/dist/credits/postgres/repositories/lease.js +148 -0
  231. package/dist/credits/postgres/repositories/lease.js.map +1 -0
  232. package/dist/credits/postgres/repositories/plan.d.ts +105 -0
  233. package/dist/credits/postgres/repositories/plan.d.ts.map +1 -0
  234. package/dist/credits/postgres/repositories/plan.js +182 -0
  235. package/dist/credits/postgres/repositories/plan.js.map +1 -0
  236. package/dist/credits/postgres/repositories/pricing.d.ts +28 -0
  237. package/dist/credits/postgres/repositories/pricing.d.ts.map +1 -0
  238. package/dist/credits/postgres/repositories/pricing.js +70 -0
  239. package/dist/credits/postgres/repositories/pricing.js.map +1 -0
  240. package/dist/credits/postgres/repositories/team.d.ts +63 -0
  241. package/dist/credits/postgres/repositories/team.d.ts.map +1 -0
  242. package/dist/credits/postgres/repositories/team.js +120 -0
  243. package/dist/credits/postgres/repositories/team.js.map +1 -0
  244. package/dist/credits/postgres/store.d.ts +84 -0
  245. package/dist/credits/postgres/store.d.ts.map +1 -0
  246. package/dist/credits/postgres/store.js +765 -0
  247. package/dist/credits/postgres/store.js.map +1 -0
  248. package/dist/credits/postgres/value-mappers.d.ts +17 -0
  249. package/dist/credits/postgres/value-mappers.d.ts.map +1 -0
  250. package/dist/credits/postgres/value-mappers.js +98 -0
  251. package/dist/credits/postgres/value-mappers.js.map +1 -0
  252. package/dist/credits/pricing-runtime.d.ts +34 -0
  253. package/dist/credits/pricing-runtime.d.ts.map +1 -0
  254. package/dist/credits/pricing-runtime.js +141 -0
  255. package/dist/credits/pricing-runtime.js.map +1 -0
  256. package/dist/credits/queries.d.ts +33 -0
  257. package/dist/credits/queries.d.ts.map +1 -0
  258. package/dist/credits/queries.js +71 -0
  259. package/dist/credits/queries.js.map +1 -0
  260. package/dist/credits/service-errors.d.ts +4 -0
  261. package/dist/credits/service-errors.d.ts.map +1 -0
  262. package/dist/credits/service-errors.js +57 -0
  263. package/dist/credits/service-errors.js.map +1 -0
  264. package/dist/credits/service-types.d.ts +122 -0
  265. package/dist/credits/service-types.d.ts.map +1 -0
  266. package/dist/credits/service-types.js +2 -0
  267. package/dist/credits/service-types.js.map +1 -0
  268. package/dist/credits/service.d.ts +207 -0
  269. package/dist/credits/service.d.ts.map +1 -0
  270. package/dist/credits/service.js +594 -0
  271. package/dist/credits/service.js.map +1 -0
  272. package/dist/credits/store.d.ts +167 -0
  273. package/dist/credits/store.d.ts.map +1 -0
  274. package/dist/{stores/credit-store.js → credits/store.js} +23 -43
  275. package/dist/credits/store.js.map +1 -0
  276. package/dist/credits/types/account.d.ts +82 -0
  277. package/dist/credits/types/account.d.ts.map +1 -0
  278. package/dist/credits/types/account.js +2 -0
  279. package/dist/credits/types/account.js.map +1 -0
  280. package/dist/credits/types/admission.d.ts +39 -0
  281. package/dist/credits/types/admission.d.ts.map +1 -0
  282. package/dist/credits/types/admission.js +2 -0
  283. package/dist/credits/types/admission.js.map +1 -0
  284. package/dist/credits/types/analytics.d.ts +52 -0
  285. package/dist/credits/types/analytics.d.ts.map +1 -0
  286. package/dist/credits/types/analytics.js +2 -0
  287. package/dist/credits/types/analytics.js.map +1 -0
  288. package/dist/credits/types/catalog.d.ts +69 -0
  289. package/dist/credits/types/catalog.d.ts.map +1 -0
  290. package/dist/credits/types/catalog.js +2 -0
  291. package/dist/credits/types/catalog.js.map +1 -0
  292. package/dist/credits/types/grants.d.ts +24 -0
  293. package/dist/credits/types/grants.d.ts.map +1 -0
  294. package/dist/credits/types/grants.js +2 -0
  295. package/dist/credits/types/grants.js.map +1 -0
  296. package/dist/credits/types/index.d.ts +9 -0
  297. package/dist/credits/types/index.d.ts.map +1 -0
  298. package/dist/credits/types/index.js +2 -0
  299. package/dist/credits/types/index.js.map +1 -0
  300. package/dist/credits/types/ledger.d.ts +77 -0
  301. package/dist/credits/types/ledger.d.ts.map +1 -0
  302. package/dist/credits/types/ledger.js +2 -0
  303. package/dist/credits/types/ledger.js.map +1 -0
  304. package/dist/credits/types/quotas.d.ts +40 -0
  305. package/dist/credits/types/quotas.d.ts.map +1 -0
  306. package/dist/credits/types/quotas.js +2 -0
  307. package/dist/credits/types/quotas.js.map +1 -0
  308. package/dist/credits/types/teams.d.ts +44 -0
  309. package/dist/credits/types/teams.d.ts.map +1 -0
  310. package/dist/credits/types/teams.js +2 -0
  311. package/dist/credits/types/teams.js.map +1 -0
  312. package/dist/engine.d.ts +18 -39
  313. package/dist/engine.d.ts.map +1 -1
  314. package/dist/engine.js +169 -145
  315. package/dist/engine.js.map +1 -1
  316. package/dist/errors.d.ts +84 -25
  317. package/dist/errors.d.ts.map +1 -1
  318. package/dist/errors.js +117 -24
  319. package/dist/errors.js.map +1 -1
  320. package/dist/expr/ast.d.ts +46 -0
  321. package/dist/expr/ast.d.ts.map +1 -0
  322. package/dist/expr/ast.js +18 -0
  323. package/dist/expr/ast.js.map +1 -0
  324. package/dist/expr/evaluator.d.ts +4 -0
  325. package/dist/expr/evaluator.d.ts.map +1 -0
  326. package/dist/expr/evaluator.js +144 -0
  327. package/dist/expr/evaluator.js.map +1 -0
  328. package/dist/expr/language.d.ts +5 -0
  329. package/dist/expr/language.d.ts.map +1 -0
  330. package/dist/expr/language.js +73 -0
  331. package/dist/expr/language.js.map +1 -0
  332. package/dist/expr/parser.d.ts +24 -0
  333. package/dist/expr/parser.d.ts.map +1 -0
  334. package/dist/expr/parser.js +175 -0
  335. package/dist/expr/parser.js.map +1 -0
  336. package/dist/expr/tokenizer.d.ts +7 -0
  337. package/dist/expr/tokenizer.d.ts.map +1 -0
  338. package/dist/expr/tokenizer.js +85 -0
  339. package/dist/expr/tokenizer.js.map +1 -0
  340. package/dist/expr.d.ts +7 -58
  341. package/dist/expr.d.ts.map +1 -1
  342. package/dist/expr.js +41 -640
  343. package/dist/expr.js.map +1 -1
  344. package/dist/generated/pricing-config.schema.json +2764 -0
  345. package/dist/generated/pricing-config.types.d.ts +674 -0
  346. package/dist/generated/pricing-config.types.d.ts.map +1 -0
  347. package/dist/generated/pricing-config.types.js +8 -0
  348. package/dist/generated/pricing-config.types.js.map +1 -0
  349. package/dist/index.d.ts +21 -17
  350. package/dist/index.d.ts.map +1 -1
  351. package/dist/index.js +12 -10
  352. package/dist/index.js.map +1 -1
  353. package/dist/load-pricing-file.d.ts.map +1 -1
  354. package/dist/load-pricing-file.js.map +1 -1
  355. package/dist/metrics.d.ts +6 -16
  356. package/dist/metrics.d.ts.map +1 -1
  357. package/dist/node.d.ts +9 -2
  358. package/dist/node.d.ts.map +1 -1
  359. package/dist/node.js +6 -2
  360. package/dist/node.js.map +1 -1
  361. package/dist/providers/_shared.d.ts +9 -0
  362. package/dist/providers/_shared.d.ts.map +1 -0
  363. package/dist/providers/_shared.js +25 -0
  364. package/dist/providers/_shared.js.map +1 -0
  365. package/dist/providers/dodo/client-contract.d.ts +106 -0
  366. package/dist/providers/dodo/client-contract.d.ts.map +1 -0
  367. package/dist/providers/dodo/client-contract.js +2 -0
  368. package/dist/providers/dodo/client-contract.js.map +1 -0
  369. package/dist/providers/dodo/event-mapper.d.ts +5 -3
  370. package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
  371. package/dist/providers/dodo/event-mapper.js +213 -38
  372. package/dist/providers/dodo/event-mapper.js.map +1 -1
  373. package/dist/providers/dodo/index.d.ts +1 -0
  374. package/dist/providers/dodo/index.d.ts.map +1 -1
  375. package/dist/providers/dodo/index.js.map +1 -1
  376. package/dist/providers/dodo/provider.d.ts +24 -14
  377. package/dist/providers/dodo/provider.d.ts.map +1 -1
  378. package/dist/providers/dodo/provider.js +249 -31
  379. package/dist/providers/dodo/provider.js.map +1 -1
  380. package/dist/providers/index.d.ts +3 -1
  381. package/dist/providers/index.d.ts.map +1 -1
  382. package/dist/providers/index.js +1 -0
  383. package/dist/providers/index.js.map +1 -1
  384. package/dist/providers/mock/provider.d.ts +14 -10
  385. package/dist/providers/mock/provider.d.ts.map +1 -1
  386. package/dist/providers/mock/provider.js +78 -10
  387. package/dist/providers/mock/provider.js.map +1 -1
  388. package/dist/providers/stripe/event-mapper.d.ts +3 -3
  389. package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
  390. package/dist/providers/stripe/event-mapper.js +139 -30
  391. package/dist/providers/stripe/event-mapper.js.map +1 -1
  392. package/dist/providers/stripe/provider.d.ts +21 -12
  393. package/dist/providers/stripe/provider.d.ts.map +1 -1
  394. package/dist/providers/stripe/provider.js +175 -17
  395. package/dist/providers/stripe/provider.js.map +1 -1
  396. package/dist/providers/types.d.ts +128 -32
  397. package/dist/providers/types.d.ts.map +1 -1
  398. package/dist/providers/types.js +17 -1
  399. package/dist/providers/types.js.map +1 -1
  400. package/dist/retry.d.ts +13 -0
  401. package/dist/retry.d.ts.map +1 -0
  402. package/dist/retry.js +28 -0
  403. package/dist/retry.js.map +1 -0
  404. package/dist/shared/logger.d.ts +10 -0
  405. package/dist/shared/logger.d.ts.map +1 -0
  406. package/dist/shared/logger.js +15 -0
  407. package/dist/shared/logger.js.map +1 -0
  408. package/dist/shared/postgres-client.d.ts +51 -0
  409. package/dist/shared/postgres-client.d.ts.map +1 -0
  410. package/dist/shared/postgres-client.js +101 -0
  411. package/dist/shared/postgres-client.js.map +1 -0
  412. package/dist/shared/postgres-types.d.ts +7 -0
  413. package/dist/shared/postgres-types.d.ts.map +1 -0
  414. package/dist/shared/postgres-types.js +2 -0
  415. package/dist/shared/postgres-types.js.map +1 -0
  416. package/dist/shared/postgres-validation.d.ts +8 -0
  417. package/dist/shared/postgres-validation.d.ts.map +1 -0
  418. package/dist/shared/postgres-validation.js +40 -0
  419. package/dist/shared/postgres-validation.js.map +1 -0
  420. package/dist/storage/adapters/clickhouse.d.ts +61 -0
  421. package/dist/storage/adapters/clickhouse.d.ts.map +1 -0
  422. package/dist/storage/adapters/clickhouse.js +329 -0
  423. package/dist/storage/adapters/clickhouse.js.map +1 -0
  424. package/dist/storage/adapters/s3.d.ts +35 -0
  425. package/dist/storage/adapters/s3.d.ts.map +1 -0
  426. package/dist/storage/adapters/s3.js +100 -0
  427. package/dist/storage/adapters/s3.js.map +1 -0
  428. package/dist/storage/outbox-worker.d.ts +40 -0
  429. package/dist/storage/outbox-worker.d.ts.map +1 -0
  430. package/dist/storage/outbox-worker.js +132 -0
  431. package/dist/storage/outbox-worker.js.map +1 -0
  432. package/dist/storage/ports.d.ts +77 -0
  433. package/dist/storage/ports.d.ts.map +1 -0
  434. package/dist/storage/ports.js +2 -0
  435. package/dist/storage/ports.js.map +1 -0
  436. package/dist/storage/postgres-repository.d.ts +14 -0
  437. package/dist/storage/postgres-repository.d.ts.map +1 -0
  438. package/dist/storage/postgres-repository.js +130 -0
  439. package/dist/storage/postgres-repository.js.map +1 -0
  440. package/dist/storage/runtime.d.ts +67 -0
  441. package/dist/storage/runtime.d.ts.map +1 -0
  442. package/dist/storage/runtime.js +313 -0
  443. package/dist/storage/runtime.js.map +1 -0
  444. package/package.json +36 -20
  445. package/dist/allowance.d.ts +0 -48
  446. package/dist/allowance.d.ts.map +0 -1
  447. package/dist/allowance.js +0 -140
  448. package/dist/allowance.js.map +0 -1
  449. package/dist/billing/billing-manager.d.ts +0 -66
  450. package/dist/billing/billing-manager.d.ts.map +0 -1
  451. package/dist/billing/billing-manager.js +0 -560
  452. package/dist/billing/billing-manager.js.map +0 -1
  453. package/dist/billing/billing-types.d.ts +0 -203
  454. package/dist/billing/billing-types.d.ts.map +0 -1
  455. package/dist/billing/billing-types.js +0 -6
  456. package/dist/billing/billing-types.js.map +0 -1
  457. package/dist/billing/memory-billing-store.d.ts +0 -37
  458. package/dist/billing/memory-billing-store.d.ts.map +0 -1
  459. package/dist/billing/memory-billing-store.js +0 -197
  460. package/dist/billing/memory-billing-store.js.map +0 -1
  461. package/dist/billing/postgres-billing-store.d.ts +0 -35
  462. package/dist/billing/postgres-billing-store.d.ts.map +0 -1
  463. package/dist/billing/postgres-billing-store.js +0 -274
  464. package/dist/billing/postgres-billing-store.js.map +0 -1
  465. package/dist/billing/supabase-billing-store.d.ts +0 -29
  466. package/dist/billing/supabase-billing-store.d.ts.map +0 -1
  467. package/dist/billing/supabase-billing-store.js +0 -250
  468. package/dist/billing/supabase-billing-store.js.map +0 -1
  469. package/dist/manager.d.ts +0 -444
  470. package/dist/manager.d.ts.map +0 -1
  471. package/dist/manager.js +0 -1153
  472. package/dist/manager.js.map +0 -1
  473. package/dist/stores/credit-store.d.ts +0 -198
  474. package/dist/stores/credit-store.d.ts.map +0 -1
  475. package/dist/stores/credit-store.js.map +0 -1
  476. package/dist/stores/events.d.ts.map +0 -1
  477. package/dist/stores/events.js.map +0 -1
  478. package/dist/stores/memory-store.d.ts +0 -187
  479. package/dist/stores/memory-store.d.ts.map +0 -1
  480. package/dist/stores/memory-store.js +0 -1890
  481. package/dist/stores/memory-store.js.map +0 -1
  482. package/dist/stores/postgres-store.d.ts +0 -100
  483. package/dist/stores/postgres-store.d.ts.map +0 -1
  484. package/dist/stores/postgres-store.js +0 -891
  485. package/dist/stores/postgres-store.js.map +0 -1
  486. package/dist/stores/supabase-store.d.ts +0 -87
  487. package/dist/stores/supabase-store.d.ts.map +0 -1
  488. package/dist/stores/supabase-store.js +0 -948
  489. package/dist/stores/supabase-store.js.map +0 -1
  490. package/dist/types.d.ts +0 -463
  491. package/dist/types.d.ts.map +0 -1
  492. package/dist/types.js.map +0 -1
  493. /package/dist/{types.js → commerce/types.js} +0 -0
@@ -0,0 +1,130 @@
1
+ import { z } from "zod";
2
+ import { pgBoolean, safeParse } from "../../../shared/postgres-validation.js";
3
+ export const DeductionRowSchema = z
4
+ .object({
5
+ entry_id: z.string().nullable().optional(),
6
+ amount: z
7
+ .union([z.string(), z.number()])
8
+ .nullable()
9
+ .optional(),
10
+ balance_after: z
11
+ .union([z.string(), z.number()])
12
+ .nullable()
13
+ .optional(),
14
+ allowance_consumed: z
15
+ .union([z.string(), z.number()])
16
+ .nullable()
17
+ .optional(),
18
+ idempotent: pgBoolean.nullable().optional(),
19
+ bucket_breakdown: z
20
+ .record(z.string(), z.union([z.string(), z.number()]))
21
+ .nullable()
22
+ .optional(),
23
+ error: z.string().nullable().optional(),
24
+ user_id: z.string().optional(),
25
+ })
26
+ .passthrough();
27
+ const RefundRowSchema = z
28
+ .object({
29
+ refund_entry_id: z.string().optional(),
30
+ user_id: z.string().optional(),
31
+ amount: z
32
+ .union([z.string(), z.number()])
33
+ .nullable()
34
+ .optional(),
35
+ new_balance: z
36
+ .union([z.string(), z.number()])
37
+ .nullable()
38
+ .optional(),
39
+ bucket_breakdown: z
40
+ .record(z.string(), z.union([z.string(), z.number()]))
41
+ .nullable()
42
+ .optional(),
43
+ error: z.string().nullable().optional(),
44
+ })
45
+ .passthrough();
46
+ const RevokeRowSchema = z
47
+ .object({
48
+ user_id: z.string().optional(),
49
+ amount: z
50
+ .union([z.string(), z.number()])
51
+ .nullable()
52
+ .optional(),
53
+ new_balance: z
54
+ .union([z.string(), z.number()])
55
+ .nullable()
56
+ .optional(),
57
+ bucket: z.string().nullable().optional(),
58
+ })
59
+ .passthrough();
60
+ /** Repository for credit deduction operations. */
61
+ export class DeductionRepository {
62
+ callproc;
63
+ constructor(callproc) {
64
+ this.callproc = callproc;
65
+ }
66
+ /** Atomically deduct credits with allowance, entitlement, quota, and credit-policy enforcement. */
67
+ async deductWithAllowance(params) {
68
+ const rows = await this.callproc("charge_usage_for_operation", [
69
+ params.userId,
70
+ params.operation,
71
+ params.amount,
72
+ params.idempotencyKey,
73
+ params.feature,
74
+ params.model,
75
+ params.region,
76
+ params.metadata,
77
+ params.measures,
78
+ params.dimensions,
79
+ ]);
80
+ const row = (rows?.[0] ?? {});
81
+ const details = row.error_code != null
82
+ ? undefined
83
+ : (await this.callproc("get_credit_operation_details", [
84
+ params.userId,
85
+ row.ledger_entry_id ?? null,
86
+ params.idempotencyKey,
87
+ ]))[0];
88
+ return safeParse(DeductionRowSchema, {
89
+ ...row,
90
+ user_id: params.userId,
91
+ entry_id: row.ledger_entry_id,
92
+ amount: row.charged,
93
+ allowance_consumed: row.allowance_covered,
94
+ balance_after: details?.balance_after ?? row.balance_after,
95
+ bucket_breakdown: details?.bucket_breakdown ?? row.bucket_breakdown,
96
+ idempotent: row.replayed,
97
+ error: row.error_code,
98
+ }, "DeductionRepository.deductWithAllowance");
99
+ }
100
+ /** Refund a previous credit deduction. */
101
+ async refundCredits(entryId, amount, idempotencyKey, reason, metadata) {
102
+ const rows = await this.callproc("refund_credit_by_entry", [
103
+ entryId,
104
+ amount,
105
+ idempotencyKey,
106
+ reason,
107
+ metadata,
108
+ ]);
109
+ const row = (rows?.[0] ?? {});
110
+ return safeParse(RefundRowSchema, {
111
+ ...row,
112
+ refund_entry_id: row.entry_id,
113
+ user_id: row.subject_id,
114
+ new_balance: row.balance_after,
115
+ error: row.error_code,
116
+ }, "DeductionRepository.refundCredits");
117
+ }
118
+ /** Revoke credits by transaction type. */
119
+ async revokeCreditsByEntryType(userId, entryType) {
120
+ const rows = await this.callproc("revoke_subject_credits_by_operation", [userId, entryType]);
121
+ const row = (rows?.[0] ?? {});
122
+ return safeParse(RevokeRowSchema, {
123
+ user_id: userId,
124
+ amount: row.revoked,
125
+ new_balance: row.balance_after,
126
+ bucket: null,
127
+ }, "DeductionRepository.revokeCreditsByEntryType");
128
+ }
129
+ }
130
+ //# sourceMappingURL=deduction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deduction.js","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/deduction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAE9E,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC;KAChC,MAAM,CAAC;IACN,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,Q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,MAAM,IAAI,CAAC,QAAQ,CAAC,wBAAwB,EAAE;YACzD,OAAO;YACP,MAAM;YACN,cAAc;YACd,MAAM;YACN,QAAQ;SACT,CAAC,CAAC;QACH,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAA4B,CAAC;QACzD,OAAO,SAAS,CACd,eAAe,EACf;YACE,GAAG,GAAG;YACN,eAAe,EAAE,GAAG,CAAC,QAAQ;YAC7B,OAAO,EAAE,GAAG,CAAC,UAAU;YACvB,WAAW,EAAE,GAAG,CAAC,aAAa;YAC9B,KAAK,EAAE,GAAG,CAAC,UAAU;SACtB,EACD,mCAAmC,CACpC,CAAC;IACJ,CAAC;IAED,0CAA0C;IAC1C,KAAK,CAAC,wBAAwB,CAAC,MAAc,EAAE,SAAiB;QAC9D,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,qCAAqC,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;QAC7F,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAA4B,CAAC;QACzD,OAAO,SAAS,CACd,eAAe,EACf;YACE,OAAO,EAAE,MAAM;YACf,MAAM,EAAE,GAAG,CAAC,OAAO;YACnB,WAAW,EAAE,GAAG,CAAC,aAAa;YAC9B,MAAM,EAAE,IAAI;SACb,EACD,8CAA8C,CAC/C,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,69 @@
1
+ import { z } from "zod";
2
+ import type { CallProc } from "../../../shared/postgres-types.js";
3
+ import type { DeductionRow } from "./deduction.js";
4
+ declare const LeaseRowSchema: z.ZodObject<{
5
+ lease_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
6
+ user_id: z.ZodOptional<z.ZodString>;
7
+ amount: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
8
+ available: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
9
+ reserved: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
10
+ billing_mode: z.ZodOptional<z.ZodString>;
11
+ minimum_balance: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
12
+ expires_at: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>;
13
+ error: z.ZodOptional<z.ZodNullable<z.ZodString>>;
14
+ }, z.core.$loose>;
15
+ declare const ReleaseRowSchema: z.ZodObject<{
16
+ released: z.ZodOptional<z.ZodNullable<z.ZodPipe<z.ZodUnion<readonly [z.ZodBoolean, z.ZodString, z.ZodNumber]>, z.ZodTransform<boolean, string | number | boolean>>>>;
17
+ reason: z.ZodOptional<z.ZodNullable<z.ZodString>>;
18
+ }, z.core.$loose>;
19
+ declare const LeasePricingContextRowSchema: z.ZodObject<{
20
+ catalog_revision_no: z.ZodCoercedNumber<unknown>;
21
+ plan_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
22
+ plan_key: z.ZodOptional<z.ZodNullable<z.ZodString>>;
23
+ rate_card: z.ZodOptional<z.ZodNullable<z.ZodString>>;
24
+ }, z.core.$loose>;
25
+ export type LeaseRow = z.infer<typeof LeaseRowSchema>;
26
+ export type ReleaseRow = z.infer<typeof ReleaseRowSchema>;
27
+ export type LeasePricingContextRow = z.infer<typeof LeasePricingContextRowSchema>;
28
+ /** Repository for lease lifecycle operations (admission control). */
29
+ export declare class LeaseRepository {
30
+ private callproc;
31
+ constructor(callproc: CallProc);
32
+ /** Atomically acquire a lease (hold) — admission control. */
33
+ createLease(params: {
34
+ userId: string;
35
+ amount: string;
36
+ operationType: string;
37
+ idempotencyKey: string;
38
+ ttlSeconds: number;
39
+ metadata: string;
40
+ feature: string | null;
41
+ measures: string;
42
+ dimensions: string;
43
+ minimumBalance: string | null;
44
+ maxConcurrent: number | null;
45
+ }): Promise<LeaseRow>;
46
+ /** Charge the actual cost against a lease and mark it settled. */
47
+ settleLease(params: {
48
+ userId: string;
49
+ leaseId: string;
50
+ amount: string;
51
+ idempotencyKey: string;
52
+ feature: string | null;
53
+ model: string | null;
54
+ region: string | null;
55
+ measures: string;
56
+ dimensions: string;
57
+ metadata: string;
58
+ }): Promise<DeductionRow>;
59
+ /** Return the immutable pricing context captured at lease admission. */
60
+ getPricingContext(userId: string, leaseId: string): Promise<LeasePricingContextRow | null>;
61
+ /** Release a lease without charging — idempotent. */
62
+ releaseLease(userId: string, leaseId: string): Promise<ReleaseRow>;
63
+ /** Extend an active lease without changing its captured policy snapshot. */
64
+ renewLease(userId: string, leaseId: string, ttlSeconds: number): Promise<LeaseRow>;
65
+ /** Expire a bounded batch of leases and release their reservations. */
66
+ expireLeases(limit: number): Promise<number>;
67
+ }
68
+ export {};
69
+ //# sourceMappingURL=lease.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lease.d.ts","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/lease.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAElE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAGnD,QAAA,MAAM,cAAc;;;;;;;;;;iBA0BJ,CAAC;AAEjB,QAAA,MAAM,gBAAgB;;;iBAKN,CAAC;AAEjB,QAAA,MAAM,4BAA4B;;;;;iBAOlB,CAAC;AAEjB,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC;AACtD,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAC1D,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,4BAA4B,CAAC,CAAC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AE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAqBxF,uEAAuE;IACjE,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAKnD"}
@@ -0,0 +1,148 @@
1
+ import { z } from "zod";
2
+ import { DeductionRowSchema } from "./deduction.js";
3
+ import { pgBoolean, safeParse } from "../../../shared/postgres-validation.js";
4
+ const LeaseRowSchema = z
5
+ .object({
6
+ lease_id: z.string().nullable().optional(),
7
+ user_id: z.string().optional(),
8
+ amount: z
9
+ .union([z.string(), z.number()])
10
+ .nullable()
11
+ .optional(),
12
+ available: z
13
+ .union([z.string(), z.number()])
14
+ .nullable()
15
+ .optional(),
16
+ reserved: z
17
+ .union([z.string(), z.number()])
18
+ .nullable()
19
+ .optional(),
20
+ billing_mode: z.string().optional(),
21
+ minimum_balance: z
22
+ .union([z.string(), z.number()])
23
+ .nullable()
24
+ .optional(),
25
+ expires_at: z
26
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
27
+ .optional(),
28
+ error: z.string().nullable().optional(),
29
+ })
30
+ .passthrough();
31
+ const ReleaseRowSchema = z
32
+ .object({
33
+ released: pgBoolean.nullable().optional(),
34
+ reason: z.string().nullable().optional(),
35
+ })
36
+ .passthrough();
37
+ const LeasePricingContextRowSchema = z
38
+ .object({
39
+ catalog_revision_no: z.coerce.number(),
40
+ plan_id: z.string().nullable().optional(),
41
+ plan_key: z.string().nullable().optional(),
42
+ rate_card: z.string().nullable().optional(),
43
+ })
44
+ .passthrough();
45
+ /** Repository for lease lifecycle operations (admission control). */
46
+ export class LeaseRepository {
47
+ callproc;
48
+ constructor(callproc) {
49
+ this.callproc = callproc;
50
+ }
51
+ /** Atomically acquire a lease (hold) — admission control. */
52
+ async createLease(params) {
53
+ const rows = await this.callproc("create_lease_for_operation", [
54
+ params.userId,
55
+ params.operationType,
56
+ params.amount,
57
+ params.idempotencyKey,
58
+ `${params.ttlSeconds} seconds`,
59
+ params.metadata,
60
+ params.feature,
61
+ params.measures,
62
+ params.dimensions,
63
+ params.minimumBalance,
64
+ params.maxConcurrent,
65
+ ]);
66
+ const row = (rows?.[0] ?? {});
67
+ const lease = row.lease_id == null
68
+ ? null
69
+ : (await this.callproc("get_credit_lease", [params.userId, row.lease_id]))[0];
70
+ return safeParse(LeaseRowSchema, {
71
+ ...row,
72
+ user_id: params.userId,
73
+ amount: row.reserved_amount,
74
+ expires_at: lease?.expires_at,
75
+ minimum_balance: lease?.minimum_balance,
76
+ error: row.error_code,
77
+ }, "LeaseRepository.createLease");
78
+ }
79
+ /** Charge the actual cost against a lease and mark it settled. */
80
+ async settleLease(params) {
81
+ const rows = await this.callproc("settle_lease", [
82
+ params.userId,
83
+ params.leaseId,
84
+ params.amount,
85
+ params.idempotencyKey,
86
+ params.feature,
87
+ params.model,
88
+ params.region,
89
+ params.measures,
90
+ params.dimensions,
91
+ params.metadata,
92
+ ]);
93
+ const row = (rows?.[0] ?? {});
94
+ const charge = (await this.callproc("get_credit_operation_details", [
95
+ params.userId,
96
+ row.ledger_entry_id ?? null,
97
+ params.idempotencyKey,
98
+ ]))[0];
99
+ return safeParse(DeductionRowSchema, {
100
+ ...row,
101
+ entry_id: row.ledger_entry_id,
102
+ amount: row.settled_amount,
103
+ allowance_consumed: charge?.allowance_covered,
104
+ balance_after: charge?.balance_after,
105
+ idempotent: row.replayed,
106
+ error: row.error_code,
107
+ }, "LeaseRepository.settleLease");
108
+ }
109
+ /** Return the immutable pricing context captured at lease admission. */
110
+ async getPricingContext(userId, leaseId) {
111
+ const rows = await this.callproc("get_credit_lease_pricing_context", [userId, leaseId]);
112
+ if (!rows?.length)
113
+ return null;
114
+ return safeParse(LeasePricingContextRowSchema, rows[0], "LeaseRepository.getPricingContext");
115
+ }
116
+ /** Release a lease without charging — idempotent. */
117
+ async releaseLease(userId, leaseId) {
118
+ const rows = await this.callproc("release_lease", [userId, leaseId]);
119
+ const result = rows?.[0];
120
+ const reason = typeof rows?.[0] === "string" ? rows[0] : result?.released === true ? "released" : null;
121
+ return safeParse(ReleaseRowSchema, {
122
+ released: reason === "released",
123
+ reason,
124
+ }, "LeaseRepository.releaseLease");
125
+ }
126
+ /** Extend an active lease without changing its captured policy snapshot. */
127
+ async renewLease(userId, leaseId, ttlSeconds) {
128
+ const rows = await this.callproc("renew_lease", [userId, leaseId, `${ttlSeconds} seconds`]);
129
+ const row = (rows?.[0] ?? {});
130
+ const lease = row.error_code == null && row.lease_id != null
131
+ ? (await this.callproc("get_credit_lease", [userId, row.lease_id]))[0]
132
+ : undefined;
133
+ return safeParse(LeaseRowSchema, {
134
+ ...row,
135
+ ...lease,
136
+ user_id: userId,
137
+ amount: row.reserved_amount,
138
+ error: row.error_code,
139
+ }, "LeaseRepository.renewLease");
140
+ }
141
+ /** Expire a bounded batch of leases and release their reservations. */
142
+ async expireLeases(limit) {
143
+ const rows = await this.callproc("expire_leases", [limit]);
144
+ const row = rows?.[0];
145
+ return Number(row?.expired ?? 0);
146
+ }
147
+ }
148
+ //# sourceMappingURL=lease.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lease.js","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/lease.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAE9E,MAAM,cAAc,GAAG,CAAC;KACrB,MAAM,CAAC;IACN,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;IAC1C,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC9B,MAAM,EAAE,CAAC;SACN,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC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AI;YAC5C,CAAC,CAAE,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC,kBAAkB,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAC9B;YACxC,CAAC,CAAC,SAAS,CAAC;QAChB,OAAO,SAAS,CACd,cAAc,EACd;YACE,GAAG,GAAG;YACN,GAAG,KAAK;YACR,OAAO,EAAE,MAAM;YACf,MAAM,EAAE,GAAG,CAAC,eAAe;YAC3B,KAAK,EAAE,GAAG,CAAC,UAAU;SACtB,EACD,4BAA4B,CAC7B,CAAC;IACJ,CAAC;IAED,uEAAuE;IACvE,KAAK,CAAC,YAAY,CAAC,KAAa;QAC9B,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,eAAe,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QAC3D,MAAM,GAAG,GAAG,IAAI,EAAE,CAAC,CAAC,CAAwC,CAAC;QAC7D,OAAO,MAAM,CAAC,GAAG,EAAE,OAAO,IAAI,CAAC,CAAC,CAAC;IACnC,CAAC;CACF"}
@@ -0,0 +1,105 @@
1
+ import { z } from "zod";
2
+ import type { CallProc } from "../../../shared/postgres-types.js";
3
+ declare const UserPlanRowSchema: z.ZodObject<{
4
+ user_id: z.ZodOptional<z.ZodString>;
5
+ plan_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
6
+ plan_key: z.ZodOptional<z.ZodNullable<z.ZodString>>;
7
+ plan_label: z.ZodOptional<z.ZodNullable<z.ZodString>>;
8
+ rate_card: z.ZodOptional<z.ZodNullable<z.ZodString>>;
9
+ credit_allowance_amount: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
10
+ credit_allowance_reset_unit: z.ZodOptional<z.ZodNullable<z.ZodString>>;
11
+ credit_allowance_reset_count: z.ZodOptional<z.ZodNullable<z.ZodCoercedNumber<unknown>>>;
12
+ credit_allowance_reset_anchor: z.ZodOptional<z.ZodNullable<z.ZodString>>;
13
+ credit_allowance_reset_timezone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
14
+ entitlements: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
15
+ credit_policy_type: z.ZodOptional<z.ZodNullable<z.ZodString>>;
16
+ credit_limit: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
17
+ admission_max_in_flight: z.ZodOptional<z.ZodNullable<z.ZodCoercedNumber<unknown>>>;
18
+ operation_admission: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
19
+ allowed_operations: z.ZodOptional<z.ZodArray<z.ZodString>>;
20
+ assignment_source_type: z.ZodOptional<z.ZodNullable<z.ZodString>>;
21
+ assignment_source_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
22
+ revision_policy: z.ZodOptional<z.ZodNullable<z.ZodString>>;
23
+ plan_assigned_at: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>>;
24
+ catalog_revision_no: z.ZodOptional<z.ZodNullable<z.ZodCoercedNumber<unknown>>>;
25
+ }, z.core.$loose>;
26
+ declare const SetUserPlanRowSchema: z.ZodObject<{
27
+ user_id: z.ZodOptional<z.ZodString>;
28
+ plan_id: z.ZodOptional<z.ZodString>;
29
+ plan_assigned_at: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>>;
30
+ }, z.core.$loose>;
31
+ declare const AllowanceRowSchema: z.ZodObject<{
32
+ plan_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
33
+ allowance_remaining: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
34
+ period_start: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>>;
35
+ period_end: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>>;
36
+ }, z.core.$loose>;
37
+ declare const UnsetPlanRowSchema: z.ZodObject<{
38
+ user_id: z.ZodOptional<z.ZodString>;
39
+ }, z.core.$loose>;
40
+ declare const PlanMigrationStartRowSchema: z.ZodNullable<z.ZodString>;
41
+ declare const PlanMigrationBatchRowSchema: z.ZodObject<{
42
+ migrated: z.ZodNumber;
43
+ done: z.ZodBoolean;
44
+ next_cursor: z.ZodNullable<z.ZodString>;
45
+ }, z.core.$loose>;
46
+ declare const QuotaStateRowSchema: z.ZodObject<{
47
+ user_id: z.ZodString;
48
+ quota_key: z.ZodString;
49
+ operation_key: z.ZodString;
50
+ measure_key: z.ZodString;
51
+ quota_limit: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
52
+ consumed: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
53
+ reserved: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
54
+ remaining: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
55
+ overage: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
56
+ enforcement: z.ZodEnum<{
57
+ block: "block";
58
+ allow: "allow";
59
+ }>;
60
+ window_start: z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>;
61
+ window_end: z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>;
62
+ emit_at_percent: z.ZodArray<z.ZodNumber>;
63
+ }, z.core.$loose>;
64
+ declare const QuotaEventRowSchema: z.ZodObject<{
65
+ event_id: z.ZodString;
66
+ quota_key: z.ZodString;
67
+ operation_key: z.ZodString;
68
+ measure_key: z.ZodString;
69
+ event_type: z.ZodEnum<{
70
+ threshold: "threshold";
71
+ blocked: "blocked";
72
+ }>;
73
+ threshold_percent: z.ZodNullable<z.ZodCoercedNumber<unknown>>;
74
+ idempotency_key: z.ZodString;
75
+ usage_charge_id: z.ZodNullable<z.ZodString>;
76
+ created_at: z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>;
77
+ }, z.core.$loose>;
78
+ export type UnsetPlanRow = z.infer<typeof UnsetPlanRowSchema>;
79
+ export type UserPlanRow = z.infer<typeof UserPlanRowSchema>;
80
+ export type SetUserPlanRow = z.infer<typeof SetUserPlanRowSchema>;
81
+ export type AllowanceRow = z.infer<typeof AllowanceRowSchema>;
82
+ export type PlanMigrationStartRow = z.infer<typeof PlanMigrationStartRowSchema>;
83
+ export type PlanMigrationBatchRow = z.infer<typeof PlanMigrationBatchRowSchema>;
84
+ export type QuotaStateRow = z.infer<typeof QuotaStateRowSchema>;
85
+ export type QuotaEventRow = z.infer<typeof QuotaEventRowSchema>;
86
+ /** Repository for plan management operations. */
87
+ export declare class PlanRepository {
88
+ private callproc;
89
+ constructor(callproc: CallProc);
90
+ /** Fetch a user's current plan. Returns null if user has no plan. */
91
+ getUserPlan(userId: string): Promise<UserPlanRow | null>;
92
+ getEntitlement(userId: string, feature: string): Promise<Record<string, unknown> | null>;
93
+ /** Assign a plan to a user. */
94
+ setUserPlan(userId: string, planKey: string, planAssignedAt: string | null): Promise<SetUserPlanRow>;
95
+ /** Remove a user's plan assignment. */
96
+ unsetUserPlan(userId: string): Promise<UnsetPlanRow>;
97
+ /** Check a user's remaining free allowance. */
98
+ startPlanMigration(fromPlanId: string | null, toPlanId: string): Promise<PlanMigrationStartRow>;
99
+ migratePlanBatch(migrationId: string, batchSize?: number): Promise<PlanMigrationBatchRow>;
100
+ getQuotaState(userId: string, quotaKey?: string | null): Promise<QuotaStateRow[]>;
101
+ listQuotaEvents(userId: string, after: string | null, limit: number, idempotencyKey: string | null, afterId: string | null): Promise<QuotaEventRow[]>;
102
+ checkAllowance(userId: string): Promise<AllowanceRow | null>;
103
+ }
104
+ export {};
105
+ //# sourceMapp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+1,182 @@
1
+ import { z } from "zod";
2
+ import { safeParse } from "../../../shared/postgres-validation.js";
3
+ const UserPlanRowSchema = z
4
+ .object({
5
+ user_id: z.string().optional(),
6
+ plan_id: z.string().nullable().optional(),
7
+ plan_key: z.string().nullable().optional(),
8
+ plan_label: z.string().nullable().optional(),
9
+ rate_card: z.string().nullable().optional(),
10
+ credit_allowance_amount: z
11
+ .union([z.string(), z.number()])
12
+ .nullable()
13
+ .optional(),
14
+ credit_allowance_reset_unit: z.string().nullable().optional(),
15
+ credit_allowance_reset_count: z.coerce.number().nullable().optional(),
16
+ credit_allowance_reset_anchor: z.string().nullable().optional(),
17
+ credit_allowance_reset_timezone: z.string().nullable().optional(),
18
+ entitlements: z.record(z.string(), z.unknown()).optional(),
19
+ credit_policy_type: z.string().nullable().optional(),
20
+ credit_limit: z
21
+ .union([z.string(), z.number()])
22
+ .nullable()
23
+ .optional(),
24
+ admission_max_in_flight: z.coerce.number().nullable().optional(),
25
+ operation_admission: z.record(z.string(), z.unknown()).optional(),
26
+ allowed_operations: z.array(z.string()).optional(),
27
+ assignment_source_type: z.string().nullable().optional(),
28
+ assignment_source_id: z.string().nullable().optional(),
29
+ revision_policy: z.string().nullable().optional(),
30
+ plan_assigned_at: z
31
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
32
+ .nullable()
33
+ .optional(),
34
+ catalog_revision_no: z.coerce.number().nullable().optional(),
35
+ })
36
+ .passthrough();
37
+ const SetUserPlanRowSchema = z
38
+ .object({
39
+ user_id: z.string().optional(),
40
+ plan_id: z.string().optional(),
41
+ plan_assigned_at: z
42
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
43
+ .nullable()
44
+ .optional(),
45
+ })
46
+ .passthrough();
47
+ const AllowanceRowSchema = z
48
+ .object({
49
+ plan_id: z.string().nullable().optional(),
50
+ allowance_remaining: z
51
+ .union([z.string(), z.number()])
52
+ .nullable()
53
+ .optional(),
54
+ period_start: z
55
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
56
+ .nullable()
57
+ .optional(),
58
+ period_end: z
59
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
60
+ .nullable()
61
+ .optional(),
62
+ })
63
+ .passthrough();
64
+ const UnsetPlanRowSchema = z
65
+ .object({
66
+ user_id: z.string().optional(),
67
+ })
68
+ .passthrough();
69
+ const PlanMigrationStartRowSchema = z.string().nullable();
70
+ const PlanMigrationBatchRowSchema = z
71
+ .object({
72
+ migrated: z.number(),
73
+ done: z.boolean(),
74
+ next_cursor: z.string().nullable(),
75
+ })
76
+ .passthrough();
77
+ const decimal = z.union([z.string(), z.number()]);
78
+ const QuotaStateRowSchema = z
79
+ .object({
80
+ user_id: z.string(),
81
+ quota_key: z.string(),
82
+ operation_key: z.string(),
83
+ measure_key: z.string(),
84
+ quota_limit: decimal,
85
+ consumed: decimal,
86
+ reserved: decimal,
87
+ remaining: decimal,
88
+ overage: decimal,
89
+ enforcement: z.enum(["block", "allow"]),
90
+ window_start: z.union([z.string(), z.date().transform((value) => value.toISOString())]),
91
+ window_end: z.union([z.string(), z.date().transform((value) => value.toISOString())]),
92
+ emit_at_percent: z.array(z.number()),
93
+ })
94
+ .passthrough();
95
+ const QuotaEventRowSchema = z
96
+ .object({
97
+ event_id: z.string(),
98
+ quota_key: z.string(),
99
+ operation_key: z.string(),
100
+ measure_key: z.string(),
101
+ event_type: z.enum(["threshold", "blocked"]),
102
+ threshold_percent: z.coerce.number().nullable(),
103
+ idempotency_key: z.string(),
104
+ usage_charge_id: z.string().nullable(),
105
+ created_at: z.union([z.string(), z.date().transform((value) => value.toISOString())]),
106
+ })
107
+ .passthrough();
108
+ /** Repository for plan management operations. */
109
+ export class PlanRepository {
110
+ callproc;
111
+ constructor(callproc) {
112
+ this.callproc = callproc;
113
+ }
114
+ /** Fetch a user's current plan. Returns null if user has no plan. */
115
+ async getUserPlan(userId) {
116
+ const rows = await this.callproc("get_subject_plan", [userId]);
117
+ if (!rows || rows.length === 0)
118
+ return null;
119
+ return safeParse(UserPlanRowSchema, rows[0], "PlanRepository.getUserPlan");
120
+ }
121
+ async getEntitlement(userId, feature) {
122
+ const rows = await this.callproc("get_subject_entitlements", [userId]);
123
+ const row = rows.find((candidate) => candidate.feature_key === feature);
124
+ return row ?? null;
125
+ }
126
+ /** Assign a plan to a user. */
127
+ async setUserPlan(userId, planKey, planAssignedAt) {
128
+ const planRows = await this.callproc("resolve_active_plan", [planKey]);
129
+ const planRow = planRows[0];
130
+ if (planRow?.id == null)
131
+ throw new Error(`unknown active plan '${planKey}'`);
132
+ const planId = String(planRow.id);
133
+ const params = planAssignedAt ? [userId, planId, planAssignedAt] : [userId, planId];
134
+ const rows = await this.callproc("assign_plan", params);
135
+ if (rows?.[0] !== true) {
136
+ throw new Error("PlanRepository.setUserPlan: assign_plan returned false");
137
+ }
138
+ return safeParse(SetUserPlanRowSchema, {
139
+ user_id: userId,
140
+ plan_id: planId,
141
+ plan_assigned_at: planAssignedAt,
142
+ }, "PlanRepository.setUserPlan");
143
+ }
144
+ /** Remove a user's plan assignment. */
145
+ async unsetUserPlan(userId) {
146
+ const rows = await this.callproc("unassign_plan", [userId, "sdk_unassignment"]);
147
+ if (rows?.[0] !== true) {
148
+ throw new Error("PlanRepository.unsetUserPlan: unassign_plan returned false");
149
+ }
150
+ return safeParse(UnsetPlanRowSchema, { user_id: userId }, "PlanRepository.unsetUserPlan");
151
+ }
152
+ /** Check a user's remaining free allowance. */
153
+ async startPlanMigration(fromPlanId, toPlanId) {
154
+ const rows = await this.callproc("start_plan_migration", [fromPlanId, toPlanId]);
155
+ return safeParse(PlanMigrationStartRowSchema, rows?.[0] ?? null, "PlanRepository.startPlanMigration");
156
+ }
157
+ async migratePlanBatch(migrationId, batchSize) {
158
+ const rows = await this.callproc("migrate_plan_batch", [migrationId, batchSize ?? 100]);
159
+ return safeParse(PlanMigrationBatchRowSchema, rows?.[0] ?? {}, "PlanRepository.migratePlanBatch");
160
+ }
161
+ async getQuotaState(userId, quotaKey) {
162
+ const rows = await this.callproc("get_subject_quota_state", [userId, quotaKey ?? null]);
163
+ return (rows ?? []).map((row) => safeParse(QuotaStateRowSchema, row, "PlanRepository.getQuotaState"));
164
+ }
165
+ async listQuotaEvents(userId, after, limit, idempotencyKey, afterId) {
166
+ const rows = await this.callproc("list_subject_quota_events", [
167
+ userId,
168
+ after,
169
+ limit,
170
+ idempotencyKey,
171
+ afterId,
172
+ ]);
173
+ return (rows ?? []).map((row) => safeParse(QuotaEventRowSchema, row, "PlanRepository.listQuotaEvents"));
174
+ }
175
+ async checkAllowance(userId) {
176
+ const rows = await this.callproc("get_subject_allowance", [userId]);
177
+ if (!rows || rows.length === 0)
178
+ return null;
179
+ return safeParse(AllowanceRowSchema, rows[0], "PlanRepository.checkAllowance");
180
+ }
181
+ }
182
+ //# sourceMappingURL=plan.js.map