@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,865 +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 JSON money value into an exact `Decimal`. Supabase returns NUMERIC as
|
|
8
|
-
* a JSON number or string; we coerce via `String(x)` so binary-float precision
|
|
9
|
-
* is never introduced (contract §1). `null`/`undefined` → 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(String(value));
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
return fallback;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
/** A money serialized for a JSON RPC 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 Supabase RPCs via raw HTTP (fetch).
|
|
92
|
-
*
|
|
93
|
-
* No supabase-js dependency — makes direct POST requests to the Supabase REST API.
|
|
94
|
-
*
|
|
95
|
-
* Error handling (M10 parity): network/fetch failures and JSON-decode errors are
|
|
96
|
-
* wrapped in `StoreError`; a non-2xx HTTP response throws `StoreError`. *Business*
|
|
97
|
-
* outcomes that the RPC returns as a `{"error": code}` envelope (insufficient
|
|
98
|
-
* credits, cap reached, over-refund, …) are NOT thrown — they are surfaced on the
|
|
99
|
-
* result model's `.error` field (e.g. `DeductionResult.error`), consistent with
|
|
100
|
-
* how the Postgres store and the Python SDK behave. The manager maps codes to
|
|
101
|
-
* typed exceptions.
|
|
102
|
-
*
|
|
103
|
-
* Args:
|
|
104
|
-
* url: Supabase project URL (e.g. ``https://<project>.supabase.co``).
|
|
105
|
-
* key: Supabase ``service_role`` key.
|
|
106
|
-
*/
|
|
107
|
-
export class HttpxSupabaseStore extends CreditStore {
|
|
108
|
-
url;
|
|
109
|
-
key;
|
|
110
|
-
constructor(url, key) {
|
|
111
|
-
super();
|
|
112
|
-
this.url = url.replace(/\/+$/, "");
|
|
113
|
-
this.key = key;
|
|
114
|
-
}
|
|
115
|
-
/** POST to an RPC, returning the parsed JSON body. Wraps transport/parse errors. */
|
|
116
|
-
async post(fn, params) {
|
|
117
|
-
let resp;
|
|
118
|
-
try {
|
|
119
|
-
resp = await fetch(`${this.url}/rest/v1/rpc/${fn}`, {
|
|
120
|
-
method: "POST",
|
|
121
|
-
headers: {
|
|
122
|
-
apikey: this.key,
|
|
123
|
-
authorization: `Bearer ${this.key}`,
|
|
124
|
-
"content-type": "application/json",
|
|
125
|
-
},
|
|
126
|
-
body: JSON.stringify(params),
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
catch (err) {
|
|
130
|
-
// Network / DNS / connection-refused etc.
|
|
131
|
-
throw new StoreError(`Supabase RPC ${fn} request failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
132
|
-
}
|
|
133
|
-
if (!resp.ok) {
|
|
134
|
-
let text = "";
|
|
135
|
-
try {
|
|
136
|
-
text = await resp.text();
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
// ignore body-read failures
|
|
140
|
-
}
|
|
141
|
-
throw new StoreError(`Supabase RPC ${fn} failed (${resp.status}): ${text}`);
|
|
142
|
-
}
|
|
143
|
-
try {
|
|
144
|
-
return await resp.json();
|
|
145
|
-
}
|
|
146
|
-
catch (err) {
|
|
147
|
-
throw new StoreError(`Supabase RPC ${fn} returned invalid JSON: ${err instanceof Error ? err.message : String(err)}`);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
/** RPC returning a single JSONB object. */
|
|
151
|
-
async rpc(fn, params) {
|
|
152
|
-
const data = await this.post(fn, params);
|
|
153
|
-
if (data === null || data === undefined)
|
|
154
|
-
return {};
|
|
155
|
-
if (Array.isArray(data)) {
|
|
156
|
-
const first = data[0];
|
|
157
|
-
return first != null && typeof first === "object" ? first : {};
|
|
158
|
-
}
|
|
159
|
-
if (typeof data === "object")
|
|
160
|
-
return data;
|
|
161
|
-
return { value: data };
|
|
162
|
-
}
|
|
163
|
-
/** RPC returning a set of rows. Always returns ALL rows. */
|
|
164
|
-
async rpcAll(fn, params) {
|
|
165
|
-
const data = await this.post(fn, params);
|
|
166
|
-
if (data === null || data === undefined)
|
|
167
|
-
return [];
|
|
168
|
-
if (!Array.isArray(data))
|
|
169
|
-
return [data];
|
|
170
|
-
return data.filter((r) => r != null);
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Return the business-error code if `row` is an `{"error": code}` envelope,
|
|
174
|
-
* else null. An unexpected `error` value that is not a known business code is
|
|
175
|
-
* still surfaced (callers decide), but recognised codes are the contract set.
|
|
176
|
-
*/
|
|
177
|
-
errorCode(row) {
|
|
178
|
-
if ("error" in row && row.error) {
|
|
179
|
-
return String(row.error);
|
|
180
|
-
}
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
183
|
-
async setup(_databaseUrl) {
|
|
184
|
-
throw new StoreError("HttpxSupabaseStore.setup() cannot run migrations over the REST API. Apply the " +
|
|
185
|
-
"bundled SQL migrations via the Python CLI (`bursar migrate`) or by executing " +
|
|
186
|
-
"`python/src/bursar/sql/*.sql` (in filename order) against your database.");
|
|
187
|
-
}
|
|
188
|
-
async getBalance(userId) {
|
|
189
|
-
const row = await this.rpc("get_credits_balance", { p_user_id: userId });
|
|
190
|
-
const code = this.errorCode(row);
|
|
191
|
-
if (code)
|
|
192
|
-
throw new StoreError(`get_credits_balance: ${code}`);
|
|
193
|
-
return {
|
|
194
|
-
userId: String(row.user_id ?? userId),
|
|
195
|
-
balance: dec(row.balance),
|
|
196
|
-
lifetimePurchased: dec(row.lifetime_purchased),
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
async addCredits(userId, amount, type = "adjustment", metadata, expiresAt, tier,
|
|
200
|
-
// NOTE: threaded through defensively as a new named param to the
|
|
201
|
-
// `credits_add` RPC. Confirm the SQL migration adds a `p_idempotency_key`
|
|
202
|
-
// parameter (with a default) before relying on this against a real
|
|
203
|
-
// database (see 011_lazy_expiry.sql).
|
|
204
|
-
idempotencyKey) {
|
|
205
|
-
const meta = { ...(metadata ?? {}) };
|
|
206
|
-
if (expiresAt) {
|
|
207
|
-
meta.expires_at = expiresAt instanceof Date ? expiresAt.toISOString() : String(expiresAt);
|
|
208
|
-
}
|
|
209
|
-
const row = await this.rpc("credits_add", {
|
|
210
|
-
p_user_id: userId,
|
|
211
|
-
p_amount: decParam(amount),
|
|
212
|
-
p_type: type,
|
|
213
|
-
p_metadata: meta,
|
|
214
|
-
p_tier: tier ?? null,
|
|
215
|
-
p_idempotency_key: idempotencyKey ?? null,
|
|
216
|
-
});
|
|
217
|
-
const code = this.errorCode(row);
|
|
218
|
-
if (code)
|
|
219
|
-
throw new StoreError(`credits_add: ${code}`);
|
|
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 row = await this.rpc("deduct_with_allowance", {
|
|
243
|
-
p_user_id: userId,
|
|
244
|
-
p_amount: decParam(amount),
|
|
245
|
-
p_idempotency_key: idempotencyKey,
|
|
246
|
-
p_min_balance: decParam(minBalance),
|
|
247
|
-
p_model: model,
|
|
248
|
-
p_metadata: metadata ?? {},
|
|
249
|
-
p_period_start: periodStart != null ? periodStart.toISOString().slice(0, 10) : null,
|
|
250
|
-
p_feature: feature,
|
|
251
|
-
p_feature_max_calls: featureLimit != null ? featureLimit.maxCalls : null,
|
|
252
|
-
p_feature_action: featureLimit != null ? featureLimit.action : null,
|
|
253
|
-
p_feature_period_start: featurePeriodStart != null ? featurePeriodStart.toISOString().slice(0, 10) : null,
|
|
254
|
-
p_feature_period_end: featurePeriodEnd != null ? featurePeriodEnd.toISOString().slice(0, 10) : null,
|
|
255
|
-
});
|
|
256
|
-
const code = this.errorCode(row);
|
|
257
|
-
if (code) {
|
|
258
|
-
return {
|
|
259
|
-
transactionId: "",
|
|
260
|
-
userId,
|
|
261
|
-
amount: ZERO,
|
|
262
|
-
allowanceConsumed: ZERO,
|
|
263
|
-
balanceAfter: dec(row.balance_after),
|
|
264
|
-
idempotent: false,
|
|
265
|
-
capWarning: null,
|
|
266
|
-
featureLimitWarning: null,
|
|
267
|
-
error: code,
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
return {
|
|
271
|
-
transactionId: String(row.transaction_id ?? ""),
|
|
272
|
-
userId,
|
|
273
|
-
amount: dec(row.amount),
|
|
274
|
-
allowanceConsumed: dec(row.allowance_consumed),
|
|
275
|
-
balanceAfter: dec(row.balance_after),
|
|
276
|
-
idempotent: Boolean(row.idempotent),
|
|
277
|
-
capWarning: row.cap_warning != null ? String(row.cap_warning) : null,
|
|
278
|
-
featureLimitWarning: row.feature_limit_warning != null ? String(row.feature_limit_warning) : null,
|
|
279
|
-
tierBreakdown: decRecord(row.tier_breakdown),
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
// ── Lease lifecycle (atomic admission) ─────────────────────────────
|
|
283
|
-
async createLease(userId, amount, operationType, options) {
|
|
284
|
-
const billingMode = options?.billingMode ?? "strict";
|
|
285
|
-
const floor = options?.floor ?? ZERO;
|
|
286
|
-
const overdraftFloor = options?.overdraftFloor ?? null;
|
|
287
|
-
const periodStart = options?.periodStart ?? null;
|
|
288
|
-
const feature = options?.feature ?? null;
|
|
289
|
-
const featureLimit = options?.featureLimit ?? null;
|
|
290
|
-
const featurePeriodStart = options?.featurePeriodStart ?? null;
|
|
291
|
-
const featurePeriodEnd = featureLimit != null && featurePeriodStart != null
|
|
292
|
-
? featureWindowEnd(featurePeriodStart, featureLimit.period)
|
|
293
|
-
: null;
|
|
294
|
-
const row = await this.rpc("create_lease", {
|
|
295
|
-
p_user_id: userId,
|
|
296
|
-
p_amount: decParam(amount),
|
|
297
|
-
p_operation_type: operationType,
|
|
298
|
-
p_billing_mode: billingMode,
|
|
299
|
-
p_floor: decParam(floor),
|
|
300
|
-
p_max_concurrent: options?.maxConcurrent ?? null,
|
|
301
|
-
p_ttl_seconds: options?.ttlSeconds ?? 600,
|
|
302
|
-
p_model: options?.model ?? null,
|
|
303
|
-
p_overdraft_floor: overdraftFloor != null ? decParam(overdraftFloor) : null,
|
|
304
|
-
p_metadata: options?.metadata ?? {},
|
|
305
|
-
p_period_start: periodStart != null ? periodStart.toISOString().slice(0, 10) : null,
|
|
306
|
-
p_feature: feature,
|
|
307
|
-
p_feature_max_calls: featureLimit != null ? featureLimit.maxCalls : null,
|
|
308
|
-
p_feature_action: featureLimit != null ? featureLimit.action : null,
|
|
309
|
-
p_feature_period_start: featurePeriodStart != null ? featurePeriodStart.toISOString().slice(0, 10) : null,
|
|
310
|
-
p_feature_period_end: featurePeriodEnd != null ? featurePeriodEnd.toISOString().slice(0, 10) : null,
|
|
311
|
-
});
|
|
312
|
-
const code = this.errorCode(row);
|
|
313
|
-
if (code) {
|
|
314
|
-
return {
|
|
315
|
-
leaseId: "",
|
|
316
|
-
userId,
|
|
317
|
-
amount: ZERO,
|
|
318
|
-
available: dec(row.available),
|
|
319
|
-
reservedTotal: dec(row.reserved),
|
|
320
|
-
billingMode,
|
|
321
|
-
expiresAt: "",
|
|
322
|
-
error: code,
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
return {
|
|
326
|
-
leaseId: String(row.lease_id ?? ""),
|
|
327
|
-
userId: String(row.user_id ?? userId),
|
|
328
|
-
amount: dec(row.amount),
|
|
329
|
-
available: dec(row.available),
|
|
330
|
-
reservedTotal: dec(row.reserved),
|
|
331
|
-
billingMode: String(row.billing_mode ?? billingMode) ?? billingMode,
|
|
332
|
-
expiresAt: String(row.expires_at ?? ""),
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
async settleLease(userId, leaseId, amount, options) {
|
|
336
|
-
const minBalance = options?.minBalance ?? ZERO;
|
|
337
|
-
const periodStart = options?.periodStart ?? null;
|
|
338
|
-
const feature = options?.feature ?? null;
|
|
339
|
-
const featureLimit = options?.featureLimit ?? null;
|
|
340
|
-
const featurePeriodStart = options?.featurePeriodStart ?? null;
|
|
341
|
-
const featurePeriodEnd = featureLimit != null && featurePeriodStart != null
|
|
342
|
-
? featureWindowEnd(featurePeriodStart, featureLimit.period)
|
|
343
|
-
: null;
|
|
344
|
-
const row = await this.rpc("settle_lease", {
|
|
345
|
-
p_user_id: userId,
|
|
346
|
-
p_lease_id: leaseId,
|
|
347
|
-
p_amount: decParam(amount),
|
|
348
|
-
p_idempotency_key: options?.idempotencyKey ?? null,
|
|
349
|
-
p_min_balance: decParam(minBalance),
|
|
350
|
-
p_model: options?.model ?? null,
|
|
351
|
-
p_metadata: options?.metadata ?? {},
|
|
352
|
-
p_period_start: periodStart != null ? periodStart.toISOString().slice(0, 10) : null,
|
|
353
|
-
p_feature: feature,
|
|
354
|
-
p_feature_max_calls: featureLimit != null ? featureLimit.maxCalls : null,
|
|
355
|
-
p_feature_action: featureLimit != null ? featureLimit.action : null,
|
|
356
|
-
p_feature_period_start: featurePeriodStart != null ? featurePeriodStart.toISOString().slice(0, 10) : null,
|
|
357
|
-
p_feature_period_end: featurePeriodEnd != null ? featurePeriodEnd.toISOString().slice(0, 10) : null,
|
|
358
|
-
});
|
|
359
|
-
const code = this.errorCode(row);
|
|
360
|
-
if (code) {
|
|
361
|
-
return {
|
|
362
|
-
transactionId: "",
|
|
363
|
-
userId,
|
|
364
|
-
amount: ZERO,
|
|
365
|
-
allowanceConsumed: ZERO,
|
|
366
|
-
balanceAfter: dec(row.balance_after),
|
|
367
|
-
idempotent: false,
|
|
368
|
-
capWarning: null,
|
|
369
|
-
featureLimitWarning: null,
|
|
370
|
-
error: code,
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
|
-
return {
|
|
374
|
-
transactionId: String(row.transaction_id ?? ""),
|
|
375
|
-
userId,
|
|
376
|
-
amount: dec(row.amount),
|
|
377
|
-
allowanceConsumed: dec(row.allowance_consumed),
|
|
378
|
-
balanceAfter: dec(row.balance_after),
|
|
379
|
-
idempotent: Boolean(row.idempotent),
|
|
380
|
-
capWarning: row.cap_warning != null ? String(row.cap_warning) : null,
|
|
381
|
-
featureLimitWarning: row.feature_limit_warning != null ? String(row.feature_limit_warning) : null,
|
|
382
|
-
tierBreakdown: decRecord(row.tier_breakdown),
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
async releaseLease(userId, leaseId) {
|
|
386
|
-
const row = await this.rpc("release_lease", { p_user_id: userId, p_lease_id: leaseId });
|
|
387
|
-
return {
|
|
388
|
-
leaseId,
|
|
389
|
-
userId,
|
|
390
|
-
released: Boolean(row.released),
|
|
391
|
-
reason: row.reason != null ? String(row.reason) : null,
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
async renewLease(userId, leaseId, ttlSeconds) {
|
|
395
|
-
const row = await this.rpc("renew_lease", {
|
|
396
|
-
p_user_id: userId,
|
|
397
|
-
p_lease_id: leaseId,
|
|
398
|
-
p_ttl_seconds: ttlSeconds,
|
|
399
|
-
});
|
|
400
|
-
const code = this.errorCode(row);
|
|
401
|
-
if (code) {
|
|
402
|
-
return {
|
|
403
|
-
leaseId,
|
|
404
|
-
userId,
|
|
405
|
-
amount: ZERO,
|
|
406
|
-
available: ZERO,
|
|
407
|
-
reservedTotal: ZERO,
|
|
408
|
-
billingMode: "strict",
|
|
409
|
-
expiresAt: "",
|
|
410
|
-
error: code,
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
return {
|
|
414
|
-
leaseId: String(row.lease_id ?? leaseId),
|
|
415
|
-
userId,
|
|
416
|
-
amount: dec(row.amount),
|
|
417
|
-
available: dec(row.available),
|
|
418
|
-
reservedTotal: dec(row.reserved),
|
|
419
|
-
billingMode: String(row.billing_mode ?? "strict"),
|
|
420
|
-
expiresAt: String(row.expires_at ?? ""),
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
async getAvailable(userId) {
|
|
424
|
-
const row = await this.rpc("get_available_credits", { p_user_id: userId });
|
|
425
|
-
return {
|
|
426
|
-
userId,
|
|
427
|
-
balance: dec(row.balance),
|
|
428
|
-
reserved: dec(row.reserved),
|
|
429
|
-
available: dec(row.available),
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
async getActivePricing() {
|
|
433
|
-
const row = await this.rpc("get_active_pricing_config", {});
|
|
434
|
-
if (!row || Object.keys(row).length === 0)
|
|
435
|
-
return null;
|
|
436
|
-
const code = this.errorCode(row);
|
|
437
|
-
if (code)
|
|
438
|
-
throw new StoreError(`get_active_pricing_config: ${code}`);
|
|
439
|
-
return row;
|
|
440
|
-
}
|
|
441
|
-
async setActivePricing(config, label) {
|
|
442
|
-
const row = await this.rpc("set_active_pricing_config", {
|
|
443
|
-
p_config: config,
|
|
444
|
-
p_label: label ?? null,
|
|
445
|
-
});
|
|
446
|
-
const code = this.errorCode(row);
|
|
447
|
-
if (code)
|
|
448
|
-
throw new StoreError(`set_active_pricing_config: ${code}`);
|
|
449
|
-
return String(row.id ?? "");
|
|
450
|
-
}
|
|
451
|
-
// H8: pricing history / activation — mirrors Python base.py:293-312.
|
|
452
|
-
async getPricingHistory() {
|
|
453
|
-
const rows = await this.rpc("get_pricing_history", {});
|
|
454
|
-
if (!rows)
|
|
455
|
-
return [];
|
|
456
|
-
const arr = Array.isArray(rows) ? rows : [rows];
|
|
457
|
-
return arr.map((r) => ({
|
|
458
|
-
id: String(r.id ?? ""),
|
|
459
|
-
version: Number(r.version ?? 0),
|
|
460
|
-
label: r.label ?? null,
|
|
461
|
-
active: Boolean(r.active ?? false),
|
|
462
|
-
createdAt: String(r.created_at ?? ""),
|
|
463
|
-
}));
|
|
464
|
-
}
|
|
465
|
-
async getPricingConfig(version) {
|
|
466
|
-
const row = await this.rpc("get_pricing_config", { p_version: version });
|
|
467
|
-
if (!row || Object.keys(row).length === 0)
|
|
468
|
-
return null;
|
|
469
|
-
const code = this.errorCode(row);
|
|
470
|
-
if (code)
|
|
471
|
-
throw new StoreError(`get_pricing_config: ${code}`);
|
|
472
|
-
return {
|
|
473
|
-
id: String(row.id ?? ""),
|
|
474
|
-
config: row.config,
|
|
475
|
-
version: Number(row.version ?? version),
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
async activatePricing(version) {
|
|
479
|
-
const row = await this.rpc("activate_pricing", { p_version: version });
|
|
480
|
-
const code = this.errorCode(row);
|
|
481
|
-
if (code)
|
|
482
|
-
throw new StoreError(`activate_pricing: ${code}`);
|
|
483
|
-
return String(row.id ?? "");
|
|
484
|
-
}
|
|
485
|
-
// ── Plan management ────────────────────────────────────────────────
|
|
486
|
-
async getUserPlan(userId) {
|
|
487
|
-
const row = await this.rpc("get_user_plan", { p_user_id: userId });
|
|
488
|
-
if (!row || Object.keys(row).length === 0) {
|
|
489
|
-
return { userId, planId: null, planName: null, freeAllowance: ZERO, features: {} };
|
|
490
|
-
}
|
|
491
|
-
const code = this.errorCode(row);
|
|
492
|
-
if (code)
|
|
493
|
-
throw new StoreError(`get_user_plan: ${code}`);
|
|
494
|
-
return {
|
|
495
|
-
userId: String(row.user_id ?? userId),
|
|
496
|
-
planId: row.plan_id ?? null,
|
|
497
|
-
planName: row.plan_name ?? null,
|
|
498
|
-
freeAllowance: dec(row.free_allowance),
|
|
499
|
-
features: row.features ?? {},
|
|
500
|
-
featureLimits: parseFeatureLimits(row.feature_limits),
|
|
501
|
-
defaultBillingMode: String(row.default_billing_mode ?? "strict") ?? "strict",
|
|
502
|
-
perOperation: parsePerOperation(row.per_operation),
|
|
503
|
-
maxConcurrent: row.max_concurrent != null ? Number(row.max_concurrent) : null,
|
|
504
|
-
overdraftFloor: row.overdraft_floor != null ? dec(row.overdraft_floor) : null,
|
|
505
|
-
allowancePeriod: row.allowance_period ?? "calendar_month",
|
|
506
|
-
planAssignedAt: row.plan_assigned_at != null ? new Date(String(row.plan_assigned_at)) : null,
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
async checkFeature(userId, feature) {
|
|
510
|
-
const plan = await this.getUserPlan(userId);
|
|
511
|
-
const present = Object.prototype.hasOwnProperty.call(plan.features, feature);
|
|
512
|
-
const value = present ? plan.features[feature] : null;
|
|
513
|
-
return {
|
|
514
|
-
userId,
|
|
515
|
-
feature,
|
|
516
|
-
value,
|
|
517
|
-
// M6: presence-vs-truthiness — numeric 0 / "" count as present.
|
|
518
|
-
hasFeature: present && value !== null && value !== undefined && value !== false,
|
|
519
|
-
};
|
|
520
|
-
}
|
|
521
|
-
async setUserPlan(userId, planId) {
|
|
522
|
-
const row = await this.rpc("set_user_plan", {
|
|
523
|
-
p_user_id: userId,
|
|
524
|
-
p_plan_key: planId,
|
|
525
|
-
});
|
|
526
|
-
const code = this.errorCode(row);
|
|
527
|
-
if (code)
|
|
528
|
-
throw new StoreError(`set_user_plan: ${code}`);
|
|
529
|
-
return {
|
|
530
|
-
userId: String(row.user_id ?? userId),
|
|
531
|
-
planId: String(row.plan_id ?? planId),
|
|
532
|
-
};
|
|
533
|
-
}
|
|
534
|
-
async checkAllowance(userId, periodStart) {
|
|
535
|
-
const row = await this.rpc("check_plan_allowance", {
|
|
536
|
-
p_user_id: userId,
|
|
537
|
-
p_period_start: periodStart != null ? periodStart.toISOString().slice(0, 10) : null,
|
|
538
|
-
});
|
|
539
|
-
if (!row || Object.keys(row).length === 0) {
|
|
540
|
-
return { planId: "", allowanceRemaining: ZERO, periodStart: "", periodEnd: "" };
|
|
541
|
-
}
|
|
542
|
-
const code = this.errorCode(row);
|
|
543
|
-
if (code)
|
|
544
|
-
throw new StoreError(`check_plan_allowance: ${code}`);
|
|
545
|
-
return {
|
|
546
|
-
planId: String(row.plan_id ?? ""),
|
|
547
|
-
allowanceRemaining: dec(row.allowance_remaining),
|
|
548
|
-
periodStart: String(row.period_start ?? ""),
|
|
549
|
-
periodEnd: String(row.period_end ?? ""),
|
|
550
|
-
};
|
|
551
|
-
}
|
|
552
|
-
async incrementUsageWindow(userId, planId, amount) {
|
|
553
|
-
const row = await this.rpc("increment_usage_window", {
|
|
554
|
-
p_user_id: userId,
|
|
555
|
-
p_plan_id: planId,
|
|
556
|
-
p_amount: decParam(amount),
|
|
557
|
-
});
|
|
558
|
-
const code = this.errorCode(row);
|
|
559
|
-
if (code)
|
|
560
|
-
throw new StoreError(`increment_usage_window: ${code}`);
|
|
561
|
-
}
|
|
562
|
-
/** Advisory, non-locking read of invocation-count usage (UI only). Mirrors `checkSpendCap`. */
|
|
563
|
-
async checkFeatureLimit(userId, feature, maxCalls, periodStart, periodEnd) {
|
|
564
|
-
const row = await this.rpc("check_feature_limit", {
|
|
565
|
-
p_user_id: userId,
|
|
566
|
-
p_feature: feature,
|
|
567
|
-
p_max_calls: maxCalls,
|
|
568
|
-
p_period_start: periodStart.toISOString().slice(0, 10),
|
|
569
|
-
p_period_end: periodEnd.toISOString().slice(0, 10),
|
|
570
|
-
});
|
|
571
|
-
if (!row || Object.keys(row).length === 0) {
|
|
572
|
-
return {
|
|
573
|
-
userId,
|
|
574
|
-
feature,
|
|
575
|
-
limited: false,
|
|
576
|
-
limit: 0,
|
|
577
|
-
used: 0,
|
|
578
|
-
remaining: 0,
|
|
579
|
-
periodStart: "",
|
|
580
|
-
periodEnd: "",
|
|
581
|
-
action: null,
|
|
582
|
-
};
|
|
583
|
-
}
|
|
584
|
-
const code = this.errorCode(row);
|
|
585
|
-
if (code)
|
|
586
|
-
throw new StoreError(`check_feature_limit: ${code}`);
|
|
587
|
-
return {
|
|
588
|
-
userId: String(row.user_id ?? userId),
|
|
589
|
-
feature: String(row.feature ?? feature),
|
|
590
|
-
limited: Boolean(row.limited ?? true),
|
|
591
|
-
limit: Number(row.limit ?? maxCalls),
|
|
592
|
-
used: Number(row.used ?? 0),
|
|
593
|
-
remaining: Number(row.remaining ?? Math.max(maxCalls - Number(row.used ?? 0), 0)),
|
|
594
|
-
periodStart: String(row.period_start ?? ""),
|
|
595
|
-
periodEnd: String(row.period_end ?? ""),
|
|
596
|
-
action: row.action ?? null,
|
|
597
|
-
};
|
|
598
|
-
}
|
|
599
|
-
// ── Spend caps and rate limiting ──────────────────────────────────────
|
|
600
|
-
async checkSpendCap(userId, model, amount) {
|
|
601
|
-
const row = await this.rpc("check_spend_cap", {
|
|
602
|
-
p_user_id: userId,
|
|
603
|
-
p_model: model ?? null,
|
|
604
|
-
p_amount: decParam(amount ?? ZERO),
|
|
605
|
-
});
|
|
606
|
-
if (!row || Object.keys(row).length === 0) {
|
|
607
|
-
return { capped: false, currentSpend: ZERO, limit: ZERO, action: null };
|
|
608
|
-
}
|
|
609
|
-
return {
|
|
610
|
-
capped: Boolean(row.capped),
|
|
611
|
-
currentSpend: dec(row.current_spend),
|
|
612
|
-
limit: dec(row.cap_limit),
|
|
613
|
-
action: row.action ?? null,
|
|
614
|
-
model: row.model ? String(row.model) : undefined,
|
|
615
|
-
};
|
|
616
|
-
}
|
|
617
|
-
// ── Refunds ──────────────────────────────────────────────────────────
|
|
618
|
-
async refundCredits(transactionId, amount, reason, metadata) {
|
|
619
|
-
const row = await this.rpc("refund_credits", {
|
|
620
|
-
p_transaction_id: transactionId,
|
|
621
|
-
p_amount: amount != null ? decParam(amount) : null,
|
|
622
|
-
p_reason: reason ?? null,
|
|
623
|
-
p_metadata: metadata ?? {},
|
|
624
|
-
});
|
|
625
|
-
const code = this.errorCode(row);
|
|
626
|
-
if (code) {
|
|
627
|
-
return {
|
|
628
|
-
refundTransactionId: "",
|
|
629
|
-
originalTransactionId: transactionId,
|
|
630
|
-
userId: String(row.user_id ?? ""),
|
|
631
|
-
amount: ZERO,
|
|
632
|
-
newBalance: dec(row.new_balance),
|
|
633
|
-
error: code,
|
|
634
|
-
};
|
|
635
|
-
}
|
|
636
|
-
return {
|
|
637
|
-
refundTransactionId: String(row.refund_transaction_id ?? ""),
|
|
638
|
-
originalTransactionId: transactionId,
|
|
639
|
-
userId: String(row.user_id ?? ""),
|
|
640
|
-
amount: dec(row.amount),
|
|
641
|
-
newBalance: dec(row.new_balance),
|
|
642
|
-
tierBreakdown: decRecord(row.tier_breakdown),
|
|
643
|
-
};
|
|
644
|
-
}
|
|
645
|
-
// ── Usage analytics ──────────────────────────────────────────────────
|
|
646
|
-
async spendByUser(start, end) {
|
|
647
|
-
const rows = await this.rpcAll("spend_by_user", {
|
|
648
|
-
p_start: start.toISOString(),
|
|
649
|
-
p_end: end.toISOString(),
|
|
650
|
-
});
|
|
651
|
-
return rows.map((row) => ({
|
|
652
|
-
userId: String(row.user_id ?? ""),
|
|
653
|
-
totalSpend: dec(row.total_spend),
|
|
654
|
-
transactionCount: Number(row.transaction_count ?? 0),
|
|
655
|
-
}));
|
|
656
|
-
}
|
|
657
|
-
async spendByModel(start, end) {
|
|
658
|
-
const rows = await this.rpcAll("spend_by_model", {
|
|
659
|
-
p_start: start.toISOString(),
|
|
660
|
-
p_end: end.toISOString(),
|
|
661
|
-
});
|
|
662
|
-
return rows.map((row) => ({
|
|
663
|
-
model: String(row.model ?? ""),
|
|
664
|
-
totalSpend: dec(row.total_spend),
|
|
665
|
-
transactionCount: Number(row.transaction_count ?? 0),
|
|
666
|
-
}));
|
|
667
|
-
}
|
|
668
|
-
async topUsers(limit, start, end) {
|
|
669
|
-
const rows = await this.rpcAll("top_users", {
|
|
670
|
-
p_limit: limit,
|
|
671
|
-
p_start: start.toISOString(),
|
|
672
|
-
p_end: end.toISOString(),
|
|
673
|
-
});
|
|
674
|
-
return rows.map((row) => ({
|
|
675
|
-
userId: String(row.user_id ?? ""),
|
|
676
|
-
totalSpend: dec(row.total_spend),
|
|
677
|
-
}));
|
|
678
|
-
}
|
|
679
|
-
async dailySpend(start, end) {
|
|
680
|
-
const rows = await this.rpcAll("daily_spend", {
|
|
681
|
-
p_start: start.toISOString(),
|
|
682
|
-
p_end: end.toISOString(),
|
|
683
|
-
});
|
|
684
|
-
return rows.map((row) => ({
|
|
685
|
-
date: String(row.date ?? ""),
|
|
686
|
-
totalSpend: dec(row.total_spend),
|
|
687
|
-
transactionCount: Number(row.transaction_count ?? 0),
|
|
688
|
-
}));
|
|
689
|
-
}
|
|
690
|
-
// ── Transaction listing ─────────────────────────────────────────────
|
|
691
|
-
async listUserTransactions(userId, options) {
|
|
692
|
-
const rows = await this.rpcAll("list_user_transactions", {
|
|
693
|
-
p_user_id: userId,
|
|
694
|
-
p_types: options?.types ?? null,
|
|
695
|
-
p_from_date: options?.fromDate?.toISOString() ?? null,
|
|
696
|
-
p_to_date: options?.toDate?.toISOString() ?? null,
|
|
697
|
-
p_limit: options?.limit ?? 50,
|
|
698
|
-
p_offset: options?.offset ?? 0,
|
|
699
|
-
});
|
|
700
|
-
const items = rows.map((r) => ({
|
|
701
|
-
id: String(r.id ?? ""),
|
|
702
|
-
userId: String(r.user_id ?? ""),
|
|
703
|
-
amount: dec(r.amount),
|
|
704
|
-
type: String(r.type ?? ""),
|
|
705
|
-
referenceType: r.reference_type != null ? String(r.reference_type) : null,
|
|
706
|
-
referenceId: r.reference_id != null ? String(r.reference_id) : null,
|
|
707
|
-
metadata: r.metadata ?? null,
|
|
708
|
-
createdAt: String(r.created_at ?? ""),
|
|
709
|
-
}));
|
|
710
|
-
const total = rows.length > 0 ? Number(rows[0].total_count ?? 0) : 0;
|
|
711
|
-
return { items, total };
|
|
712
|
-
}
|
|
713
|
-
async listUsageEvents(userId, options) {
|
|
714
|
-
const rows = await this.rpcAll("list_usage_events", {
|
|
715
|
-
p_user_id: userId,
|
|
716
|
-
p_from_date: options?.fromDate?.toISOString() ?? null,
|
|
717
|
-
p_to_date: options?.toDate?.toISOString() ?? null,
|
|
718
|
-
p_limit: options?.limit ?? 50,
|
|
719
|
-
p_offset: options?.offset ?? 0,
|
|
720
|
-
});
|
|
721
|
-
const items = rows.map((r) => ({
|
|
722
|
-
id: String(r.id ?? ""),
|
|
723
|
-
userId: String(r.user_id ?? ""),
|
|
724
|
-
amount: dec(r.amount),
|
|
725
|
-
type: String(r.type ?? ""),
|
|
726
|
-
referenceType: r.reference_type != null ? String(r.reference_type) : null,
|
|
727
|
-
referenceId: r.reference_id != null ? String(r.reference_id) : null,
|
|
728
|
-
metadata: r.metadata ?? null,
|
|
729
|
-
createdAt: String(r.created_at ?? ""),
|
|
730
|
-
}));
|
|
731
|
-
const total = rows.length > 0 ? Number(rows[0].total_count ?? 0) : 0;
|
|
732
|
-
return { items, total };
|
|
733
|
-
}
|
|
734
|
-
// ── Aggregate stats ────────────────────────────────────────────────
|
|
735
|
-
async aggregateStats(start, end) {
|
|
736
|
-
const row = await this.rpc("aggregate_stats", {
|
|
737
|
-
p_start: start.toISOString(),
|
|
738
|
-
p_end: end.toISOString(),
|
|
739
|
-
});
|
|
740
|
-
return {
|
|
741
|
-
totalCreditsConsumed: dec(row.total_credits_consumed),
|
|
742
|
-
activeUsers: Number(row.active_users ?? 0),
|
|
743
|
-
avgDailySpend: dec(row.avg_daily_spend),
|
|
744
|
-
topModel: String(row.top_model ?? ""),
|
|
745
|
-
topUser: String(row.top_user ?? ""),
|
|
746
|
-
};
|
|
747
|
-
}
|
|
748
|
-
// ── Team/shared balance pools ────────────────────────────────────────
|
|
749
|
-
async createTeam(name, initialBalance = ZERO) {
|
|
750
|
-
const row = await this.rpc("create_team", {
|
|
751
|
-
p_name: name,
|
|
752
|
-
p_initial_balance: decParam(initialBalance),
|
|
753
|
-
});
|
|
754
|
-
const code = this.errorCode(row);
|
|
755
|
-
if (code)
|
|
756
|
-
throw new StoreError(`create_team: ${code}`);
|
|
757
|
-
return {
|
|
758
|
-
teamId: String(row.team_id ?? ""),
|
|
759
|
-
name: String(row.name ?? name),
|
|
760
|
-
};
|
|
761
|
-
}
|
|
762
|
-
async getTeamBalance(teamId) {
|
|
763
|
-
const row = await this.rpc("get_team_balance", { p_team_id: teamId });
|
|
764
|
-
if (!row || Object.keys(row).length === 0 || ("error" in row && row.error)) {
|
|
765
|
-
return { teamId, name: "", balance: ZERO, memberCount: 0 };
|
|
766
|
-
}
|
|
767
|
-
return {
|
|
768
|
-
teamId: String(row.team_id ?? teamId),
|
|
769
|
-
name: String(row.name ?? ""),
|
|
770
|
-
balance: dec(row.balance),
|
|
771
|
-
memberCount: Number(row.member_count ?? 0),
|
|
772
|
-
};
|
|
773
|
-
}
|
|
774
|
-
async addTeamMember(teamId, userId, role = "member", spendCap) {
|
|
775
|
-
const row = await this.rpc("add_team_member", {
|
|
776
|
-
p_team_id: teamId,
|
|
777
|
-
p_user_id: userId,
|
|
778
|
-
p_role: role,
|
|
779
|
-
p_spend_cap: spendCap != null ? decParam(spendCap) : null,
|
|
780
|
-
});
|
|
781
|
-
const code = this.errorCode(row);
|
|
782
|
-
if (code)
|
|
783
|
-
throw new StoreError(`add_team_member: ${code}`);
|
|
784
|
-
return {
|
|
785
|
-
teamId: String(row.team_id ?? teamId),
|
|
786
|
-
userId: String(row.user_id ?? userId),
|
|
787
|
-
role: String(row.role ?? role),
|
|
788
|
-
};
|
|
789
|
-
}
|
|
790
|
-
async getTeamMembers(teamId) {
|
|
791
|
-
const rows = await this.rpcAll("get_team_members", { p_team_id: teamId });
|
|
792
|
-
return rows.map((row) => ({
|
|
793
|
-
userId: String(row.user_id ?? ""),
|
|
794
|
-
role: String(row.role ?? "member"),
|
|
795
|
-
spendCap: row.spend_cap != null ? dec(row.spend_cap) : null,
|
|
796
|
-
totalSpent: dec(row.total_spent),
|
|
797
|
-
}));
|
|
798
|
-
}
|
|
799
|
-
async deductTeam(teamId, userId, amount, metadata, idempotencyKey) {
|
|
800
|
-
const meta = { ...(metadata ?? {}) };
|
|
801
|
-
if (idempotencyKey)
|
|
802
|
-
meta.idempotency_key = idempotencyKey;
|
|
803
|
-
const row = await this.rpc("deduct_team", {
|
|
804
|
-
p_team_id: teamId,
|
|
805
|
-
p_user_id: userId,
|
|
806
|
-
p_amount: decParam(amount),
|
|
807
|
-
p_metadata: meta,
|
|
808
|
-
});
|
|
809
|
-
const code = this.errorCode(row);
|
|
810
|
-
if (code) {
|
|
811
|
-
return {
|
|
812
|
-
transactionId: "",
|
|
813
|
-
teamId,
|
|
814
|
-
userId,
|
|
815
|
-
amount: ZERO,
|
|
816
|
-
teamBalanceAfter: dec(row.team_balance_after),
|
|
817
|
-
error: code,
|
|
818
|
-
};
|
|
819
|
-
}
|
|
820
|
-
return {
|
|
821
|
-
transactionId: String(row.transaction_id ?? ""),
|
|
822
|
-
teamId: String(row.team_id ?? teamId),
|
|
823
|
-
userId: String(row.user_id ?? userId),
|
|
824
|
-
amount: dec(row.amount, amount.negated()),
|
|
825
|
-
teamBalanceAfter: dec(row.team_balance_after),
|
|
826
|
-
};
|
|
827
|
-
}
|
|
828
|
-
// ── Credit expiry ────────────────────────────────────────────────────
|
|
829
|
-
async sweepExpiredCredits(dryRun = false, userId) {
|
|
830
|
-
// NOTE: `userId` is threaded through defensively as a new named param to
|
|
831
|
-
// `expire_credits`. Confirm the SQL migration adds a `p_user_id` parameter
|
|
832
|
-
// (with a default of NULL, meaning "global sweep") before relying on this
|
|
833
|
-
// against a real database (see 011_lazy_expiry.sql).
|
|
834
|
-
const row = await this.rpc("expire_credits", {
|
|
835
|
-
p_dry_run: dryRun,
|
|
836
|
-
p_user_id: userId ?? null,
|
|
837
|
-
});
|
|
838
|
-
const code = this.errorCode(row);
|
|
839
|
-
if (code)
|
|
840
|
-
throw new StoreError(`expire_credits: ${code}`);
|
|
841
|
-
return {
|
|
842
|
-
expiredCount: Number(row.expired_count ?? 0),
|
|
843
|
-
expiredAmount: dec(row.expired_amount),
|
|
844
|
-
dryRun,
|
|
845
|
-
expiredByTier: decRecord(row.expired_by_tier),
|
|
846
|
-
};
|
|
847
|
-
}
|
|
848
|
-
// ── Credit tiers ─────────────────────────────────────────────────────
|
|
849
|
-
async getCreditTiers(userId) {
|
|
850
|
-
// get_user_credit_tiers returns one JSONB envelope object (not a rowset):
|
|
851
|
-
// {user_id, tiers: [...], total_balance} — use rpc() (single-object), not
|
|
852
|
-
// rpcAll() (which is for genuine SETOF/TABLE-returning functions).
|
|
853
|
-
const envelope = await this.rpc("get_user_credit_tiers", { p_user_id: userId });
|
|
854
|
-
const tierRows = envelope.tiers ?? [];
|
|
855
|
-
const tiers = tierRows.map((row) => ({
|
|
856
|
-
tierKey: String(row.tier_key ?? ""),
|
|
857
|
-
name: String(row.name ?? ""),
|
|
858
|
-
priority: Number(row.priority ?? 0),
|
|
859
|
-
expires: Boolean(row.expires ?? false),
|
|
860
|
-
balance: dec(row.balance),
|
|
861
|
-
}));
|
|
862
|
-
return { userId, tiers, totalBalance: dec(envelope.total_balance) };
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
//# sourceMappingURL=supabase-store.js.map
|