@zonastery/bursar 0.0.2 → 1.0.1

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 (505) hide show
  1. package/README.md +118 -333
  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 +684 -0
  24. package/dist/billing/event-handlers.js.map +1 -0
  25. package/dist/billing/event-processor.d.ts +20 -0
  26. package/dist/billing/event-processor.d.ts.map +1 -0
  27. package/dist/billing/event-processor.js +131 -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 +9 -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 +180 -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 +45 -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 +89 -0
  90. package/dist/billing/postgres/store.d.ts.map +1 -0
  91. package/dist/billing/postgres/store.js +542 -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 +84 -0
  138. package/dist/bursar.d.ts.map +1 -0
  139. package/dist/bursar.js +137 -0
  140. package/dist/bursar.js.map +1 -0
  141. package/dist/catalog.d.ts +62 -0
  142. package/dist/catalog.d.ts.map +1 -0
  143. package/dist/catalog.js +96 -0
  144. package/dist/catalog.js.map +1 -0
  145. package/dist/commerce/errors.d.ts +68 -0
  146. package/dist/commerce/errors.d.ts.map +1 -0
  147. package/dist/commerce/errors.js +105 -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 +1016 -0
  164. package/dist/commerce/service.js.map +1 -0
  165. package/dist/commerce/types.d.ts +225 -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 +142 -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 +340 -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 +12 -23
  197. package/dist/config.d.ts.map +1 -1
  198. package/dist/config.js +141 -377
  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 +81 -0
  213. package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -0
  214. package/dist/credits/postgres/repositories/analytics.js +153 -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 +64 -0
  225. package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -0
  226. package/dist/credits/postgres/repositories/deduction.js +154 -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 +110 -0
  233. package/dist/credits/postgres/repositories/plan.d.ts.map +1 -0
  234. package/dist/credits/postgres/repositories/plan.js +194 -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 +76 -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 +89 -0
  245. package/dist/credits/postgres/store.d.ts.map +1 -0
  246. package/dist/credits/postgres/store.js +838 -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 +102 -0
  251. package/dist/credits/postgres/value-mappers.js.map +1 -0
  252. package/dist/credits/pricing-runtime.d.ts +35 -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 +220 -0
  269. package/dist/credits/service.d.ts.map +1 -0
  270. package/dist/credits/service.js +640 -0
  271. package/dist/credits/service.js.map +1 -0
  272. package/dist/credits/store.d.ts +172 -0
  273. package/dist/credits/store.d.ts.map +1 -0
  274. package/dist/{stores/credit-store.js → credits/store.js} +27 -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 +71 -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 +87 -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 +133 -25
  317. package/dist/errors.d.ts.map +1 -1
  318. package/dist/errors.js +173 -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 +2792 -0
  345. package/dist/generated/pricing-config.types.d.ts +685 -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 +23 -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 +8 -4
  355. package/dist/load-pricing-file.js.map +1 -1
  356. package/dist/metrics.d.ts +6 -16
  357. package/dist/metrics.d.ts.map +1 -1
  358. package/dist/metrics.js.map +1 -1
  359. package/dist/node.d.ts +9 -2
  360. package/dist/node.d.ts.map +1 -1
  361. package/dist/node.js +6 -2
  362. package/dist/node.js.map +1 -1
  363. package/dist/providers/_shared.d.ts +9 -0
  364. package/dist/providers/_shared.d.ts.map +1 -0
  365. package/dist/providers/_shared.js +52 -0
  366. package/dist/providers/_shared.js.map +1 -0
  367. package/dist/providers/dodo/client-contract.d.ts +106 -0
  368. package/dist/providers/dodo/client-contract.d.ts.map +1 -0
  369. package/dist/providers/dodo/client-contract.js +2 -0
  370. package/dist/providers/dodo/client-contract.js.map +1 -0
  371. package/dist/providers/dodo/event-mapper.d.ts +5 -3
  372. package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
  373. package/dist/providers/dodo/event-mapper.js +213 -38
  374. package/dist/providers/dodo/event-mapper.js.map +1 -1
  375. package/dist/providers/dodo/index.d.ts +1 -0
  376. package/dist/providers/dodo/index.d.ts.map +1 -1
  377. package/dist/providers/dodo/index.js.map +1 -1
  378. package/dist/providers/dodo/provider.d.ts +24 -14
  379. package/dist/providers/dodo/provider.d.ts.map +1 -1
  380. package/dist/providers/dodo/provider.js +249 -31
  381. package/dist/providers/dodo/provider.js.map +1 -1
  382. package/dist/providers/index.d.ts +3 -1
  383. package/dist/providers/index.d.ts.map +1 -1
  384. package/dist/providers/index.js +1 -0
  385. package/dist/providers/index.js.map +1 -1
  386. package/dist/providers/mock/index.js.map +1 -1
  387. package/dist/providers/mock/provider.d.ts +14 -10
  388. package/dist/providers/mock/provider.d.ts.map +1 -1
  389. package/dist/providers/mock/provider.js +78 -10
  390. package/dist/providers/mock/provider.js.map +1 -1
  391. package/dist/providers/stripe/event-mapper.d.ts +3 -3
  392. package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
  393. package/dist/providers/stripe/event-mapper.js +139 -30
  394. package/dist/providers/stripe/event-mapper.js.map +1 -1
  395. package/dist/providers/stripe/index.js.map +1 -1
  396. package/dist/providers/stripe/provider.d.ts +21 -12
  397. package/dist/providers/stripe/provider.d.ts.map +1 -1
  398. package/dist/providers/stripe/provider.js +175 -17
  399. package/dist/providers/stripe/provider.js.map +1 -1
  400. package/dist/providers/types.d.ts +128 -32
  401. package/dist/providers/types.d.ts.map +1 -1
  402. package/dist/providers/types.js +17 -1
  403. package/dist/providers/types.js.map +1 -1
  404. package/dist/retry.d.ts +35 -0
  405. package/dist/retry.d.ts.map +1 -0
  406. package/dist/retry.js +70 -0
  407. package/dist/retry.js.map +1 -0
  408. package/dist/shared/diagnostics.d.ts +7 -0
  409. package/dist/shared/diagnostics.d.ts.map +1 -0
  410. package/dist/shared/diagnostics.js +18 -0
  411. package/dist/shared/diagnostics.js.map +1 -0
  412. package/dist/shared/logger.d.ts +10 -0
  413. package/dist/shared/logger.d.ts.map +1 -0
  414. package/dist/shared/logger.js +15 -0
  415. package/dist/shared/logger.js.map +1 -0
  416. package/dist/shared/postgres-client.d.ts +84 -0
  417. package/dist/shared/postgres-client.d.ts.map +1 -0
  418. package/dist/shared/postgres-client.js +337 -0
  419. package/dist/shared/postgres-client.js.map +1 -0
  420. package/dist/shared/postgres-errors.d.ts +11 -0
  421. package/dist/shared/postgres-errors.d.ts.map +1 -0
  422. package/dist/shared/postgres-errors.js +105 -0
  423. package/dist/shared/postgres-errors.js.map +1 -0
  424. package/dist/shared/postgres-types.d.ts +7 -0
  425. package/dist/shared/postgres-types.d.ts.map +1 -0
  426. package/dist/shared/postgres-types.js +2 -0
  427. package/dist/shared/postgres-types.js.map +1 -0
  428. package/dist/shared/postgres-validation.d.ts +8 -0
  429. package/dist/shared/postgres-validation.d.ts.map +1 -0
  430. package/dist/shared/postgres-validation.js +40 -0
  431. package/dist/shared/postgres-validation.js.map +1 -0
  432. package/dist/storage/adapters/clickhouse.d.ts +61 -0
  433. package/dist/storage/adapters/clickhouse.d.ts.map +1 -0
  434. package/dist/storage/adapters/clickhouse.js +347 -0
  435. package/dist/storage/adapters/clickhouse.js.map +1 -0
  436. package/dist/storage/adapters/s3.d.ts +35 -0
  437. package/dist/storage/adapters/s3.d.ts.map +1 -0
  438. package/dist/storage/adapters/s3.js +100 -0
  439. package/dist/storage/adapters/s3.js.map +1 -0
  440. package/dist/storage/outbox-worker.d.ts +41 -0
  441. package/dist/storage/outbox-worker.d.ts.map +1 -0
  442. package/dist/storage/outbox-worker.js +146 -0
  443. package/dist/storage/outbox-worker.js.map +1 -0
  444. package/dist/storage/ports.d.ts +78 -0
  445. package/dist/storage/ports.d.ts.map +1 -0
  446. package/dist/storage/ports.js +2 -0
  447. package/dist/storage/ports.js.map +1 -0
  448. package/dist/storage/postgres-repository.d.ts +14 -0
  449. package/dist/storage/postgres-repository.d.ts.map +1 -0
  450. package/dist/storage/postgres-repository.js +140 -0
  451. package/dist/storage/postgres-repository.js.map +1 -0
  452. package/dist/storage/runtime.d.ts +77 -0
  453. package/dist/storage/runtime.d.ts.map +1 -0
  454. package/dist/storage/runtime.js +369 -0
  455. package/dist/storage/runtime.js.map +1 -0
  456. package/package.json +43 -21
  457. package/dist/allowance.d.ts +0 -48
  458. package/dist/allowance.d.ts.map +0 -1
  459. package/dist/allowance.js +0 -140
  460. package/dist/allowance.js.map +0 -1
  461. package/dist/billing/billing-manager.d.ts +0 -66
  462. package/dist/billing/billing-manager.d.ts.map +0 -1
  463. package/dist/billing/billing-manager.js +0 -560
  464. package/dist/billing/billing-manager.js.map +0 -1
  465. package/dist/billing/billing-types.d.ts +0 -203
  466. package/dist/billing/billing-types.d.ts.map +0 -1
  467. package/dist/billing/billing-types.js +0 -6
  468. package/dist/billing/billing-types.js.map +0 -1
  469. package/dist/billing/memory-billing-store.d.ts +0 -37
  470. package/dist/billing/memory-billing-store.d.ts.map +0 -1
  471. package/dist/billing/memory-billing-store.js +0 -197
  472. package/dist/billing/memory-billing-store.js.map +0 -1
  473. package/dist/billing/postgres-billing-store.d.ts +0 -35
  474. package/dist/billing/postgres-billing-store.d.ts.map +0 -1
  475. package/dist/billing/postgres-billing-store.js +0 -274
  476. package/dist/billing/postgres-billing-store.js.map +0 -1
  477. package/dist/billing/supabase-billing-store.d.ts +0 -29
  478. package/dist/billing/supabase-billing-store.d.ts.map +0 -1
  479. package/dist/billing/supabase-billing-store.js +0 -250
  480. package/dist/billing/supabase-billing-store.js.map +0 -1
  481. package/dist/manager.d.ts +0 -444
  482. package/dist/manager.d.ts.map +0 -1
  483. package/dist/manager.js +0 -1153
  484. package/dist/manager.js.map +0 -1
  485. package/dist/stores/credit-store.d.ts +0 -198
  486. package/dist/stores/credit-store.d.ts.map +0 -1
  487. package/dist/stores/credit-store.js.map +0 -1
  488. package/dist/stores/events.d.ts.map +0 -1
  489. package/dist/stores/events.js.map +0 -1
  490. package/dist/stores/memory-store.d.ts +0 -187
  491. package/dist/stores/memory-store.d.ts.map +0 -1
  492. package/dist/stores/memory-store.js +0 -1890
  493. package/dist/stores/memory-store.js.map +0 -1
  494. package/dist/stores/postgres-store.d.ts +0 -100
  495. package/dist/stores/postgres-store.d.ts.map +0 -1
  496. package/dist/stores/postgres-store.js +0 -891
  497. package/dist/stores/postgres-store.js.map +0 -1
  498. package/dist/stores/supabase-store.d.ts +0 -87
  499. package/dist/stores/supabase-store.d.ts.map +0 -1
  500. package/dist/stores/supabase-store.js +0 -948
  501. package/dist/stores/supabase-store.js.map +0 -1
  502. package/dist/types.d.ts +0 -463
  503. package/dist/types.d.ts.map +0 -1
  504. package/dist/types.js.map +0 -1
  505. /package/dist/{types.js → commerce/types.js} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"balance.js","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/balance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,OAAO,MAAM,YAAY,CAAC;AAEjC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAE9E,MAAM,gBAAgB,GAAG,CAAC;KACvB,MAAM,CAAC;IACN,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;IACnB,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAU,CAAC,CAAC,QAAQ,EAAE;IAC9D,kBAAkB,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAU,CAAC,CAAC,QAAQ,EAAE;CAC1E,CAAC;KACD,OAAO,EAAE;KACT,WAAW,EAAE,CAAC;AAEjB,MAAM,mBAAmB,GAAG,CAAC;KAC1B,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,MAAM,EAAE,CAAU,CAAC;SACxC,QAAQ,EAAE;SACV,QAAQ,EAAE;IACb,WAAW,EAAE,CAAC;SACX,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAU,CAAC;SAC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QAAQ,CAAC,kBAAkB,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;QAC/D,OAAO,SAAS,CAAC,kBAAkB,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,gCAAgC,CAAC,CAAC;IAC1F,CAAC;IAED,2EAA2E;IAC3E,KAAK,CAAC,mBAAmB,CAAC,MAQzB;QACC,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,uBAAuB,EAAE;YACxD,MAAM,CAAC,OAAO;YACd,MAAM,CAAC,UAAU;YACjB,MAAM,CAAC,SAAS;YAChB,MAAM,CAAC,QAAQ;YACf,MAAM,CAAC,iBAAiB;YACxB,MAAM,CAAC,MAAM;YACb,MAAM,CAAC,QAAQ;SAChB,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAC9B,SAAS,CAAC,0BAA0B,EAAE,GAAG,EAAE,uCAAuC,CAAC,CACpF,CAAC;IACJ,CAAC;CACF","sourcesContent":["import { z } from \"zod\";\nimport Decimal from \"decimal.js\";\nimport type { CallProc } from \"../../../shared/postgres-types.js\";\nimport { pgBoolean, safeParse } from \"../../../shared/postgres-validation.js\";\n\nconst BalanceRowSchema = z\n .object({\n user_id: z.string(),\n balance: z.union([z.string(), z.number()] as const).nullable(),\n lifetime_purchased: z.union([z.string(), z.number()] as const).nullable(),\n })\n .partial()\n .passthrough();\n\nconst AddCreditsRowSchema = z\n .object({\n entry_id: z.string().nullable().optional(),\n user_id: z.string().optional(),\n amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n new_balance: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n lifetime_purchased: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n bucket: z.string().optional(),\n idempotent: pgBoolean.nullable().optional(),\n error: z.string().nullable().optional(),\n })\n .passthrough();\n\nconst AvailableRowSchema = z\n .object({\n balance: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n reserved: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n available: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n })\n .passthrough();\n\nconst GrantProgramAwardRowSchema = z\n .object({\n grant_event_id: z.string().nullable().optional(),\n grant_award_id: z.string().nullable().optional(),\n recipient_subject_id: z.string().nullable().optional(),\n ledger_entry_id: z.string().nullable().optional(),\n amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n replayed: pgBoolean.nullable().optional(),\n error_code: z.string().nullable().optional(),\n })\n .passthrough();\n\nexport type BalanceRow = z.infer<typeof BalanceRowSchema>;\nexport type AddCreditsRow = z.infer<typeof AddCreditsRowSchema>;\nexport type AvailableRow = z.infer<typeof AvailableRowSchema>;\nexport type GrantProgramAwardRow = z.infer<typeof GrantProgramAwardRowSchema>;\n\n/** Repository for user credit balance operations.\n *\n * All methods call Postgres RPCs via the callproc function.\n *\n * NOTE: getBalance returns null when no row exists (new user), while\n * addCredits and getAvailable always return a parsed object (empty object\n * fallback). This inconsistency is intentional: balance queries distinguish\n * \"no data\" from \"zeroed data\" whereas mutation reads always produce a result.\n */\nexport class BalanceRepository {\n constructor(private callproc: CallProc) {}\n\n /** Fetch a user's credit balance. Returns null for new users with no balance row. */\n async getBalance(userId: string): Promise<BalanceRow | null> {\n const rows = await this.callproc(\"get_credit_state\", [userId]);\n if (!rows || rows.length === 0) return null;\n return safeParse(\n BalanceRowSchema,\n {\n user_id: userId,\n ...(rows[0] as Record<string, unknown>),\n },\n \"BalanceRepository.getBalance\",\n );\n }\n\n /** Add credits to a user's account. */\n async addCredits(\n userId: string,\n amount: string,\n type: string,\n metadata: string,\n expiresAt: string | null,\n bucket: string | null,\n idempotencyKey: string | null,\n ): Promise<AddCreditsRow> {\n const decimalAmount = new Decimal(amount);\n const rows = await this.callproc(\"post_credit\", [\n userId,\n decimalAmount.isNegative() ? \"adjustment\" : type === \"purchase\" ? \"purchase\" : \"grant\",\n amount,\n type,\n idempotencyKey,\n metadata,\n bucket,\n null,\n expiresAt,\n \"0\",\n ]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n const state =\n row.error_code == null\n ? ((await this.callproc(\"get_credit_state\", [userId]))[0] as\n Record<string, unknown> | undefined)\n : undefined;\n const grant =\n row.error_code == null && decimalAmount.isPositive() && row.entry_id != null\n ? ((await this.callproc(\"get_credit_grant_details\", [userId, row.entry_id]))[0] as\n Record<string, unknown> | undefined)\n : undefined;\n return safeParse(\n AddCreditsRowSchema,\n {\n ...row,\n user_id: userId,\n amount,\n new_balance: row.balance_after,\n lifetime_purchased: state?.lifetime_purchased,\n bucket: grant?.bucket_key ?? bucket ?? \"default\",\n idempotent: row.replayed,\n error: row.error_code,\n },\n \"BalanceRepository.addCredits\",\n );\n }\n\n /** Fetch available balance (balance minus reserved holds). */\n async getAvailable(userId: string): Promise<AvailableRow> {\n const rows = await this.callproc(\"get_credit_state\", [userId]);\n return safeParse(AvailableRowSchema, rows?.[0] ?? {}, \"BalanceRepository.getAvailable\");\n }\n\n /** Execute a configured grant-program event and return every award row. */\n async executeGrantProgram(params: {\n trigger: string;\n programKey: string;\n subjectId: string;\n eventKey: string;\n referrerSubjectId: string | null;\n region: string | null;\n metadata: string;\n }): Promise<GrantProgramAwardRow[]> {\n const rows = await this.callproc(\"execute_grant_program\", [\n params.trigger,\n params.programKey,\n params.subjectId,\n params.eventKey,\n params.referrerSubjectId,\n params.region,\n params.metadata,\n ]);\n return (rows ?? []).map((row) =>\n safeParse(GrantProgramAwardRowSchema, row, \"BalanceRepository.executeGrantProgram\"),\n );\n }\n}\n"]}
