@zonastery/bursar 0.0.2 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -333
- 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 +45 -13
- package/dist/billing/billing-store.d.ts.map +1 -1
- package/dist/billing/billing-store.js.map +1 -1
- 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 +684 -0
- package/dist/billing/event-handlers.js.map +1 -0
- package/dist/billing/event-processor.d.ts +20 -0
- package/dist/billing/event-processor.d.ts.map +1 -0
- package/dist/billing/event-processor.js +131 -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 +9 -5
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js +4 -4
- package/dist/billing/index.js.map +1 -1
- 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 +180 -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 +45 -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 +89 -0
- package/dist/billing/postgres/store.d.ts.map +1 -0
- package/dist/billing/postgres/store.js +542 -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 +84 -0
- package/dist/bursar.d.ts.map +1 -0
- package/dist/bursar.js +137 -0
- package/dist/bursar.js.map +1 -0
- package/dist/catalog.d.ts +62 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +96 -0
- package/dist/catalog.js.map +1 -0
- package/dist/commerce/errors.d.ts +68 -0
- package/dist/commerce/errors.d.ts.map +1 -0
- package/dist/commerce/errors.js +105 -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 +1016 -0
- package/dist/commerce/service.js.map +1 -0
- package/dist/commerce/types.d.ts +225 -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 +142 -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 +340 -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 +12 -23
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +141 -377
- 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 +81 -0
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/analytics.js +153 -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 +64 -0
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/deduction.js +154 -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 +110 -0
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/plan.js +194 -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 +76 -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 +89 -0
- package/dist/credits/postgres/store.d.ts.map +1 -0
- package/dist/credits/postgres/store.js +838 -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 +102 -0
- package/dist/credits/postgres/value-mappers.js.map +1 -0
- package/dist/credits/pricing-runtime.d.ts +35 -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 +220 -0
- package/dist/credits/service.d.ts.map +1 -0
- package/dist/credits/service.js +640 -0
- package/dist/credits/service.js.map +1 -0
- package/dist/credits/store.d.ts +172 -0
- package/dist/credits/store.d.ts.map +1 -0
- package/dist/{stores/credit-store.js → credits/store.js} +27 -43
- 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 +71 -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 +87 -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 +133 -25
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +173 -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 +2792 -0
- package/dist/generated/pricing-config.types.d.ts +685 -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 +23 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -10
- package/dist/index.js.map +1 -1
- package/dist/load-pricing-file.d.ts.map +1 -1
- package/dist/load-pricing-file.js +8 -4
- 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/metrics.js.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 +52 -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 +5 -3
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +213 -38
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/index.d.ts +1 -0
- package/dist/providers/dodo/index.d.ts.map +1 -1
- package/dist/providers/dodo/index.js.map +1 -1
- package/dist/providers/dodo/provider.d.ts +24 -14
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +249 -31
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/index.d.ts +3 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/mock/index.js.map +1 -1
- package/dist/providers/mock/provider.d.ts +14 -10
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +78 -10
- package/dist/providers/mock/provider.js.map +1 -1
- package/dist/providers/stripe/event-mapper.d.ts +3 -3
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
- package/dist/providers/stripe/event-mapper.js +139 -30
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/index.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts +21 -12
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +175 -17
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts +128 -32
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js +17 -1
- package/dist/providers/types.js.map +1 -1
- package/dist/retry.d.ts +35 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +70 -0
- package/dist/retry.js.map +1 -0
- package/dist/shared/diagnostics.d.ts +7 -0
- package/dist/shared/diagnostics.d.ts.map +1 -0
- package/dist/shared/diagnostics.js +18 -0
- package/dist/shared/diagnostics.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 +84 -0
- package/dist/shared/postgres-client.d.ts.map +1 -0
- package/dist/shared/postgres-client.js +337 -0
- package/dist/shared/postgres-client.js.map +1 -0
- package/dist/shared/postgres-errors.d.ts +11 -0
- package/dist/shared/postgres-errors.d.ts.map +1 -0
- package/dist/shared/postgres-errors.js +105 -0
- package/dist/shared/postgres-errors.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 +347 -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 +41 -0
- package/dist/storage/outbox-worker.d.ts.map +1 -0
- package/dist/storage/outbox-worker.js +146 -0
- package/dist/storage/outbox-worker.js.map +1 -0
- package/dist/storage/ports.d.ts +78 -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 +140 -0
- package/dist/storage/postgres-repository.js.map +1 -0
- package/dist/storage/runtime.d.ts +77 -0
- package/dist/storage/runtime.d.ts.map +1 -0
- package/dist/storage/runtime.js +369 -0
- package/dist/storage/runtime.js.map +1 -0
- package/package.json +43 -21
- 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/billing/billing-manager.d.ts +0 -66
- package/dist/billing/billing-manager.d.ts.map +0 -1
- package/dist/billing/billing-manager.js +0 -560
- package/dist/billing/billing-manager.js.map +0 -1
- package/dist/billing/billing-types.d.ts +0 -203
- package/dist/billing/billing-types.d.ts.map +0 -1
- package/dist/billing/billing-types.js +0 -6
- package/dist/billing/billing-types.js.map +0 -1
- package/dist/billing/memory-billing-store.d.ts +0 -37
- package/dist/billing/memory-billing-store.d.ts.map +0 -1
- package/dist/billing/memory-billing-store.js +0 -197
- package/dist/billing/memory-billing-store.js.map +0 -1
- package/dist/billing/postgres-billing-store.d.ts +0 -35
- package/dist/billing/postgres-billing-store.d.ts.map +0 -1
- package/dist/billing/postgres-billing-store.js +0 -274
- package/dist/billing/postgres-billing-store.js.map +0 -1
- package/dist/billing/supabase-billing-store.d.ts +0 -29
- package/dist/billing/supabase-billing-store.d.ts.map +0 -1
- package/dist/billing/supabase-billing-store.js +0 -250
- package/dist/billing/supabase-billing-store.js.map +0 -1
- package/dist/manager.d.ts +0 -444
- package/dist/manager.d.ts.map +0 -1
- package/dist/manager.js +0 -1153
- package/dist/manager.js.map +0 -1
- package/dist/stores/credit-store.d.ts +0 -198
- 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 -187
- package/dist/stores/memory-store.d.ts.map +0 -1
- package/dist/stores/memory-store.js +0 -1890
- package/dist/stores/memory-store.js.map +0 -1
- package/dist/stores/postgres-store.d.ts +0 -100
- package/dist/stores/postgres-store.d.ts.map +0 -1
- package/dist/stores/postgres-store.js +0 -891
- package/dist/stores/postgres-store.js.map +0 -1
- package/dist/stores/supabase-store.d.ts +0 -87
- package/dist/stores/supabase-store.d.ts.map +0 -1
- package/dist/stores/supabase-store.js +0 -948
- package/dist/stores/supabase-store.js.map +0 -1
- package/dist/types.d.ts +0 -463
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- /package/dist/{types.js → commerce/types.js} +0 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { safeParse } from "../../../shared/postgres-validation.js";
|
|
3
|
+
const BillingPaymentRowSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
provider: z.string().optional(),
|
|
6
|
+
provider_payment_id: z.string().optional(),
|
|
7
|
+
subject_id: z.string().nullable().optional(),
|
|
8
|
+
user_id: z.string().nullable().optional(),
|
|
9
|
+
amount_minor: z.union([z.string(), z.number()]).optional(),
|
|
10
|
+
tax_minor: z.union([z.string(), z.number()]).nullable().optional(),
|
|
11
|
+
currency: z.string().optional(),
|
|
12
|
+
purpose: z.string().nullable().optional(),
|
|
13
|
+
status: z.string().optional(),
|
|
14
|
+
provider_updated_at: z.unknown().optional(),
|
|
15
|
+
metadata: z.record(z.string(), z.unknown()).nullable().optional(),
|
|
16
|
+
})
|
|
17
|
+
.passthrough();
|
|
18
|
+
const ForRefundRowSchema = z
|
|
19
|
+
.object({
|
|
20
|
+
credits_per_unit: z.union([z.string(), z.number()]).nullable().optional(),
|
|
21
|
+
credits_per_major_unit: z.union([z.string(), z.number()]).nullable().optional(),
|
|
22
|
+
tier: z.string().optional(),
|
|
23
|
+
deposit_to: z.string().optional(),
|
|
24
|
+
})
|
|
25
|
+
.passthrough();
|
|
26
|
+
export class BillingPaymentRepository {
|
|
27
|
+
query;
|
|
28
|
+
constructor(query) {
|
|
29
|
+
this.query = query;
|
|
30
|
+
}
|
|
31
|
+
async upsert(provider, providerPaymentId, providerInvoiceId, userId, amountMinor, taxMinor, currency, purpose, metadata, status, providerUpdatedAt) {
|
|
32
|
+
if (!userId)
|
|
33
|
+
throw new Error("billing payment requires a subject");
|
|
34
|
+
if (purpose !== "subscription" && purpose !== "credit_topup") {
|
|
35
|
+
throw new Error("billing payment requires a known purpose");
|
|
36
|
+
}
|
|
37
|
+
const rows = await this.query(`SELECT bursar.upsert_billing_payment(
|
|
38
|
+
$1::uuid,$2,$3,$4,$5,$6,$7,$8::bursar.billing_payment_status,$9,$10,$11::jsonb
|
|
39
|
+
)`, [
|
|
40
|
+
userId,
|
|
41
|
+
provider,
|
|
42
|
+
providerPaymentId,
|
|
43
|
+
amountMinor,
|
|
44
|
+
taxMinor ?? 0,
|
|
45
|
+
currency,
|
|
46
|
+
purpose,
|
|
47
|
+
status,
|
|
48
|
+
providerUpdatedAt,
|
|
49
|
+
providerInvoiceId,
|
|
50
|
+
metadata ?? "{}",
|
|
51
|
+
]);
|
|
52
|
+
const id = rows[0]?.upsert_billing_payment;
|
|
53
|
+
if (!id)
|
|
54
|
+
throw new Error("billing payment upsert returned no ID");
|
|
55
|
+
return String(id);
|
|
56
|
+
}
|
|
57
|
+
async getForRefund(provider, providerPaymentId) {
|
|
58
|
+
const rows = await this.query("SELECT * FROM bursar.get_billing_payment_by_provider($1,$2)", [
|
|
59
|
+
provider,
|
|
60
|
+
providerPaymentId,
|
|
61
|
+
]);
|
|
62
|
+
const row = rows[0];
|
|
63
|
+
return row?.id == null
|
|
64
|
+
? null
|
|
65
|
+
: safeParse(ForRefundRowSchema, row, "BillingPaymentRepository.getForRefund");
|
|
66
|
+
}
|
|
67
|
+
async getDirect(provider, providerPaymentId) {
|
|
68
|
+
const rows = await this.query("SELECT * FROM bursar.get_billing_payment_by_provider($1,$2)", [
|
|
69
|
+
provider,
|
|
70
|
+
providerPaymentId,
|
|
71
|
+
]);
|
|
72
|
+
const row = rows[0];
|
|
73
|
+
return row?.id == null
|
|
74
|
+
? null
|
|
75
|
+
: safeParse(BillingPaymentRowSchema, row, "BillingPaymentRepository.getDirect");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=payment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment.js","sourceRoot":"","sources":["../../../../src/billing/postgres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I,CAAC,CAAC,CAAyC,EAAE,sBAAsB,CAAC;QACpF,IAAI,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;QAClE,OAAO,MAAM,CAAC,EAAE,CAAC,CAAC;IACpB,CAAC;IACD,KAAK,CAAC,YAAY,CAAC,QAAgB,EAAE,iBAAyB;QAC5D,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,6DAA6D,EAAE;YAC3F,QAAQ;YACR,iBAAiB;SAClB,CAAC,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAwC,CAAC;QAC3D,OAAO,GAAG,EAAE,EAAE,IAAI,IAAI;YACpB,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,SAAS,CAAC,kBAAkB,EAAE,GAAG,EAAE,uCAAuC,CAAC,CAAC;IAClF,CAAC;IACD,KAAK,CAAC,SAAS,CAAC,QAAgB,EAAE,iBAAyB;QACzD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,6DAA6D,EAAE;YAC3F,QAAQ;YACR,iBAAiB;SAClB,CAAC,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAwC,CAAC;QAC3D,OAAO,GAAG,EAAE,EAAE,IAAI,IAAI;YACpB,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,SAAS,CAAC,uBAAuB,EAAE,GAAG,EAAE,oCAAoC,CAAC,CAAC;IACpF,CAAC;CACF","sourcesContent":["import { z } from \"zod\";\nimport type { QueryFn } from \"../../../shared/postgres-types.js\";\nimport { safeParse } from \"../../../shared/postgres-validation.js\";\n\nconst BillingPaymentRowSchema = z\n .object({\n provider: z.string().optional(),\n provider_payment_id: z.string().optional(),\n subject_id: z.string().nullable().optional(),\n user_id: z.string().nullable().optional(),\n amount_minor: z.union([z.string(), z.number()]).optional(),\n tax_minor: z.union([z.string(), z.number()]).nullable().optional(),\n currency: z.string().optional(),\n purpose: z.string().nullable().optional(),\n status: z.string().optional(),\n provider_updated_at: z.unknown().optional(),\n metadata: z.record(z.string(), z.unknown()).nullable().optional(),\n })\n .passthrough();\nconst ForRefundRowSchema = z\n .object({\n credits_per_unit: z.union([z.string(), z.number()]).nullable().optional(),\n credits_per_major_unit: z.union([z.string(), z.number()]).nullable().optional(),\n tier: z.string().optional(),\n deposit_to: z.string().optional(),\n })\n .passthrough();\nexport type BillingPaymentRow = z.infer<typeof BillingPaymentRowSchema>;\nexport type ForRefundRow = z.infer<typeof ForRefundRowSchema>;\n\nexport class BillingPaymentRepository {\n constructor(private query: QueryFn) {}\n async upsert(\n provider: string,\n providerPaymentId: string,\n providerInvoiceId: string | null,\n userId: string | null,\n amountMinor: number,\n taxMinor: number | null,\n currency: string,\n purpose: string | null,\n metadata: string | null,\n status: string,\n providerUpdatedAt: string,\n ): Promise<string> {\n if (!userId) throw new Error(\"billing payment requires a subject\");\n if (purpose !== \"subscription\" && purpose !== \"credit_topup\") {\n throw new Error(\"billing payment requires a known purpose\");\n }\n const rows = await this.query(\n `SELECT bursar.upsert_billing_payment(\n $1::uuid,$2,$3,$4,$5,$6,$7,$8::bursar.billing_payment_status,$9,$10,$11::jsonb\n )`,\n [\n userId,\n provider,\n providerPaymentId,\n amountMinor,\n taxMinor ?? 0,\n currency,\n purpose,\n status,\n providerUpdatedAt,\n providerInvoiceId,\n metadata ?? \"{}\",\n ],\n );\n const id = (rows[0] as Record<string, unknown> | undefined)?.upsert_billing_payment;\n if (!id) throw new Error(\"billing payment upsert returned no ID\");\n return String(id);\n }\n async getForRefund(provider: string, providerPaymentId: string): Promise<ForRefundRow | null> {\n const rows = await this.query(\"SELECT * FROM bursar.get_billing_payment_by_provider($1,$2)\", [\n provider,\n providerPaymentId,\n ]);\n const row = rows[0] as Record<string, unknown> | undefined;\n return row?.id == null\n ? null\n : safeParse(ForRefundRowSchema, row, \"BillingPaymentRepository.getForRefund\");\n }\n async getDirect(provider: string, providerPaymentId: string): Promise<BillingPaymentRow | null> {\n const rows = await this.query(\"SELECT * FROM bursar.get_billing_payment_by_provider($1,$2)\", [\n provider,\n providerPaymentId,\n ]);\n const row = rows[0] as Record<string, unknown> | undefined;\n return row?.id == null\n ? null\n : safeParse(BillingPaymentRowSchema, row, \"BillingPaymentRepository.getDirect\");\n }\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { QueryFn } from "../../../shared/postgres-types.js";
|
|
2
|
+
export declare class BillingPreferencesRepository {
|
|
3
|
+
private query;
|
|
4
|
+
constructor(query: QueryFn);
|
|
5
|
+
get(userId: string): Promise<Record<string, unknown> | null>;
|
|
6
|
+
upsert(prefs: {
|
|
7
|
+
userId: string;
|
|
8
|
+
autoRecharge?: boolean;
|
|
9
|
+
overageProtection?: boolean;
|
|
10
|
+
emailNotifications?: boolean;
|
|
11
|
+
usageAlerts?: boolean;
|
|
12
|
+
invoiceReminders?: boolean;
|
|
13
|
+
}): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=preferences.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preferences.d.ts","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/preferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAEjE,qBAAa,4BAA4B;IAC3B,OAAO,CAAC,KAAK;gBAAL,KAAK,EAAE,OAAO;IAE5B,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAO5D,MAAM,CAAC,KAAK,EAAE;QAClB,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,iBAAiB,CAAC,EAAE,OAAO,CAAC;QAC5B,kBAAkB,CAAC,EAAE,OAAO,CAAC;QAC7B,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,gBAAgB,CAAC,EAAE,OAAO,CAAC;KAC5B,GAAG,OAAO,CAAC,IAAI,CAAC;CAUlB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export class BillingPreferencesRepository {
|
|
2
|
+
query;
|
|
3
|
+
constructor(query) {
|
|
4
|
+
this.query = query;
|
|
5
|
+
}
|
|
6
|
+
async get(userId) {
|
|
7
|
+
const rows = await this.query("SELECT * FROM bursar.get_billing_preferences($1::uuid)", [
|
|
8
|
+
userId,
|
|
9
|
+
]);
|
|
10
|
+
return rows[0] ?? null;
|
|
11
|
+
}
|
|
12
|
+
async upsert(prefs) {
|
|
13
|
+
await this.query("SELECT bursar.upsert_billing_preferences($1::uuid, $2, $3, $4, $5, $6)", [
|
|
14
|
+
prefs.userId,
|
|
15
|
+
prefs.autoRecharge ?? false,
|
|
16
|
+
prefs.overageProtection ?? true,
|
|
17
|
+
prefs.emailNotifications ?? true,
|
|
18
|
+
prefs.usageAlerts ?? true,
|
|
19
|
+
prefs.invoiceReminders ?? false,
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=preferences.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preferences.js","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/preferences.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,4BAA4B;IACnB;IAApB,YAAoB,KAAc;QAAd,UAAK,GAAL,KAAK,CAAS;IAAG,CAAC;IAEtC,KAAK,CAAC,GAAG,CAAC,MAAc;QACtB,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,wDAAwD,EAAE;YACtF,MAAM;SACP,CAAC,CAAC;QACH,OAAQ,IAAI,CAAC,CAAC,CAAyC,IAAI,IAAI,CAAC;IAClE,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,KAOZ;QACC,MAAM,IAAI,CAAC,KAAK,CAAC,wEAAwE,EAAE;YACzF,KAAK,CAAC,MAAM;YACZ,KAAK,CAAC,YAAY,IAAI,KAAK;YAC3B,KAAK,CAAC,iBAAiB,IAAI,IAAI;YAC/B,KAAK,CAAC,kBAAkB,IAAI,IAAI;YAChC,KAAK,CAAC,WAAW,IAAI,IAAI;YACzB,KAAK,CAAC,gBAAgB,IAAI,KAAK;SAChC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type { QueryFn } from \"../../../shared/postgres-types.js\";\n\nexport class BillingPreferencesRepository {\n constructor(private query: QueryFn) {}\n\n async get(userId: string): Promise<Record<string, unknown> | null> {\n const rows = await this.query(\"SELECT * FROM bursar.get_billing_preferences($1::uuid)\", [\n userId,\n ]);\n return (rows[0] as Record<string, unknown> | undefined) ?? null;\n }\n\n async upsert(prefs: {\n userId: string;\n autoRecharge?: boolean;\n overageProtection?: boolean;\n emailNotifications?: boolean;\n usageAlerts?: boolean;\n invoiceReminders?: boolean;\n }): Promise<void> {\n await this.query(\"SELECT bursar.upsert_billing_preferences($1::uuid, $2, $3, $4, $5, $6)\", [\n prefs.userId,\n prefs.autoRecharge ?? false,\n prefs.overageProtection ?? true,\n prefs.emailNotifications ?? true,\n prefs.usageAlerts ?? true,\n prefs.invoiceReminders ?? false,\n ]);\n }\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { QueryFn } from "../../../shared/postgres-types.js";
|
|
2
|
+
export declare class BillingRefundRepository {
|
|
3
|
+
private query;
|
|
4
|
+
constructor(query: QueryFn);
|
|
5
|
+
upsert(provider: string, providerRefundId: string, providerPaymentId: string | null, userId: string | null, amountMinor: number, currency: string, reason: string | null, metadata: string | null, status: string, providerUpdatedAt: string): Promise<string>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=refund.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refund.d.ts","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/refund.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAEjE,qBAAa,uBAAuB;IACtB,OAAO,CAAC,KAAK;gBAAL,KAAK,EAAE,OAAO;IAE5B,MAAM,CACV,QAAQ,EAAE,MAAM,EAChB,gBAAgB,EAAE,MAAM,EACxB,iBAAiB,EAAE,MAAM,GAAG,IAAI,EAChC,MAAM,EAAE,MAAM,GAAG,IAAI,EACrB,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GAAG,IAAI,EACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,EACvB,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,MAAM,GACxB,OAAO,CAAC,MAAM,CAAC;CA4BnB"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export class BillingRefundRepository {
|
|
2
|
+
query;
|
|
3
|
+
constructor(query) {
|
|
4
|
+
this.query = query;
|
|
5
|
+
}
|
|
6
|
+
async upsert(provider, providerRefundId, providerPaymentId, userId, amountMinor, currency, reason, metadata, status, providerUpdatedAt) {
|
|
7
|
+
if (!providerPaymentId)
|
|
8
|
+
throw new Error("refund requires providerPaymentId");
|
|
9
|
+
const payments = await this.query("SELECT * FROM bursar.get_billing_payment_by_provider($1, $2)", [provider, providerPaymentId]);
|
|
10
|
+
const paymentId = payments[0]?.id;
|
|
11
|
+
if (!paymentId)
|
|
12
|
+
throw new Error("refund payment not found");
|
|
13
|
+
const rows = await this.query(`SELECT bursar.upsert_billing_refund(
|
|
14
|
+
$1::uuid, $2, $3, $4, $5, $6, $7::uuid, $8::char(3), $9::jsonb
|
|
15
|
+
)`, [
|
|
16
|
+
paymentId,
|
|
17
|
+
providerRefundId,
|
|
18
|
+
amountMinor,
|
|
19
|
+
status,
|
|
20
|
+
reason,
|
|
21
|
+
providerUpdatedAt,
|
|
22
|
+
userId,
|
|
23
|
+
currency,
|
|
24
|
+
metadata ?? "{}",
|
|
25
|
+
]);
|
|
26
|
+
const refundId = rows[0]?.upsert_billing_refund;
|
|
27
|
+
if (!refundId)
|
|
28
|
+
throw new Error("billing refund upsert returned no ID");
|
|
29
|
+
return String(refundId);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=refund.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refund.js","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/refund.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,uBAAuB;IACd;IAApB,YAAoB,KAAc;QAAd,UAAK,GAAL,KAAK,CAAS;IAAG,CAAC;IAEtC,KAAK,CAAC,MAAM,CACV,QAAgB,EAChB,gBAAwB,EACxB,iBAAgC,EAChC,MAAqB,EACrB,WAAmB,EACnB,QAAgB,EAChB,MAAqB,EACrB,QAAuB,EACvB,MAAc,EACd,iBAAyB;QAEzB,IAAI,CAAC,iBAAiB;YAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;QAC7E,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,KAAK,CAC/B,8DAA8D,EAC9D,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAC9B,CAAC;QACF,MAAM,SAAS,GAAI,QAAQ,CAAC,CAAC,CAAyC,EAAE,EAAE,CAAC;QAC3E,IAAI,CAAC,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAC5D,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAC3B;;SAEG,EACH;YACE,SAAS;YACT,gBAAgB;YAChB,WAAW;YACX,MAAM;YACN,MAAM;YACN,iBAAiB;YACjB,MAAM;YACN,QAAQ;YACR,QAAQ,IAAI,IAAI;SACjB,CACF,CAAC;QACF,MAAM,QAAQ,GAAI,IAAI,CAAC,CAAC,CAAyC,EAAE,qBAAqB,CAAC;QACzF,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QACvE,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC1B,CAAC;CACF","sourcesContent":["import type { QueryFn } from \"../../../shared/postgres-types.js\";\n\nexport class BillingRefundRepository {\n constructor(private query: QueryFn) {}\n\n async upsert(\n provider: string,\n providerRefundId: string,\n providerPaymentId: string | null,\n userId: string | null,\n amountMinor: number,\n currency: string,\n reason: string | null,\n metadata: string | null,\n status: string,\n providerUpdatedAt: string,\n ): Promise<string> {\n if (!providerPaymentId) throw new Error(\"refund requires providerPaymentId\");\n const payments = await this.query(\n \"SELECT * FROM bursar.get_billing_payment_by_provider($1, $2)\",\n [provider, providerPaymentId],\n );\n const paymentId = (payments[0] as Record<string, unknown> | undefined)?.id;\n if (!paymentId) throw new Error(\"refund payment not found\");\n const rows = await this.query(\n `SELECT bursar.upsert_billing_refund(\n $1::uuid, $2, $3, $4, $5, $6, $7::uuid, $8::char(3), $9::jsonb\n )`,\n [\n paymentId,\n providerRefundId,\n amountMinor,\n status,\n reason,\n providerUpdatedAt,\n userId,\n currency,\n metadata ?? \"{}\",\n ],\n );\n const refundId = (rows[0] as Record<string, unknown> | undefined)?.upsert_billing_refund;\n if (!refundId) throw new Error(\"billing refund upsert returned no ID\");\n return String(refundId);\n }\n}\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { QueryFn } from "../../../shared/postgres-types.js";
|
|
3
|
+
declare const SubscriptionRowSchema: z.ZodObject<{
|
|
4
|
+
user_id: z.ZodOptional<z.ZodString>;
|
|
5
|
+
subject_id: z.ZodOptional<z.ZodString>;
|
|
6
|
+
provider: z.ZodString;
|
|
7
|
+
provider_subscription_id: z.ZodString;
|
|
8
|
+
provider_customer_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
9
|
+
offer_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
10
|
+
status: z.ZodOptional<z.ZodString>;
|
|
11
|
+
current_period_start: z.ZodOptional<z.ZodUnknown>;
|
|
12
|
+
current_period_end: z.ZodOptional<z.ZodUnknown>;
|
|
13
|
+
trial_end: z.ZodOptional<z.ZodUnknown>;
|
|
14
|
+
cancel_at: z.ZodOptional<z.ZodUnknown>;
|
|
15
|
+
ended_at: z.ZodOptional<z.ZodUnknown>;
|
|
16
|
+
grace_ends_at: z.ZodOptional<z.ZodUnknown>;
|
|
17
|
+
grace_expired_at: z.ZodOptional<z.ZodUnknown>;
|
|
18
|
+
provider_updated_at: z.ZodOptional<z.ZodUnknown>;
|
|
19
|
+
cancel_at_period_end: z.ZodOptional<z.ZodNullable<z.ZodPipe<z.ZodUnion<readonly [z.ZodBoolean, z.ZodString, z.ZodNumber]>, z.ZodTransform<boolean, string | number | boolean>>>>;
|
|
20
|
+
metadata: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
21
|
+
}, z.core.$loose>;
|
|
22
|
+
export type SubscriptionRow = z.infer<typeof SubscriptionRowSchema>;
|
|
23
|
+
export declare class BillingSubscriptionRepository {
|
|
24
|
+
private query;
|
|
25
|
+
constructor(query: QueryFn);
|
|
26
|
+
upsert(state: Record<string, unknown>): Promise<void>;
|
|
27
|
+
private map;
|
|
28
|
+
private withOfferContext;
|
|
29
|
+
get(provider: string, providerSubscriptionId: string): Promise<SubscriptionRow | null>;
|
|
30
|
+
getUserSubscription(userId: string, statuses?: string[]): Promise<SubscriptionRow | null>;
|
|
31
|
+
getUserSubscriptions(userId: string): Promise<SubscriptionRow[]>;
|
|
32
|
+
listExpiredGraceSubscriptions(now: string, limit?: number): Promise<SubscriptionRow[]>;
|
|
33
|
+
markGraceExpired(subscriptionId: string, expectedGraceEndsAt: string, expiredAt: string): Promise<boolean>;
|
|
34
|
+
recordConflict(input: {
|
|
35
|
+
userId?: string | null;
|
|
36
|
+
provider: string;
|
|
37
|
+
duplicateSubscriptionId: string;
|
|
38
|
+
existingSubscriptionId?: string | null;
|
|
39
|
+
eventId?: string | null;
|
|
40
|
+
metadata?: Record<string, unknown>;
|
|
41
|
+
}): Promise<string>;
|
|
42
|
+
/** Select a provider subscription without modifying provider-reported state. */
|
|
43
|
+
selectEntitlementSource(userId: string, provider: string, providerSubscriptionId?: string | null): Promise<boolean>;
|
|
44
|
+
}
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=subscription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAGjE,QAAA,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;iBAoBX,CAAC;AAqBjB,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,qBAAa,6BAA6B;IAC5B,OAAO,CAAC,KAAK;gBAAL,KAAK,EAAE,OAAO;IAE5B,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IA6D3D,OAAO,CAAC,GAAG;YAUG,gBAAgB;IAiBxB,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,sBAAsB,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAOtF,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAUzF,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAUhE,6BAA6B,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,SAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAWnF,gBAAgB,CACpB,cAAc,EAAE,MAAM,EACtB,mBAAmB,EAAE,MAAM,EAC3B,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,OAAO,CAAC;IAQb,cAAc,CAAC,KAAK,EAAE;QAC1B,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACvB,QAAQ,EAAE,MAAM,CAAC;QACjB,uBAAuB,EAAE,MAAM,CAAC;QAChC,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACvC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACxB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACpC,GAAG,OAAO,CAAC,MAAM,CAAC;IAmBnB,gFAAgF;IAC1E,uBAAuB,CAC3B,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,GACrC,OAAO,CAAC,OAAO,CAAC;CA0BpB"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { pgBoolean, safeParse } from "../../../shared/postgres-validation.js";
|
|
3
|
+
const SubscriptionRowSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
user_id: z.string().optional(),
|
|
6
|
+
subject_id: z.string().optional(),
|
|
7
|
+
provider: z.string(),
|
|
8
|
+
provider_subscription_id: z.string(),
|
|
9
|
+
provider_customer_id: z.string().nullable().optional(),
|
|
10
|
+
offer_id: z.string().nullable().optional(),
|
|
11
|
+
status: z.string().optional(),
|
|
12
|
+
current_period_start: z.unknown().optional(),
|
|
13
|
+
current_period_end: z.unknown().optional(),
|
|
14
|
+
trial_end: z.unknown().optional(),
|
|
15
|
+
cancel_at: z.unknown().optional(),
|
|
16
|
+
ended_at: z.unknown().optional(),
|
|
17
|
+
grace_ends_at: z.unknown().optional(),
|
|
18
|
+
grace_expired_at: z.unknown().optional(),
|
|
19
|
+
provider_updated_at: z.unknown().optional(),
|
|
20
|
+
cancel_at_period_end: pgBoolean.nullable().optional(),
|
|
21
|
+
metadata: z.record(z.string(), z.unknown()).nullable().optional(),
|
|
22
|
+
})
|
|
23
|
+
.passthrough();
|
|
24
|
+
function timestampValue(value) {
|
|
25
|
+
if (value instanceof Date) {
|
|
26
|
+
const timestamp = value.getTime();
|
|
27
|
+
return Number.isNaN(timestamp) ? null : timestamp;
|
|
28
|
+
}
|
|
29
|
+
const timestamp = Date.parse(String(value ?? ""));
|
|
30
|
+
return Number.isNaN(timestamp) ? null : timestamp;
|
|
31
|
+
}
|
|
32
|
+
function compareProviderTimestampsDescending(left, right) {
|
|
33
|
+
const leftTimestamp = timestampValue(left.provider_updated_at);
|
|
34
|
+
const rightTimestamp = timestampValue(right.provider_updated_at);
|
|
35
|
+
if (leftTimestamp === null)
|
|
36
|
+
return rightTimestamp === null ? 0 : 1;
|
|
37
|
+
if (rightTimestamp === null)
|
|
38
|
+
return -1;
|
|
39
|
+
return rightTimestamp - leftTimestamp;
|
|
40
|
+
}
|
|
41
|
+
export class BillingSubscriptionRepository {
|
|
42
|
+
query;
|
|
43
|
+
constructor(query) {
|
|
44
|
+
this.query = query;
|
|
45
|
+
}
|
|
46
|
+
async upsert(state) {
|
|
47
|
+
const provider = String(state.provider ?? "");
|
|
48
|
+
const providerSubscriptionId = String(state.providerSubscriptionId ?? "");
|
|
49
|
+
let userId = String(state.userId ?? state.subjectId ?? "");
|
|
50
|
+
let providerCustomerId = state.providerCustomerId ?? null;
|
|
51
|
+
let offerId = String(state.offerId ?? state.offer_id ?? "");
|
|
52
|
+
if (!provider || !providerSubscriptionId) {
|
|
53
|
+
throw new Error("subscription.upsert: subject, provider, subscription, and offer are required");
|
|
54
|
+
}
|
|
55
|
+
const existing = await this.query(`SELECT * FROM bursar.get_billing_subscription_by_provider($1, $2)`, [provider, providerSubscriptionId]);
|
|
56
|
+
const existingRow = existing[0];
|
|
57
|
+
if (!userId && existingRow?.subject_id != null)
|
|
58
|
+
userId = String(existingRow.subject_id);
|
|
59
|
+
if (providerCustomerId == null && existingRow?.provider_customer_id != null) {
|
|
60
|
+
providerCustomerId = String(existingRow.provider_customer_id);
|
|
61
|
+
}
|
|
62
|
+
if (!offerId && existingRow?.offer_id != null)
|
|
63
|
+
offerId = String(existingRow.offer_id);
|
|
64
|
+
const offerKey = String(state.offerKey ?? state.offer_key ?? "");
|
|
65
|
+
if (!offerId && offerKey) {
|
|
66
|
+
const offerRows = await this.query(`SELECT * FROM bursar.resolve_active_catalog_offer($1)`, [
|
|
67
|
+
offerKey,
|
|
68
|
+
]);
|
|
69
|
+
const offerRow = offerRows[0];
|
|
70
|
+
if (offerRow?.id != null)
|
|
71
|
+
offerId = String(offerRow.id);
|
|
72
|
+
}
|
|
73
|
+
if (!userId || !offerId) {
|
|
74
|
+
throw new Error("subscription.upsert: subject, provider, subscription, and offer are required");
|
|
75
|
+
}
|
|
76
|
+
await this.query("SELECT bursar.upsert_billing_subscription($1::uuid,$2,$3,$4,$5::uuid,$6::bursar.billing_subscription_status,$7,$8,$9,$10::jsonb,$11,$12,$13,$14,$15)", [
|
|
77
|
+
userId,
|
|
78
|
+
provider,
|
|
79
|
+
providerSubscriptionId,
|
|
80
|
+
providerCustomerId,
|
|
81
|
+
offerId,
|
|
82
|
+
state.status ?? "incomplete",
|
|
83
|
+
state.currentPeriodStart ?? null,
|
|
84
|
+
state.currentPeriodEnd ?? null,
|
|
85
|
+
state.cancelAtPeriodEnd ?? false,
|
|
86
|
+
JSON.stringify(state.metadata ?? {}),
|
|
87
|
+
state.trialEnd ?? null,
|
|
88
|
+
state.cancelAt ?? null,
|
|
89
|
+
state.endedAt ?? null,
|
|
90
|
+
state.providerUpdatedAt ?? new Date().toISOString(),
|
|
91
|
+
state.graceEndsAt ?? null,
|
|
92
|
+
]);
|
|
93
|
+
}
|
|
94
|
+
map(row) {
|
|
95
|
+
const r = row;
|
|
96
|
+
if (r.subject_id == null && r.provider == null)
|
|
97
|
+
return null;
|
|
98
|
+
return safeParse(SubscriptionRowSchema, { ...r, user_id: r.subject_id }, "BillingSubscriptionRepository");
|
|
99
|
+
}
|
|
100
|
+
async withOfferContext(row) {
|
|
101
|
+
const value = row;
|
|
102
|
+
if (value.offer_id == null || value.catalog_revision_id == null)
|
|
103
|
+
return value;
|
|
104
|
+
const contextRows = await this.query(`SELECT * FROM bursar.get_catalog_offer_context($1::uuid, $2::uuid)`, [value.offer_id, value.catalog_revision_id]);
|
|
105
|
+
const context = contextRows[0] ?? {};
|
|
106
|
+
return {
|
|
107
|
+
...value,
|
|
108
|
+
...context,
|
|
109
|
+
plan: context.plan_key,
|
|
110
|
+
interval: context.billing_unit,
|
|
111
|
+
interval_count: context.billing_count,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async get(provider, providerSubscriptionId) {
|
|
115
|
+
const rows = await this.query(`SELECT * FROM bursar.get_billing_subscription_by_provider($1, $2)`, [provider, providerSubscriptionId]);
|
|
116
|
+
return rows[0] ? this.map(await this.withOfferContext(rows[0])) : null;
|
|
117
|
+
}
|
|
118
|
+
async getUserSubscription(userId, statuses) {
|
|
119
|
+
const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [
|
|
120
|
+
userId,
|
|
121
|
+
]);
|
|
122
|
+
const allowed = statuses ?? ["active", "trialing"];
|
|
123
|
+
const candidates = rows
|
|
124
|
+
.filter((row) => allowed.includes(String(row.status)))
|
|
125
|
+
.sort(compareProviderTimestampsDescending);
|
|
126
|
+
return candidates[0] ? this.map(await this.withOfferContext(candidates[0])) : null;
|
|
127
|
+
}
|
|
128
|
+
async getUserSubscriptions(userId) {
|
|
129
|
+
const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [
|
|
130
|
+
userId,
|
|
131
|
+
]);
|
|
132
|
+
const enriched = await Promise.all(rows.map((row) => this.withOfferContext(row)));
|
|
133
|
+
return enriched
|
|
134
|
+
.map((row) => this.map(row))
|
|
135
|
+
.filter((row) => row !== null);
|
|
136
|
+
}
|
|
137
|
+
async listExpiredGraceSubscriptions(now, limit = 100) {
|
|
138
|
+
const rows = await this.query(`SELECT * FROM bursar.list_expired_grace_subscriptions($1, $2)`, [
|
|
139
|
+
now,
|
|
140
|
+
limit,
|
|
141
|
+
]);
|
|
142
|
+
const enriched = await Promise.all(rows.map((row) => this.withOfferContext(row)));
|
|
143
|
+
return enriched
|
|
144
|
+
.map((row) => this.map(row))
|
|
145
|
+
.filter((row) => row !== null);
|
|
146
|
+
}
|
|
147
|
+
async markGraceExpired(subscriptionId, expectedGraceEndsAt, expiredAt) {
|
|
148
|
+
const rows = await this.query(`SELECT bursar.mark_subscription_grace_expired($1::uuid, $2, $3) AS marked`, [subscriptionId, expectedGraceEndsAt, expiredAt]);
|
|
149
|
+
return rows[0]?.marked === true;
|
|
150
|
+
}
|
|
151
|
+
async recordConflict(input) {
|
|
152
|
+
const rows = await this.query(`SELECT bursar.record_subscription_conflict(
|
|
153
|
+
$1::uuid, $2, $3, $4, $5, $6::jsonb
|
|
154
|
+
) AS id`, [
|
|
155
|
+
input.userId ?? null,
|
|
156
|
+
input.provider,
|
|
157
|
+
input.duplicateSubscriptionId,
|
|
158
|
+
input.existingSubscriptionId ?? null,
|
|
159
|
+
input.eventId ?? null,
|
|
160
|
+
JSON.stringify(input.metadata ?? {}),
|
|
161
|
+
]);
|
|
162
|
+
const id = rows[0]?.id;
|
|
163
|
+
if (id == null)
|
|
164
|
+
throw new Error("subscription conflict audit returned no ID");
|
|
165
|
+
return String(id);
|
|
166
|
+
}
|
|
167
|
+
/** Select a provider subscription without modifying provider-reported state. */
|
|
168
|
+
async selectEntitlementSource(userId, provider, providerSubscriptionId) {
|
|
169
|
+
const rows = (await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [
|
|
170
|
+
userId,
|
|
171
|
+
]));
|
|
172
|
+
const eligibleStatuses = new Set(["trialing", "active", "past_due", "paused"]);
|
|
173
|
+
const replacement = rows
|
|
174
|
+
.filter((row) => row.provider === provider &&
|
|
175
|
+
eligibleStatuses.has(String(row.status)) &&
|
|
176
|
+
(!providerSubscriptionId || row.provider_subscription_id === providerSubscriptionId))
|
|
177
|
+
.sort(compareProviderTimestampsDescending)[0];
|
|
178
|
+
if (replacement?.id == null)
|
|
179
|
+
return false;
|
|
180
|
+
const selectedRows = await this.query(`SELECT bursar.select_entitlement_source($1::uuid, $2::uuid) AS selected`, [userId, replacement.id]);
|
|
181
|
+
if (selectedRows[0]?.selected !== true) {
|
|
182
|
+
throw new Error("subscription entitlement source selection was rejected");
|
|
183
|
+
}
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
//# sourceMappingURL=subscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAE9E,MAAM,qBAAqB,GAAG,CAAC;KAC5B,MAAM,CAAC;IACN,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC9B,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACjC,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,wBAAwB,EAAE,CAAC,CAAC,MAAM,EAAE;IACpC,oBAAoB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;IACtD,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;IAC1C,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC7B,oBAAoB,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE;IAC5C,kBAAkB,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE;IAC1C,S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Boolean, safeParse } from \"../../../shared/postgres-validation.js\";\n\nconst SubscriptionRowSchema = z\n .object({\n user_id: z.string().optional(),\n subject_id: z.string().optional(),\n provider: z.string(),\n provider_subscription_id: z.string(),\n provider_customer_id: z.string().nullable().optional(),\n offer_id: z.string().nullable().optional(),\n status: z.string().optional(),\n current_period_start: z.unknown().optional(),\n current_period_end: z.unknown().optional(),\n trial_end: z.unknown().optional(),\n cancel_at: z.unknown().optional(),\n ended_at: z.unknown().optional(),\n grace_ends_at: z.unknown().optional(),\n grace_expired_at: z.unknown().optional(),\n provider_updated_at: z.unknown().optional(),\n cancel_at_period_end: pgBoolean.nullable().optional(),\n metadata: z.record(z.string(), z.unknown()).nullable().optional(),\n })\n .passthrough();\n\nfunction timestampValue(value: unknown): number | null {\n if (value instanceof Date) {\n const timestamp = value.getTime();\n return Number.isNaN(timestamp) ? null : timestamp;\n }\n const timestamp = Date.parse(String(value ?? \"\"));\n return Number.isNaN(timestamp) ? null : timestamp;\n}\n\nfunction compareProviderTimestampsDescending(\n left: Record<string, unknown>,\n right: Record<string, unknown>,\n): number {\n const leftTimestamp = timestampValue(left.provider_updated_at);\n const rightTimestamp = timestampValue(right.provider_updated_at);\n if (leftTimestamp === null) return rightTimestamp === null ? 0 : 1;\n if (rightTimestamp === null) return -1;\n return rightTimestamp - leftTimestamp;\n}\nexport type SubscriptionRow = z.infer<typeof SubscriptionRowSchema>;\n\nexport class BillingSubscriptionRepository {\n constructor(private query: QueryFn) {}\n\n async upsert(state: Record<string, unknown>): Promise<void> {\n const provider = String(state.provider ?? \"\");\n const providerSubscriptionId = String(state.providerSubscriptionId ?? \"\");\n let userId = String(state.userId ?? state.subjectId ?? \"\");\n let providerCustomerId = state.providerCustomerId ?? null;\n let offerId = String(state.offerId ?? state.offer_id ?? \"\");\n\n if (!provider || !providerSubscriptionId) {\n throw new Error(\n \"subscription.upsert: subject, provider, subscription, and offer are required\",\n );\n }\n\n const existing = await this.query(\n `SELECT * FROM bursar.get_billing_subscription_by_provider($1, $2)`,\n [provider, providerSubscriptionId],\n );\n const existingRow = existing[0] as Record<string, unknown> | undefined;\n if (!userId && existingRow?.subject_id != null) userId = String(existingRow.subject_id);\n if (providerCustomerId == null && existingRow?.provider_customer_id != null) {\n providerCustomerId = String(existingRow.provider_customer_id);\n }\n if (!offerId && existingRow?.offer_id != null) offerId = String(existingRow.offer_id);\n\n const offerKey = String(state.offerKey ?? state.offer_key ?? \"\");\n if (!offerId && offerKey) {\n const offerRows = await this.query(`SELECT * FROM bursar.resolve_active_catalog_offer($1)`, [\n offerKey,\n ]);\n const offerRow = offerRows[0] as Record<string, unknown> | undefined;\n if (offerRow?.id != null) offerId = String(offerRow.id);\n }\n\n if (!userId || !offerId) {\n throw new Error(\n \"subscription.upsert: subject, provider, subscription, and offer are required\",\n );\n }\n\n await this.query(\n \"SELECT bursar.upsert_billing_subscription($1::uuid,$2,$3,$4,$5::uuid,$6::bursar.billing_subscription_status,$7,$8,$9,$10::jsonb,$11,$12,$13,$14,$15)\",\n [\n userId,\n provider,\n providerSubscriptionId,\n providerCustomerId,\n offerId,\n state.status ?? \"incomplete\",\n state.currentPeriodStart ?? null,\n state.currentPeriodEnd ?? null,\n state.cancelAtPeriodEnd ?? false,\n JSON.stringify(state.metadata ?? {}),\n state.trialEnd ?? null,\n state.cancelAt ?? null,\n state.endedAt ?? null,\n state.providerUpdatedAt ?? new Date().toISOString(),\n state.graceEndsAt ?? null,\n ],\n );\n }\n\n private map(row: unknown): SubscriptionRow | null {\n const r = row as Record<string, unknown>;\n if (r.subject_id == null && r.provider == null) return null;\n return safeParse(\n SubscriptionRowSchema,\n { ...r, user_id: r.subject_id },\n \"BillingSubscriptionRepository\",\n );\n }\n\n private async withOfferContext(row: unknown): Promise<Record<string, unknown>> {\n const value = row as Record<string, unknown>;\n if (value.offer_id == null || value.catalog_revision_id == null) return value;\n const contextRows = await this.query(\n `SELECT * FROM bursar.get_catalog_offer_context($1::uuid, $2::uuid)`,\n [value.offer_id, value.catalog_revision_id],\n );\n const context = (contextRows[0] as Record<string, unknown> | undefined) ?? {};\n return {\n ...value,\n ...context,\n plan: context.plan_key,\n interval: context.billing_unit,\n interval_count: context.billing_count,\n };\n }\n\n async get(provider: string, providerSubscriptionId: string): Promise<SubscriptionRow | null> {\n const rows = await this.query(\n `SELECT * FROM bursar.get_billing_subscription_by_provider($1, $2)`,\n [provider, providerSubscriptionId],\n );\n return rows[0] ? this.map(await this.withOfferContext(rows[0])) : null;\n }\n async getUserSubscription(userId: string, statuses?: string[]): Promise<SubscriptionRow | null> {\n const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [\n userId,\n ]);\n const allowed = statuses ?? [\"active\", \"trialing\"];\n const candidates = (rows as Array<Record<string, unknown>>)\n .filter((row) => allowed.includes(String(row.status)))\n .sort(compareProviderTimestampsDescending);\n return candidates[0] ? this.map(await this.withOfferContext(candidates[0])) : null;\n }\n async getUserSubscriptions(userId: string): Promise<SubscriptionRow[]> {\n const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [\n userId,\n ]);\n const enriched = await Promise.all(rows.map((row) => this.withOfferContext(row)));\n return enriched\n .map((row) => this.map(row))\n .filter((row): row is SubscriptionRow => row !== null);\n }\n\n async listExpiredGraceSubscriptions(now: string, limit = 100): Promise<SubscriptionRow[]> {\n const rows = await this.query(`SELECT * FROM bursar.list_expired_grace_subscriptions($1, $2)`, [\n now,\n limit,\n ]);\n const enriched = await Promise.all(rows.map((row) => this.withOfferContext(row)));\n return enriched\n .map((row) => this.map(row))\n .filter((row): row is SubscriptionRow => row !== null);\n }\n\n async markGraceExpired(\n subscriptionId: string,\n expectedGraceEndsAt: string,\n expiredAt: string,\n ): Promise<boolean> {\n const rows = await this.query(\n `SELECT bursar.mark_subscription_grace_expired($1::uuid, $2, $3) AS marked`,\n [subscriptionId, expectedGraceEndsAt, expiredAt],\n );\n return (rows[0] as Record<string, unknown> | undefined)?.marked === true;\n }\n\n async recordConflict(input: {\n userId?: string | null;\n provider: string;\n duplicateSubscriptionId: string;\n existingSubscriptionId?: string | null;\n eventId?: string | null;\n metadata?: Record<string, unknown>;\n }): Promise<string> {\n const rows = await this.query(\n `SELECT bursar.record_subscription_conflict(\n $1::uuid, $2, $3, $4, $5, $6::jsonb\n ) AS id`,\n [\n input.userId ?? null,\n input.provider,\n input.duplicateSubscriptionId,\n input.existingSubscriptionId ?? null,\n input.eventId ?? null,\n JSON.stringify(input.metadata ?? {}),\n ],\n );\n const id = (rows[0] as Record<string, unknown> | undefined)?.id;\n if (id == null) throw new Error(\"subscription conflict audit returned no ID\");\n return String(id);\n }\n\n /** Select a provider subscription without modifying provider-reported state. */\n async selectEntitlementSource(\n userId: string,\n provider: string,\n providerSubscriptionId?: string | null,\n ): Promise<boolean> {\n const rows = (await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [\n userId,\n ])) as Array<Record<string, unknown>>;\n const eligibleStatuses = new Set([\"trialing\", \"active\", \"past_due\", \"paused\"]);\n const replacement = rows\n .filter(\n (row) =>\n row.provider === provider &&\n eligibleStatuses.has(String(row.status)) &&\n (!providerSubscriptionId || row.provider_subscription_id === providerSubscriptionId),\n )\n .sort(compareProviderTimestampsDescending)[0];\n\n if (replacement?.id == null) return false;\n\n const selectedRows = await this.query(\n `SELECT bursar.select_entitlement_source($1::uuid, $2::uuid) AS selected`,\n [userId, replacement.id],\n );\n if ((selectedRows[0] as Record<string, unknown> | undefined)?.selected !== true) {\n throw new Error(\"subscription entitlement source selection was rejected\");\n }\n\n return true;\n }\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { QueryFn } from "../../../shared/postgres-types.js";
|
|
3
|
+
declare const BillingTopupRowSchema: z.ZodObject<{
|
|
4
|
+
topup_key: z.ZodOptional<z.ZodString>;
|
|
5
|
+
credits_per_unit: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
|
|
6
|
+
credits_per_major_unit: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
|
|
7
|
+
tier: z.ZodOptional<z.ZodString>;
|
|
8
|
+
deposit_to: z.ZodOptional<z.ZodString>;
|
|
9
|
+
bucket_key: z.ZodOptional<z.ZodString>;
|
|
10
|
+
amount_minor: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
|
|
11
|
+
currency: z.ZodOptional<z.ZodString>;
|
|
12
|
+
min_quantity: z.ZodOptional<z.ZodNumber>;
|
|
13
|
+
max_quantity: z.ZodOptional<z.ZodNumber>;
|
|
14
|
+
default_quantity: z.ZodOptional<z.ZodNumber>;
|
|
15
|
+
}, z.core.$loose>;
|
|
16
|
+
export type BillingTopupRow = z.infer<typeof BillingTopupRowSchema>;
|
|
17
|
+
/** Repository for credit top-up resolution. */
|
|
18
|
+
export declare class BillingTopupRepository {
|
|
19
|
+
private query;
|
|
20
|
+
constructor(query: QueryFn);
|
|
21
|
+
/** Resolve a credit top-up by price ID and product ID. */
|
|
22
|
+
resolveByPrice(provider: string, priceId: string | null, productId: string | null): Promise<BillingTopupRow | null>;
|
|
23
|
+
/** Resolve a credit top-up by provider lookup key. */
|
|
24
|
+
resolveByLookup(provider: string, lookupKey: string): Promise<BillingTopupRow | null>;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=topup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"topup.d.ts","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/topup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAGjE,QAAA,MAAM,qBAAqB;;;;;;;;;;;;iBAoBX,CAAC;AAEjB,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,+CAA+C;AAC/C,qBAAa,sBAAsB;IACrB,OAAO,CAAC,KAAK;gBAAL,KAAK,EAAE,OAAO;IAElC,0DAA0D;IACpD,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GAAG,IAAI,EACtB,SAAS,EAAE,MAAM,GAAG,IAAI,GACvB,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAYlC,sDAAsD;IAChD,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;CAU5F"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { unwrapJsonb, safeParse } from "../../../shared/postgres-validation.js";
|
|
3
|
+
const BillingTopupRowSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
topup_key: z.string().optional(),
|
|
6
|
+
credits_per_unit: z
|
|
7
|
+
.union([z.string(), z.number()])
|
|
8
|
+
.nullable()
|
|
9
|
+
.optional(),
|
|
10
|
+
credits_per_major_unit: z
|
|
11
|
+
.union([z.string(), z.number()])
|
|
12
|
+
.nullable()
|
|
13
|
+
.optional(),
|
|
14
|
+
tier: z.string().optional(),
|
|
15
|
+
deposit_to: z.string().optional(),
|
|
16
|
+
bucket_key: z.string().optional(),
|
|
17
|
+
amount_minor: z.union([z.string(), z.number()]).optional(),
|
|
18
|
+
currency: z.string().optional(),
|
|
19
|
+
min_quantity: z.number().optional(),
|
|
20
|
+
max_quantity: z.number().optional(),
|
|
21
|
+
default_quantity: z.number().optional(),
|
|
22
|
+
})
|
|
23
|
+
.passthrough();
|
|
24
|
+
/** Repository for credit top-up resolution. */
|
|
25
|
+
export class BillingTopupRepository {
|
|
26
|
+
query;
|
|
27
|
+
constructor(query) {
|
|
28
|
+
this.query = query;
|
|
29
|
+
}
|
|
30
|
+
/** Resolve a credit top-up by price ID and product ID. */
|
|
31
|
+
async resolveByPrice(provider, priceId, productId) {
|
|
32
|
+
const rows = await this.query("SELECT * FROM bursar.resolve_catalog_topup($1, $2, $3)", [
|
|
33
|
+
provider,
|
|
34
|
+
priceId != null ? "price_id" : "product_id",
|
|
35
|
+
priceId ?? productId,
|
|
36
|
+
]);
|
|
37
|
+
const data = unwrapJsonb(rows);
|
|
38
|
+
return data && data.topup_key != null
|
|
39
|
+
? safeParse(BillingTopupRowSchema, data, "BillingTopupRepository.resolveByPrice")
|
|
40
|
+
: null;
|
|
41
|
+
}
|
|
42
|
+
/** Resolve a credit top-up by provider lookup key. */
|
|
43
|
+
async resolveByLookup(provider, lookupKey) {
|
|
44
|
+
const rows = await this.query("SELECT * FROM bursar.resolve_catalog_topup($1, 'external_id', $2)", [provider, lookupKey]);
|
|
45
|
+
const data = unwrapJsonb(rows);
|
|
46
|
+
return data && data.topup_key != null
|
|
47
|
+
? safeParse(BillingTopupRowSchema, data, "BillingTopupRepository.resolveByLookup")
|
|
48
|
+
: null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=topup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"topup.js","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/topup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAEhF,MAAM,qBAAqB,GAAG,CAAC;KAC5B,MAAM,CAAC;IACN,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAChC,gBAAgB,EAAE,CAAC;SAChB,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAU,CAAC;SACxC,QAAQ,EAAE;SACV,QAAQ,EAAE;IACb,sBAAsB,EAAE,CAAC;SACtB,KAAK,CAAC,CAAC,CAA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AqB,EAAE,IAAI,EAAE,uCAAuC,CAAC;YACjF,CAAC,CAAC,IAAI,CAAC;IACX,CAAC;IAED,sDAAsD;IACtD,KAAK,CAAC,eAAe,CAAC,QAAgB,EAAE,SAAiB;QACvD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAC3B,mEAAmE,EACnE,CAAC,QAAQ,EAAE,SAAS,CAAC,CACtB,CAAC;QACF,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;QAC/B,OAAO,IAAI,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI;YACnC,CAAC,CAAC,SAAS,CAAC,qBAAqB,EAAE,IAAI,EAAE,wCAAwC,CAAC;YAClF,CAAC,CAAC,IAAI,CAAC;IACX,CAAC;CACF","sourcesContent":["import { z } from \"zod\";\nimport type { QueryFn } from \"../../../shared/postgres-types.js\";\nimport { unwrapJsonb, safeParse } from \"../../../shared/postgres-validation.js\";\n\nconst BillingTopupRowSchema = z\n .object({\n topup_key: z.string().optional(),\n credits_per_unit: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n credits_per_major_unit: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n tier: z.string().optional(),\n deposit_to: z.string().optional(),\n bucket_key: z.string().optional(),\n amount_minor: z.union([z.string(), z.number()]).optional(),\n currency: z.string().optional(),\n min_quantity: z.number().optional(),\n max_quantity: z.number().optional(),\n default_quantity: z.number().optional(),\n })\n .passthrough();\n\nexport type BillingTopupRow = z.infer<typeof BillingTopupRowSchema>;\n\n/** Repository for credit top-up resolution. */\nexport class BillingTopupRepository {\n constructor(private query: QueryFn) {}\n\n /** Resolve a credit top-up by price ID and product ID. */\n async resolveByPrice(\n provider: string,\n priceId: string | null,\n productId: string | null,\n ): Promise<BillingTopupRow | null> {\n const rows = await this.query(\"SELECT * FROM bursar.resolve_catalog_topup($1, $2, $3)\", [\n provider,\n priceId != null ? \"price_id\" : \"product_id\",\n priceId ?? productId,\n ]);\n const data = unwrapJsonb(rows);\n return data && data.topup_key != null\n ? safeParse(BillingTopupRowSchema, data, \"BillingTopupRepository.resolveByPrice\")\n : null;\n }\n\n /** Resolve a credit top-up by provider lookup key. */\n async resolveByLookup(provider: string, lookupKey: string): Promise<BillingTopupRow | null> {\n const rows = await this.query(\n \"SELECT * FROM bursar.resolve_catalog_topup($1, 'external_id', $2)\",\n [provider, lookupKey],\n );\n const data = unwrapJsonb(rows);\n return data && data.topup_key != null\n ? safeParse(BillingTopupRowSchema, data, \"BillingTopupRepository.resolveByLookup\")\n : null;\n }\n}\n"]}
|