@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,110 @@
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_priority: z.ZodOptional<z.ZodNullable<z.ZodCoercedNumber<unknown>>>;
11
+ credit_allowance_reset_unit: z.ZodOptional<z.ZodNullable<z.ZodString>>;
12
+ credit_allowance_reset_count: z.ZodOptional<z.ZodNullable<z.ZodCoercedNumber<unknown>>>;
13
+ credit_allowance_reset_anchor: z.ZodOptional<z.ZodNullable<z.ZodString>>;
14
+ credit_allowance_reset_timezone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
15
+ entitlements: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
16
+ credit_policy_type: z.ZodOptional<z.ZodNullable<z.ZodString>>;
17
+ credit_limit: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
18
+ admission_max_in_flight: z.ZodOptional<z.ZodNullable<z.ZodCoercedNumber<unknown>>>;
19
+ operation_admission: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
20
+ allowed_operations: z.ZodOptional<z.ZodArray<z.ZodString>>;
21
+ assignment_source_type: z.ZodOptional<z.ZodNullable<z.ZodString>>;
22
+ assignment_source_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
23
+ catalog_revision_pinned: z.ZodOptional<z.ZodBoolean>;
24
+ plan_assigned_at: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>>;
25
+ catalog_revision_no: z.ZodOptional<z.ZodNullable<z.ZodCoercedNumber<unknown>>>;
26
+ }, z.core.$loose>;
27
+ declare const SetUserPlanRowSchema: z.ZodObject<{
28
+ user_id: z.ZodOptional<z.ZodString>;
29
+ plan_id: z.ZodOptional<z.ZodString>;
30
+ plan_assigned_at: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>>;
31
+ }, z.core.$loose>;
32
+ declare const AllowanceRowSchema: z.ZodObject<{
33
+ plan_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
34
+ allowance_remaining: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
35
+ period_start: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>>;
36
+ period_end: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>>;
37
+ }, z.core.$loose>;
38
+ declare const UnsetPlanRowSchema: z.ZodObject<{
39
+ user_id: z.ZodOptional<z.ZodString>;
40
+ }, z.core.$loose>;
41
+ declare const PlanMigrationStartRowSchema: z.ZodNullable<z.ZodString>;
42
+ declare const PlanMigrationBatchRowSchema: z.ZodObject<{
43
+ migrated: z.ZodNumber;
44
+ done: z.ZodBoolean;
45
+ next_cursor: z.ZodNullable<z.ZodString>;
46
+ }, z.core.$loose>;
47
+ declare const QuotaStateRowSchema: z.ZodObject<{
48
+ user_id: z.ZodString;
49
+ quota_key: z.ZodString;
50
+ operation_key: z.ZodString;
51
+ measure_key: z.ZodString;
52
+ quota_limit: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
53
+ consumed: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
54
+ reserved: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
55
+ remaining: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
56
+ overage: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
57
+ enforcement: z.ZodEnum<{
58
+ block: "block";
59
+ allow: "allow";
60
+ }>;
61
+ window_start: z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>;
62
+ window_end: z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>;
63
+ emit_at_percent: z.ZodArray<z.ZodNumber>;
64
+ }, z.core.$loose>;
65
+ declare const QuotaEventRowSchema: z.ZodObject<{
66
+ event_id: z.ZodString;
67
+ quota_key: z.ZodString;
68
+ operation_key: z.ZodString;
69
+ measure_key: z.ZodString;
70
+ event_type: z.ZodEnum<{
71
+ threshold: "threshold";
72
+ blocked: "blocked";
73
+ }>;
74
+ threshold_percent: z.ZodNullable<z.ZodCoercedNumber<unknown>>;
75
+ idempotency_key: z.ZodString;
76
+ usage_charge_id: z.ZodNullable<z.ZodString>;
77
+ created_at: z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>;
78
+ }, z.core.$loose>;
79
+ export type UnsetPlanRow = z.infer<typeof UnsetPlanRowSchema>;
80
+ export type UserPlanRow = z.infer<typeof UserPlanRowSchema>;
81
+ export type SetUserPlanRow = z.infer<typeof SetUserPlanRowSchema>;
82
+ export type AllowanceRow = z.infer<typeof AllowanceRowSchema>;
83
+ export type PlanMigrationStartRow = z.infer<typeof PlanMigrationStartRowSchema>;
84
+ export type PlanMigrationBatchRow = z.infer<typeof PlanMigrationBatchRowSchema>;
85
+ export type QuotaStateRow = z.infer<typeof QuotaStateRowSchema>;
86
+ export type QuotaEventRow = z.infer<typeof QuotaEventRowSchema>;
87
+ /** Repository for plan management operations. */
88
+ export declare class PlanRepository {
89
+ private callproc;
90
+ constructor(callproc: CallProc);
91
+ /** Fetch a user's current plan. Returns null if user has no plan. */
92
+ getUserPlan(userId: string): Promise<UserPlanRow | null>;
93
+ getEntitlement(userId: string, feature: string): Promise<Record<string, unknown> | null>;
94
+ /** Assign a plan to a user. */
95
+ setUserPlan(userId: string, planKey: string, planAssignedAt: string | null): Promise<SetUserPlanRow>;
96
+ /** Remove a user's plan assignment. */
97
+ unsetUserPlan(userId: string): Promise<UnsetPlanRow>;
98
+ /** Pin or unpin the user's current assignment to its catalog revision. */
99
+ setPlanRevisionPin(userId: string, pinned: boolean): Promise<boolean>;
100
+ /** Apply one bounded batch of renewal-effective plan changes. */
101
+ applyDuePlanChanges(limit: number): Promise<number>;
102
+ /** Check a user's remaining free allowance. */
103
+ startPlanMigration(fromPlanId: string | null, toPlanId: string): Promise<PlanMigrationStartRow>;
104
+ migratePlanBatch(migrationId: string, batchSize?: number): Promise<PlanMigrationBatchRow>;
105
+ getQuotaState(userId: string, quotaKey?: string | null): Promise<QuotaStateRow[]>;
106
+ listQuotaEvents(userId: string, after: string | null, limit: number, idempotencyKey: string | null, afterId: string | null): Promise<QuotaEventRow[]>;
107
+ checkAllowance(userId: string): Promise<AllowanceRow | null>;
108
+ }
109
+ export {};
110
+ //# sourceMappingURL=plan.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/plan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAGlE,QAAA,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;iBAkCP,CAAC;AAEjB,QAAA,MAAM,oBAAoB;;;;iBASV,CAAC;AAEjB,QAAA,MAAM,kBAAkB;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C;IAQ9F,+BAA+B;IACzB,WAAW,CACf,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,cAAc,EAAE,MAAM,GAAG,IAAI,GAC5B,OAAO,CAAC,cAAc,CAAC;IAsB1B,uCAAuC;IACjC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAQ1D,0EAA0E;IACpE,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAK3E,iEAAiE;IAC3D,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAKzD,+CAA+C;IACzC,kBAAkB,CACtB,UAAU,EAAE,MAAM,GAAG,IAAI,EACzB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,qBAAqB,CAAC;IAS3B,gBAAgB,CAAC,WAAW,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC;IASzF,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAOjF,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,KAAK,EAAE,MAAM,EACb,cAAc,EAAE,MAAM,GAAG,IAAI,EAC7B,OAAO,EAAE,MAAM,GAAG,IAAI,GACrB,OAAO,CAAC,aAAa,EAAE,CAAC;IAarB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;CAKnE"}
@@ -0,0 +1,194 @@
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_priority: z.coerce.number().int().nonnegative().nullable().optional(),
15
+ credit_allowance_reset_unit: z.string().nullable().optional(),
16
+ credit_allowance_reset_count: z.coerce.number().nullable().optional(),
17
+ credit_allowance_reset_anchor: z.string().nullable().optional(),
18
+ credit_allowance_reset_timezone: z.string().nullable().optional(),
19
+ entitlements: z.record(z.string(), z.unknown()).optional(),
20
+ credit_policy_type: z.string().nullable().optional(),
21
+ credit_limit: z
22
+ .union([z.string(), z.number()])
23
+ .nullable()
24
+ .optional(),
25
+ admission_max_in_flight: z.coerce.number().nullable().optional(),
26
+ operation_admission: z.record(z.string(), z.unknown()).optional(),
27
+ allowed_operations: z.array(z.string()).optional(),
28
+ assignment_source_type: z.string().nullable().optional(),
29
+ assignment_source_id: z.string().nullable().optional(),
30
+ catalog_revision_pinned: z.boolean().optional(),
31
+ plan_assigned_at: z
32
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
33
+ .nullable()
34
+ .optional(),
35
+ catalog_revision_no: z.coerce.number().nullable().optional(),
36
+ })
37
+ .passthrough();
38
+ const SetUserPlanRowSchema = z
39
+ .object({
40
+ user_id: z.string().optional(),
41
+ plan_id: z.string().optional(),
42
+ plan_assigned_at: z
43
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
44
+ .nullable()
45
+ .optional(),
46
+ })
47
+ .passthrough();
48
+ const AllowanceRowSchema = z
49
+ .object({
50
+ plan_id: z.string().nullable().optional(),
51
+ allowance_remaining: z
52
+ .union([z.string(), z.number()])
53
+ .nullable()
54
+ .optional(),
55
+ period_start: z
56
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
57
+ .nullable()
58
+ .optional(),
59
+ period_end: z
60
+ .union([z.string(), z.date().transform((value) => value.toISOString())])
61
+ .nullable()
62
+ .optional(),
63
+ })
64
+ .passthrough();
65
+ const UnsetPlanRowSchema = z
66
+ .object({
67
+ user_id: z.string().optional(),
68
+ })
69
+ .passthrough();
70
+ const PlanMigrationStartRowSchema = z.string().nullable();
71
+ const PlanMigrationBatchRowSchema = z
72
+ .object({
73
+ migrated: z.number(),
74
+ done: z.boolean(),
75
+ next_cursor: z.string().nullable(),
76
+ })
77
+ .passthrough();
78
+ const decimal = z.union([z.string(), z.number()]);
79
+ const QuotaStateRowSchema = z
80
+ .object({
81
+ user_id: z.string(),
82
+ quota_key: z.string(),
83
+ operation_key: z.string(),
84
+ measure_key: z.string(),
85
+ quota_limit: decimal,
86
+ consumed: decimal,
87
+ reserved: decimal,
88
+ remaining: decimal,
89
+ overage: decimal,
90
+ enforcement: z.enum(["block", "allow"]),
91
+ window_start: z.union([z.string(), z.date().transform((value) => value.toISOString())]),
92
+ window_end: z.union([z.string(), z.date().transform((value) => value.toISOString())]),
93
+ emit_at_percent: z.array(z.number()),
94
+ })
95
+ .passthrough();
96
+ const QuotaEventRowSchema = z
97
+ .object({
98
+ event_id: z.string(),
99
+ quota_key: z.string(),
100
+ operation_key: z.string(),
101
+ measure_key: z.string(),
102
+ event_type: z.enum(["threshold", "blocked"]),
103
+ threshold_percent: z.coerce.number().nullable(),
104
+ idempotency_key: z.string(),
105
+ usage_charge_id: z.string().nullable(),
106
+ created_at: z.union([z.string(), z.date().transform((value) => value.toISOString())]),
107
+ })
108
+ .passthrough();
109
+ /** Repository for plan management operations. */
110
+ export class PlanRepository {
111
+ callproc;
112
+ constructor(callproc) {
113
+ this.callproc = callproc;
114
+ }
115
+ /** Fetch a user's current plan. Returns null if user has no plan. */
116
+ async getUserPlan(userId) {
117
+ const rows = await this.callproc("get_subject_plan", [userId]);
118
+ if (!rows || rows.length === 0)
119
+ return null;
120
+ return safeParse(UserPlanRowSchema, rows[0], "PlanRepository.getUserPlan");
121
+ }
122
+ async getEntitlement(userId, feature) {
123
+ const rows = await this.callproc("get_subject_entitlements", [userId]);
124
+ const row = rows.find((candidate) => candidate.feature_key === feature);
125
+ return row ?? null;
126
+ }
127
+ /** Assign a plan to a user. */
128
+ async setUserPlan(userId, planKey, planAssignedAt) {
129
+ const planRows = await this.callproc("resolve_active_plan", [planKey]);
130
+ const planRow = planRows[0];
131
+ if (planRow?.id == null)
132
+ throw new Error(`unknown active plan '${planKey}'`);
133
+ const planId = String(planRow.id);
134
+ const params = planAssignedAt ? [userId, planId, planAssignedAt] : [userId, planId];
135
+ const rows = await this.callproc("assign_plan", params);
136
+ if (rows?.[0] !== true) {
137
+ throw new Error("PlanRepository.setUserPlan: assign_plan returned false");
138
+ }
139
+ const assigned = await this.getUserPlan(userId);
140
+ return safeParse(SetUserPlanRowSchema, {
141
+ user_id: userId,
142
+ plan_id: assigned?.plan_id ?? planId,
143
+ plan_assigned_at: assigned?.plan_assigned_at ?? planAssignedAt,
144
+ }, "PlanRepository.setUserPlan");
145
+ }
146
+ /** Remove a user's plan assignment. */
147
+ async unsetUserPlan(userId) {
148
+ const rows = await this.callproc("unassign_plan", [userId, "sdk_unassignment"]);
149
+ if (rows?.[0] !== true) {
150
+ throw new Error("PlanRepository.unsetUserPlan: unassign_plan returned false");
151
+ }
152
+ return safeParse(UnsetPlanRowSchema, { user_id: userId }, "PlanRepository.unsetUserPlan");
153
+ }
154
+ /** Pin or unpin the user's current assignment to its catalog revision. */
155
+ async setPlanRevisionPin(userId, pinned) {
156
+ const rows = await this.callproc("set_plan_revision_pin", [userId, pinned]);
157
+ return rows?.[0] === true;
158
+ }
159
+ /** Apply one bounded batch of renewal-effective plan changes. */
160
+ async applyDuePlanChanges(limit) {
161
+ const rows = await this.callproc("apply_due_plan_assignment_changes", [limit]);
162
+ return Number(rows?.[0] ?? 0);
163
+ }
164
+ /** Check a user's remaining free allowance. */
165
+ async startPlanMigration(fromPlanId, toPlanId) {
166
+ const rows = await this.callproc("start_plan_migration", [fromPlanId, toPlanId]);
167
+ return safeParse(PlanMigrationStartRowSchema, rows?.[0] ?? null, "PlanRepository.startPlanMigration");
168
+ }
169
+ async migratePlanBatch(migrationId, batchSize) {
170
+ const rows = await this.callproc("migrate_plan_batch", [migrationId, batchSize ?? 100]);
171
+ return safeParse(PlanMigrationBatchRowSchema, rows?.[0] ?? {}, "PlanRepository.migratePlanBatch");
172
+ }
173
+ async getQuotaState(userId, quotaKey) {
174
+ const rows = await this.callproc("get_subject_quota_state", [userId, quotaKey ?? null]);
175
+ return (rows ?? []).map((row) => safeParse(QuotaStateRowSchema, row, "PlanRepository.getQuotaState"));
176
+ }
177
+ async listQuotaEvents(userId, after, limit, idempotencyKey, afterId) {
178
+ const rows = await this.callproc("list_subject_quota_events", [
179
+ userId,
180
+ after,
181
+ limit,
182
+ idempotencyKey,
183
+ afterId,
184
+ ]);
185
+ return (rows ?? []).map((row) => safeParse(QuotaEventRowSchema, row, "PlanRepository.listQuotaEvents"));
186
+ }
187
+ async checkAllowance(userId) {
188
+ const rows = await this.callproc("get_subject_allowance", [userId]);
189
+ if (!rows || rows.length === 0)
190
+ return null;
191
+ return safeParse(AllowanceRowSchema, rows[0], "PlanRepository.checkAllowance");
192
+ }
193
+ }
194
+ //# sourceMappingURL=plan.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan.js","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/plan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAEnE,MAAM,iBAAiB,GAAG,CAAC;KACxB,MAAM,CAAC;IACN,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC9B,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;IACzC,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;IAC1C,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,C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";\nimport { safeParse } from \"../../../shared/postgres-validation.js\";\n\nconst UserPlanRowSchema = z\n .object({\n user_id: z.string().optional(),\n plan_id: z.string().nullable().optional(),\n plan_key: z.string().nullable().optional(),\n plan_label: z.string().nullable().optional(),\n rate_card: z.string().nullable().optional(),\n credit_allowance_amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n credit_allowance_priority: z.coerce.number().int().nonnegative().nullable().optional(),\n credit_allowance_reset_unit: z.string().nullable().optional(),\n credit_allowance_reset_count: z.coerce.number().nullable().optional(),\n credit_allowance_reset_anchor: z.string().nullable().optional(),\n credit_allowance_reset_timezone: z.string().nullable().optional(),\n entitlements: z.record(z.string(), z.unknown()).optional(),\n credit_policy_type: z.string().nullable().optional(),\n credit_limit: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n admission_max_in_flight: z.coerce.number().nullable().optional(),\n operation_admission: z.record(z.string(), z.unknown()).optional(),\n allowed_operations: z.array(z.string()).optional(),\n assignment_source_type: z.string().nullable().optional(),\n assignment_source_id: z.string().nullable().optional(),\n catalog_revision_pinned: z.boolean().optional(),\n plan_assigned_at: z\n .union([z.string(), z.date().transform((value) => value.toISOString())])\n .nullable()\n .optional(),\n catalog_revision_no: z.coerce.number().nullable().optional(),\n })\n .passthrough();\n\nconst SetUserPlanRowSchema = z\n .object({\n user_id: z.string().optional(),\n plan_id: z.string().optional(),\n plan_assigned_at: z\n .union([z.string(), z.date().transform((value) => value.toISOString())])\n .nullable()\n .optional(),\n })\n .passthrough();\n\nconst AllowanceRowSchema = z\n .object({\n plan_id: z.string().nullable().optional(),\n allowance_remaining: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n period_start: z\n .union([z.string(), z.date().transform((value) => value.toISOString())])\n .nullable()\n .optional(),\n period_end: z\n .union([z.string(), z.date().transform((value) => value.toISOString())])\n .nullable()\n .optional(),\n })\n .passthrough();\n\nconst UnsetPlanRowSchema = z\n .object({\n user_id: z.string().optional(),\n })\n .passthrough();\n\nconst PlanMigrationStartRowSchema = z.string().nullable();\nconst PlanMigrationBatchRowSchema = z\n .object({\n migrated: z.number(),\n done: z.boolean(),\n next_cursor: z.string().nullable(),\n })\n .passthrough();\nconst decimal = z.union([z.string(), z.number()] as const);\nconst QuotaStateRowSchema = z\n .object({\n user_id: z.string(),\n quota_key: z.string(),\n operation_key: z.string(),\n measure_key: z.string(),\n quota_limit: decimal,\n consumed: decimal,\n reserved: decimal,\n remaining: decimal,\n overage: decimal,\n enforcement: z.enum([\"block\", \"allow\"]),\n window_start: z.union([z.string(), z.date().transform((value) => value.toISOString())]),\n window_end: z.union([z.string(), z.date().transform((value) => value.toISOString())]),\n emit_at_percent: z.array(z.number()),\n })\n .passthrough();\nconst QuotaEventRowSchema = z\n .object({\n event_id: z.string(),\n quota_key: z.string(),\n operation_key: z.string(),\n measure_key: z.string(),\n event_type: z.enum([\"threshold\", \"blocked\"]),\n threshold_percent: z.coerce.number().nullable(),\n idempotency_key: z.string(),\n usage_charge_id: z.string().nullable(),\n created_at: z.union([z.string(), z.date().transform((value) => value.toISOString())]),\n })\n .passthrough();\n\nexport type UnsetPlanRow = z.infer<typeof UnsetPlanRowSchema>;\nexport type UserPlanRow = z.infer<typeof UserPlanRowSchema>;\nexport type SetUserPlanRow = z.infer<typeof SetUserPlanRowSchema>;\nexport type AllowanceRow = z.infer<typeof AllowanceRowSchema>;\nexport type PlanMigrationStartRow = z.infer<typeof PlanMigrationStartRowSchema>;\nexport type PlanMigrationBatchRow = z.infer<typeof PlanMigrationBatchRowSchema>;\nexport type QuotaStateRow = z.infer<typeof QuotaStateRowSchema>;\nexport type QuotaEventRow = z.infer<typeof QuotaEventRowSchema>;\n\n/** Repository for plan management operations. */\nexport class PlanRepository {\n constructor(private callproc: CallProc) {}\n\n /** Fetch a user's current plan. Returns null if user has no plan. */\n async getUserPlan(userId: string): Promise<UserPlanRow | null> {\n const rows = await this.callproc(\"get_subject_plan\", [userId]);\n if (!rows || rows.length === 0) return null;\n return safeParse(UserPlanRowSchema, rows[0], \"PlanRepository.getUserPlan\");\n }\n\n async getEntitlement(userId: string, feature: string): Promise<Record<string, unknown> | null> {\n const rows = await this.callproc(\"get_subject_entitlements\", [userId]);\n const row = (rows as Array<Record<string, unknown>>).find(\n (candidate) => candidate.feature_key === feature,\n );\n return row ?? null;\n }\n\n /** Assign a plan to a user. */\n async setUserPlan(\n userId: string,\n planKey: string,\n planAssignedAt: string | null,\n ): Promise<SetUserPlanRow> {\n const planRows = await this.callproc(\"resolve_active_plan\", [planKey]);\n const planRow = planRows[0] as Record<string, unknown> | undefined;\n if (planRow?.id == null) throw new Error(`unknown active plan '${planKey}'`);\n const planId = String(planRow.id);\n const params = planAssignedAt ? [userId, planId, planAssignedAt] : [userId, planId];\n const rows = await this.callproc(\"assign_plan\", params);\n if (rows?.[0] !== true) {\n throw new Error(\"PlanRepository.setUserPlan: assign_plan returned false\");\n }\n const assigned = await this.getUserPlan(userId);\n return safeParse(\n SetUserPlanRowSchema,\n {\n user_id: userId,\n plan_id: assigned?.plan_id ?? planId,\n plan_assigned_at: assigned?.plan_assigned_at ?? planAssignedAt,\n },\n \"PlanRepository.setUserPlan\",\n );\n }\n\n /** Remove a user's plan assignment. */\n async unsetUserPlan(userId: string): Promise<UnsetPlanRow> {\n const rows = await this.callproc(\"unassign_plan\", [userId, \"sdk_unassignment\"]);\n if (rows?.[0] !== true) {\n throw new Error(\"PlanRepository.unsetUserPlan: unassign_plan returned false\");\n }\n return safeParse(UnsetPlanRowSchema, { user_id: userId }, \"PlanRepository.unsetUserPlan\");\n }\n\n /** Pin or unpin the user's current assignment to its catalog revision. */\n async setPlanRevisionPin(userId: string, pinned: boolean): Promise<boolean> {\n const rows = await this.callproc(\"set_plan_revision_pin\", [userId, pinned]);\n return rows?.[0] === true;\n }\n\n /** Apply one bounded batch of renewal-effective plan changes. */\n async applyDuePlanChanges(limit: number): Promise<number> {\n const rows = await this.callproc(\"apply_due_plan_assignment_changes\", [limit]);\n return Number(rows?.[0] ?? 0);\n }\n\n /** Check a user's remaining free allowance. */\n async startPlanMigration(\n fromPlanId: string | null,\n toPlanId: string,\n ): Promise<PlanMigrationStartRow> {\n const rows = await this.callproc(\"start_plan_migration\", [fromPlanId, toPlanId]);\n return safeParse(\n PlanMigrationStartRowSchema,\n rows?.[0] ?? null,\n \"PlanRepository.startPlanMigration\",\n );\n }\n\n async migratePlanBatch(migrationId: string, batchSize?: number): Promise<PlanMigrationBatchRow> {\n const rows = await this.callproc(\"migrate_plan_batch\", [migrationId, batchSize ?? 100]);\n return safeParse(\n PlanMigrationBatchRowSchema,\n rows?.[0] ?? {},\n \"PlanRepository.migratePlanBatch\",\n );\n }\n\n async getQuotaState(userId: string, quotaKey?: string | null): Promise<QuotaStateRow[]> {\n const rows = await this.callproc(\"get_subject_quota_state\", [userId, quotaKey ?? null]);\n return (rows ?? []).map((row) =>\n safeParse(QuotaStateRowSchema, row, \"PlanRepository.getQuotaState\"),\n );\n }\n\n async listQuotaEvents(\n userId: string,\n after: string | null,\n limit: number,\n idempotencyKey: string | null,\n afterId: string | null,\n ): Promise<QuotaEventRow[]> {\n const rows = await this.callproc(\"list_subject_quota_events\", [\n userId,\n after,\n limit,\n idempotencyKey,\n afterId,\n ]);\n return (rows ?? []).map((row) =>\n safeParse(QuotaEventRowSchema, row, \"PlanRepository.listQuotaEvents\"),\n );\n }\n\n async checkAllowance(userId: string): Promise<AllowanceRow | null> {\n const rows = await this.callproc(\"get_subject_allowance\", [userId]);\n if (!rows || rows.length === 0) return null;\n return safeParse(AllowanceRowSchema, rows[0], \"PlanRepository.checkAllowance\");\n }\n}\n"]}
@@ -0,0 +1,28 @@
1
+ import { z } from "zod";
2
+ import type { CallProc } from "../../../shared/postgres-types.js";
3
+ declare const ActivePricingRowSchema: z.ZodObject<{
4
+ id: z.ZodOptional<z.ZodString>;
5
+ config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
6
+ source_document: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
7
+ version: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
8
+ revision_no: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
9
+ label: z.ZodOptional<z.ZodNullable<z.ZodString>>;
10
+ active: z.ZodOptional<z.ZodNullable<z.ZodPipe<z.ZodUnion<readonly [z.ZodBoolean, z.ZodString, z.ZodNumber]>, z.ZodTransform<boolean, string | number | boolean>>>>;
11
+ status: z.ZodOptional<z.ZodNullable<z.ZodString>>;
12
+ created_at: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodDate, z.ZodTransform<string, Date>>]>>;
13
+ }, z.core.$loose>;
14
+ export type ActivePricingRow = z.infer<typeof ActivePricingRowSchema>;
15
+ export type PricingHistoryRow = ActivePricingRow;
16
+ export declare class PricingRepository {
17
+ private readonly callproc;
18
+ constructor(callproc: CallProc);
19
+ private parseRevision;
20
+ getActivePricing(): Promise<ActivePricingRow | null>;
21
+ setActivePricing(config: string, label: string | null, rollout: Record<string, unknown>): Promise<ActivePricingRow>;
22
+ publishPricing(config: string, label: string | null, activate?: boolean, rollout?: Record<string, unknown>): Promise<ActivePricingRow>;
23
+ getPricingHistory(): Promise<PricingHistoryRow[]>;
24
+ getBursarConfig(version: number): Promise<ActivePricingRow | null>;
25
+ activatePricing(version: number, rollout: Record<string, unknown>): Promise<ActivePricingRow>;
26
+ }
27
+ export {};
28
+ //# sourceMappingURL=pricing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/pricing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAGlE,QAAA,MAAM,sBAAsB;;;;;;;;;;iBAYZ,CAAC;AACjB,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AACtE,MAAM,MAAM,iBAAiB,GAAG,gBAAgB,CAAC;AAEjD,qBAAa,iBAAiB;IAChB,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,QAAQ;IAE/C,OAAO,CAAC,aAAa;IAaf,gBAAgB,IAAI,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC;IAOpD,gBAAgB,CACpB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,OAAO,CAAC,gBAAgB,CAAC;IAItB,cAAc,CAClB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,QAAQ,UAAQ,EAChB,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAiB,GAC/C,OAAO,CAAC,gBAAgB,CAAC;IA6BtB,iBAAiB,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC;IAOjD,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC;IASlE,eAAe,CACnB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,OAAO,CAAC,gBAAgB,CAAC;CAQ7B"}
@@ -0,0 +1,76 @@
1
+ import { z } from "zod";
2
+ import { pgBoolean, safeParse } from "../../../shared/postgres-validation.js";
3
+ const ActivePricingRowSchema = z
4
+ .object({
5
+ id: z.string().optional(),
6
+ config: z.record(z.string(), z.unknown()).optional(),
7
+ source_document: z.record(z.string(), z.unknown()).optional(),
8
+ version: z.coerce.number().optional(),
9
+ revision_no: z.coerce.number().optional(),
10
+ label: z.string().nullable().optional(),
11
+ active: pgBoolean.nullable().optional(),
12
+ status: z.string().nullable().optional(),
13
+ created_at: z.union([z.string(), z.date().transform((v) => v.toISOString())]).optional(),
14
+ })
15
+ .passthrough();
16
+ export class PricingRepository {
17
+ callproc;
18
+ constructor(callproc) {
19
+ this.callproc = callproc;
20
+ }
21
+ parseRevision(row, context) {
22
+ return safeParse(ActivePricingRowSchema, {
23
+ ...row,
24
+ config: row.source_document,
25
+ version: row.revision_no,
26
+ active: row.status === "active",
27
+ }, context);
28
+ }
29
+ async getActivePricing() {
30
+ const rows = await this.callproc("active_catalog_revision", []);
31
+ if (!rows?.length)
32
+ return null;
33
+ const row = rows[0];
34
+ return this.parseRevision(row, "PricingRepository.getActivePricing");
35
+ }
36
+ async setActivePricing(config, label, rollout) {
37
+ return this.publishPricing(config, label, true, rollout);
38
+ }
39
+ async publishPricing(config, label, activate = false, rollout = { plans: {} }) {
40
+ const published = (await this.callproc("publish_and_activate_catalog", [
41
+ 1,
42
+ JSON.parse(config),
43
+ label,
44
+ activate,
45
+ rollout,
46
+ ]))[0];
47
+ if (published?.revision_no == null) {
48
+ return safeParse(ActivePricingRowSchema, {}, "PricingRepository.publishPricing");
49
+ }
50
+ const revision = await this.getBursarConfig(Number(published.revision_no));
51
+ return (revision ??
52
+ safeParse(ActivePricingRowSchema, {
53
+ id: published.revision_id,
54
+ version: published.revision_no,
55
+ status: published.status,
56
+ active: published.status === "active",
57
+ }, "PricingRepository.publishPricing"));
58
+ }
59
+ async getPricingHistory() {
60
+ const rows = await this.callproc("list_catalog_revisions", [500]);
61
+ return (rows ?? []).map((row) => this.parseRevision(row, "PricingRepository.getPricingHistory"));
62
+ }
63
+ async getBursarConfig(version) {
64
+ const rows = await this.callproc("catalog_revision_by_number", [version]);
65
+ if (!rows?.length)
66
+ return null;
67
+ return this.parseRevision(rows[0], "PricingRepository.getBursarConfig");
68
+ }
69
+ async activatePricing(version, rollout) {
70
+ const rows = await this.callproc("activate_catalog_revision", [version, rollout]);
71
+ if (!rows?.length)
72
+ throw new Error(`Catalog revision ${version} was not found`);
73
+ return this.parseRevision(rows[0], "PricingRepository.activatePricing");
74
+ }
75
+ }
76
+ //# sourceMapping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B,CAAC,CAAC;QAChF,OAAO,IAAI,CAAC,aAAa,CACvB,IAAI,CAAC,CAAC,CAA4B,EAClC,mCAAmC,CACpC,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\nconst ActivePricingRowSchema = z\n .object({\n id: z.string().optional(),\n config: z.record(z.string(), z.unknown()).optional(),\n source_document: z.record(z.string(), z.unknown()).optional(),\n version: z.coerce.number().optional(),\n revision_no: z.coerce.number().optional(),\n label: z.string().nullable().optional(),\n active: pgBoolean.nullable().optional(),\n status: z.string().nullable().optional(),\n created_at: z.union([z.string(), z.date().transform((v) => v.toISOString())]).optional(),\n })\n .passthrough();\nexport type ActivePricingRow = z.infer<typeof ActivePricingRowSchema>;\nexport type PricingHistoryRow = ActivePricingRow;\n\nexport class PricingRepository {\n constructor(private readonly callproc: CallProc) {}\n\n private parseRevision(row: Record<string, unknown>, context: string): ActivePricingRow {\n return safeParse(\n ActivePricingRowSchema,\n {\n ...row,\n config: row.source_document,\n version: row.revision_no,\n active: row.status === \"active\",\n },\n context,\n );\n }\n\n async getActivePricing(): Promise<ActivePricingRow | null> {\n const rows = await this.callproc(\"active_catalog_revision\", []);\n if (!rows?.length) return null;\n const row = rows[0] as Record<string, unknown>;\n return this.parseRevision(row, \"PricingRepository.getActivePricing\");\n }\n\n async setActivePricing(\n config: string,\n label: string | null,\n rollout: Record<string, unknown>,\n ): Promise<ActivePricingRow> {\n return this.publishPricing(config, label, true, rollout);\n }\n\n async publishPricing(\n config: string,\n label: string | null,\n activate = false,\n rollout: Record<string, unknown> = { plans: {} },\n ): Promise<ActivePricingRow> {\n const published = (\n await this.callproc(\"publish_and_activate_catalog\", [\n 1,\n JSON.parse(config),\n label,\n activate,\n rollout,\n ])\n )[0] as Record<string, unknown> | undefined;\n if (published?.revision_no == null) {\n return safeParse(ActivePricingRowSchema, {}, \"PricingRepository.publishPricing\");\n }\n const revision = await this.getBursarConfig(Number(published.revision_no));\n return (\n revision ??\n safeParse(\n ActivePricingRowSchema,\n {\n id: published.revision_id,\n version: published.revision_no,\n status: published.status,\n active: published.status === \"active\",\n },\n \"PricingRepository.publishPricing\",\n )\n );\n }\n\n async getPricingHistory(): Promise<PricingHistoryRow[]> {\n const rows = await this.callproc(\"list_catalog_revisions\", [500]);\n return (rows ?? []).map((row) =>\n this.parseRevision(row as Record<string, unknown>, \"PricingRepository.getPricingHistory\"),\n );\n }\n\n async getBursarConfig(version: number): Promise<ActivePricingRow | null> {\n const rows = await this.callproc(\"catalog_revision_by_number\", [version]);\n if (!rows?.length) return null;\n return this.parseRevision(\n rows[0] as Record<string, unknown>,\n \"PricingRepository.getBursarConfig\",\n );\n }\n\n async activatePricing(\n version: number,\n rollout: Record<string, unknown>,\n ): Promise<ActivePricingRow> {\n const rows = await this.callproc(\"activate_catalog_revision\", [version, rollout]);\n if (!rows?.length) throw new Error(`Catalog revision ${version} was not found`);\n return this.parseRevision(\n rows[0] as Record<string, unknown>,\n \"PricingRepository.activatePricing\",\n );\n }\n}\n"]}
@@ -0,0 +1,63 @@
1
+ import { z } from "zod";
2
+ import type { CallProc } from "../../../shared/postgres-types.js";
3
+ declare const CreateTeamRowSchema: z.ZodObject<{
4
+ team_id: z.ZodOptional<z.ZodString>;
5
+ name: z.ZodOptional<z.ZodString>;
6
+ }, z.core.$loose>;
7
+ declare const TeamBalanceRowSchema: z.ZodObject<{
8
+ team_id: z.ZodOptional<z.ZodString>;
9
+ name: z.ZodOptional<z.ZodString>;
10
+ balance: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
11
+ member_count: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
12
+ error: z.ZodOptional<z.ZodNullable<z.ZodString>>;
13
+ }, z.core.$loose>;
14
+ declare const AddTeamMemberRowSchema: z.ZodObject<{
15
+ team_id: z.ZodOptional<z.ZodString>;
16
+ user_id: z.ZodOptional<z.ZodString>;
17
+ role: z.ZodOptional<z.ZodString>;
18
+ }, z.core.$loose>;
19
+ declare const TeamMemberRowSchema: z.ZodObject<{
20
+ user_id: z.ZodOptional<z.ZodString>;
21
+ role: z.ZodOptional<z.ZodString>;
22
+ spend_cap: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
23
+ total_spent: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
24
+ }, z.core.$loose>;
25
+ declare const TeamDeductionRowSchema: z.ZodObject<{
26
+ entry_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
27
+ team_id: z.ZodOptional<z.ZodString>;
28
+ user_id: z.ZodOptional<z.ZodString>;
29
+ amount: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
30
+ team_balance_after: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
31
+ error: z.ZodOptional<z.ZodNullable<z.ZodString>>;
32
+ error_code: z.ZodOptional<z.ZodNullable<z.ZodString>>;
33
+ replayed: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodString, z.ZodNumber]>>;
34
+ }, z.core.$loose>;
35
+ export type CreateTeamRow = z.infer<typeof CreateTeamRowSchema>;
36
+ export type TeamBalanceRow = z.infer<typeof TeamBalanceRowSchema>;
37
+ export type AddTeamMemberRow = z.infer<typeof AddTeamMemberRowSchema>;
38
+ export type TeamMemberRow = z.infer<typeof TeamMemberRowSchema>;
39
+ export type TeamDeductionRow = z.infer<typeof TeamDeductionRowSchema>;
40
+ /** Repository for team credit operations.
41
+ *
42
+ * All methods call Postgres RPCs via the callproc function.
43
+ * Returns None when the RPC returns no rows.
44
+ * Returns typed models for successful results.
45
+ */
46
+ export declare class TeamRepository {
47
+ private callproc;
48
+ constructor(callproc: CallProc);
49
+ /** Create a new team with an initial balance. */
50
+ createTeam(ownerSubjectId: string, name: string, initialBalance: string): Promise<CreateTeamRow>;
51
+ /** Fetch a team's balance and member count. Returns null if the team does not exist. */
52
+ getTeamBalance(teamId: string): Promise<TeamBalanceRow | null>;
53
+ /** Add a member to a team with an optional spend cap. */
54
+ addTeamMember(teamId: string, userId: string, role: string, spendCap: string | null): Promise<AddTeamMemberRow>;
55
+ /** List all members of a team with their spend caps and totals. */
56
+ getTeamMembers(teamId: string): Promise<TeamMemberRow[]>;
57
+ /** Remove a team member, returning false when absent or the final owner. */
58
+ removeTeamMember(teamId: string, userId: string): Promise<boolean>;
59
+ /** Deduct credits from a team's balance on behalf of a user. */
60
+ deductTeam(teamId: string, userId: string, amount: string, idempotencyKey: string, operation: string, metadata: string): Promise<TeamDeductionRow>;
61
+ }
62
+ export {};
63
+ //# sourceMappingURL=team.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"team.d.ts","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/team.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAGlE,QAAA,MAAM,mBAAmB;;;iBAKT,CAAC;AAEjB,QAAA,MAAM,oBAAoB;;;;;;iBAWV,CAAC;AAEjB,QAAA,MAAM,sBAAsB;;;;iBAMZ,CAAC;AAEjB,QAAA,MAAM,mBAAmB;;;;;iBAaT,CAAC;AAEjB,QAAA,MAAM,sBAAsB;;;;;;;;;iBAiBZ,CAAC;AAEjB,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAChE,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAClE,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AACtE,MAAM,MAAM,aAAa,G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