@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
|
@@ -1,872 +0,0 @@
|
|
|
1
|
-
import Decimal from "decimal.js";
|
|
2
|
-
import { StoreError } from "../errors.js";
|
|
3
|
-
import { resolveCalendarWindow } from "../allowance.js";
|
|
4
|
-
import { CreditStore } from "./credit-store.js";
|
|
5
|
-
const ZERO = new Decimal(0);
|
|
6
|
-
/**
|
|
7
|
-
* Parse a Postgres NUMERIC column into an exact `Decimal`. Postgres returns
|
|
8
|
-
* NUMERIC as a *string* via `pg`, so this preserves full precision (contract
|
|
9
|
-
* §1). `null`/`undefined` become the supplied fallback.
|
|
10
|
-
*/
|
|
11
|
-
function dec(value, fallback = ZERO) {
|
|
12
|
-
if (value === null || value === undefined)
|
|
13
|
-
return fallback;
|
|
14
|
-
if (value instanceof Decimal)
|
|
15
|
-
return value;
|
|
16
|
-
try {
|
|
17
|
-
return new Decimal(typeof value === "string" ? value : String(value));
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
return fallback;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
/** A money serialized for an SQL parameter: send as a decimal string. */
|
|
24
|
-
function decParam(value) {
|
|
25
|
-
return value.toString();
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Derive the feature-limit window END from its (already calendar-aligned)
|
|
29
|
-
* START — the manager only resolves/threads the start (mirrors Python
|
|
30
|
-
* `base.py`); the RPC needs an explicit end for its `WHERE` clause, so this
|
|
31
|
-
* store computes it via `resolveCalendarWindow` (idempotent on an aligned
|
|
32
|
-
* start — re-resolving it yields the identical window).
|
|
33
|
-
*/
|
|
34
|
-
function featureWindowEnd(start, period) {
|
|
35
|
-
return resolveCalendarWindow(start, period).end;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Parse a JSON `{tier_key: "3.0000", ...}` object (e.g. `tier_breakdown`,
|
|
39
|
-
* `expired_by_tier`) into `Record<string, Decimal>`, converting every value
|
|
40
|
-
* the same way scalar money fields are (never left as a raw string/number).
|
|
41
|
-
* Returns `null` when `raw` is not an object (absent/error responses).
|
|
42
|
-
*/
|
|
43
|
-
function decRecord(raw) {
|
|
44
|
-
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
45
|
-
return null;
|
|
46
|
-
const out = {};
|
|
47
|
-
for (const [k, v] of Object.entries(raw)) {
|
|
48
|
-
out[k] = dec(v);
|
|
49
|
-
}
|
|
50
|
-
return out;
|
|
51
|
-
}
|
|
52
|
-
/** Parse the ``feature_limits`` JSONB map into typed `FeatureLimit` records. */
|
|
53
|
-
function parseFeatureLimits(raw) {
|
|
54
|
-
if (!raw || typeof raw !== "object")
|
|
55
|
-
return {};
|
|
56
|
-
const out = {};
|
|
57
|
-
for (const [k, v] of Object.entries(raw)) {
|
|
58
|
-
const fl = (v ?? {});
|
|
59
|
-
out[k] = {
|
|
60
|
-
maxCalls: Number(fl.max_calls ?? fl.maxCalls ?? 0),
|
|
61
|
-
period: String(fl.period ?? "monthly") ?? "monthly",
|
|
62
|
-
action: String(fl.action ?? "deny") ?? "deny",
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
return out;
|
|
66
|
-
}
|
|
67
|
-
/** Parse the ``per_operation`` JSONB map into typed `OperationPolicy` records. */
|
|
68
|
-
function parsePerOperation(raw) {
|
|
69
|
-
if (!raw || typeof raw !== "object")
|
|
70
|
-
return {};
|
|
71
|
-
const out = {};
|
|
72
|
-
for (const [k, v] of Object.entries(raw)) {
|
|
73
|
-
const op = (v ?? {});
|
|
74
|
-
out[k] = {
|
|
75
|
-
billingMode: String(op.billing_mode ?? op.billingMode ?? "strict") ?? "strict",
|
|
76
|
-
maxConcurrent: op.max_concurrent != null
|
|
77
|
-
? Number(op.max_concurrent)
|
|
78
|
-
: op.maxConcurrent != null
|
|
79
|
-
? Number(op.maxConcurrent)
|
|
80
|
-
: null,
|
|
81
|
-
overdraftFloor: op.overdraft_floor != null
|
|
82
|
-
? dec(op.overdraft_floor)
|
|
83
|
-
: op.overdraftFloor != null
|
|
84
|
-
? dec(op.overdraftFloor)
|
|
85
|
-
: null,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return out;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Credit store backed by a raw Postgres connection.
|
|
92
|
-
*
|
|
93
|
-
* Uses dependency injection for the PG pool — supply a real ``pg.Pool`` class
|
|
94
|
-
* for production, a mock constructor for tests, or a pre-created pool instance
|
|
95
|
-
* to share across stores (avoids exhausting Postgres connections).
|
|
96
|
-
*
|
|
97
|
-
* Args:
|
|
98
|
-
* databaseUrl: Postgres connection string.
|
|
99
|
-
* poolOrCtor: A pre-created PgPool instance, a PgPoolConstructor, or omitted
|
|
100
|
-
* (loads ``pg`` on first use).
|
|
101
|
-
*/
|
|
102
|
-
export class PostgresStore extends CreditStore {
|
|
103
|
-
databaseUrl;
|
|
104
|
-
poolCtor = null;
|
|
105
|
-
pool = null;
|
|
106
|
-
ownsPool;
|
|
107
|
-
constructor(databaseUrl, poolOrCtor) {
|
|
108
|
-
super();
|
|
109
|
-
this.databaseUrl = databaseUrl;
|
|
110
|
-
if (poolOrCtor && typeof poolOrCtor.query === "function") {
|
|
111
|
-
// Pre-created pool instance — share, don't own.
|
|
112
|
-
this.pool = poolOrCtor;
|
|
113
|
-
this.ownsPool = false;
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
this.poolCtor = poolOrCtor ?? null;
|
|
117
|
-
this.ownsPool = true;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
async getPool() {
|
|
121
|
-
if (!this.pool) {
|
|
122
|
-
const Pool = await this.getPoolCtor();
|
|
123
|
-
this.pool = new Pool({ connectionString: this.databaseUrl });
|
|
124
|
-
}
|
|
125
|
-
return this.pool;
|
|
126
|
-
}
|
|
127
|
-
async getPoolCtor() {
|
|
128
|
-
if (this.poolCtor)
|
|
129
|
-
return this.poolCtor;
|
|
130
|
-
const mod = await import("pg");
|
|
131
|
-
this.poolCtor = mod.Pool;
|
|
132
|
-
return this.poolCtor;
|
|
133
|
-
}
|
|
134
|
-
async query(text, params) {
|
|
135
|
-
const pool = await this.getPool();
|
|
136
|
-
const res = await pool.query(text, params);
|
|
137
|
-
return res.rows;
|
|
138
|
-
}
|
|
139
|
-
async close() {
|
|
140
|
-
if (this.pool && this.ownsPool) {
|
|
141
|
-
await this.pool.end();
|
|
142
|
-
this.pool = null;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Call a SQL function and return its result rows.
|
|
147
|
-
*
|
|
148
|
-
* bursar's RPCs come in two shapes:
|
|
149
|
-
* 1. **Scalar JSONB** (`RETURNS JSONB`) — `pg` returns one row whose single
|
|
150
|
-
* column holds the parsed object. We unwrap that to `[object]`.
|
|
151
|
-
* 2. **Set-returning** (`RETURNS TABLE/SETOF`) — many rows of named columns.
|
|
152
|
-
* We return them as-is.
|
|
153
|
-
*
|
|
154
|
-
* The previous `rows[0]` heuristic was fragile: a single-row set-returning
|
|
155
|
-
* function whose first column happened to be an object would be misread. We
|
|
156
|
-
* instead detect the JSONB-scalar case precisely: exactly one row with exactly
|
|
157
|
-
* one column whose value is a non-array object. Lists therefore always return
|
|
158
|
-
* all their rows.
|
|
159
|
-
*/
|
|
160
|
-
async callproc(name, params) {
|
|
161
|
-
const placeholders = params.map((_, i) => `$${i + 1}`).join(", ");
|
|
162
|
-
const rows = await this.query(`SELECT * FROM ${name}(${placeholders})`, params);
|
|
163
|
-
if (rows.length === 1) {
|
|
164
|
-
const row = rows[0];
|
|
165
|
-
const keys = Object.keys(row);
|
|
166
|
-
if (keys.length === 1) {
|
|
167
|
-
const v = row[keys[0]];
|
|
168
|
-
if (v !== null && typeof v === "object" && !Array.isArray(v)) {
|
|
169
|
-
// Scalar JSONB result: unwrap to the parsed object.
|
|
170
|
-
return [v];
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return rows;
|
|
175
|
-
}
|
|
176
|
-
async setup(_databaseUrl) {
|
|
177
|
-
// H17: do NOT silently report success for a no-op. bursar's schema is managed
|
|
178
|
-
// as a set of ordered SQL migrations bundled with the Python package; this
|
|
179
|
-
// store does not embed them. Surface that clearly instead of green-lighting
|
|
180
|
-
// a missing schema (which would only fail later as missing-RPC errors).
|
|
181
|
-
throw new StoreError("PostgresStore.setup() does not run migrations. Apply the bundled SQL " +
|
|
182
|
-
"migrations first — run `bursar migrate` via the Python CLI, or execute the " +
|
|
183
|
-
"files in `python/src/bursar/sql/*.sql` (in filename order) against your " +
|
|
184
|
-
"database. This store assumes the schema already exists.");
|
|
185
|
-
}
|
|
186
|
-
async getBalance(userId) {
|
|
187
|
-
const rows = await this.callproc("get_credits_balance", [userId]);
|
|
188
|
-
if (!rows || rows.length === 0) {
|
|
189
|
-
return { userId, balance: ZERO, lifetimePurchased: ZERO };
|
|
190
|
-
}
|
|
191
|
-
const row = rows[0];
|
|
192
|
-
return {
|
|
193
|
-
userId: String(row.user_id ?? userId),
|
|
194
|
-
balance: dec(row.balance),
|
|
195
|
-
lifetimePurchased: dec(row.lifetime_purchased),
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
async addCredits(userId, amount, type = "adjustment", metadata, expiresAt, tier,
|
|
199
|
-
// NOTE: threaded through defensively as a trailing positional param to the
|
|
200
|
-
// `credits_add` RPC. Confirm the SQL migration adds a `p_idempotency_key`
|
|
201
|
-
// parameter (with a default, so existing 5-arg call sites keep working)
|
|
202
|
-
// before relying on this against a real database (see 011_lazy_expiry.sql).
|
|
203
|
-
idempotencyKey) {
|
|
204
|
-
const meta = { ...(metadata ?? {}) };
|
|
205
|
-
if (expiresAt) {
|
|
206
|
-
meta.expires_at = expiresAt instanceof Date ? expiresAt.toISOString() : String(expiresAt);
|
|
207
|
-
}
|
|
208
|
-
const rows = await this.callproc("credits_add", [
|
|
209
|
-
userId,
|
|
210
|
-
decParam(amount),
|
|
211
|
-
type,
|
|
212
|
-
JSON.stringify(meta),
|
|
213
|
-
tier ?? null,
|
|
214
|
-
idempotencyKey ?? null,
|
|
215
|
-
]);
|
|
216
|
-
const row = (rows?.[0] ?? {});
|
|
217
|
-
if ("error" in row && row.error) {
|
|
218
|
-
throw new StoreError(`credits_add: ${String(row.error)}`);
|
|
219
|
-
}
|
|
220
|
-
return {
|
|
221
|
-
transactionId: String(row.id ?? ""),
|
|
222
|
-
userId: String(row.user_id ?? userId),
|
|
223
|
-
amount: dec(row.amount, amount),
|
|
224
|
-
newBalance: dec(row.new_balance),
|
|
225
|
-
lifetimePurchased: dec(row.lifetime_purchased),
|
|
226
|
-
tier: String(row.tier ?? tier ?? "default"),
|
|
227
|
-
idempotent: Boolean(row.idempotent),
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
async deductWithAllowance(userId, amount, options) {
|
|
231
|
-
const idempotencyKey = options?.idempotencyKey ?? null;
|
|
232
|
-
const minBalance = options?.minBalance ?? ZERO;
|
|
233
|
-
const model = options?.model ?? null;
|
|
234
|
-
const metadata = options?.metadata ?? {};
|
|
235
|
-
const periodStart = options?.periodStart ?? null;
|
|
236
|
-
const feature = options?.feature ?? null;
|
|
237
|
-
const featureLimit = options?.featureLimit ?? null;
|
|
238
|
-
const featurePeriodStart = options?.featurePeriodStart ?? null;
|
|
239
|
-
const featurePeriodEnd = featureLimit != null && featurePeriodStart != null
|
|
240
|
-
? featureWindowEnd(featurePeriodStart, featureLimit.period)
|
|
241
|
-
: null;
|
|
242
|
-
const rows = await this.callproc("deduct_with_allowance", [
|
|
243
|
-
userId,
|
|
244
|
-
decParam(amount),
|
|
245
|
-
idempotencyKey,
|
|
246
|
-
decParam(minBalance),
|
|
247
|
-
model,
|
|
248
|
-
JSON.stringify(metadata ?? {}),
|
|
249
|
-
false, // p_skip_allowance: not yet exposed as a JS option; keep the SQL default
|
|
250
|
-
periodStart != null ? periodStart.toISOString().slice(0, 10) : null,
|
|
251
|
-
feature,
|
|
252
|
-
featureLimit != null ? featureLimit.maxCalls : null,
|
|
253
|
-
featureLimit != null ? featureLimit.action : null,
|
|
254
|
-
featurePeriodStart != null ? featurePeriodStart.toISOString().slice(0, 10) : null,
|
|
255
|
-
featurePeriodEnd != null ? featurePeriodEnd.toISOString().slice(0, 10) : null,
|
|
256
|
-
]);
|
|
257
|
-
const row = (rows?.[0] ?? {});
|
|
258
|
-
if ("error" in row && row.error) {
|
|
259
|
-
// Map the SQL error envelope to DeductionResult.error (the manager maps
|
|
260
|
-
// codes to typed exceptions). cap_reached / insufficient_credits /
|
|
261
|
-
// invalid_amount / feature_limit_reached all flow through here without
|
|
262
|
-
// throwing.
|
|
263
|
-
return {
|
|
264
|
-
transactionId: "",
|
|
265
|
-
userId,
|
|
266
|
-
amount: ZERO,
|
|
267
|
-
allowanceConsumed: ZERO,
|
|
268
|
-
balanceAfter: dec(row.balance_after),
|
|
269
|
-
idempotent: false,
|
|
270
|
-
capWarning: null,
|
|
271
|
-
featureLimitWarning: null,
|
|
272
|
-
error: String(row.error),
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
return {
|
|
276
|
-
transactionId: String(row.transaction_id ?? ""),
|
|
277
|
-
userId,
|
|
278
|
-
amount: dec(row.amount),
|
|
279
|
-
allowanceConsumed: dec(row.allowance_consumed),
|
|
280
|
-
balanceAfter: dec(row.balance_after),
|
|
281
|
-
idempotent: Boolean(row.idempotent),
|
|
282
|
-
capWarning: row.cap_warning != null ? String(row.cap_warning) : null,
|
|
283
|
-
featureLimitWarning: row.feature_limit_warning != null ? String(row.feature_limit_warning) : null,
|
|
284
|
-
tierBreakdown: decRecord(row.tier_breakdown),
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
// ── Lease lifecycle (atomic admission) ─────────────────────────────
|
|
288
|
-
async createLease(userId, amount, operationType, options) {
|
|
289
|
-
const billingMode = options?.billingMode ?? "strict";
|
|
290
|
-
const floor = options?.floor ?? ZERO;
|
|
291
|
-
const overdraftFloor = options?.overdraftFloor ?? null;
|
|
292
|
-
const periodStart = options?.periodStart ?? null;
|
|
293
|
-
const feature = options?.feature ?? null;
|
|
294
|
-
const featureLimit = options?.featureLimit ?? null;
|
|
295
|
-
const featurePeriodStart = options?.featurePeriodStart ?? null;
|
|
296
|
-
const featurePeriodEnd = featureLimit != null && featurePeriodStart != null
|
|
297
|
-
? featureWindowEnd(featurePeriodStart, featureLimit.period)
|
|
298
|
-
: null;
|
|
299
|
-
const rows = await this.callproc("create_lease", [
|
|
300
|
-
userId,
|
|
301
|
-
decParam(amount),
|
|
302
|
-
operationType,
|
|
303
|
-
billingMode,
|
|
304
|
-
decParam(floor),
|
|
305
|
-
options?.maxConcurrent ?? null,
|
|
306
|
-
options?.ttlSeconds ?? 600,
|
|
307
|
-
options?.model ?? null,
|
|
308
|
-
overdraftFloor != null ? decParam(overdraftFloor) : null,
|
|
309
|
-
JSON.stringify(options?.metadata ?? {}),
|
|
310
|
-
periodStart != null ? periodStart.toISOString().slice(0, 10) : null,
|
|
311
|
-
feature,
|
|
312
|
-
featureLimit != null ? featureLimit.maxCalls : null,
|
|
313
|
-
featureLimit != null ? featureLimit.action : null,
|
|
314
|
-
featurePeriodStart != null ? featurePeriodStart.toISOString().slice(0, 10) : null,
|
|
315
|
-
featurePeriodEnd != null ? featurePeriodEnd.toISOString().slice(0, 10) : null,
|
|
316
|
-
]);
|
|
317
|
-
const row = (rows?.[0] ?? {});
|
|
318
|
-
if (!row || Object.keys(row).length === 0) {
|
|
319
|
-
return {
|
|
320
|
-
leaseId: "",
|
|
321
|
-
userId,
|
|
322
|
-
amount: ZERO,
|
|
323
|
-
available: ZERO,
|
|
324
|
-
reservedTotal: ZERO,
|
|
325
|
-
billingMode,
|
|
326
|
-
expiresAt: "",
|
|
327
|
-
error: "no result",
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
if ("error" in row && row.error) {
|
|
331
|
-
return {
|
|
332
|
-
leaseId: "",
|
|
333
|
-
userId,
|
|
334
|
-
amount: ZERO,
|
|
335
|
-
available: dec(row.available),
|
|
336
|
-
reservedTotal: dec(row.reserved),
|
|
337
|
-
billingMode,
|
|
338
|
-
expiresAt: "",
|
|
339
|
-
error: String(row.error),
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
return {
|
|
343
|
-
leaseId: String(row.lease_id ?? ""),
|
|
344
|
-
userId: String(row.user_id ?? userId),
|
|
345
|
-
amount: dec(row.amount),
|
|
346
|
-
available: dec(row.available),
|
|
347
|
-
reservedTotal: dec(row.reserved),
|
|
348
|
-
billingMode: String(row.billing_mode ?? billingMode) ?? billingMode,
|
|
349
|
-
expiresAt: String(row.expires_at ?? ""),
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
async settleLease(userId, leaseId, amount, options) {
|
|
353
|
-
const minBalance = options?.minBalance ?? ZERO;
|
|
354
|
-
const periodStart = options?.periodStart ?? null;
|
|
355
|
-
const feature = options?.feature ?? null;
|
|
356
|
-
const featureLimit = options?.featureLimit ?? null;
|
|
357
|
-
const featurePeriodStart = options?.featurePeriodStart ?? null;
|
|
358
|
-
const featurePeriodEnd = featureLimit != null && featurePeriodStart != null
|
|
359
|
-
? featureWindowEnd(featurePeriodStart, featureLimit.period)
|
|
360
|
-
: null;
|
|
361
|
-
const rows = await this.callproc("settle_lease", [
|
|
362
|
-
userId,
|
|
363
|
-
leaseId,
|
|
364
|
-
decParam(amount),
|
|
365
|
-
options?.idempotencyKey ?? null,
|
|
366
|
-
decParam(minBalance),
|
|
367
|
-
options?.model ?? null,
|
|
368
|
-
JSON.stringify(options?.metadata ?? {}),
|
|
369
|
-
false, // p_skip_allowance: not yet exposed as a JS option; keep the SQL default
|
|
370
|
-
periodStart != null ? periodStart.toISOString().slice(0, 10) : null,
|
|
371
|
-
feature,
|
|
372
|
-
featureLimit != null ? featureLimit.maxCalls : null,
|
|
373
|
-
featureLimit != null ? featureLimit.action : null,
|
|
374
|
-
featurePeriodStart != null ? featurePeriodStart.toISOString().slice(0, 10) : null,
|
|
375
|
-
featurePeriodEnd != null ? featurePeriodEnd.toISOString().slice(0, 10) : null,
|
|
376
|
-
]);
|
|
377
|
-
const row = (rows?.[0] ?? {});
|
|
378
|
-
if (!row || Object.keys(row).length === 0) {
|
|
379
|
-
return {
|
|
380
|
-
transactionId: "",
|
|
381
|
-
userId,
|
|
382
|
-
amount: ZERO,
|
|
383
|
-
allowanceConsumed: ZERO,
|
|
384
|
-
balanceAfter: ZERO,
|
|
385
|
-
idempotent: false,
|
|
386
|
-
capWarning: null,
|
|
387
|
-
featureLimitWarning: null,
|
|
388
|
-
error: "no result",
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
if ("error" in row && row.error) {
|
|
392
|
-
return {
|
|
393
|
-
transactionId: "",
|
|
394
|
-
userId,
|
|
395
|
-
amount: ZERO,
|
|
396
|
-
allowanceConsumed: ZERO,
|
|
397
|
-
balanceAfter: dec(row.balance_after),
|
|
398
|
-
idempotent: false,
|
|
399
|
-
capWarning: null,
|
|
400
|
-
featureLimitWarning: null,
|
|
401
|
-
error: String(row.error),
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
return {
|
|
405
|
-
transactionId: String(row.transaction_id ?? ""),
|
|
406
|
-
userId,
|
|
407
|
-
amount: dec(row.amount),
|
|
408
|
-
allowanceConsumed: dec(row.allowance_consumed),
|
|
409
|
-
balanceAfter: dec(row.balance_after),
|
|
410
|
-
idempotent: Boolean(row.idempotent),
|
|
411
|
-
capWarning: row.cap_warning != null ? String(row.cap_warning) : null,
|
|
412
|
-
featureLimitWarning: row.feature_limit_warning != null ? String(row.feature_limit_warning) : null,
|
|
413
|
-
tierBreakdown: decRecord(row.tier_breakdown),
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
async releaseLease(userId, leaseId) {
|
|
417
|
-
const rows = await this.callproc("release_lease", [userId, leaseId]);
|
|
418
|
-
const row = (rows?.[0] ?? {});
|
|
419
|
-
return {
|
|
420
|
-
leaseId,
|
|
421
|
-
userId,
|
|
422
|
-
released: Boolean(row.released),
|
|
423
|
-
reason: row.reason != null ? String(row.reason) : null,
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
async renewLease(userId, leaseId, ttlSeconds) {
|
|
427
|
-
const rows = await this.callproc("renew_lease", [userId, leaseId, ttlSeconds]);
|
|
428
|
-
const row = (rows?.[0] ?? {});
|
|
429
|
-
if ("error" in row && row.error) {
|
|
430
|
-
return {
|
|
431
|
-
leaseId,
|
|
432
|
-
userId,
|
|
433
|
-
amount: ZERO,
|
|
434
|
-
available: ZERO,
|
|
435
|
-
reservedTotal: ZERO,
|
|
436
|
-
billingMode: "strict",
|
|
437
|
-
expiresAt: "",
|
|
438
|
-
error: String(row.error),
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
return {
|
|
442
|
-
leaseId: String(row.lease_id ?? leaseId),
|
|
443
|
-
userId,
|
|
444
|
-
amount: dec(row.amount),
|
|
445
|
-
available: dec(row.available),
|
|
446
|
-
reservedTotal: dec(row.reserved),
|
|
447
|
-
billingMode: String(row.billing_mode ?? "strict"),
|
|
448
|
-
expiresAt: String(row.expires_at ?? ""),
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
async getAvailable(userId) {
|
|
452
|
-
const rows = await this.callproc("get_available_credits", [userId]);
|
|
453
|
-
const row = (rows?.[0] ?? {});
|
|
454
|
-
return {
|
|
455
|
-
userId,
|
|
456
|
-
balance: dec(row.balance),
|
|
457
|
-
reserved: dec(row.reserved),
|
|
458
|
-
available: dec(row.available),
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
async getActivePricing() {
|
|
462
|
-
const rows = await this.callproc("get_active_pricing_config", []);
|
|
463
|
-
if (!rows || rows.length === 0)
|
|
464
|
-
return null;
|
|
465
|
-
const row = rows[0];
|
|
466
|
-
if (!row || !row.config)
|
|
467
|
-
return null;
|
|
468
|
-
return row;
|
|
469
|
-
}
|
|
470
|
-
async setActivePricing(config, label) {
|
|
471
|
-
const rows = await this.callproc("set_active_pricing_config", [
|
|
472
|
-
JSON.stringify(config),
|
|
473
|
-
label ?? null,
|
|
474
|
-
]);
|
|
475
|
-
const row = (rows?.[0] ?? {});
|
|
476
|
-
return String(row.id ?? "");
|
|
477
|
-
}
|
|
478
|
-
// H8: pricing history / activation — mirrors Python base.py:293-312.
|
|
479
|
-
async getPricingHistory() {
|
|
480
|
-
const rows = await this.callproc("get_pricing_history", []);
|
|
481
|
-
if (!rows)
|
|
482
|
-
return [];
|
|
483
|
-
return rows.map((r) => ({
|
|
484
|
-
id: String(r.id ?? ""),
|
|
485
|
-
version: Number(r.version ?? 0),
|
|
486
|
-
label: r.label ?? null,
|
|
487
|
-
active: Boolean(r.active ?? false),
|
|
488
|
-
createdAt: String(r.created_at ?? ""),
|
|
489
|
-
}));
|
|
490
|
-
}
|
|
491
|
-
async getPricingConfig(version) {
|
|
492
|
-
const rows = await this.callproc("get_pricing_config", [version]);
|
|
493
|
-
if (!rows || rows.length === 0)
|
|
494
|
-
return null;
|
|
495
|
-
const row = rows[0];
|
|
496
|
-
if (!row.config)
|
|
497
|
-
return null;
|
|
498
|
-
return {
|
|
499
|
-
id: String(row.id ?? ""),
|
|
500
|
-
config: row.config,
|
|
501
|
-
version: Number(row.version ?? version),
|
|
502
|
-
};
|
|
503
|
-
}
|
|
504
|
-
async activatePricing(version) {
|
|
505
|
-
const rows = await this.callproc("activate_pricing", [version]);
|
|
506
|
-
const row = (rows?.[0] ?? {});
|
|
507
|
-
return String(row.id ?? "");
|
|
508
|
-
}
|
|
509
|
-
// ── Plan management ────────────────────────────────────────────────
|
|
510
|
-
async getUserPlan(userId) {
|
|
511
|
-
const rows = await this.callproc("get_user_plan", [userId]);
|
|
512
|
-
if (!rows || rows.length === 0) {
|
|
513
|
-
return { userId, planId: null, planName: null, freeAllowance: ZERO, features: {} };
|
|
514
|
-
}
|
|
515
|
-
const row = rows[0];
|
|
516
|
-
return {
|
|
517
|
-
userId: String(row.user_id ?? userId),
|
|
518
|
-
planId: row.plan_id ?? null,
|
|
519
|
-
planName: row.plan_name ?? null,
|
|
520
|
-
freeAllowance: dec(row.free_allowance),
|
|
521
|
-
features: row.features ?? {},
|
|
522
|
-
featureLimits: parseFeatureLimits(row.feature_limits),
|
|
523
|
-
defaultBillingMode: String(row.default_billing_mode ?? "strict") ?? "strict",
|
|
524
|
-
perOperation: parsePerOperation(row.per_operation),
|
|
525
|
-
maxConcurrent: row.max_concurrent != null ? Number(row.max_concurrent) : null,
|
|
526
|
-
overdraftFloor: row.overdraft_floor != null ? dec(row.overdraft_floor) : null,
|
|
527
|
-
allowancePeriod: row.allowance_period ?? "calendar_month",
|
|
528
|
-
planAssignedAt: row.plan_assigned_at != null ? new Date(String(row.plan_assigned_at)) : null,
|
|
529
|
-
};
|
|
530
|
-
}
|
|
531
|
-
async checkFeature(userId, feature) {
|
|
532
|
-
const plan = await this.getUserPlan(userId);
|
|
533
|
-
const present = Object.prototype.hasOwnProperty.call(plan.features, feature);
|
|
534
|
-
const value = present ? plan.features[feature] : null;
|
|
535
|
-
return {
|
|
536
|
-
userId,
|
|
537
|
-
feature,
|
|
538
|
-
value,
|
|
539
|
-
// M6: presence-vs-truthiness — numeric 0 / "" count as present.
|
|
540
|
-
hasFeature: present && value !== null && value !== undefined && value !== false,
|
|
541
|
-
};
|
|
542
|
-
}
|
|
543
|
-
async setUserPlan(userId, planId) {
|
|
544
|
-
const rows = await this.callproc("set_user_plan", [userId, planId]);
|
|
545
|
-
const row = (rows?.[0] ?? {});
|
|
546
|
-
return {
|
|
547
|
-
userId: String(row.user_id ?? userId),
|
|
548
|
-
planId: String(row.plan_id ?? planId),
|
|
549
|
-
};
|
|
550
|
-
}
|
|
551
|
-
async checkAllowance(userId, periodStart) {
|
|
552
|
-
const rows = await this.callproc("check_plan_allowance", [
|
|
553
|
-
userId,
|
|
554
|
-
periodStart != null ? periodStart.toISOString().slice(0, 10) : null,
|
|
555
|
-
]);
|
|
556
|
-
if (!rows || rows.length === 0) {
|
|
557
|
-
return { planId: "", allowanceRemaining: ZERO, periodStart: "", periodEnd: "" };
|
|
558
|
-
}
|
|
559
|
-
const row = rows[0];
|
|
560
|
-
return {
|
|
561
|
-
planId: String(row.plan_id ?? ""),
|
|
562
|
-
allowanceRemaining: dec(row.allowance_remaining),
|
|
563
|
-
periodStart: String(row.period_start ?? ""),
|
|
564
|
-
periodEnd: String(row.period_end ?? ""),
|
|
565
|
-
};
|
|
566
|
-
}
|
|
567
|
-
async incrementUsageWindow(userId, planId, amount) {
|
|
568
|
-
await this.callproc("increment_usage_window", [userId, planId, decParam(amount)]);
|
|
569
|
-
}
|
|
570
|
-
/** Advisory, non-locking read of invocation-count usage (UI only). Mirrors `checkSpendCap`. */
|
|
571
|
-
async checkFeatureLimit(userId, feature, maxCalls, periodStart, periodEnd) {
|
|
572
|
-
const rows = await this.callproc("check_feature_limit", [
|
|
573
|
-
userId,
|
|
574
|
-
feature,
|
|
575
|
-
maxCalls,
|
|
576
|
-
periodStart.toISOString().slice(0, 10),
|
|
577
|
-
periodEnd.toISOString().slice(0, 10),
|
|
578
|
-
]);
|
|
579
|
-
if (!rows || rows.length === 0) {
|
|
580
|
-
return {
|
|
581
|
-
userId,
|
|
582
|
-
feature,
|
|
583
|
-
limited: false,
|
|
584
|
-
limit: 0,
|
|
585
|
-
used: 0,
|
|
586
|
-
remaining: 0,
|
|
587
|
-
periodStart: "",
|
|
588
|
-
periodEnd: "",
|
|
589
|
-
action: null,
|
|
590
|
-
};
|
|
591
|
-
}
|
|
592
|
-
const row = rows[0];
|
|
593
|
-
return {
|
|
594
|
-
userId: String(row.user_id ?? userId),
|
|
595
|
-
feature: String(row.feature ?? feature),
|
|
596
|
-
limited: Boolean(row.limited ?? true),
|
|
597
|
-
limit: Number(row.limit ?? maxCalls),
|
|
598
|
-
used: Number(row.used ?? 0),
|
|
599
|
-
remaining: Number(row.remaining ?? Math.max(maxCalls - Number(row.used ?? 0), 0)),
|
|
600
|
-
periodStart: String(row.period_start ?? ""),
|
|
601
|
-
periodEnd: String(row.period_end ?? ""),
|
|
602
|
-
action: row.action ?? null,
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
// ── Spend caps and rate limiting ──────────────────────────────────────
|
|
606
|
-
async checkSpendCap(userId, model, amount) {
|
|
607
|
-
const rows = await this.callproc("check_spend_cap", [
|
|
608
|
-
userId,
|
|
609
|
-
model ?? null,
|
|
610
|
-
decParam(amount ?? ZERO),
|
|
611
|
-
]);
|
|
612
|
-
if (!rows || rows.length === 0) {
|
|
613
|
-
return { capped: false, currentSpend: ZERO, limit: ZERO, action: null };
|
|
614
|
-
}
|
|
615
|
-
const row = rows[0];
|
|
616
|
-
return {
|
|
617
|
-
capped: Boolean(row.capped),
|
|
618
|
-
currentSpend: dec(row.current_spend),
|
|
619
|
-
limit: dec(row.cap_limit),
|
|
620
|
-
action: row.action ?? null,
|
|
621
|
-
model: row.model ? String(row.model) : undefined,
|
|
622
|
-
};
|
|
623
|
-
}
|
|
624
|
-
// ── Refunds ──────────────────────────────────────────────────────────
|
|
625
|
-
async refundCredits(transactionId, amount, reason, metadata) {
|
|
626
|
-
const rows = await this.callproc("refund_credits", [
|
|
627
|
-
transactionId,
|
|
628
|
-
amount != null ? decParam(amount) : null,
|
|
629
|
-
reason ?? null,
|
|
630
|
-
JSON.stringify(metadata ?? {}),
|
|
631
|
-
]);
|
|
632
|
-
const row = (rows?.[0] ?? {});
|
|
633
|
-
if ("error" in row && row.error) {
|
|
634
|
-
return {
|
|
635
|
-
refundTransactionId: "",
|
|
636
|
-
originalTransactionId: transactionId,
|
|
637
|
-
userId: String(row.user_id ?? ""),
|
|
638
|
-
amount: ZERO,
|
|
639
|
-
newBalance: dec(row.new_balance),
|
|
640
|
-
error: String(row.error),
|
|
641
|
-
};
|
|
642
|
-
}
|
|
643
|
-
return {
|
|
644
|
-
refundTransactionId: String(row.refund_transaction_id ?? ""),
|
|
645
|
-
originalTransactionId: transactionId,
|
|
646
|
-
userId: String(row.user_id ?? ""),
|
|
647
|
-
amount: dec(row.amount),
|
|
648
|
-
newBalance: dec(row.new_balance),
|
|
649
|
-
tierBreakdown: decRecord(row.tier_breakdown),
|
|
650
|
-
};
|
|
651
|
-
}
|
|
652
|
-
// ── Usage analytics ──────────────────────────────────────────────────
|
|
653
|
-
async spendByUser(start, end) {
|
|
654
|
-
const rows = await this.callproc("spend_by_user", [start.toISOString(), end.toISOString()]);
|
|
655
|
-
return (rows ?? []).map((r) => {
|
|
656
|
-
const row = r;
|
|
657
|
-
return {
|
|
658
|
-
userId: String(row.user_id ?? ""),
|
|
659
|
-
totalSpend: dec(row.total_spend),
|
|
660
|
-
transactionCount: Number(row.transaction_count ?? 0),
|
|
661
|
-
};
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
async spendByModel(start, end) {
|
|
665
|
-
const rows = await this.callproc("spend_by_model", [start.toISOString(), end.toISOString()]);
|
|
666
|
-
return (rows ?? []).map((r) => {
|
|
667
|
-
const row = r;
|
|
668
|
-
return {
|
|
669
|
-
model: String(row.model ?? ""),
|
|
670
|
-
totalSpend: dec(row.total_spend),
|
|
671
|
-
transactionCount: Number(row.transaction_count ?? 0),
|
|
672
|
-
};
|
|
673
|
-
});
|
|
674
|
-
}
|
|
675
|
-
async topUsers(limit, start, end) {
|
|
676
|
-
const rows = await this.callproc("top_users", [limit, start.toISOString(), end.toISOString()]);
|
|
677
|
-
return (rows ?? []).map((r) => {
|
|
678
|
-
const row = r;
|
|
679
|
-
return {
|
|
680
|
-
userId: String(row.user_id ?? ""),
|
|
681
|
-
totalSpend: dec(row.total_spend),
|
|
682
|
-
};
|
|
683
|
-
});
|
|
684
|
-
}
|
|
685
|
-
async dailySpend(start, end) {
|
|
686
|
-
const rows = await this.callproc("daily_spend", [start.toISOString(), end.toISOString()]);
|
|
687
|
-
return (rows ?? []).map((r) => {
|
|
688
|
-
const row = r;
|
|
689
|
-
return {
|
|
690
|
-
date: String(row.date ?? ""),
|
|
691
|
-
totalSpend: dec(row.total_spend),
|
|
692
|
-
transactionCount: Number(row.transaction_count ?? 0),
|
|
693
|
-
};
|
|
694
|
-
});
|
|
695
|
-
}
|
|
696
|
-
// ── Transaction listing ─────────────────────────────────────────────
|
|
697
|
-
async listUserTransactions(userId, options) {
|
|
698
|
-
const rows = await this.callproc("list_user_transactions", [
|
|
699
|
-
userId,
|
|
700
|
-
options?.types ?? null,
|
|
701
|
-
options?.fromDate?.toISOString() ?? null,
|
|
702
|
-
options?.toDate?.toISOString() ?? null,
|
|
703
|
-
options?.limit ?? 50,
|
|
704
|
-
options?.offset ?? 0,
|
|
705
|
-
]);
|
|
706
|
-
const items = (rows ?? []).map((r) => {
|
|
707
|
-
const row = r;
|
|
708
|
-
return {
|
|
709
|
-
id: String(row.id ?? ""),
|
|
710
|
-
userId: String(row.user_id ?? ""),
|
|
711
|
-
amount: dec(row.amount),
|
|
712
|
-
type: String(row.type ?? ""),
|
|
713
|
-
referenceType: row.reference_type != null ? String(row.reference_type) : null,
|
|
714
|
-
referenceId: row.reference_id != null ? String(row.reference_id) : null,
|
|
715
|
-
metadata: row.metadata ?? null,
|
|
716
|
-
createdAt: String(row.created_at ?? ""),
|
|
717
|
-
};
|
|
718
|
-
});
|
|
719
|
-
const total = rows.length > 0 ? Number(rows[0].total_count ?? 0) : 0;
|
|
720
|
-
return { items, total };
|
|
721
|
-
}
|
|
722
|
-
async listUsageEvents(userId, options) {
|
|
723
|
-
const rows = await this.callproc("list_usage_events", [
|
|
724
|
-
userId,
|
|
725
|
-
options?.fromDate?.toISOString() ?? null,
|
|
726
|
-
options?.toDate?.toISOString() ?? null,
|
|
727
|
-
options?.limit ?? 50,
|
|
728
|
-
options?.offset ?? 0,
|
|
729
|
-
]);
|
|
730
|
-
const items = (rows ?? []).map((r) => {
|
|
731
|
-
const row = r;
|
|
732
|
-
return {
|
|
733
|
-
id: String(row.id ?? ""),
|
|
734
|
-
userId: String(row.user_id ?? ""),
|
|
735
|
-
amount: dec(row.amount),
|
|
736
|
-
type: String(row.type ?? ""),
|
|
737
|
-
referenceType: row.reference_type != null ? String(row.reference_type) : null,
|
|
738
|
-
referenceId: row.reference_id != null ? String(row.reference_id) : null,
|
|
739
|
-
metadata: row.metadata ?? null,
|
|
740
|
-
createdAt: String(row.created_at ?? ""),
|
|
741
|
-
};
|
|
742
|
-
});
|
|
743
|
-
const total = rows.length > 0 ? Number(rows[0].total_count ?? 0) : 0;
|
|
744
|
-
return { items, total };
|
|
745
|
-
}
|
|
746
|
-
// ── Aggregate stats ────────────────────────────────────────────────
|
|
747
|
-
async aggregateStats(start, end) {
|
|
748
|
-
const rows = await this.callproc("aggregate_stats", [start.toISOString(), end.toISOString()]);
|
|
749
|
-
const row = (rows?.[0] ?? {});
|
|
750
|
-
return {
|
|
751
|
-
totalCreditsConsumed: dec(row.total_credits_consumed),
|
|
752
|
-
activeUsers: Number(row.active_users ?? 0),
|
|
753
|
-
avgDailySpend: dec(row.avg_daily_spend),
|
|
754
|
-
topModel: String(row.top_model ?? ""),
|
|
755
|
-
topUser: String(row.top_user ?? ""),
|
|
756
|
-
};
|
|
757
|
-
}
|
|
758
|
-
// ── Team/shared balance pools ────────────────────────────────────────
|
|
759
|
-
async createTeam(name, initialBalance = ZERO) {
|
|
760
|
-
const rows = await this.callproc("create_team", [name, decParam(initialBalance)]);
|
|
761
|
-
const row = (rows?.[0] ?? {});
|
|
762
|
-
return {
|
|
763
|
-
teamId: String(row.team_id ?? ""),
|
|
764
|
-
name: String(row.name ?? name),
|
|
765
|
-
};
|
|
766
|
-
}
|
|
767
|
-
async getTeamBalance(teamId) {
|
|
768
|
-
const rows = await this.callproc("get_team_balance", [teamId]);
|
|
769
|
-
if (!rows || rows.length === 0) {
|
|
770
|
-
return { teamId, name: "", balance: ZERO, memberCount: 0 };
|
|
771
|
-
}
|
|
772
|
-
const row = rows[0];
|
|
773
|
-
if ("error" in row && row.error) {
|
|
774
|
-
return { teamId, name: "", balance: ZERO, memberCount: 0 };
|
|
775
|
-
}
|
|
776
|
-
return {
|
|
777
|
-
teamId: String(row.team_id ?? teamId),
|
|
778
|
-
name: String(row.name ?? ""),
|
|
779
|
-
balance: dec(row.balance),
|
|
780
|
-
memberCount: Number(row.member_count ?? 0),
|
|
781
|
-
};
|
|
782
|
-
}
|
|
783
|
-
async addTeamMember(teamId, userId, role = "member", spendCap) {
|
|
784
|
-
const rows = await this.callproc("add_team_member", [
|
|
785
|
-
teamId,
|
|
786
|
-
userId,
|
|
787
|
-
role,
|
|
788
|
-
spendCap != null ? decParam(spendCap) : null,
|
|
789
|
-
]);
|
|
790
|
-
const row = (rows?.[0] ?? {});
|
|
791
|
-
return {
|
|
792
|
-
teamId: String(row.team_id ?? teamId),
|
|
793
|
-
userId: String(row.user_id ?? userId),
|
|
794
|
-
role: String(row.role ?? role),
|
|
795
|
-
};
|
|
796
|
-
}
|
|
797
|
-
async getTeamMembers(teamId) {
|
|
798
|
-
const rows = await this.callproc("get_team_members", [teamId]);
|
|
799
|
-
return (rows ?? []).map((r) => {
|
|
800
|
-
const row = r;
|
|
801
|
-
return {
|
|
802
|
-
userId: String(row.user_id ?? ""),
|
|
803
|
-
role: String(row.role ?? "member"),
|
|
804
|
-
spendCap: row.spend_cap != null ? dec(row.spend_cap) : null,
|
|
805
|
-
totalSpent: dec(row.total_spent),
|
|
806
|
-
};
|
|
807
|
-
});
|
|
808
|
-
}
|
|
809
|
-
async deductTeam(teamId, userId, amount, metadata, idempotencyKey) {
|
|
810
|
-
const meta = { ...(metadata ?? {}) };
|
|
811
|
-
if (idempotencyKey)
|
|
812
|
-
meta.idempotency_key = idempotencyKey;
|
|
813
|
-
const rows = await this.callproc("deduct_team", [
|
|
814
|
-
teamId,
|
|
815
|
-
userId,
|
|
816
|
-
decParam(amount),
|
|
817
|
-
JSON.stringify(meta),
|
|
818
|
-
]);
|
|
819
|
-
const row = (rows?.[0] ?? {});
|
|
820
|
-
if ("error" in row && row.error) {
|
|
821
|
-
return {
|
|
822
|
-
transactionId: "",
|
|
823
|
-
teamId,
|
|
824
|
-
userId,
|
|
825
|
-
amount: ZERO,
|
|
826
|
-
teamBalanceAfter: dec(row.team_balance_after),
|
|
827
|
-
error: String(row.error),
|
|
828
|
-
};
|
|
829
|
-
}
|
|
830
|
-
return {
|
|
831
|
-
transactionId: String(row.transaction_id ?? ""),
|
|
832
|
-
teamId: String(row.team_id ?? teamId),
|
|
833
|
-
userId: String(row.user_id ?? userId),
|
|
834
|
-
amount: dec(row.amount, amount.negated()),
|
|
835
|
-
teamBalanceAfter: dec(row.team_balance_after),
|
|
836
|
-
};
|
|
837
|
-
}
|
|
838
|
-
// ── Credit expiry ────────────────────────────────────────────────────
|
|
839
|
-
async sweepExpiredCredits(dryRun = false, userId) {
|
|
840
|
-
// NOTE: `userId` is threaded through defensively as a trailing positional
|
|
841
|
-
// param to `expire_credits`. Confirm the SQL migration adds a `p_user_id`
|
|
842
|
-
// parameter (with a `DEFAULT NULL`, so the existing 1-arg call sites keep
|
|
843
|
-
// meaning "global sweep") before relying on this against a real database
|
|
844
|
-
// (see 011_lazy_expiry.sql).
|
|
845
|
-
const rows = await this.callproc("expire_credits", [dryRun, userId ?? null]);
|
|
846
|
-
const row = (rows?.[0] ?? {});
|
|
847
|
-
return {
|
|
848
|
-
expiredCount: Number(row.expired_count ?? 0),
|
|
849
|
-
expiredAmount: dec(row.expired_amount),
|
|
850
|
-
dryRun,
|
|
851
|
-
expiredByTier: decRecord(row.expired_by_tier),
|
|
852
|
-
};
|
|
853
|
-
}
|
|
854
|
-
// ── Credit tiers ─────────────────────────────────────────────────────
|
|
855
|
-
async getCreditTiers(userId) {
|
|
856
|
-
// get_user_credit_tiers returns one JSONB envelope object (not a rowset):
|
|
857
|
-
// {user_id, tiers: [...], total_balance}. callproc's scalar-JSONB
|
|
858
|
-
// unwrapping surfaces it as a single-element array.
|
|
859
|
-
const rows = await this.callproc("get_user_credit_tiers", [userId]);
|
|
860
|
-
const envelope = (rows?.[0] ?? {});
|
|
861
|
-
const tierRows = envelope.tiers ?? [];
|
|
862
|
-
const tiers = tierRows.map((row) => ({
|
|
863
|
-
tierKey: String(row.tier_key ?? ""),
|
|
864
|
-
name: String(row.name ?? ""),
|
|
865
|
-
priority: Number(row.priority ?? 0),
|
|
866
|
-
expires: Boolean(row.expires ?? false),
|
|
867
|
-
balance: dec(row.balance),
|
|
868
|
-
}));
|
|
869
|
-
return { userId, tiers, totalBalance: dec(envelope.total_balance) };
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
//# sourceMappingURL=postgres-store.js.map
|