@@ -0,0 +1,25 @@
1
+ import { z } from "zod";
2
+ import type { CallProc } from "../../../shared/postgres-types.js";
3
+ declare const BucketEnvelopeRowSchema: z.ZodObject<{
4
+ user_id: z.ZodOptional<z.ZodString>;
5
+ buckets: z.ZodOptional<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
6
+ total_balance: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
7
+ }, z.core.$loose>;
8
+ declare const SweepRowSchema: z.ZodObject<{
9
+ expired_count: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
10
+ expired_amount: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
11
+ expired_by_bucket: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>>;
12
+ }, z.core.$loose>;
13
+ export type BucketEnvelopeRow = z.infer<typeof BucketEnvelopeRowSchema>;
14
+ export type SweepRow = z.infer<typeof SweepRowSchema>;
15
+ /** Repository for credit bucket operations. */
16
+ export declare class BucketRepository {
17
+ private callproc;
18
+ constructor(callproc: CallProc);
19
+ /** Fetch per-bucket credit balances for a user. */
20
+ getBucketBalances(userId: string): Promise<BucketEnvelopeRow>;
21
+ /** Sweep expired credit grants. */
22
+ sweepExpiredCredits(dryRun?: boolean, userId?: string, limit?: number): Promise<SweepRow>;
23
+ }
24
+ export {};
25
+ //# sourceMappingURL=bucket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bucket.d.ts","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/bucket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAGlE,QAAA,MAAM,uBAAuB;;;;iBASb,CAAC;AAEjB,QAAA,MAAM,cAAc;;;;iBAYJ,CAAC;AAEjB,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AACxE,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC;AAEtD,+CAA+C;AAC/C,qBAAa,gBAAgB;IACf,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,QAAQ;IAEtC,mDAAmD;IAC7C,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAoBnE,mCAAmC;IAC7B,mBAAmB,CAAC,MAAM,UAAQ,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,KAAK,SAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;CAI3F"}
@@ -0,0 +1,52 @@
1
+ import { z } from "zod";
2
+ import Decimal from "decimal.js";
3
+ import { safeParse } from "../../../shared/postgres-validation.js";
4
+ const BucketEnvelopeRowSchema = z
5
+ .object({
6
+ user_id: z.string().optional(),
7
+ buckets: z.array(z.record(z.string(), z.unknown())).optional(),
8
+ total_balance: z
9
+ .union([z.string(), z.number()])
10
+ .nullable()
11
+ .optional(),
12
+ })
13
+ .passthrough();
14
+ const SweepRowSchema = z
15
+ .object({
16
+ expired_count: z.coerce.number().optional(),
17
+ expired_amount: z
18
+ .union([z.string(), z.number()])
19
+ .nullable()
20
+ .optional(),
21
+ expired_by_bucket: z
22
+ .record(z.string(), z.union([z.string(), z.number()]))
23
+ .nullable()
24
+ .optional(),
25
+ })
26
+ .passthrough();
27
+ /** Repository for credit bucket operations. */
28
+ export class BucketRepository {
29
+ callproc;
30
+ constructor(callproc) {
31
+ this.callproc = callproc;
32
+ }
33
+ /** Fetch per-bucket credit balances for a user. */
34
+ async getBucketBalances(userId) {
35
+ const rows = await this.callproc("get_credit_bucket_balances", [userId]);
36
+ const buckets = (rows ?? []).filter((row) => row != null && typeof row === "object" && !Array.isArray(row));
37
+ const totalBalance = buckets
38
+ .reduce((total, row) => total.plus(String(row.balance ?? 0)), new Decimal(0))
39
+ .toString();
40
+ return safeParse(BucketEnvelopeRowSchema, {
41
+ user_id: userId,
42
+ buckets,
43
+ total_balance: totalBalance,
44
+ }, "BucketRepository.getBucketBalances");
45
+ }
46
+ /** Sweep expired credit grants. */
47
+ async sweepExpiredCredits(dryRun = false, userId, limit = 100) {
48
+ const rows = await this.callproc("sweep_expired_lots", [limit, userId ?? null, dryRun]);
49
+ return safeParse(SweepRowSchema, rows?.[0] ?? {}, "BucketRepository.sweepExpiredCredits");
50
+ }
51
+ }
52
+ //# sourceMappingURL=bucket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bucket.js","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/bucket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,OAAO,MAAM,YAAY,CAAC;AAEjC,OAAO,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAEnE,MAAM,uBAAuB,GAAG,CAAC;KAC9B,MAAM,CAAC;IACN,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC9B,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE;IAC9D,aAAa,EAAE,CAAC;SACb,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAU,CAAC;SACxC,QAAQ,EAAE;SACV,QAAQ,EAAE;CACd,CAAC;KACD,WAAW,EAAE,CAAC;AAEjB,MAAM,cAAc,GAAG,CAAC;KACrB,MAAM,CAAC;IACN,aAAa,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC3C,cAAc,EAAE,CAAC;SACd,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAU,CAAC;SACxC,QAAQ,EAAE;SACV,QAAQ,EAAE;IACb,iBAAiB,EAAE,CAAC;SACjB,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,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E,MAAM,CAAC,CAAC,CAAC;QACxF,OAAO,SAAS,CAAC,cAAc,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,sCAAsC,CAAC,CAAC;IAC5F,CAAC;CACF","sourcesContent":["import { z } from \"zod\";\nimport Decimal from \"decimal.js\";\nimport type { CallProc } from \"../../../shared/postgres-types.js\";\nimport { safeParse } from \"../../../shared/postgres-validation.js\";\n\nconst BucketEnvelopeRowSchema = z\n .object({\n user_id: z.string().optional(),\n buckets: z.array(z.record(z.string(), z.unknown())).optional(),\n total_balance: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n })\n .passthrough();\n\nconst SweepRowSchema = z\n .object({\n expired_count: z.coerce.number().optional(),\n expired_amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n expired_by_bucket: z\n .record(z.string(), z.union([z.string(), z.number()] as const))\n .nullable()\n .optional(),\n })\n .passthrough();\n\nexport type BucketEnvelopeRow = z.infer<typeof BucketEnvelopeRowSchema>;\nexport type SweepRow = z.infer<typeof SweepRowSchema>;\n\n/** Repository for credit bucket operations. */\nexport class BucketRepository {\n constructor(private callproc: CallProc) {}\n\n /** Fetch per-bucket credit balances for a user. */\n async getBucketBalances(userId: string): Promise<BucketEnvelopeRow> {\n const rows = await this.callproc(\"get_credit_bucket_balances\", [userId]);\n const buckets = (rows ?? []).filter(\n (row): row is Record<string, unknown> =>\n row != null && typeof row === \"object\" && !Array.isArray(row),\n );\n const totalBalance = buckets\n .reduce((total, row) => total.plus(String(row.balance ?? 0)), new Decimal(0))\n .toString();\n return safeParse(\n BucketEnvelopeRowSchema,\n {\n user_id: userId,\n buckets,\n total_balance: totalBalance,\n },\n \"BucketRepository.getBucketBalances\",\n );\n }\n\n /** Sweep expired credit grants. */\n async sweepExpiredCredits(dryRun = false, userId?: string, limit = 100): Promise<SweepRow> {\n const rows = await this.callproc(\"sweep_expired_lots\", [limit, userId ?? null, dryRun]);\n return safeParse(SweepRowSchema, rows?.[0] ?? {}, \"BucketRepository.sweepExpiredCredits\");\n }\n}\n"]}
@@ -0,0 +1,64 @@
1
+ import { z } from "zod";
2
+ import type { CallProc } from "../../../shared/postgres-types.js";
3
+ export declare const DeductionRowSchema: z.ZodObject<{
4
+ entry_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
5
+ amount: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
6
+ balance_after: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
7
+ allowance_consumed: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
8
+ idempotent: z.ZodOptional<z.ZodNullable<z.ZodPipe<z.ZodUnion<readonly [z.ZodBoolean, z.ZodString, z.ZodNumber]>, z.ZodTransform<boolean, string | number | boolean>>>>;
9
+ bucket_breakdown: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>>;
10
+ error: z.ZodOptional<z.ZodNullable<z.ZodString>>;
11
+ user_id: z.ZodOptional<z.ZodString>;
12
+ }, z.core.$loose>;
13
+ declare const RefundRowSchema: z.ZodObject<{
14
+ refund_entry_id: z.ZodOptional<z.ZodString>;
15
+ user_id: z.ZodOptional<z.ZodString>;
16
+ amount: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
17
+ new_balance: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
18
+ bucket_breakdown: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>>;
19
+ error: z.ZodOptional<z.ZodNullable<z.ZodString>>;
20
+ }, z.core.$loose>;
21
+ declare const RevokeRowSchema: z.ZodObject<{
22
+ user_id: z.ZodOptional<z.ZodString>;
23
+ amount: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
24
+ new_balance: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
25
+ bucket: z.ZodOptional<z.ZodNullable<z.ZodString>>;
26
+ }, z.core.$loose>;
27
+ export type DeductionRow = z.infer<typeof DeductionRowSchema>;
28
+ export type RefundRow = z.infer<typeof RefundRowSchema>;
29
+ export type RevokeRow = z.infer<typeof RevokeRowSchema>;
30
+ declare const UsageRecordRowSchema: z.ZodObject<{
31
+ charge_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
32
+ requested: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
33
+ replayed: z.ZodOptional<z.ZodNullable<z.ZodPipe<z.ZodUnion<readonly [z.ZodBoolean, z.ZodString, z.ZodNumber]>, z.ZodTransform<boolean, string | number | boolean>>>>;
34
+ error_code: z.ZodOptional<z.ZodNullable<z.ZodString>>;
35
+ }, z.core.$loose>;
36
+ export type UsageRecordRow = z.infer<typeof UsageRecordRowSchema>;
37
+ /** Typed input for the plan-aware operation charge RPC. */
38
+ export interface DeductParams {
39
+ userId: string;
40
+ operation: string;
41
+ amount: string;
42
+ idempotencyKey: string;
43
+ feature: string | null;
44
+ model: string | null;
45
+ region: string | null;
46
+ measures: string;
47
+ dimensions: string;
48
+ metadata: string;
49
+ }
50
+ /** Repository for credit deduction operations. */
51
+ export declare class DeductionRepository {
52
+ private callproc;
53
+ constructor(callproc: CallProc);
54
+ /** Atomically deduct credits with allowance, entitlement, quota, and credit-policy enforcement. */
55
+ deductWithAllowance(params: DeductParams): Promise<DeductionRow>;
56
+ /** Append a priced usage event without creating another balance debit. */
57
+ recordUsage(params: DeductParams): Promise<UsageRecordRow>;
58
+ /** Refund a previous credit deduction. */
59
+ refundCredits(entryId: string, amount: string | null, idempotencyKey: string, reason: string | null, metadata: string): Promise<RefundRow>;
60
+ /** Revoke credits by transaction type. */
61
+ revokeCreditsByEntryType(userId: string, entryType: string): Promise<RevokeRow>;
62
+ }
63
+ export {};
64
+ //# sourceMappingURL=deduction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deduction.d.ts","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/deduction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAGlE,eAAO,MAAM,kBAAkB;;;;;;;;;iBAuBf,CAAC;AAEjB,QAAA,MAAM,eAAe;;;;;;;iBAkBL,CAAC;AAEjB,QAAA,MAAM,eAAe;;;;;iBAaL,CAAC;AAEjB,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAC9D,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AACxD,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAExD,QAAA,MAAM,oBAAoB;;;;;iBAOV,CAAC;AAEjB,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAElE,2DAA2D;AAC3D,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,kDAAkD;AAClD,qBAAa,mBAAmB;IAClB,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,QAAQ;IAEtC,mGAAmG;IAC7F,mBAAmB,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAyCtE,0EAA0E;IACpE,WAAW,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,CAAC;IAgBhE,0CAA0C;IACpC,aAAa,CACjB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GAAG,IAAI,EACrB,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,MAAM,GAAG,IAAI,EACrB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,SAAS,CAAC;IAsBrB,0CAA0C;IACpC,wBAAwB,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;CActF"}
@@ -0,0 +1,154 @@
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
+ const UsageRecordRowSchema = z
61
+ .object({
62
+ charge_id: z.string().nullable().optional(),
63
+ requested: z.union([z.string(), z.number()]).nullable().optional(),
64
+ replayed: pgBoolean.nullable().optional(),
65
+ error_code: z.string().nullable().optional(),
66
+ })
67
+ .passthrough();
68
+ /** Repository for credit deduction operations. */
69
+ export class DeductionRepository {
70
+ callproc;
71
+ constructor(callproc) {
72
+ this.callproc = callproc;
73
+ }
74
+ /** Atomically deduct credits with allowance, entitlement, quota, and credit-policy enforcement. */
75
+ async deductWithAllowance(params) {
76
+ const rows = await this.callproc("charge_usage_for_operation", [
77
+ params.userId,
78
+ params.operation,
79
+ params.amount,
80
+ params.idempotencyKey,
81
+ params.feature,
82
+ params.model,
83
+ params.region,
84
+ params.metadata,
85
+ params.measures,
86
+ params.dimensions,
87
+ ]);
88
+ const row = (rows?.[0] ?? {});
89
+ const details = row.error_code != null
90
+ ? undefined
91
+ : (await this.callproc("get_credit_operation_details", [
92
+ params.userId,
93
+ row.ledger_entry_id ?? null,
94
+ params.idempotencyKey,
95
+ ]))[0];
96
+ return safeParse(DeductionRowSchema, {
97
+ ...row,
98
+ user_id: params.userId,
99
+ entry_id: row.ledger_entry_id,
100
+ amount: row.charged,
101
+ allowance_consumed: row.allowance_covered,
102
+ balance_after: details?.balance_after ?? row.balance_after,
103
+ bucket_breakdown: details?.bucket_breakdown ?? row.bucket_breakdown,
104
+ idempotent: row.replayed,
105
+ error: row.error_code,
106
+ }, "DeductionRepository.deductWithAllowance");
107
+ }
108
+ /** Append a priced usage event without creating another balance debit. */
109
+ async recordUsage(params) {
110
+ const rows = await this.callproc("record_usage", [
111
+ params.userId,
112
+ params.operation,
113
+ params.amount,
114
+ params.idempotencyKey,
115
+ params.feature,
116
+ params.model,
117
+ params.region,
118
+ params.metadata,
119
+ params.measures,
120
+ params.dimensions,
121
+ ]);
122
+ return safeParse(UsageRecordRowSchema, rows?.[0] ?? {}, "DeductionRepository.recordUsage");
123
+ }
124
+ /** Refund a previous credit deduction. */
125
+ async refundCredits(entryId, amount, idempotencyKey, reason, metadata) {
126
+ const rows = await this.callproc("refund_credit_by_entry", [
127
+ entryId,
128
+ amount,
129
+ idempotencyKey,
130
+ reason,
131
+ metadata,
132
+ ]);
133
+ const row = (rows?.[0] ?? {});
134
+ return safeParse(RefundRowSchema, {
135
+ ...row,
136
+ refund_entry_id: row.entry_id,
137
+ user_id: row.subject_id,
138
+ new_balance: row.balance_after,
139
+ error: row.error_code,
140
+ }, "DeductionRepository.refundCredits");
141
+ }
142
+ /** Revoke credits by transaction type. */
143
+ async revokeCreditsByEntryType(userId, entryType) {
144
+ const rows = await this.callproc("revoke_subject_credits_by_operation", [userId, entryType]);
145
+ const row = (rows?.[0] ?? {});
146
+ return safeParse(RevokeRowSchema, {
147
+ user_id: userId,
148
+ amount: row.revoked,
149
+ new_balance: row.balance_after,
150
+ bucket: null,
151
+ }, "DeductionRepository.revokeCreditsByEntryType");
152
+ }
153
+ }
154
+ //# 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,CA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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","sourcesContent":["import { z } from \"zod\";\nimport type { CallProc } from \"../../../shared/postgres-types.js\";\nimport { pgBoolean, safeParse } from \"../../../shared/postgres-validation.js\";\n\nexport const DeductionRowSchema = z\n .object({\n entry_id: z.string().nullable().optional(),\n amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n balance_after: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n allowance_consumed: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n idempotent: pgBoolean.nullable().optional(),\n bucket_breakdown: z\n .record(z.string(), z.union([z.string(), z.number()] as const))\n .nullable()\n .optional(),\n error: z.string().nullable().optional(),\n user_id: z.string().optional(),\n })\n .passthrough();\n\nconst RefundRowSchema = z\n .object({\n refund_entry_id: z.string().optional(),\n user_id: z.string().optional(),\n amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n new_balance: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n bucket_breakdown: z\n .record(z.string(), z.union([z.string(), z.number()] as const))\n .nullable()\n .optional(),\n error: z.string().nullable().optional(),\n })\n .passthrough();\n\nconst RevokeRowSchema = z\n .object({\n user_id: z.string().optional(),\n amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n new_balance: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n bucket: z.string().nullable().optional(),\n })\n .passthrough();\n\nexport type DeductionRow = z.infer<typeof DeductionRowSchema>;\nexport type RefundRow = z.infer<typeof RefundRowSchema>;\nexport type RevokeRow = z.infer<typeof RevokeRowSchema>;\n\nconst UsageRecordRowSchema = z\n .object({\n charge_id: z.string().nullable().optional(),\n requested: z.union([z.string(), z.number()]).nullable().optional(),\n replayed: pgBoolean.nullable().optional(),\n error_code: z.string().nullable().optional(),\n })\n .passthrough();\n\nexport type UsageRecordRow = z.infer<typeof UsageRecordRowSchema>;\n\n/** Typed input for the plan-aware operation charge RPC. */\nexport interface DeductParams {\n userId: string;\n operation: string;\n amount: string;\n idempotencyKey: string;\n feature: string | null;\n model: string | null;\n region: string | null;\n measures: string;\n dimensions: string;\n metadata: string;\n}\n\n/** Repository for credit deduction operations. */\nexport class DeductionRepository {\n constructor(private callproc: CallProc) {}\n\n /** Atomically deduct credits with allowance, entitlement, quota, and credit-policy enforcement. */\n async deductWithAllowance(params: DeductParams): Promise<DeductionRow> {\n const rows = await this.callproc(\"charge_usage_for_operation\", [\n params.userId,\n params.operation,\n params.amount,\n params.idempotencyKey,\n params.feature,\n params.model,\n params.region,\n params.metadata,\n params.measures,\n params.dimensions,\n ]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n const details =\n row.error_code != null\n ? undefined\n : ((\n await this.callproc(\"get_credit_operation_details\", [\n params.userId,\n row.ledger_entry_id ?? null,\n params.idempotencyKey,\n ])\n )[0] as Record<string, unknown> | undefined);\n return safeParse(\n DeductionRowSchema,\n {\n ...row,\n user_id: params.userId,\n entry_id: row.ledger_entry_id,\n amount: row.charged,\n allowance_consumed: row.allowance_covered,\n balance_after: details?.balance_after ?? row.balance_after,\n bucket_breakdown: details?.bucket_breakdown ?? row.bucket_breakdown,\n idempotent: row.replayed,\n error: row.error_code,\n },\n \"DeductionRepository.deductWithAllowance\",\n );\n }\n\n /** Append a priced usage event without creating another balance debit. */\n async recordUsage(params: DeductParams): Promise<UsageRecordRow> {\n const rows = await this.callproc(\"record_usage\", [\n params.userId,\n params.operation,\n params.amount,\n params.idempotencyKey,\n params.feature,\n params.model,\n params.region,\n params.metadata,\n params.measures,\n params.dimensions,\n ]);\n return safeParse(UsageRecordRowSchema, rows?.[0] ?? {}, \"DeductionRepository.recordUsage\");\n }\n\n /** Refund a previous credit deduction. */\n async refundCredits(\n entryId: string,\n amount: string | null,\n idempotencyKey: string,\n reason: string | null,\n metadata: string,\n ): Promise<RefundRow> {\n const rows = await this.callproc(\"refund_credit_by_entry\", [\n entryId,\n amount,\n idempotencyKey,\n reason,\n metadata,\n ]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n return safeParse(\n RefundRowSchema,\n {\n ...row,\n refund_entry_id: row.entry_id,\n user_id: row.subject_id,\n new_balance: row.balance_after,\n error: row.error_code,\n },\n \"DeductionRepository.refundCredits\",\n );\n }\n\n /** Revoke credits by transaction type. */\n async revokeCreditsByEntryType(userId: string, entryType: string): Promise<RevokeRow> {\n const rows = await this.callproc(\"revoke_subject_credits_by_operation\", [userId, entryType]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n return safeParse(\n RevokeRowSchema,\n {\n user_id: userId,\n amount: row.revoked,\n new_balance: row.balance_after,\n bucket: null,\n },\n \"DeductionRepository.revokeCreditsByEntryType\",\n );\n }\n}\n"]}
@@ -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,cAA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AC,sBAAsB,GAAG,IAAI,CAAC;IAMhG,qDAAqD;IAC/C,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAexE,4EAA4E;IACtE,UAAU,CAAC,MAAM,EAAE,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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AAC;QAC5F,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAA4B,CAAC;QACzD,MAAM,KAAK,GACT,GAAG,CAAC,UAAU,IAAI,IAAI,IAAI,GAAG,CAAC,QAAQ,IAAI,IAAI;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","sourcesContent":["import { z } from \"zod\";\nimport type { CallProc } from \"../../../shared/postgres-types.js\";\nimport { DeductionRowSchema } from \"./deduction.js\";\nimport type { DeductionRow } from \"./deduction.js\";\nimport { pgBoolean, safeParse } from \"../../../shared/postgres-validation.js\";\n\nconst LeaseRowSchema = z\n .object({\n lease_id: z.string().nullable().optional(),\n user_id: z.string().optional(),\n amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n available: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n reserved: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n billing_mode: z.string().optional(),\n minimum_balance: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n expires_at: z\n .union([z.string(), z.date().transform((value) => value.toISOString())])\n .optional(),\n error: z.string().nullable().optional(),\n })\n .passthrough();\n\nconst ReleaseRowSchema = z\n .object({\n released: pgBoolean.nullable().optional(),\n reason: z.string().nullable().optional(),\n })\n .passthrough();\n\nconst LeasePricingContextRowSchema = z\n .object({\n catalog_revision_no: z.coerce.number(),\n plan_id: z.string().nullable().optional(),\n plan_key: z.string().nullable().optional(),\n rate_card: z.string().nullable().optional(),\n })\n .passthrough();\n\nexport type LeaseRow = z.infer<typeof LeaseRowSchema>;\nexport type ReleaseRow = z.infer<typeof ReleaseRowSchema>;\nexport type LeasePricingContextRow = z.infer<typeof LeasePricingContextRowSchema>;\n\n/** Repository for lease lifecycle operations (admission control). */\nexport class LeaseRepository {\n constructor(private callproc: CallProc) {}\n\n /** Atomically acquire a lease (hold) — admission control. */\n async createLease(params: {\n userId: string;\n amount: string;\n operationType: string;\n idempotencyKey: string;\n ttlSeconds: number;\n metadata: string;\n feature: string | null;\n measures: string;\n dimensions: string;\n minimumBalance: string | null;\n maxConcurrent: number | null;\n }): Promise<LeaseRow> {\n const rows = await this.callproc(\"create_lease_for_operation\", [\n params.userId,\n params.operationType,\n params.amount,\n params.idempotencyKey,\n `${params.ttlSeconds} seconds`,\n params.metadata,\n params.feature,\n params.measures,\n params.dimensions,\n params.minimumBalance,\n params.maxConcurrent,\n ]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n const lease =\n row.lease_id == null\n ? null\n : ((await this.callproc(\"get_credit_lease\", [params.userId, row.lease_id]))[0] as\n Record<string, unknown> | undefined);\n return safeParse(\n LeaseRowSchema,\n {\n ...row,\n user_id: params.userId,\n amount: row.reserved_amount,\n expires_at: lease?.expires_at,\n minimum_balance: lease?.minimum_balance,\n error: row.error_code,\n },\n \"LeaseRepository.createLease\",\n );\n }\n\n /** Charge the actual cost against a lease and mark it settled. */\n async settleLease(params: {\n userId: string;\n leaseId: string;\n amount: string;\n idempotencyKey: string;\n feature: string | null;\n model: string | null;\n region: string | null;\n measures: string;\n dimensions: string;\n metadata: string;\n }): Promise<DeductionRow> {\n const rows = await this.callproc(\"settle_lease\", [\n params.userId,\n params.leaseId,\n params.amount,\n params.idempotencyKey,\n params.feature,\n params.model,\n params.region,\n params.measures,\n params.dimensions,\n params.metadata,\n ]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n const charge = (\n await this.callproc(\"get_credit_operation_details\", [\n params.userId,\n row.ledger_entry_id ?? null,\n params.idempotencyKey,\n ])\n )[0] as Record<string, unknown> | undefined;\n return safeParse(\n DeductionRowSchema,\n {\n ...row,\n entry_id: row.ledger_entry_id,\n amount: row.settled_amount,\n allowance_consumed: charge?.allowance_covered,\n balance_after: charge?.balance_after,\n idempotent: row.replayed,\n error: row.error_code,\n },\n \"LeaseRepository.settleLease\",\n );\n }\n\n /** Return the immutable pricing context captured at lease admission. */\n async getPricingContext(userId: string, leaseId: string): Promise<LeasePricingContextRow | null> {\n const rows = await this.callproc(\"get_credit_lease_pricing_context\", [userId, leaseId]);\n if (!rows?.length) return null;\n return safeParse(LeasePricingContextRowSchema, rows[0], \"LeaseRepository.getPricingContext\");\n }\n\n /** Release a lease without charging — idempotent. */\n async releaseLease(userId: string, leaseId: string): Promise<ReleaseRow> {\n const rows = await this.callproc(\"release_lease\", [userId, leaseId]);\n const result = rows?.[0] as { released?: boolean } | undefined;\n const reason =\n typeof rows?.[0] === \"string\" ? rows[0] : result?.released === true ? \"released\" : null;\n return safeParse(\n ReleaseRowSchema,\n {\n released: reason === \"released\",\n reason,\n },\n \"LeaseRepository.releaseLease\",\n );\n }\n\n /** Extend an active lease without changing its captured policy snapshot. */\n async renewLease(userId: string, leaseId: string, ttlSeconds: number): Promise<LeaseRow> {\n const rows = await this.callproc(\"renew_lease\", [userId, leaseId, `${ttlSeconds} seconds`]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n const lease =\n row.error_code == null && row.lease_id != null\n ? ((await this.callproc(\"get_credit_lease\", [userId, row.lease_id]))[0] as\n Record<string, unknown> | undefined)\n : undefined;\n return safeParse(\n LeaseRowSchema,\n {\n ...row,\n ...lease,\n user_id: userId,\n amount: row.reserved_amount,\n error: row.error_code,\n },\n \"LeaseRepository.renewLease\",\n );\n }\n\n /** Expire a bounded batch of leases and release their reservations. */\n async expireLeases(limit: number): Promise<number> {\n const rows = await this.callproc(\"expire_leases\", [limit]);\n const row = rows?.[0] as Record<string, unknown> | undefined;\n return Number(row?.expired ?? 0);\n }\n}\n"]}