@zonastery/bursar 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -344
- package/dist/billing/auto-recharge-port.d.ts +27 -0
- package/dist/billing/auto-recharge-port.d.ts.map +1 -0
- package/dist/billing/auto-recharge-port.js +2 -0
- package/dist/billing/auto-recharge-port.js.map +1 -0
- package/dist/billing/auto-recharge-service.d.ts +43 -0
- package/dist/billing/auto-recharge-service.d.ts.map +1 -0
- package/dist/billing/auto-recharge-service.js +218 -0
- package/dist/billing/auto-recharge-service.js.map +1 -0
- package/dist/billing/billing-service.d.ts +71 -0
- package/dist/billing/billing-service.d.ts.map +1 -0
- package/dist/billing/billing-service.js +172 -0
- package/dist/billing/billing-service.js.map +1 -0
- package/dist/billing/billing-store.d.ts +59 -0
- package/dist/billing/billing-store.d.ts.map +1 -0
- package/dist/billing/billing-store.js +9 -0
- package/dist/billing/billing-store.js.map +1 -0
- package/dist/billing/contracts.d.ts +149 -0
- package/dist/billing/contracts.d.ts.map +1 -0
- package/dist/billing/contracts.js +2 -0
- package/dist/billing/contracts.js.map +1 -0
- package/dist/billing/event-handlers.d.ts +57 -0
- package/dist/billing/event-handlers.d.ts.map +1 -0
- package/dist/billing/event-handlers.js +667 -0
- package/dist/billing/event-handlers.js.map +1 -0
- package/dist/billing/event-processor.d.ts +19 -0
- package/dist/billing/event-processor.d.ts.map +1 -0
- package/dist/billing/event-processor.js +105 -0
- package/dist/billing/event-processor.js.map +1 -0
- package/dist/billing/financial-event-handlers.d.ts +26 -0
- package/dist/billing/financial-event-handlers.d.ts.map +1 -0
- package/dist/billing/financial-event-handlers.js +241 -0
- package/dist/billing/financial-event-handlers.js.map +1 -0
- package/dist/billing/index.d.ts +10 -0
- package/dist/billing/index.d.ts.map +1 -0
- package/dist/billing/index.js +6 -0
- package/dist/billing/index.js.map +1 -0
- package/dist/billing/management.d.ts +59 -0
- package/dist/billing/management.d.ts.map +1 -0
- package/dist/billing/management.js +113 -0
- package/dist/billing/management.js.map +1 -0
- package/dist/billing/policy-window.d.ts +24 -0
- package/dist/billing/policy-window.d.ts.map +1 -0
- package/dist/billing/policy-window.js +88 -0
- package/dist/billing/policy-window.js.map +1 -0
- package/dist/billing/postgres/repositories/auto-recharge.d.ts +32 -0
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/auto-recharge.js +178 -0
- package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -0
- package/dist/billing/postgres/repositories/customer.d.ts +12 -0
- package/dist/billing/postgres/repositories/customer.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/customer.js +33 -0
- package/dist/billing/postgres/repositories/customer.js.map +1 -0
- package/dist/billing/postgres/repositories/dispute.d.ts +7 -0
- package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/dispute.js +18 -0
- package/dist/billing/postgres/repositories/dispute.js.map +1 -0
- package/dist/billing/postgres/repositories/event.d.ts +21 -0
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/event.js +47 -0
- package/dist/billing/postgres/repositories/event.js.map +1 -0
- package/dist/billing/postgres/repositories/invoice.d.ts +9 -0
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/invoice.js +36 -0
- package/dist/billing/postgres/repositories/invoice.js.map +1 -0
- package/dist/billing/postgres/repositories/offer.d.ts +26 -0
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/offer.js +73 -0
- package/dist/billing/postgres/repositories/offer.js.map +1 -0
- package/dist/billing/postgres/repositories/payment.d.ts +32 -0
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/payment.js +78 -0
- package/dist/billing/postgres/repositories/payment.js.map +1 -0
- package/dist/billing/postgres/repositories/preferences.d.ts +15 -0
- package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/preferences.js +23 -0
- package/dist/billing/postgres/repositories/preferences.js.map +1 -0
- package/dist/billing/postgres/repositories/refund.d.ts +7 -0
- package/dist/billing/postgres/repositories/refund.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/refund.js +32 -0
- package/dist/billing/postgres/repositories/refund.js.map +1 -0
- package/dist/billing/postgres/repositories/subscription.d.ts +46 -0
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/subscription.js +187 -0
- package/dist/billing/postgres/repositories/subscription.js.map +1 -0
- package/dist/billing/postgres/repositories/topup.d.ts +27 -0
- package/dist/billing/postgres/repositories/topup.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/topup.js +51 -0
- package/dist/billing/postgres/repositories/topup.js.map +1 -0
- package/dist/billing/postgres/store.d.ts +87 -0
- package/dist/billing/postgres/store.d.ts.map +1 -0
- package/dist/billing/postgres/store.js +527 -0
- package/dist/billing/postgres/store.js.map +1 -0
- package/dist/billing/service-types.d.ts +37 -0
- package/dist/billing/service-types.d.ts.map +1 -0
- package/dist/billing/service-types.js +2 -0
- package/dist/billing/service-types.js.map +1 -0
- package/dist/billing/types/auto-recharge.d.ts +56 -0
- package/dist/billing/types/auto-recharge.d.ts.map +1 -0
- package/dist/billing/types/auto-recharge.js +2 -0
- package/dist/billing/types/auto-recharge.js.map +1 -0
- package/dist/billing/types/catalog.d.ts +47 -0
- package/dist/billing/types/catalog.d.ts.map +1 -0
- package/dist/billing/types/catalog.js +2 -0
- package/dist/billing/types/catalog.js.map +1 -0
- package/dist/billing/types/common.d.ts +16 -0
- package/dist/billing/types/common.d.ts.map +1 -0
- package/dist/billing/types/common.js +2 -0
- package/dist/billing/types/common.js.map +1 -0
- package/dist/billing/types/documents.d.ts +39 -0
- package/dist/billing/types/documents.d.ts.map +1 -0
- package/dist/billing/types/documents.js +2 -0
- package/dist/billing/types/documents.js.map +1 -0
- package/dist/billing/types/events.d.ts +75 -0
- package/dist/billing/types/events.d.ts.map +1 -0
- package/dist/billing/types/events.js +38 -0
- package/dist/billing/types/events.js.map +1 -0
- package/dist/billing/types/index.d.ts +8 -0
- package/dist/billing/types/index.d.ts.map +1 -0
- package/dist/billing/types/index.js +8 -0
- package/dist/billing/types/index.js.map +1 -0
- package/dist/billing/types/preferences.d.ts +13 -0
- package/dist/billing/types/preferences.d.ts.map +1 -0
- package/dist/billing/types/preferences.js +2 -0
- package/dist/billing/types/preferences.js.map +1 -0
- package/dist/billing/types/subscriptions.d.ts +97 -0
- package/dist/billing/types/subscriptions.d.ts.map +1 -0
- package/dist/billing/types/subscriptions.js +12 -0
- package/dist/billing/types/subscriptions.js.map +1 -0
- package/dist/breakdown.d.ts +3 -1
- package/dist/breakdown.d.ts.map +1 -1
- package/dist/breakdown.js +4 -1
- package/dist/breakdown.js.map +1 -1
- package/dist/browser.d.ts +8 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +7 -0
- package/dist/browser.js.map +1 -0
- package/dist/bursar.d.ts +80 -0
- package/dist/bursar.d.ts.map +1 -0
- package/dist/bursar.js +129 -0
- package/dist/bursar.js.map +1 -0
- package/dist/catalog.d.ts +61 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +95 -0
- package/dist/catalog.js.map +1 -0
- package/dist/commerce/errors.d.ts +69 -0
- package/dist/commerce/errors.d.ts.map +1 -0
- package/dist/commerce/errors.js +107 -0
- package/dist/commerce/errors.js.map +1 -0
- package/dist/commerce/index.d.ts +6 -0
- package/dist/commerce/index.d.ts.map +1 -0
- package/dist/commerce/index.js +6 -0
- package/dist/commerce/index.js.map +1 -0
- package/dist/commerce/plan-change.d.ts +10 -0
- package/dist/commerce/plan-change.d.ts.map +1 -0
- package/dist/commerce/plan-change.js +42 -0
- package/dist/commerce/plan-change.js.map +1 -0
- package/dist/commerce/provider-registry.d.ts +18 -0
- package/dist/commerce/provider-registry.d.ts.map +1 -0
- package/dist/commerce/provider-registry.js +70 -0
- package/dist/commerce/provider-registry.js.map +1 -0
- package/dist/commerce/service.d.ts +73 -0
- package/dist/commerce/service.d.ts.map +1 -0
- package/dist/commerce/service.js +981 -0
- package/dist/commerce/service.js.map +1 -0
- package/dist/commerce/types.d.ts +217 -0
- package/dist/commerce/types.d.ts.map +1 -0
- package/dist/commerce/types.js.map +1 -0
- package/dist/config/parse-access.d.ts +6 -0
- package/dist/config/parse-access.d.ts.map +1 -0
- package/dist/config/parse-access.js +72 -0
- package/dist/config/parse-access.js.map +1 -0
- package/dist/config/parse-commerce.d.ts +3 -0
- package/dist/config/parse-commerce.d.ts.map +1 -0
- package/dist/config/parse-commerce.js +236 -0
- package/dist/config/parse-commerce.js.map +1 -0
- package/dist/config/parse-credits.d.ts +3 -0
- package/dist/config/parse-credits.d.ts.map +1 -0
- package/dist/config/parse-credits.js +99 -0
- package/dist/config/parse-credits.js.map +1 -0
- package/dist/config/parse-plans.d.ts +5 -0
- package/dist/config/parse-plans.d.ts.map +1 -0
- package/dist/config/parse-plans.js +126 -0
- package/dist/config/parse-plans.js.map +1 -0
- package/dist/config/parse-pricing.d.ts +4 -0
- package/dist/config/parse-pricing.d.ts.map +1 -0
- package/dist/config/parse-pricing.js +252 -0
- package/dist/config/parse-pricing.js.map +1 -0
- package/dist/config/parse-utils.d.ts +18 -0
- package/dist/config/parse-utils.d.ts.map +1 -0
- package/dist/config/parse-utils.js +118 -0
- package/dist/config/parse-utils.js.map +1 -0
- package/dist/config/types.d.ts +327 -0
- package/dist/config/types.d.ts.map +1 -0
- package/dist/config/types.js +2 -0
- package/dist/config/types.js.map +1 -0
- package/dist/config.d.ts +6 -22
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +82 -263
- package/dist/config.js.map +1 -1
- package/dist/{stores → credits}/events.d.ts +6 -6
- package/dist/credits/events.d.ts.map +1 -0
- package/dist/{stores → credits}/events.js +5 -5
- package/dist/credits/events.js.map +1 -0
- package/dist/credits/lease-workflow.d.ts +90 -0
- package/dist/credits/lease-workflow.d.ts.map +1 -0
- package/dist/credits/lease-workflow.js +378 -0
- package/dist/credits/lease-workflow.js.map +1 -0
- package/dist/credits/low-balance-monitor.d.ts +26 -0
- package/dist/credits/low-balance-monitor.d.ts.map +1 -0
- package/dist/credits/low-balance-monitor.js +110 -0
- package/dist/credits/low-balance-monitor.js.map +1 -0
- package/dist/credits/postgres/repositories/analytics.d.ts +77 -0
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/analytics.js +151 -0
- package/dist/credits/postgres/repositories/analytics.js.map +1 -0
- package/dist/credits/postgres/repositories/balance.d.ts +66 -0
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/balance.js +139 -0
- package/dist/credits/postgres/repositories/balance.js.map +1 -0
- package/dist/credits/postgres/repositories/bucket.d.ts +25 -0
- package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/bucket.js +52 -0
- package/dist/credits/postgres/repositories/bucket.js.map +1 -0
- package/dist/credits/postgres/repositories/deduction.d.ts +55 -0
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/deduction.js +130 -0
- package/dist/credits/postgres/repositories/deduction.js.map +1 -0
- package/dist/credits/postgres/repositories/lease.d.ts +69 -0
- package/dist/credits/postgres/repositories/lease.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/lease.js +148 -0
- package/dist/credits/postgres/repositories/lease.js.map +1 -0
- package/dist/credits/postgres/repositories/plan.d.ts +105 -0
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/plan.js +182 -0
- package/dist/credits/postgres/repositories/plan.js.map +1 -0
- package/dist/credits/postgres/repositories/pricing.d.ts +28 -0
- package/dist/credits/postgres/repositories/pricing.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/pricing.js +70 -0
- package/dist/credits/postgres/repositories/pricing.js.map +1 -0
- package/dist/credits/postgres/repositories/team.d.ts +63 -0
- package/dist/credits/postgres/repositories/team.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/team.js +120 -0
- package/dist/credits/postgres/repositories/team.js.map +1 -0
- package/dist/credits/postgres/store.d.ts +84 -0
- package/dist/credits/postgres/store.d.ts.map +1 -0
- package/dist/credits/postgres/store.js +765 -0
- package/dist/credits/postgres/store.js.map +1 -0
- package/dist/credits/postgres/value-mappers.d.ts +17 -0
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -0
- package/dist/credits/postgres/value-mappers.js +98 -0
- package/dist/credits/postgres/value-mappers.js.map +1 -0
- package/dist/credits/pricing-runtime.d.ts +34 -0
- package/dist/credits/pricing-runtime.d.ts.map +1 -0
- package/dist/credits/pricing-runtime.js +141 -0
- package/dist/credits/pricing-runtime.js.map +1 -0
- package/dist/credits/queries.d.ts +33 -0
- package/dist/credits/queries.d.ts.map +1 -0
- package/dist/credits/queries.js +71 -0
- package/dist/credits/queries.js.map +1 -0
- package/dist/credits/service-errors.d.ts +4 -0
- package/dist/credits/service-errors.d.ts.map +1 -0
- package/dist/credits/service-errors.js +57 -0
- package/dist/credits/service-errors.js.map +1 -0
- package/dist/credits/service-types.d.ts +122 -0
- package/dist/credits/service-types.d.ts.map +1 -0
- package/dist/credits/service-types.js +2 -0
- package/dist/credits/service-types.js.map +1 -0
- package/dist/credits/service.d.ts +207 -0
- package/dist/credits/service.d.ts.map +1 -0
- package/dist/credits/service.js +594 -0
- package/dist/credits/service.js.map +1 -0
- package/dist/{stores/credit-store.d.ts → credits/store.d.ts} +67 -71
- package/dist/credits/store.d.ts.map +1 -0
- package/dist/{stores/credit-store.js → credits/store.js} +30 -6
- package/dist/credits/store.js.map +1 -0
- package/dist/credits/types/account.d.ts +82 -0
- package/dist/credits/types/account.d.ts.map +1 -0
- package/dist/credits/types/account.js +2 -0
- package/dist/credits/types/account.js.map +1 -0
- package/dist/credits/types/admission.d.ts +39 -0
- package/dist/credits/types/admission.d.ts.map +1 -0
- package/dist/credits/types/admission.js +2 -0
- package/dist/credits/types/admission.js.map +1 -0
- package/dist/credits/types/analytics.d.ts +52 -0
- package/dist/credits/types/analytics.d.ts.map +1 -0
- package/dist/credits/types/analytics.js +2 -0
- package/dist/credits/types/analytics.js.map +1 -0
- package/dist/credits/types/catalog.d.ts +69 -0
- package/dist/credits/types/catalog.d.ts.map +1 -0
- package/dist/credits/types/catalog.js +2 -0
- package/dist/credits/types/catalog.js.map +1 -0
- package/dist/credits/types/grants.d.ts +24 -0
- package/dist/credits/types/grants.d.ts.map +1 -0
- package/dist/credits/types/grants.js +2 -0
- package/dist/credits/types/grants.js.map +1 -0
- package/dist/credits/types/index.d.ts +9 -0
- package/dist/credits/types/index.d.ts.map +1 -0
- package/dist/credits/types/index.js +2 -0
- package/dist/credits/types/index.js.map +1 -0
- package/dist/credits/types/ledger.d.ts +77 -0
- package/dist/credits/types/ledger.d.ts.map +1 -0
- package/dist/credits/types/ledger.js +2 -0
- package/dist/credits/types/ledger.js.map +1 -0
- package/dist/credits/types/quotas.d.ts +40 -0
- package/dist/credits/types/quotas.d.ts.map +1 -0
- package/dist/credits/types/quotas.js +2 -0
- package/dist/credits/types/quotas.js.map +1 -0
- package/dist/credits/types/teams.d.ts +44 -0
- package/dist/credits/types/teams.d.ts.map +1 -0
- package/dist/credits/types/teams.js +2 -0
- package/dist/credits/types/teams.js.map +1 -0
- package/dist/engine.d.ts +18 -39
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +169 -145
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +84 -25
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +117 -24
- package/dist/errors.js.map +1 -1
- package/dist/expr/ast.d.ts +46 -0
- package/dist/expr/ast.d.ts.map +1 -0
- package/dist/expr/ast.js +18 -0
- package/dist/expr/ast.js.map +1 -0
- package/dist/expr/evaluator.d.ts +4 -0
- package/dist/expr/evaluator.d.ts.map +1 -0
- package/dist/expr/evaluator.js +144 -0
- package/dist/expr/evaluator.js.map +1 -0
- package/dist/expr/language.d.ts +5 -0
- package/dist/expr/language.d.ts.map +1 -0
- package/dist/expr/language.js +73 -0
- package/dist/expr/language.js.map +1 -0
- package/dist/expr/parser.d.ts +24 -0
- package/dist/expr/parser.d.ts.map +1 -0
- package/dist/expr/parser.js +175 -0
- package/dist/expr/parser.js.map +1 -0
- package/dist/expr/tokenizer.d.ts +7 -0
- package/dist/expr/tokenizer.d.ts.map +1 -0
- package/dist/expr/tokenizer.js +85 -0
- package/dist/expr/tokenizer.js.map +1 -0
- package/dist/expr.d.ts +7 -58
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +41 -640
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +2764 -0
- package/dist/generated/pricing-config.types.d.ts +674 -0
- package/dist/generated/pricing-config.types.d.ts.map +1 -0
- package/dist/generated/pricing-config.types.js +8 -0
- package/dist/generated/pricing-config.types.js.map +1 -0
- package/dist/index.d.ts +21 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -9
- package/dist/index.js.map +1 -1
- package/dist/load-pricing-file.d.ts.map +1 -1
- package/dist/load-pricing-file.js +61 -6
- package/dist/load-pricing-file.js.map +1 -1
- package/dist/metrics.d.ts +6 -16
- package/dist/metrics.d.ts.map +1 -1
- package/dist/node.d.ts +9 -2
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +6 -2
- package/dist/node.js.map +1 -1
- package/dist/providers/_shared.d.ts +9 -0
- package/dist/providers/_shared.d.ts.map +1 -0
- package/dist/providers/_shared.js +25 -0
- package/dist/providers/_shared.js.map +1 -0
- package/dist/providers/dodo/client-contract.d.ts +106 -0
- package/dist/providers/dodo/client-contract.d.ts.map +1 -0
- package/dist/providers/dodo/client-contract.js +2 -0
- package/dist/providers/dodo/client-contract.js.map +1 -0
- package/dist/providers/dodo/event-mapper.d.ts +6 -0
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -0
- package/dist/providers/dodo/event-mapper.js +344 -0
- package/dist/providers/dodo/event-mapper.js.map +1 -0
- package/dist/providers/dodo/index.d.ts +4 -0
- package/dist/providers/dodo/index.d.ts.map +1 -0
- package/dist/providers/dodo/index.js +3 -0
- package/dist/providers/dodo/index.js.map +1 -0
- package/dist/providers/dodo/provider.d.ts +53 -0
- package/dist/providers/dodo/provider.d.ts.map +1 -0
- package/dist/providers/dodo/provider.js +353 -0
- package/dist/providers/dodo/provider.js.map +1 -0
- package/dist/providers/index.d.ts +9 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +7 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/mock/index.d.ts +2 -0
- package/dist/providers/mock/index.d.ts.map +1 -0
- package/dist/providers/mock/index.js +2 -0
- package/dist/providers/mock/index.js.map +1 -0
- package/dist/providers/mock/provider.d.ts +41 -0
- package/dist/providers/mock/provider.d.ts.map +1 -0
- package/dist/providers/mock/provider.js +124 -0
- package/dist/providers/mock/provider.js.map +1 -0
- package/dist/providers/stripe/event-mapper.d.ts +7 -0
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -0
- package/dist/providers/stripe/event-mapper.js +342 -0
- package/dist/providers/stripe/event-mapper.js.map +1 -0
- package/dist/providers/stripe/index.d.ts +3 -0
- package/dist/providers/stripe/index.d.ts.map +1 -0
- package/dist/providers/stripe/index.js +3 -0
- package/dist/providers/stripe/index.js.map +1 -0
- package/dist/providers/stripe/provider.d.ts +48 -0
- package/dist/providers/stripe/provider.d.ts.map +1 -0
- package/dist/providers/stripe/provider.js +289 -0
- package/dist/providers/stripe/provider.js.map +1 -0
- package/dist/providers/types.d.ts +175 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +18 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/retry.d.ts +13 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +28 -0
- package/dist/retry.js.map +1 -0
- package/dist/shared/logger.d.ts +10 -0
- package/dist/shared/logger.d.ts.map +1 -0
- package/dist/shared/logger.js +15 -0
- package/dist/shared/logger.js.map +1 -0
- package/dist/shared/postgres-client.d.ts +51 -0
- package/dist/shared/postgres-client.d.ts.map +1 -0
- package/dist/shared/postgres-client.js +101 -0
- package/dist/shared/postgres-client.js.map +1 -0
- package/dist/shared/postgres-types.d.ts +7 -0
- package/dist/shared/postgres-types.d.ts.map +1 -0
- package/dist/shared/postgres-types.js +2 -0
- package/dist/shared/postgres-types.js.map +1 -0
- package/dist/shared/postgres-validation.d.ts +8 -0
- package/dist/shared/postgres-validation.d.ts.map +1 -0
- package/dist/shared/postgres-validation.js +40 -0
- package/dist/shared/postgres-validation.js.map +1 -0
- package/dist/storage/adapters/clickhouse.d.ts +61 -0
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -0
- package/dist/storage/adapters/clickhouse.js +329 -0
- package/dist/storage/adapters/clickhouse.js.map +1 -0
- package/dist/storage/adapters/s3.d.ts +35 -0
- package/dist/storage/adapters/s3.d.ts.map +1 -0
- package/dist/storage/adapters/s3.js +100 -0
- package/dist/storage/adapters/s3.js.map +1 -0
- package/dist/storage/outbox-worker.d.ts +40 -0
- package/dist/storage/outbox-worker.d.ts.map +1 -0
- package/dist/storage/outbox-worker.js +132 -0
- package/dist/storage/outbox-worker.js.map +1 -0
- package/dist/storage/ports.d.ts +77 -0
- package/dist/storage/ports.d.ts.map +1 -0
- package/dist/storage/ports.js +2 -0
- package/dist/storage/ports.js.map +1 -0
- package/dist/storage/postgres-repository.d.ts +14 -0
- package/dist/storage/postgres-repository.d.ts.map +1 -0
- package/dist/storage/postgres-repository.js +130 -0
- package/dist/storage/postgres-repository.js.map +1 -0
- package/dist/storage/runtime.d.ts +67 -0
- package/dist/storage/runtime.d.ts.map +1 -0
- package/dist/storage/runtime.js +313 -0
- package/dist/storage/runtime.js.map +1 -0
- package/package.json +70 -12
- package/dist/allowance.d.ts +0 -48
- package/dist/allowance.d.ts.map +0 -1
- package/dist/allowance.js +0 -140
- package/dist/allowance.js.map +0 -1
- package/dist/manager.d.ts +0 -421
- package/dist/manager.d.ts.map +0 -1
- package/dist/manager.js +0 -1112
- package/dist/manager.js.map +0 -1
- package/dist/stores/credit-store.d.ts.map +0 -1
- package/dist/stores/credit-store.js.map +0 -1
- package/dist/stores/events.d.ts.map +0 -1
- package/dist/stores/events.js.map +0 -1
- package/dist/stores/memory-store.d.ts +0 -178
- package/dist/stores/memory-store.d.ts.map +0 -1
- package/dist/stores/memory-store.js +0 -1833
- package/dist/stores/memory-store.js.map +0 -1
- package/dist/stores/postgres-store.d.ts +0 -95
- package/dist/stores/postgres-store.d.ts.map +0 -1
- package/dist/stores/postgres-store.js +0 -872
- package/dist/stores/postgres-store.js.map +0 -1
- package/dist/stores/supabase-store.d.ts +0 -76
- package/dist/stores/supabase-store.d.ts.map +0 -1
- package/dist/stores/supabase-store.js +0 -865
- package/dist/stores/supabase-store.js.map +0 -1
- package/dist/types.d.ts +0 -462
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- /package/dist/{types.js → commerce/types.js} +0 -0
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { StoreError } from "../../errors.js";
|
|
4
|
+
import { canonicalBursarConfigDict } from "../../config.js";
|
|
5
|
+
import { PostgresClient, } from "../../shared/postgres-client.js";
|
|
6
|
+
import { CreditStore } from "../store.js";
|
|
7
|
+
import { BalanceRepository } from "./repositories/balance.js";
|
|
8
|
+
import { DeductionRepository } from "./repositories/deduction.js";
|
|
9
|
+
import { LeaseRepository } from "./repositories/lease.js";
|
|
10
|
+
import { PricingRepository } from "./repositories/pricing.js";
|
|
11
|
+
import { PlanRepository } from "./repositories/plan.js";
|
|
12
|
+
import { AnalyticsRepository } from "./repositories/analytics.js";
|
|
13
|
+
import { TeamRepository } from "./repositories/team.js";
|
|
14
|
+
import { BucketRepository } from "./repositories/bucket.js";
|
|
15
|
+
import { ZERO, decimalParameter as decParam, decimalRecord as decRecord, decimalValue as dec, mapLedgerEntry, mapUsageCharge, normalizeBursarConfig, parseAdmissionOperations, parseEntitlements, } from "./value-mappers.js";
|
|
16
|
+
const DEFAULT_LEASE_TTL_SECONDS = 600;
|
|
17
|
+
const DEFAULT_PAGE_SIZE = 50;
|
|
18
|
+
const MAX_PAGE_SIZE = 200;
|
|
19
|
+
export class PostgresStore extends CreditStore {
|
|
20
|
+
postgres;
|
|
21
|
+
_balanceRepo = null;
|
|
22
|
+
_deductionRepo = null;
|
|
23
|
+
_leaseRepo = null;
|
|
24
|
+
_pricingRepo = null;
|
|
25
|
+
_planRepo = null;
|
|
26
|
+
_analyticsRepo = null;
|
|
27
|
+
_teamRepo = null;
|
|
28
|
+
_bucketRepo = null;
|
|
29
|
+
get balanceRepo() {
|
|
30
|
+
if (!this._balanceRepo) {
|
|
31
|
+
this._balanceRepo = new BalanceRepository(this.callproc.bind(this));
|
|
32
|
+
}
|
|
33
|
+
return this._balanceRepo;
|
|
34
|
+
}
|
|
35
|
+
get deductionRepo() {
|
|
36
|
+
if (!this._deductionRepo) {
|
|
37
|
+
this._deductionRepo = new DeductionRepository(this.callproc.bind(this));
|
|
38
|
+
}
|
|
39
|
+
return this._deductionRepo;
|
|
40
|
+
}
|
|
41
|
+
get leaseRepo() {
|
|
42
|
+
if (!this._leaseRepo) {
|
|
43
|
+
this._leaseRepo = new LeaseRepository(this.callproc.bind(this));
|
|
44
|
+
}
|
|
45
|
+
return this._leaseRepo;
|
|
46
|
+
}
|
|
47
|
+
get pricingRepo() {
|
|
48
|
+
if (!this._pricingRepo) {
|
|
49
|
+
this._pricingRepo = new PricingRepository(this.callproc.bind(this));
|
|
50
|
+
}
|
|
51
|
+
return this._pricingRepo;
|
|
52
|
+
}
|
|
53
|
+
get planRepo() {
|
|
54
|
+
if (!this._planRepo) {
|
|
55
|
+
this._planRepo = new PlanRepository(this.callproc.bind(this));
|
|
56
|
+
}
|
|
57
|
+
return this._planRepo;
|
|
58
|
+
}
|
|
59
|
+
get analyticsRepo() {
|
|
60
|
+
if (!this._analyticsRepo) {
|
|
61
|
+
this._analyticsRepo = new AnalyticsRepository(this.callproc.bind(this));
|
|
62
|
+
}
|
|
63
|
+
return this._analyticsRepo;
|
|
64
|
+
}
|
|
65
|
+
get teamRepo() {
|
|
66
|
+
if (!this._teamRepo) {
|
|
67
|
+
this._teamRepo = new TeamRepository(this.callproc.bind(this));
|
|
68
|
+
}
|
|
69
|
+
return this._teamRepo;
|
|
70
|
+
}
|
|
71
|
+
get bucketRepo() {
|
|
72
|
+
if (!this._bucketRepo) {
|
|
73
|
+
this._bucketRepo = new BucketRepository(this.callproc.bind(this));
|
|
74
|
+
}
|
|
75
|
+
return this._bucketRepo;
|
|
76
|
+
}
|
|
77
|
+
constructor(databaseUrl, tenantId, poolOrCtor, storageOptions) {
|
|
78
|
+
super();
|
|
79
|
+
if (poolOrCtor && typeof poolOrCtor.query === "function") {
|
|
80
|
+
this.postgres = new PostgresClient(poolOrCtor, {
|
|
81
|
+
tenantId,
|
|
82
|
+
usageBackend: storageOptions?.usageBackend,
|
|
83
|
+
closedError: () => new StoreError("Store has been closed"),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
this.postgres = new PostgresClient(databaseUrl, {
|
|
88
|
+
tenantId,
|
|
89
|
+
usageBackend: storageOptions?.usageBackend,
|
|
90
|
+
poolConstructor: poolOrCtor,
|
|
91
|
+
closedError: () => new StoreError("Store has been closed"),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async query(text, params) {
|
|
96
|
+
return this.postgres.query(text, params);
|
|
97
|
+
}
|
|
98
|
+
async close() {
|
|
99
|
+
await this.postgres.close();
|
|
100
|
+
}
|
|
101
|
+
static RPC_NAME_RE = /^[a-z_][a-z0-9_]*$/;
|
|
102
|
+
static SCALAR_RPC_NAMES = new Set([
|
|
103
|
+
"assign_plan",
|
|
104
|
+
"release_lease",
|
|
105
|
+
"remove_team_member",
|
|
106
|
+
"set_team_member",
|
|
107
|
+
"start_plan_migration",
|
|
108
|
+
"unassign_plan",
|
|
109
|
+
]);
|
|
110
|
+
async callproc(name, params) {
|
|
111
|
+
if (!PostgresStore.RPC_NAME_RE.test(name)) {
|
|
112
|
+
throw new StoreError(`Invalid RPC name: ${name}`);
|
|
113
|
+
}
|
|
114
|
+
const placeholders = params.map((_, i) => `$${i + 1}`).join(", ");
|
|
115
|
+
const rows = await this.query(`SELECT * FROM bursar.${name}(${placeholders})`, params);
|
|
116
|
+
if (PostgresStore.SCALAR_RPC_NAMES.has(name) && rows.length === 1) {
|
|
117
|
+
const row = rows[0];
|
|
118
|
+
const keys = Object.keys(row);
|
|
119
|
+
if (keys.length === 1) {
|
|
120
|
+
return [row[keys[0]]];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return rows;
|
|
124
|
+
}
|
|
125
|
+
async getBalance(userId) {
|
|
126
|
+
const row = await this.balanceRepo.getBalance(userId);
|
|
127
|
+
if (!row) {
|
|
128
|
+
return { userId, balance: ZERO, lifetimePurchased: ZERO };
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
userId: String(row.user_id ?? userId),
|
|
132
|
+
balance: dec(row.balance),
|
|
133
|
+
lifetimePurchased: dec(row.lifetime_purchased),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
async addCredits(userId, amount, type = "adjustment", metadata, expiresAt, bucket, idempotencyKey) {
|
|
137
|
+
const meta = { ...(metadata ?? {}) };
|
|
138
|
+
if (expiresAt) {
|
|
139
|
+
meta.expires_at = expiresAt instanceof Date ? expiresAt.toISOString() : String(expiresAt);
|
|
140
|
+
}
|
|
141
|
+
const row = await this.balanceRepo.addCredits(userId, decParam(amount), type, JSON.stringify(meta), expiresAt ? expiresAt.toISOString() : null, bucket ?? null, idempotencyKey ?? `credit:${randomUUID()}`);
|
|
142
|
+
if ("error" in row && row.error) {
|
|
143
|
+
throw new StoreError(`post_credit: ${String(row.error)}`);
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
entryId: String(row.entry_id ?? ""),
|
|
147
|
+
userId: String(row.user_id ?? userId),
|
|
148
|
+
amount: dec(row.amount, amount),
|
|
149
|
+
newBalance: dec(row.new_balance),
|
|
150
|
+
lifetimePurchased: dec(row.lifetime_purchased),
|
|
151
|
+
bucket: String(row.bucket ?? "default"),
|
|
152
|
+
idempotent: Boolean(row.idempotent),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async deductWithAllowance(userId, amount, options) {
|
|
156
|
+
const idempotencyKey = options?.idempotencyKey ?? `usage:${randomUUID()}`;
|
|
157
|
+
const operation = options?.operation ??
|
|
158
|
+
(typeof options?.metadata?.operation === "string" ? options.metadata.operation : "usage");
|
|
159
|
+
const model = options?.model ?? null;
|
|
160
|
+
const region = options?.region ?? null;
|
|
161
|
+
const metadata = options?.metadata ?? {};
|
|
162
|
+
const feature = options?.feature ?? null;
|
|
163
|
+
const measures = options?.measures ?? {};
|
|
164
|
+
const dimensions = options?.dimensions ?? {};
|
|
165
|
+
const row = await this.deductionRepo.deductWithAllowance({
|
|
166
|
+
userId,
|
|
167
|
+
operation,
|
|
168
|
+
amount: decParam(amount),
|
|
169
|
+
idempotencyKey,
|
|
170
|
+
feature,
|
|
171
|
+
model,
|
|
172
|
+
region,
|
|
173
|
+
measures: JSON.stringify(measures),
|
|
174
|
+
dimensions: JSON.stringify(dimensions),
|
|
175
|
+
metadata: JSON.stringify(metadata ?? {}),
|
|
176
|
+
});
|
|
177
|
+
if ("error" in row && row.error) {
|
|
178
|
+
return {
|
|
179
|
+
entryId: "",
|
|
180
|
+
userId,
|
|
181
|
+
amount: ZERO,
|
|
182
|
+
allowanceConsumed: ZERO,
|
|
183
|
+
balanceAfter: dec(row.balance_after),
|
|
184
|
+
idempotent: false,
|
|
185
|
+
error: String(row.error),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
entryId: String(row.entry_id ?? ""),
|
|
190
|
+
userId,
|
|
191
|
+
amount: dec(row.amount),
|
|
192
|
+
allowanceConsumed: dec(row.allowance_consumed),
|
|
193
|
+
balanceAfter: dec(row.balance_after),
|
|
194
|
+
idempotent: Boolean(row.idempotent),
|
|
195
|
+
bucketBreakdown: decRecord(row.bucket_breakdown),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async createLease(userId, amount, operationType, options) {
|
|
199
|
+
const row = await this.leaseRepo.createLease({
|
|
200
|
+
userId,
|
|
201
|
+
amount: decParam(amount),
|
|
202
|
+
operationType,
|
|
203
|
+
idempotencyKey: options?.idempotencyKey ?? `lease:${randomUUID()}`,
|
|
204
|
+
ttlSeconds: options?.ttlSeconds ?? DEFAULT_LEASE_TTL_SECONDS,
|
|
205
|
+
metadata: JSON.stringify(options?.metadata ?? {}),
|
|
206
|
+
feature: options?.feature ?? null,
|
|
207
|
+
measures: JSON.stringify(options?.measures ?? {}),
|
|
208
|
+
dimensions: JSON.stringify(options?.dimensions ?? {}),
|
|
209
|
+
minimumBalance: options?.floor == null ? null : decParam(options.floor),
|
|
210
|
+
maxConcurrent: options?.maxConcurrent ?? null,
|
|
211
|
+
});
|
|
212
|
+
if (!row || Object.keys(row).length === 0) {
|
|
213
|
+
return {
|
|
214
|
+
leaseId: "",
|
|
215
|
+
userId,
|
|
216
|
+
amount: ZERO,
|
|
217
|
+
available: ZERO,
|
|
218
|
+
reservedTotal: ZERO,
|
|
219
|
+
minimumBalance: ZERO,
|
|
220
|
+
billingMode: options?.billingMode ?? "strict",
|
|
221
|
+
expiresAt: "",
|
|
222
|
+
error: "no result",
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const availability = await this.getAvailable(userId);
|
|
226
|
+
if ("error" in row && row.error) {
|
|
227
|
+
return {
|
|
228
|
+
leaseId: "",
|
|
229
|
+
userId,
|
|
230
|
+
amount: ZERO,
|
|
231
|
+
available: availability.available,
|
|
232
|
+
reservedTotal: availability.reserved,
|
|
233
|
+
minimumBalance: ZERO,
|
|
234
|
+
billingMode: options?.billingMode ?? "strict",
|
|
235
|
+
expiresAt: "",
|
|
236
|
+
error: String(row.error),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
leaseId: String(row.lease_id ?? ""),
|
|
241
|
+
userId: String(row.user_id ?? userId),
|
|
242
|
+
amount: dec(row.amount),
|
|
243
|
+
available: availability.available,
|
|
244
|
+
reservedTotal: availability.reserved,
|
|
245
|
+
minimumBalance: dec(row.minimum_balance),
|
|
246
|
+
billingMode: dec(row.minimum_balance).lt(0) ? "overdraft" : "strict",
|
|
247
|
+
expiresAt: String(row.expires_at ?? ""),
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async settleLease(userId, leaseId, amount, options) {
|
|
251
|
+
const row = await this.leaseRepo.settleLease({
|
|
252
|
+
userId,
|
|
253
|
+
leaseId,
|
|
254
|
+
amount: decParam(amount),
|
|
255
|
+
idempotencyKey: options?.idempotencyKey ?? `lease:${leaseId}:settle`,
|
|
256
|
+
feature: options?.feature ?? null,
|
|
257
|
+
model: options?.model ?? null,
|
|
258
|
+
region: options?.region ?? null,
|
|
259
|
+
measures: JSON.stringify(options?.measures ?? {}),
|
|
260
|
+
dimensions: JSON.stringify(options?.dimensions ?? {}),
|
|
261
|
+
metadata: JSON.stringify(options?.metadata ?? {}),
|
|
262
|
+
});
|
|
263
|
+
if (!row || Object.keys(row).length === 0) {
|
|
264
|
+
return {
|
|
265
|
+
entryId: "",
|
|
266
|
+
userId,
|
|
267
|
+
amount: ZERO,
|
|
268
|
+
allowanceConsumed: ZERO,
|
|
269
|
+
balanceAfter: ZERO,
|
|
270
|
+
idempotent: false,
|
|
271
|
+
error: "no result",
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
if ("error" in row && row.error) {
|
|
275
|
+
return {
|
|
276
|
+
entryId: "",
|
|
277
|
+
userId,
|
|
278
|
+
amount: ZERO,
|
|
279
|
+
allowanceConsumed: ZERO,
|
|
280
|
+
balanceAfter: dec(row.balance_after),
|
|
281
|
+
idempotent: false,
|
|
282
|
+
error: String(row.error),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
entryId: String(row.entry_id ?? ""),
|
|
287
|
+
userId,
|
|
288
|
+
amount: dec(row.amount),
|
|
289
|
+
allowanceConsumed: dec(row.allowance_consumed),
|
|
290
|
+
balanceAfter: dec(row.balance_after),
|
|
291
|
+
idempotent: Boolean(row.idempotent),
|
|
292
|
+
bucketBreakdown: decRecord(row.bucket_breakdown),
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
async getLeasePricingContext(userId, leaseId) {
|
|
296
|
+
const row = await this.leaseRepo.getPricingContext(userId, leaseId);
|
|
297
|
+
if (!row)
|
|
298
|
+
return null;
|
|
299
|
+
return {
|
|
300
|
+
catalogVersion: row.catalog_revision_no,
|
|
301
|
+
planId: row.plan_id ?? null,
|
|
302
|
+
planKey: row.plan_key ?? null,
|
|
303
|
+
rateCard: row.rate_card ?? null,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
async releaseLease(userId, leaseId) {
|
|
307
|
+
const row = await this.leaseRepo.releaseLease(userId, leaseId);
|
|
308
|
+
return {
|
|
309
|
+
leaseId,
|
|
310
|
+
userId,
|
|
311
|
+
released: Boolean(row.released),
|
|
312
|
+
reason: row.reason != null ? String(row.reason) : null,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
async renewLease(userId, leaseId, ttlSeconds) {
|
|
316
|
+
if (!Number.isInteger(ttlSeconds) || ttlSeconds < 1) {
|
|
317
|
+
throw new RangeError("ttlSeconds must be a positive integer");
|
|
318
|
+
}
|
|
319
|
+
const row = await this.leaseRepo.renewLease(userId, leaseId, ttlSeconds);
|
|
320
|
+
const availability = await this.getAvailable(userId);
|
|
321
|
+
const minimumBalance = dec(row.minimum_balance);
|
|
322
|
+
return {
|
|
323
|
+
leaseId: String(row.lease_id ?? leaseId),
|
|
324
|
+
userId,
|
|
325
|
+
amount: dec(row.amount),
|
|
326
|
+
available: availability.available,
|
|
327
|
+
reservedTotal: availability.reserved,
|
|
328
|
+
minimumBalance,
|
|
329
|
+
billingMode: minimumBalance.lt(0) ? "overdraft" : "strict",
|
|
330
|
+
expiresAt: String(row.expires_at ?? ""),
|
|
331
|
+
error: row.error != null ? String(row.error) : null,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
async expireLeases(limit = 100) {
|
|
335
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 1000) {
|
|
336
|
+
throw new RangeError("lease expiry limit must be an integer between 1 and 1000");
|
|
337
|
+
}
|
|
338
|
+
return this.leaseRepo.expireLeases(limit);
|
|
339
|
+
}
|
|
340
|
+
async getAvailable(userId) {
|
|
341
|
+
const row = await this.balanceRepo.getAvailable(userId);
|
|
342
|
+
return {
|
|
343
|
+
userId,
|
|
344
|
+
balance: dec(row.balance),
|
|
345
|
+
reserved: dec(row.reserved),
|
|
346
|
+
available: dec(row.available),
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
async getActivePricing() {
|
|
350
|
+
return this._loadActivePricing();
|
|
351
|
+
}
|
|
352
|
+
async _loadActivePricing() {
|
|
353
|
+
const row = await this.pricingRepo.getActivePricing();
|
|
354
|
+
if (!row || !row.config)
|
|
355
|
+
return null;
|
|
356
|
+
return normalizeBursarConfig(row, 0);
|
|
357
|
+
}
|
|
358
|
+
async setActivePricing(config, label) {
|
|
359
|
+
const canonical = canonicalBursarConfigDict(config);
|
|
360
|
+
const row = await this.pricingRepo.setActivePricing(JSON.stringify(canonical), label ?? null);
|
|
361
|
+
return String(row.id ?? "");
|
|
362
|
+
}
|
|
363
|
+
async publishPricing(config, label) {
|
|
364
|
+
const canonical = canonicalBursarConfigDict(config);
|
|
365
|
+
const row = await this.pricingRepo.publishPricing(JSON.stringify(canonical), label ?? null);
|
|
366
|
+
return String(row.id ?? "");
|
|
367
|
+
}
|
|
368
|
+
async getPricingHistory() {
|
|
369
|
+
const rows = await this.pricingRepo.getPricingHistory();
|
|
370
|
+
if (!rows)
|
|
371
|
+
return [];
|
|
372
|
+
return rows.map((r) => ({
|
|
373
|
+
id: String(r.id ?? ""),
|
|
374
|
+
version: Number(r.version ?? 0),
|
|
375
|
+
label: r.label ?? null,
|
|
376
|
+
active: Boolean(r.active ?? false),
|
|
377
|
+
createdAt: String(r.created_at ?? ""),
|
|
378
|
+
}));
|
|
379
|
+
}
|
|
380
|
+
async getBursarConfig(version) {
|
|
381
|
+
const row = await this.pricingRepo.getBursarConfig(version);
|
|
382
|
+
if (!row || !row.config)
|
|
383
|
+
return null;
|
|
384
|
+
return normalizeBursarConfig(row, version);
|
|
385
|
+
}
|
|
386
|
+
async activatePricing(version) {
|
|
387
|
+
const row = await this.pricingRepo.activatePricing(version);
|
|
388
|
+
return String(row.id ?? "");
|
|
389
|
+
}
|
|
390
|
+
async getUserPlan(userId) {
|
|
391
|
+
const row = await this.planRepo.getUserPlan(userId);
|
|
392
|
+
if (!row) {
|
|
393
|
+
return {
|
|
394
|
+
userId,
|
|
395
|
+
planId: null,
|
|
396
|
+
planKey: null,
|
|
397
|
+
planLabel: null,
|
|
398
|
+
allowance: null,
|
|
399
|
+
entitlements: {},
|
|
400
|
+
creditPolicy: null,
|
|
401
|
+
admission: null,
|
|
402
|
+
allowedOperations: [],
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
const allowanceAmount = dec(row.credit_allowance_amount);
|
|
406
|
+
const admissionOperations = parseAdmissionOperations(row.operation_admission);
|
|
407
|
+
return {
|
|
408
|
+
userId: String(row.user_id ?? userId),
|
|
409
|
+
planId: row.plan_id ?? null,
|
|
410
|
+
planKey: row.plan_key ?? null,
|
|
411
|
+
planLabel: row.plan_label ?? null,
|
|
412
|
+
allowance: row.credit_allowance_amount == null
|
|
413
|
+
? null
|
|
414
|
+
: {
|
|
415
|
+
amount: allowanceAmount,
|
|
416
|
+
resetUnit: row.credit_allowance_reset_unit == null
|
|
417
|
+
? null
|
|
418
|
+
: String(row.credit_allowance_reset_unit),
|
|
419
|
+
resetCount: row.credit_allowance_reset_count == null
|
|
420
|
+
? null
|
|
421
|
+
: Number(row.credit_allowance_reset_count),
|
|
422
|
+
resetAnchor: row.credit_allowance_reset_anchor == null
|
|
423
|
+
? null
|
|
424
|
+
: String(row.credit_allowance_reset_anchor),
|
|
425
|
+
resetTimezone: row.credit_allowance_reset_timezone == null
|
|
426
|
+
? null
|
|
427
|
+
: String(row.credit_allowance_reset_timezone),
|
|
428
|
+
},
|
|
429
|
+
entitlements: parseEntitlements(row.entitlements),
|
|
430
|
+
rateCard: row.rate_card != null ? String(row.rate_card) : null,
|
|
431
|
+
creditPolicy: row.credit_policy_type == null
|
|
432
|
+
? null
|
|
433
|
+
: {
|
|
434
|
+
type: String(row.credit_policy_type),
|
|
435
|
+
creditLimit: row.credit_limit == null ? null : dec(row.credit_limit),
|
|
436
|
+
},
|
|
437
|
+
admission: row.admission_max_in_flight == null && Object.keys(admissionOperations).length === 0
|
|
438
|
+
? null
|
|
439
|
+
: {
|
|
440
|
+
maxInFlight: row.admission_max_in_flight == null ? null : Number(row.admission_max_in_flight),
|
|
441
|
+
operations: admissionOperations,
|
|
442
|
+
},
|
|
443
|
+
allowedOperations: Array.isArray(row.allowed_operations)
|
|
444
|
+
? row.allowed_operations.map(String)
|
|
445
|
+
: [],
|
|
446
|
+
planAssignedAt: row.plan_assigned_at != null ? new Date(String(row.plan_assigned_at)) : null,
|
|
447
|
+
assignmentSourceType: row.assignment_source_type == null ? null : String(row.assignment_source_type),
|
|
448
|
+
assignmentSourceId: row.assignment_source_id == null ? null : String(row.assignment_source_id),
|
|
449
|
+
revisionPolicy: row.revision_policy == null ? null : String(row.revision_policy),
|
|
450
|
+
catalogVersion: row.catalog_revision_no != null ? Number(row.catalog_revision_no) : null,
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
async checkFeature(userId, feature) {
|
|
454
|
+
const entitlement = await this.planRepo.getEntitlement(userId, feature);
|
|
455
|
+
const value = entitlement?.feature_value ?? null;
|
|
456
|
+
return {
|
|
457
|
+
userId,
|
|
458
|
+
feature,
|
|
459
|
+
value,
|
|
460
|
+
hasFeature: entitlement != null && value !== null && value !== undefined && value !== false,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
async setUserPlan(userId, planKey, planAssignedAt) {
|
|
464
|
+
const row = await this.planRepo.setUserPlan(userId, planKey, planAssignedAt?.toISOString() ?? null);
|
|
465
|
+
return {
|
|
466
|
+
userId: String(row.user_id ?? userId),
|
|
467
|
+
planId: String(row.plan_id),
|
|
468
|
+
planAssignedAt: row.plan_assigned_at != null ? String(row.plan_assigned_at) : null,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
async unsetUserPlan(userId) {
|
|
472
|
+
const row = await this.planRepo.unsetUserPlan(userId);
|
|
473
|
+
return { userId: String(row.user_id ?? userId) };
|
|
474
|
+
}
|
|
475
|
+
async startPlanMigration(fromPlanId, toPlanId) {
|
|
476
|
+
const migrationId = await this.planRepo.startPlanMigration(fromPlanId, toPlanId);
|
|
477
|
+
if (!migrationId)
|
|
478
|
+
throw new StoreError("start_plan_migration returned no migration id");
|
|
479
|
+
return { migrationId };
|
|
480
|
+
}
|
|
481
|
+
async migratePlanBatch(migrationId, batchSize) {
|
|
482
|
+
const row = await this.planRepo.migratePlanBatch(migrationId, batchSize);
|
|
483
|
+
return {
|
|
484
|
+
migrated: Number(row.migrated ?? 0),
|
|
485
|
+
done: Boolean(row.done),
|
|
486
|
+
nextCursor: row.next_cursor != null ? String(row.next_cursor) : null,
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
async getQuotaState(userId, quotaKey) {
|
|
490
|
+
const rows = await this.planRepo.getQuotaState(userId, quotaKey);
|
|
491
|
+
return rows.map((row) => ({
|
|
492
|
+
userId: row.user_id,
|
|
493
|
+
quotaKey: row.quota_key,
|
|
494
|
+
operation: row.operation_key,
|
|
495
|
+
measure: row.measure_key,
|
|
496
|
+
limit: dec(row.quota_limit),
|
|
497
|
+
consumed: dec(row.consumed),
|
|
498
|
+
reserved: dec(row.reserved),
|
|
499
|
+
remaining: dec(row.remaining),
|
|
500
|
+
overage: dec(row.overage),
|
|
501
|
+
enforcement: row.enforcement,
|
|
502
|
+
windowStart: row.window_start,
|
|
503
|
+
windowEnd: row.window_end,
|
|
504
|
+
emitAtPercent: row.emit_at_percent,
|
|
505
|
+
}));
|
|
506
|
+
}
|
|
507
|
+
async listQuotaEvents(userId, options) {
|
|
508
|
+
const limit = options?.limit ?? 100;
|
|
509
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 500) {
|
|
510
|
+
throw new RangeError("quota event limit must be an integer between 1 and 500");
|
|
511
|
+
}
|
|
512
|
+
if (options?.afterId != null && options.after == null) {
|
|
513
|
+
throw new TypeError("afterId requires after");
|
|
514
|
+
}
|
|
515
|
+
const rows = await this.planRepo.listQuotaEvents(userId, options?.after?.toISOString() ?? null, limit, options?.idempotencyKey ?? null, options?.afterId ?? null);
|
|
516
|
+
return rows.map((row) => ({
|
|
517
|
+
eventId: row.event_id,
|
|
518
|
+
quotaKey: row.quota_key,
|
|
519
|
+
operation: row.operation_key,
|
|
520
|
+
measure: row.measure_key,
|
|
521
|
+
eventType: row.event_type,
|
|
522
|
+
thresholdPercent: row.threshold_percent,
|
|
523
|
+
idempotencyKey: row.idempotency_key,
|
|
524
|
+
usageChargeId: row.usage_charge_id,
|
|
525
|
+
createdAt: row.created_at,
|
|
526
|
+
}));
|
|
527
|
+
}
|
|
528
|
+
async checkAllowance(userId) {
|
|
529
|
+
const row = await this.planRepo.checkAllowance(userId);
|
|
530
|
+
if (!row) {
|
|
531
|
+
return { planId: "", allowanceRemaining: ZERO, periodStart: "", periodEnd: "" };
|
|
532
|
+
}
|
|
533
|
+
return {
|
|
534
|
+
planId: String(row.plan_id ?? ""),
|
|
535
|
+
allowanceRemaining: dec(row.allowance_remaining),
|
|
536
|
+
periodStart: String(row.period_start ?? ""),
|
|
537
|
+
periodEnd: String(row.period_end ?? ""),
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
async revokeCreditsByEntryType(userId, entryType) {
|
|
541
|
+
return this.deductionRepo.revokeCreditsByEntryType(userId, entryType);
|
|
542
|
+
}
|
|
543
|
+
async refundCredits(entryId, amount, reason, metadata, idempotencyKey) {
|
|
544
|
+
const row = await this.deductionRepo.refundCredits(entryId, amount != null ? decParam(amount) : null, idempotencyKey ?? `refund:${entryId}:${amount != null ? decParam(amount) : "remaining"}`, reason ?? null, JSON.stringify(metadata ?? {}));
|
|
545
|
+
if ("error" in row && row.error) {
|
|
546
|
+
return {
|
|
547
|
+
refundEntryId: "",
|
|
548
|
+
originalEntryId: entryId,
|
|
549
|
+
userId: String(row.user_id ?? ""),
|
|
550
|
+
amount: ZERO,
|
|
551
|
+
newBalance: dec(row.new_balance),
|
|
552
|
+
error: String(row.error),
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
refundEntryId: String(row.refund_entry_id ?? ""),
|
|
557
|
+
originalEntryId: entryId,
|
|
558
|
+
userId: String(row.user_id ?? ""),
|
|
559
|
+
amount: dec(row.amount),
|
|
560
|
+
newBalance: dec(row.new_balance),
|
|
561
|
+
bucketBreakdown: decRecord(row.bucket_breakdown),
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
async aggregateStats(start, end) {
|
|
565
|
+
if (!(start instanceof Date) || !(end instanceof Date) || end <= start) {
|
|
566
|
+
throw new RangeError("aggregateStats requires end after start");
|
|
567
|
+
}
|
|
568
|
+
const row = await this.analyticsRepo.aggregateStats(start.toISOString(), end.toISOString());
|
|
569
|
+
return {
|
|
570
|
+
totalCreditsConsumed: dec(row.total_credits_consumed),
|
|
571
|
+
activeUsers: Number(row.active_users ?? 0),
|
|
572
|
+
avgDailySpend: dec(row.avg_daily_spend),
|
|
573
|
+
topModel: String(row.top_model ?? ""),
|
|
574
|
+
topUser: String(row.top_user ?? ""),
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
async spendByUser(start, end) {
|
|
578
|
+
const rows = await this.analyticsRepo.spendByUser(start.toISOString(), end.toISOString());
|
|
579
|
+
return (rows ?? []).map((r) => ({
|
|
580
|
+
userId: String(r.user_id ?? ""),
|
|
581
|
+
totalSpend: dec(r.total_spend),
|
|
582
|
+
entryCount: Number(r.entry_count ?? 0),
|
|
583
|
+
}));
|
|
584
|
+
}
|
|
585
|
+
async spendByModel(start, end) {
|
|
586
|
+
const rows = await this.analyticsRepo.spendByModel(start.toISOString(), end.toISOString());
|
|
587
|
+
return (rows ?? []).map((r) => ({
|
|
588
|
+
model: String(r.model ?? ""),
|
|
589
|
+
totalSpend: dec(r.total_spend),
|
|
590
|
+
entryCount: Number(r.entry_count ?? 0),
|
|
591
|
+
}));
|
|
592
|
+
}
|
|
593
|
+
async topUsers(limit, start, end) {
|
|
594
|
+
const rows = await this.analyticsRepo.topUsers(limit, start.toISOString(), end.toISOString());
|
|
595
|
+
return (rows ?? []).map((r) => ({
|
|
596
|
+
userId: String(r.user_id ?? ""),
|
|
597
|
+
totalSpend: dec(r.total_spend),
|
|
598
|
+
}));
|
|
599
|
+
}
|
|
600
|
+
async dailySpend(start, end) {
|
|
601
|
+
const rows = await this.analyticsRepo.dailySpend(start.toISOString(), end.toISOString());
|
|
602
|
+
return (rows ?? []).map((r) => ({
|
|
603
|
+
date: String(r.date ?? ""),
|
|
604
|
+
totalSpend: dec(r.total_spend),
|
|
605
|
+
entryCount: Number(r.entry_count ?? 0),
|
|
606
|
+
}));
|
|
607
|
+
}
|
|
608
|
+
async getLedgerEntry(userId, entryId) {
|
|
609
|
+
const row = await this.analyticsRepo.getLedgerEntry(userId, entryId);
|
|
610
|
+
if (!row)
|
|
611
|
+
return null;
|
|
612
|
+
return mapLedgerEntry(row);
|
|
613
|
+
}
|
|
614
|
+
async listLedgerPage(userId, options, usageOnly) {
|
|
615
|
+
const limit = options?.limit ?? DEFAULT_PAGE_SIZE;
|
|
616
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > MAX_PAGE_SIZE) {
|
|
617
|
+
throw new RangeError(`limit must be an integer between 1 and ${MAX_PAGE_SIZE}`);
|
|
618
|
+
}
|
|
619
|
+
const cursor = options?.cursor ?? null;
|
|
620
|
+
const entryTypes = usageOnly
|
|
621
|
+
? ["usage"]
|
|
622
|
+
: (options?.entryTypes ?? null);
|
|
623
|
+
const rows = await this.analyticsRepo.listLedgerEntries(userId, entryTypes, options?.fromDate?.toISOString() ?? null, options?.toDate?.toISOString() ?? null, limit + 1, cursor?.createdAt ?? null, cursor?.entryId ?? null, usageOnly);
|
|
624
|
+
const hasMore = rows.length > limit;
|
|
625
|
+
const items = rows.slice(0, limit).map(mapLedgerEntry);
|
|
626
|
+
const last = items.at(-1);
|
|
627
|
+
return {
|
|
628
|
+
items,
|
|
629
|
+
nextCursor: hasMore && last ? { createdAt: last.createdAt, entryId: last.entryId } : null,
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
async listLedgerEntries(userId, options) {
|
|
633
|
+
return this.listLedgerPage(userId, options, false);
|
|
634
|
+
}
|
|
635
|
+
async listUsageEntries(userId, options) {
|
|
636
|
+
return this.listLedgerPage(userId, options, true);
|
|
637
|
+
}
|
|
638
|
+
async listUsageCharges(userId, options) {
|
|
639
|
+
const limit = options?.limit ?? DEFAULT_PAGE_SIZE;
|
|
640
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > MAX_PAGE_SIZE) {
|
|
641
|
+
throw new RangeError(`limit must be an integer between 1 and ${MAX_PAGE_SIZE}`);
|
|
642
|
+
}
|
|
643
|
+
const cursor = options?.cursor ?? null;
|
|
644
|
+
const rows = await this.analyticsRepo.listUsageCharges(userId, options?.fromDate?.toISOString() ?? null, options?.toDate?.toISOString() ?? null, limit + 1, cursor?.eventAt ?? null, cursor?.usageId ?? null);
|
|
645
|
+
const hasMore = rows.length > limit;
|
|
646
|
+
const items = rows.slice(0, limit).map(mapUsageCharge);
|
|
647
|
+
const last = items.at(-1);
|
|
648
|
+
return {
|
|
649
|
+
items,
|
|
650
|
+
nextCursor: hasMore && last ? { eventAt: last.eventAt, usageId: last.usageId } : null,
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
async createTeam(ownerSubjectId, name, initialBalance = ZERO) {
|
|
654
|
+
const row = await this.teamRepo.createTeam(ownerSubjectId, name, decParam(initialBalance));
|
|
655
|
+
if (row.error_code)
|
|
656
|
+
throw new StoreError(String(row.error_code));
|
|
657
|
+
return {
|
|
658
|
+
teamId: String(row.team_id ?? ""),
|
|
659
|
+
name: String(row.name ?? name),
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
async getTeamBalance(teamId) {
|
|
663
|
+
const row = await this.teamRepo.getTeamBalance(teamId);
|
|
664
|
+
if (!row) {
|
|
665
|
+
return { teamId, name: "", balance: ZERO, memberCount: 0 };
|
|
666
|
+
}
|
|
667
|
+
if ("error" in row && row.error) {
|
|
668
|
+
return { teamId, name: "", balance: ZERO, memberCount: 0 };
|
|
669
|
+
}
|
|
670
|
+
return {
|
|
671
|
+
teamId: String(row.team_id ?? teamId),
|
|
672
|
+
name: String(row.name ?? ""),
|
|
673
|
+
balance: dec(row.balance),
|
|
674
|
+
memberCount: Number(row.member_count ?? 0),
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
async addTeamMember(teamId, userId, role = "member", spendCap) {
|
|
678
|
+
const row = await this.teamRepo.addTeamMember(teamId, userId, role, spendCap != null ? decParam(spendCap) : null);
|
|
679
|
+
return {
|
|
680
|
+
teamId: String(row.team_id ?? teamId),
|
|
681
|
+
userId: String(row.user_id ?? userId),
|
|
682
|
+
role: String(row.role ?? role),
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
async getTeamMembers(teamId) {
|
|
686
|
+
const rows = await this.teamRepo.getTeamMembers(teamId);
|
|
687
|
+
return (rows ?? []).map((r) => ({
|
|
688
|
+
userId: String(r.user_id ?? ""),
|
|
689
|
+
role: String(r.role ?? "member"),
|
|
690
|
+
spendCap: r.spend_cap != null ? dec(r.spend_cap) : null,
|
|
691
|
+
totalSpent: dec(r.total_spent),
|
|
692
|
+
}));
|
|
693
|
+
}
|
|
694
|
+
async removeTeamMember(teamId, userId) {
|
|
695
|
+
return this.teamRepo.removeTeamMember(teamId, userId);
|
|
696
|
+
}
|
|
697
|
+
async deductTeam(teamId, userId, amount, metadata, idempotencyKey) {
|
|
698
|
+
const meta = { ...(metadata ?? {}) };
|
|
699
|
+
const effectiveIdempotencyKey = idempotencyKey ?? `team-usage:${randomUUID()}`;
|
|
700
|
+
meta.idempotency_key = effectiveIdempotencyKey;
|
|
701
|
+
const operation = typeof meta.operation === "string" && meta.operation.length > 0
|
|
702
|
+
? meta.operation
|
|
703
|
+
: "team_usage";
|
|
704
|
+
const row = await this.teamRepo.deductTeam(teamId, userId, decParam(amount), effectiveIdempotencyKey, operation, JSON.stringify(meta));
|
|
705
|
+
if ("error" in row && row.error) {
|
|
706
|
+
return {
|
|
707
|
+
entryId: "",
|
|
708
|
+
teamId,
|
|
709
|
+
userId,
|
|
710
|
+
amount: ZERO,
|
|
711
|
+
teamBalanceAfter: dec(row.team_balance_after),
|
|
712
|
+
error: String(row.error),
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
return {
|
|
716
|
+
entryId: String(row.entry_id ?? ""),
|
|
717
|
+
teamId: String(row.team_id ?? teamId),
|
|
718
|
+
userId: String(row.user_id ?? userId),
|
|
719
|
+
amount: dec(row.amount, amount),
|
|
720
|
+
teamBalanceAfter: dec(row.team_balance_after),
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
async sweepExpiredCredits(dryRun = false, userId, limit = 100) {
|
|
724
|
+
const row = await this.bucketRepo.sweepExpiredCredits(dryRun, userId, limit);
|
|
725
|
+
return {
|
|
726
|
+
expiredCount: Number(row.expired_count ?? 0),
|
|
727
|
+
expiredAmount: dec(row.expired_amount),
|
|
728
|
+
dryRun,
|
|
729
|
+
expiredByBucket: decRecord(row.expired_by_bucket),
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
async getBucketBalances(userId) {
|
|
733
|
+
const envelope = await this.bucketRepo.getBucketBalances(userId);
|
|
734
|
+
const bucketRows = envelope.buckets ?? [];
|
|
735
|
+
const buckets = bucketRows.map((row) => ({
|
|
736
|
+
bucketKey: String(row.bucket_key ?? ""),
|
|
737
|
+
label: String(row.label ?? ""),
|
|
738
|
+
priority: Number(row.priority ?? 0),
|
|
739
|
+
expires: Boolean(row.expires ?? false),
|
|
740
|
+
balance: dec(row.balance),
|
|
741
|
+
}));
|
|
742
|
+
return { userId, buckets, totalBalance: dec(envelope.total_balance) };
|
|
743
|
+
}
|
|
744
|
+
async executeGrantProgram(request) {
|
|
745
|
+
const rows = await this.balanceRepo.executeGrantProgram({
|
|
746
|
+
trigger: request.trigger,
|
|
747
|
+
programKey: request.programKey,
|
|
748
|
+
subjectId: request.subjectId,
|
|
749
|
+
eventKey: request.eventKey,
|
|
750
|
+
referrerSubjectId: request.referrerSubjectId ?? null,
|
|
751
|
+
region: request.region ?? null,
|
|
752
|
+
metadata: JSON.stringify(request.metadata ?? {}),
|
|
753
|
+
});
|
|
754
|
+
return rows.map((row) => ({
|
|
755
|
+
grantEventId: row.grant_event_id ?? null,
|
|
756
|
+
grantAwardId: row.grant_award_id ?? null,
|
|
757
|
+
recipientSubjectId: row.recipient_subject_id ?? null,
|
|
758
|
+
ledgerEntryId: row.ledger_entry_id ?? null,
|
|
759
|
+
amount: dec(row.amount),
|
|
760
|
+
replayed: Boolean(row.replayed),
|
|
761
|
+
error: row.error_code ?? null,
|
|
762
|
+
}));
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
//# sourceMappingURL=store.js.map
|