@zonastery/bursar 1.0.0 → 2.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 +189 -62
- package/dist/billing/auto-recharge-port.d.ts +3 -3
- package/dist/billing/auto-recharge-port.d.ts.map +1 -1
- package/dist/billing/auto-recharge-port.js.map +1 -1
- package/dist/billing/auto-recharge-service.d.ts +5 -4
- package/dist/billing/auto-recharge-service.d.ts.map +1 -1
- package/dist/billing/auto-recharge-service.js +64 -34
- package/dist/billing/auto-recharge-service.js.map +1 -1
- package/dist/billing/billing-service.d.ts +5 -5
- package/dist/billing/billing-service.d.ts.map +1 -1
- package/dist/billing/billing-service.js +2 -2
- package/dist/billing/billing-service.js.map +1 -1
- package/dist/billing/billing-store.d.ts +10 -9
- 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 +31 -31
- package/dist/billing/contracts.d.ts.map +1 -1
- package/dist/billing/contracts.js.map +1 -1
- package/dist/billing/event-handlers.d.ts.map +1 -1
- package/dist/billing/event-handlers.js +49 -10
- package/dist/billing/event-handlers.js.map +1 -1
- package/dist/billing/event-processor.d.ts +1 -0
- package/dist/billing/event-processor.d.ts.map +1 -1
- package/dist/billing/event-processor.js +37 -10
- package/dist/billing/event-processor.js.map +1 -1
- package/dist/billing/financial-event-handlers.d.ts.map +1 -1
- package/dist/billing/financial-event-handlers.js +69 -72
- package/dist/billing/financial-event-handlers.js.map +1 -1
- package/dist/billing/index.d.ts +2 -1
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/management.d.ts +5 -5
- package/dist/billing/management.d.ts.map +1 -1
- package/dist/billing/management.js +2 -2
- package/dist/billing/management.js.map +1 -1
- package/dist/billing/policy-window.js.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.d.ts +3 -3
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.js +205 -60
- package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -1
- package/dist/billing/postgres/repositories/customer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/customer.js +19 -11
- package/dist/billing/postgres/repositories/customer.js.map +1 -1
- package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/dispute.js +3 -1
- package/dist/billing/postgres/repositories/dispute.js.map +1 -1
- package/dist/billing/postgres/repositories/event.d.ts +14 -7
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/event.js +21 -20
- package/dist/billing/postgres/repositories/event.js.map +1 -1
- package/dist/billing/postgres/repositories/invoice.d.ts +3 -3
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/invoice.js +53 -11
- package/dist/billing/postgres/repositories/invoice.js.map +1 -1
- package/dist/billing/postgres/repositories/offer.d.ts +20 -17
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/offer.js +123 -57
- package/dist/billing/postgres/repositories/offer.js.map +1 -1
- package/dist/billing/postgres/repositories/payment.d.ts +23 -20
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/payment.js +43 -37
- package/dist/billing/postgres/repositories/payment.js.map +1 -1
- package/dist/billing/postgres/repositories/preferences.d.ts +3 -9
- package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/preferences.js +45 -7
- package/dist/billing/postgres/repositories/preferences.js.map +1 -1
- package/dist/billing/postgres/repositories/refund.d.ts +1 -1
- package/dist/billing/postgres/repositories/refund.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/refund.js +13 -9
- package/dist/billing/postgres/repositories/refund.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.d.ts +15 -0
- package/dist/billing/postgres/repositories/subscription-change.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/subscription-change.js +234 -0
- package/dist/billing/postgres/repositories/subscription-change.js.map +1 -0
- package/dist/billing/postgres/repositories/subscription.d.ts +38 -17
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription.js +199 -91
- package/dist/billing/postgres/repositories/subscription.js.map +1 -1
- package/dist/billing/postgres/repositories/topup.d.ts +13 -15
- package/dist/billing/postgres/repositories/topup.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/topup.js +65 -34
- package/dist/billing/postgres/repositories/topup.js.map +1 -1
- package/dist/billing/postgres/store.d.ts +23 -14
- package/dist/billing/postgres/store.d.ts.map +1 -1
- package/dist/billing/postgres/store.js +265 -228
- package/dist/billing/postgres/store.js.map +1 -1
- package/dist/billing/service-types.d.ts +6 -5
- package/dist/billing/service-types.d.ts.map +1 -1
- package/dist/billing/service-types.js.map +1 -1
- package/dist/billing/types/auto-recharge.d.ts +6 -4
- package/dist/billing/types/auto-recharge.d.ts.map +1 -1
- package/dist/billing/types/auto-recharge.js.map +1 -1
- package/dist/billing/types/catalog.d.ts +18 -35
- package/dist/billing/types/catalog.d.ts.map +1 -1
- package/dist/billing/types/catalog.js.map +1 -1
- package/dist/billing/types/common.d.ts +0 -1
- package/dist/billing/types/common.d.ts.map +1 -1
- package/dist/billing/types/common.js.map +1 -1
- package/dist/billing/types/documents.d.ts +38 -12
- package/dist/billing/types/documents.d.ts.map +1 -1
- package/dist/billing/types/documents.js.map +1 -1
- package/dist/billing/types/events.d.ts +2 -0
- package/dist/billing/types/events.d.ts.map +1 -1
- package/dist/billing/types/events.js +238 -0
- package/dist/billing/types/events.js.map +1 -1
- package/dist/billing/types/index.js.map +1 -1
- package/dist/billing/types/preferences.js.map +1 -1
- package/dist/billing/types/subscriptions.d.ts +8 -8
- package/dist/billing/types/subscriptions.d.ts.map +1 -1
- package/dist/billing/types/subscriptions.js.map +1 -1
- package/dist/breakdown.js.map +1 -1
- package/dist/browser.js.map +1 -1
- package/dist/bursar.d.ts +33 -11
- package/dist/bursar.d.ts.map +1 -1
- package/dist/bursar.js +58 -22
- package/dist/bursar.js.map +1 -1
- package/dist/catalog.d.ts +1 -0
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +1 -0
- package/dist/catalog.js.map +1 -1
- package/dist/commerce/errors.d.ts +4 -14
- package/dist/commerce/errors.d.ts.map +1 -1
- package/dist/commerce/errors.js +4 -21
- package/dist/commerce/errors.js.map +1 -1
- package/dist/commerce/index.d.ts +1 -0
- package/dist/commerce/index.d.ts.map +1 -1
- package/dist/commerce/index.js.map +1 -1
- package/dist/commerce/plan-change.js.map +1 -1
- package/dist/commerce/provider-registry.d.ts.map +1 -1
- package/dist/commerce/provider-registry.js +27 -2
- package/dist/commerce/provider-registry.js.map +1 -1
- package/dist/commerce/service.d.ts +15 -7
- package/dist/commerce/service.d.ts.map +1 -1
- package/dist/commerce/service.js +130 -77
- package/dist/commerce/service.js.map +1 -1
- package/dist/commerce/types.d.ts +33 -13
- package/dist/commerce/types.d.ts.map +1 -1
- package/dist/commerce/types.js.map +1 -1
- package/dist/config/parse-access.js.map +1 -1
- package/dist/config/parse-commerce.js.map +1 -1
- package/dist/config/parse-credits.js.map +1 -1
- package/dist/config/parse-plans.d.ts.map +1 -1
- package/dist/config/parse-plans.js +30 -13
- package/dist/config/parse-plans.js.map +1 -1
- package/dist/config/parse-pricing.d.ts.map +1 -1
- package/dist/config/parse-pricing.js +16 -5
- package/dist/config/parse-pricing.js.map +1 -1
- package/dist/config/parse-utils.js.map +1 -1
- package/dist/config/types.d.ts +14 -1
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js.map +1 -1
- package/dist/config.d.ts +7 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +64 -1
- package/dist/config.js.map +1 -1
- package/dist/credits/{pricing-runtime.d.ts → catalog-runtime.d.ts} +9 -8
- package/dist/credits/catalog-runtime.d.ts.map +1 -0
- package/dist/credits/{pricing-runtime.js → catalog-runtime.js} +58 -38
- package/dist/credits/catalog-runtime.js.map +1 -0
- package/dist/credits/events.d.ts +4 -4
- package/dist/credits/events.d.ts.map +1 -1
- package/dist/credits/events.js +1 -1
- package/dist/credits/events.js.map +1 -1
- package/dist/credits/lease-workflow.d.ts +14 -14
- package/dist/credits/lease-workflow.d.ts.map +1 -1
- package/dist/credits/lease-workflow.js +18 -17
- package/dist/credits/lease-workflow.js.map +1 -1
- package/dist/credits/low-balance-monitor.d.ts +2 -2
- package/dist/credits/low-balance-monitor.d.ts.map +1 -1
- package/dist/credits/low-balance-monitor.js.map +1 -1
- package/dist/credits/postgres/repositories/analytics.d.ts +39 -35
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/analytics.js +55 -53
- package/dist/credits/postgres/repositories/analytics.js.map +1 -1
- package/dist/credits/postgres/repositories/balance.d.ts +28 -30
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/balance.js +100 -59
- package/dist/credits/postgres/repositories/balance.js.map +1 -1
- package/dist/credits/postgres/repositories/bucket.d.ts +14 -8
- package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/bucket.js +22 -21
- package/dist/credits/postgres/repositories/bucket.js.map +1 -1
- package/dist/credits/postgres/repositories/catalog.d.ts +31 -0
- package/dist/credits/postgres/repositories/catalog.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/catalog.js +90 -0
- package/dist/credits/postgres/repositories/catalog.js.map +1 -0
- package/dist/credits/postgres/repositories/deduction.d.ts +32 -21
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/deduction.js +171 -62
- package/dist/credits/postgres/repositories/deduction.js.map +1 -1
- package/dist/credits/postgres/repositories/lease.d.ts +21 -18
- package/dist/credits/postgres/repositories/lease.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/lease.js +157 -65
- package/dist/credits/postgres/repositories/lease.js.map +1 -1
- package/dist/credits/postgres/repositories/plan.d.ts +94 -42
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/plan.js +134 -104
- package/dist/credits/postgres/repositories/plan.js.map +1 -1
- package/dist/credits/postgres/repositories/team.d.ts +36 -27
- package/dist/credits/postgres/repositories/team.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/team.js +96 -52
- package/dist/credits/postgres/repositories/team.js.map +1 -1
- package/dist/credits/postgres/store.d.ts +31 -21
- package/dist/credits/postgres/store.d.ts.map +1 -1
- package/dist/credits/postgres/store.js +325 -235
- package/dist/credits/postgres/store.js.map +1 -1
- package/dist/credits/postgres/value-mappers.d.ts +7 -9
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -1
- package/dist/credits/postgres/value-mappers.js +42 -63
- package/dist/credits/postgres/value-mappers.js.map +1 -1
- package/dist/credits/queries.d.ts +5 -5
- package/dist/credits/queries.d.ts.map +1 -1
- package/dist/credits/queries.js +4 -4
- package/dist/credits/queries.js.map +1 -1
- package/dist/credits/service-errors.js.map +1 -1
- package/dist/credits/service-types.d.ts +42 -7
- package/dist/credits/service-types.d.ts.map +1 -1
- package/dist/credits/service-types.js.map +1 -1
- package/dist/credits/service.d.ts +51 -58
- package/dist/credits/service.d.ts.map +1 -1
- package/dist/credits/service.js +178 -113
- package/dist/credits/service.js.map +1 -1
- package/dist/credits/store.d.ts +25 -22
- package/dist/credits/store.d.ts.map +1 -1
- package/dist/credits/store.js +9 -5
- package/dist/credits/store.js.map +1 -1
- package/dist/credits/types/account.d.ts +50 -12
- package/dist/credits/types/account.d.ts.map +1 -1
- package/dist/credits/types/account.js.map +1 -1
- package/dist/credits/types/admission.d.ts +17 -6
- package/dist/credits/types/admission.d.ts.map +1 -1
- package/dist/credits/types/admission.js.map +1 -1
- package/dist/credits/types/analytics.d.ts +2 -2
- package/dist/credits/types/analytics.d.ts.map +1 -1
- package/dist/credits/types/analytics.js.map +1 -1
- package/dist/credits/types/catalog.d.ts +21 -14
- package/dist/credits/types/catalog.d.ts.map +1 -1
- package/dist/credits/types/catalog.js.map +1 -1
- package/dist/credits/types/grants.d.ts +22 -8
- package/dist/credits/types/grants.d.ts.map +1 -1
- package/dist/credits/types/grants.js.map +1 -1
- package/dist/credits/types/index.js.map +1 -1
- package/dist/credits/types/ledger.d.ts +19 -0
- package/dist/credits/types/ledger.d.ts.map +1 -1
- package/dist/credits/types/ledger.js.map +1 -1
- package/dist/credits/types/quotas.js.map +1 -1
- package/dist/credits/types/teams.d.ts +18 -6
- package/dist/credits/types/teams.d.ts.map +1 -1
- package/dist/credits/types/teams.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +24 -10
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +105 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +137 -9
- package/dist/errors.js.map +1 -1
- package/dist/expr/ast.d.ts.map +1 -1
- package/dist/expr/ast.js.map +1 -1
- package/dist/expr/evaluator.d.ts.map +1 -1
- package/dist/expr/evaluator.js +29 -17
- package/dist/expr/evaluator.js.map +1 -1
- package/dist/expr/language.js.map +1 -1
- package/dist/expr/parser.d.ts.map +1 -1
- package/dist/expr/parser.js +11 -4
- package/dist/expr/parser.js.map +1 -1
- package/dist/expr/tokenizer.d.ts.map +1 -1
- package/dist/expr/tokenizer.js +17 -13
- package/dist/expr/tokenizer.js.map +1 -1
- package/dist/expr.d.ts +2 -2
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +7 -9
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +30 -9
- package/dist/generated/pricing-config.types.d.ts +14 -3
- package/dist/generated/pricing-config.types.d.ts.map +1 -1
- package/dist/generated/pricing-config.types.js.map +1 -1
- package/dist/index.d.ts +11 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/integrations/better-auth.d.ts +23 -0
- package/dist/integrations/better-auth.d.ts.map +1 -0
- package/dist/integrations/better-auth.js +54 -0
- package/dist/integrations/better-auth.js.map +1 -0
- package/dist/{load-pricing-file.d.ts → load-config-file.d.ts} +4 -4
- package/dist/load-config-file.d.ts.map +1 -0
- package/dist/{load-pricing-file.js → load-config-file.js} +23 -14
- package/dist/load-config-file.js.map +1 -0
- package/dist/metrics.js.map +1 -1
- package/dist/node.d.ts +3 -3
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +2 -2
- package/dist/node.js.map +1 -1
- package/dist/providers/_shared.d.ts +5 -0
- package/dist/providers/_shared.d.ts.map +1 -1
- package/dist/providers/_shared.js +77 -14
- package/dist/providers/_shared.js.map +1 -1
- package/dist/providers/dodo/better-auth.d.ts +29 -0
- package/dist/providers/dodo/better-auth.d.ts.map +1 -0
- package/dist/providers/dodo/better-auth.js +23 -0
- package/dist/providers/dodo/better-auth.js.map +1 -0
- package/dist/providers/dodo/client-contract.d.ts +32 -70
- package/dist/providers/dodo/client-contract.d.ts.map +1 -1
- package/dist/providers/dodo/client-contract.js.map +1 -1
- package/dist/providers/dodo/event-mapper.d.ts +3 -1
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +219 -148
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/index.d.ts +5 -2
- package/dist/providers/dodo/index.d.ts.map +1 -1
- package/dist/providers/dodo/index.js +2 -1
- package/dist/providers/dodo/index.js.map +1 -1
- package/dist/providers/dodo/nextjs.d.ts +16 -0
- package/dist/providers/dodo/nextjs.d.ts.map +1 -0
- package/dist/providers/dodo/nextjs.js +16 -0
- package/dist/providers/dodo/nextjs.js.map +1 -0
- package/dist/providers/dodo/provider.d.ts +30 -11
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +192 -122
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/dodo/react-native.d.ts +56 -0
- package/dist/providers/dodo/react-native.d.ts.map +1 -0
- package/dist/providers/dodo/react-native.js +57 -0
- package/dist/providers/dodo/react-native.js.map +1 -0
- package/dist/providers/dodo/webhook-bridge.d.ts +17 -0
- package/dist/providers/dodo/webhook-bridge.d.ts.map +1 -0
- package/dist/providers/dodo/webhook-bridge.js +21 -0
- package/dist/providers/dodo/webhook-bridge.js.map +1 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/mock/index.d.ts +1 -0
- package/dist/providers/mock/index.d.ts.map +1 -1
- package/dist/providers/mock/index.js.map +1 -1
- package/dist/providers/mock/provider.d.ts +8 -8
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +26 -19
- package/dist/providers/mock/provider.js.map +1 -1
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
- package/dist/providers/stripe/event-mapper.js +214 -173
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/index.d.ts +1 -0
- package/dist/providers/stripe/index.d.ts.map +1 -1
- package/dist/providers/stripe/index.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts +13 -11
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +287 -107
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts +1 -2
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js.map +1 -1
- package/dist/retry.d.ts +26 -4
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +65 -23
- package/dist/retry.js.map +1 -1
- 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.js.map +1 -1
- package/dist/shared/postgres-client.d.ts +43 -5
- package/dist/shared/postgres-client.d.ts.map +1 -1
- package/dist/shared/postgres-client.js +282 -30
- package/dist/shared/postgres-client.js.map +1 -1
- 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.js.map +1 -1
- package/dist/shared/postgres-validation.d.ts +15 -3
- package/dist/shared/postgres-validation.d.ts.map +1 -1
- package/dist/shared/postgres-validation.js +58 -14
- package/dist/shared/postgres-validation.js.map +1 -1
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -1
- package/dist/storage/adapters/clickhouse.js +18 -3
- package/dist/storage/adapters/clickhouse.js.map +1 -1
- package/dist/storage/adapters/s3.d.ts +0 -6
- package/dist/storage/adapters/s3.d.ts.map +1 -1
- package/dist/storage/adapters/s3.js +0 -2
- package/dist/storage/adapters/s3.js.map +1 -1
- package/dist/storage/outbox-worker.d.ts +2 -1
- package/dist/storage/outbox-worker.d.ts.map +1 -1
- package/dist/storage/outbox-worker.js +17 -3
- package/dist/storage/outbox-worker.js.map +1 -1
- package/dist/storage/ports.d.ts +1 -1
- package/dist/storage/ports.d.ts.map +1 -1
- package/dist/storage/ports.js.map +1 -1
- package/dist/storage/postgres-repository.d.ts +1 -1
- package/dist/storage/postgres-repository.d.ts.map +1 -1
- package/dist/storage/postgres-repository.js +13 -3
- package/dist/storage/postgres-repository.js.map +1 -1
- package/dist/storage/runtime.d.ts +35 -7
- package/dist/storage/runtime.d.ts.map +1 -1
- package/dist/storage/runtime.js +166 -53
- package/dist/storage/runtime.js.map +1 -1
- package/package.json +62 -7
- package/dist/credits/postgres/repositories/pricing.d.ts +0 -28
- package/dist/credits/postgres/repositories/pricing.d.ts.map +0 -1
- package/dist/credits/postgres/repositories/pricing.js +0 -70
- package/dist/credits/postgres/repositories/pricing.js.map +0 -1
- package/dist/credits/pricing-runtime.d.ts.map +0 -1
- package/dist/credits/pricing-runtime.js.map +0 -1
- package/dist/load-pricing-file.d.ts.map +0 -1
- package/dist/load-pricing-file.js.map +0 -1
|
@@ -1,42 +1,111 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
2
|
+
import { StoreError } from "../../../errors.js";
|
|
3
|
+
import { optionalRecordRow, pgBoolean, postgresUuid, requireResultField, safeParse, } from "../../../shared/postgres-validation.js";
|
|
4
|
+
const timestamp = z
|
|
5
|
+
.union([
|
|
6
|
+
z.date().refine((value) => !Number.isNaN(value.getTime())),
|
|
7
|
+
z.string().datetime({ offset: true }),
|
|
8
|
+
])
|
|
9
|
+
.transform((value) => (value instanceof Date ? value : new Date(value)).toISOString());
|
|
10
|
+
const SubscriptionStatusSchema = z.enum([
|
|
11
|
+
"incomplete",
|
|
12
|
+
"incomplete_expired",
|
|
13
|
+
"trialing",
|
|
14
|
+
"active",
|
|
15
|
+
"past_due",
|
|
16
|
+
"canceled",
|
|
17
|
+
"unpaid",
|
|
18
|
+
"paused",
|
|
19
|
+
"expired",
|
|
20
|
+
]);
|
|
21
|
+
const PersistedSubscriptionRowSchema = z
|
|
22
|
+
.object({
|
|
23
|
+
id: postgresUuid,
|
|
24
|
+
subject_id: postgresUuid,
|
|
25
|
+
provider: z.string().min(1),
|
|
26
|
+
provider_subscription_id: z.string().min(1),
|
|
27
|
+
provider_customer_id: z.string().min(1).nullable(),
|
|
28
|
+
offer_id: postgresUuid,
|
|
29
|
+
catalog_revision_id: postgresUuid,
|
|
30
|
+
status: SubscriptionStatusSchema,
|
|
31
|
+
current_period_start: timestamp.nullable(),
|
|
32
|
+
current_period_end: timestamp.nullable(),
|
|
33
|
+
trial_end: timestamp.nullable(),
|
|
34
|
+
cancel_at: timestamp.nullable(),
|
|
35
|
+
ended_at: timestamp.nullable(),
|
|
36
|
+
grace_ends_at: timestamp.nullable(),
|
|
37
|
+
grace_expired_at: timestamp.nullable(),
|
|
38
|
+
provider_updated_at: timestamp,
|
|
39
|
+
cancel_at_period_end: pgBoolean,
|
|
40
|
+
metadata: z.record(z.string(), z.unknown()),
|
|
41
|
+
})
|
|
42
|
+
.strict();
|
|
43
|
+
const CatalogReferenceSchema = z
|
|
44
|
+
.object({
|
|
45
|
+
offer_id: postgresUuid,
|
|
46
|
+
catalog_revision_id: postgresUuid,
|
|
47
|
+
})
|
|
48
|
+
.strict();
|
|
49
|
+
const OfferContextSchema = z
|
|
50
|
+
.object({
|
|
51
|
+
offer_key: z.string().min(1),
|
|
52
|
+
plan_id: postgresUuid,
|
|
53
|
+
plan_key: z.string().min(1),
|
|
54
|
+
billing_unit: z.enum(["day", "week", "month", "year"]),
|
|
55
|
+
billing_count: z.number().int().positive(),
|
|
56
|
+
})
|
|
57
|
+
.strict();
|
|
3
58
|
const SubscriptionRowSchema = z
|
|
4
59
|
.object({
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
provider: z.string(),
|
|
8
|
-
provider_subscription_id: z.string(),
|
|
9
|
-
provider_customer_id: z.string().
|
|
10
|
-
offer_id:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
60
|
+
id: postgresUuid,
|
|
61
|
+
user_id: postgresUuid,
|
|
62
|
+
provider: z.string().min(1),
|
|
63
|
+
provider_subscription_id: z.string().min(1),
|
|
64
|
+
provider_customer_id: z.string().min(1).nullable(),
|
|
65
|
+
offer_id: postgresUuid,
|
|
66
|
+
offer_key: z.string().min(1),
|
|
67
|
+
plan_id: postgresUuid,
|
|
68
|
+
plan: z.string().min(1),
|
|
69
|
+
status: SubscriptionStatusSchema,
|
|
70
|
+
current_period_start: timestamp.nullable(),
|
|
71
|
+
current_period_end: timestamp.nullable(),
|
|
72
|
+
trial_end: timestamp.nullable(),
|
|
73
|
+
cancel_at: timestamp.nullable(),
|
|
74
|
+
ended_at: timestamp.nullable(),
|
|
75
|
+
grace_ends_at: timestamp.nullable(),
|
|
76
|
+
grace_expired_at: timestamp.nullable(),
|
|
77
|
+
provider_updated_at: timestamp,
|
|
78
|
+
cancel_at_period_end: pgBoolean,
|
|
79
|
+
interval: z.enum(["day", "week", "month", "year"]),
|
|
80
|
+
interval_count: z.number().int().positive(),
|
|
81
|
+
metadata: z.record(z.string(), z.unknown()),
|
|
22
82
|
})
|
|
23
|
-
.
|
|
24
|
-
function
|
|
25
|
-
|
|
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;
|
|
83
|
+
.strict();
|
|
84
|
+
function record(value, context) {
|
|
85
|
+
return safeParse(z.record(z.string(), z.unknown()), value, context);
|
|
31
86
|
}
|
|
32
|
-
function
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
87
|
+
function persistedSubscription(row, context) {
|
|
88
|
+
const value = record(row, context);
|
|
89
|
+
return safeParse(PersistedSubscriptionRowSchema, {
|
|
90
|
+
id: value.id,
|
|
91
|
+
subject_id: value.subject_id,
|
|
92
|
+
provider: value.provider,
|
|
93
|
+
provider_subscription_id: value.provider_subscription_id,
|
|
94
|
+
provider_customer_id: value.provider_customer_id,
|
|
95
|
+
offer_id: value.offer_id,
|
|
96
|
+
catalog_revision_id: value.catalog_revision_id,
|
|
97
|
+
status: value.status,
|
|
98
|
+
current_period_start: value.current_period_start,
|
|
99
|
+
current_period_end: value.current_period_end,
|
|
100
|
+
trial_end: value.trial_end,
|
|
101
|
+
cancel_at: value.cancel_at,
|
|
102
|
+
ended_at: value.ended_at,
|
|
103
|
+
grace_ends_at: value.grace_ends_at,
|
|
104
|
+
grace_expired_at: value.grace_expired_at,
|
|
105
|
+
provider_updated_at: value.provider_updated_at,
|
|
106
|
+
cancel_at_period_end: value.cancel_at_period_end,
|
|
107
|
+
metadata: value.metadata,
|
|
108
|
+
}, context);
|
|
40
109
|
}
|
|
41
110
|
export class BillingSubscriptionRepository {
|
|
42
111
|
query;
|
|
@@ -44,65 +113,103 @@ export class BillingSubscriptionRepository {
|
|
|
44
113
|
this.query = query;
|
|
45
114
|
}
|
|
46
115
|
async upsert(state) {
|
|
47
|
-
const provider =
|
|
48
|
-
const providerSubscriptionId =
|
|
49
|
-
|
|
116
|
+
const provider = state.provider;
|
|
117
|
+
const providerSubscriptionId = state.providerSubscriptionId;
|
|
118
|
+
const userId = state.userId;
|
|
50
119
|
let providerCustomerId = state.providerCustomerId ?? null;
|
|
51
|
-
let offerId =
|
|
120
|
+
let offerId = state.offerId ?? null;
|
|
52
121
|
if (!provider || !providerSubscriptionId) {
|
|
53
|
-
throw new
|
|
122
|
+
throw new TypeError("subscription.upsert: subject, provider, subscription, and offer are required");
|
|
54
123
|
}
|
|
55
124
|
const existing = await this.query(`SELECT * FROM bursar.get_billing_subscription_by_provider($1, $2)`, [provider, providerSubscriptionId]);
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
125
|
+
const rawExisting = optionalRecordRow(existing, "BillingSubscriptionRepository.upsert.existing");
|
|
126
|
+
const existingRow = rawExisting === null
|
|
127
|
+
? null
|
|
128
|
+
: persistedSubscription(rawExisting, "BillingSubscriptionRepository.upsert.existing");
|
|
129
|
+
if (providerCustomerId === null && existingRow !== null) {
|
|
130
|
+
providerCustomerId = existingRow.provider_customer_id;
|
|
61
131
|
}
|
|
62
|
-
if (
|
|
63
|
-
offerId =
|
|
64
|
-
const offerKey =
|
|
65
|
-
if (
|
|
132
|
+
if (offerId === null && existingRow !== null)
|
|
133
|
+
offerId = existingRow.offer_id;
|
|
134
|
+
const offerKey = state.offerKey ?? null;
|
|
135
|
+
if (offerId === null && offerKey !== null) {
|
|
66
136
|
const offerRows = await this.query(`SELECT * FROM bursar.resolve_active_catalog_offer($1)`, [
|
|
67
137
|
offerKey,
|
|
68
138
|
]);
|
|
69
|
-
const offerRow = offerRows
|
|
70
|
-
if (offerRow
|
|
71
|
-
offerId =
|
|
139
|
+
const offerRow = optionalRecordRow(offerRows, "BillingSubscriptionRepository.upsert.offer");
|
|
140
|
+
if (offerRow !== null) {
|
|
141
|
+
offerId = safeParse(postgresUuid, offerRow.id, "BillingSubscriptionRepository.upsert.offer.id");
|
|
142
|
+
}
|
|
72
143
|
}
|
|
73
144
|
if (!userId || !offerId) {
|
|
74
|
-
throw new
|
|
145
|
+
throw new TypeError("subscription.upsert: subject, provider, subscription, and offer are required");
|
|
75
146
|
}
|
|
76
|
-
|
|
147
|
+
const providerUpdatedAt = safeParse(timestamp, state.providerUpdatedAt, "BillingSubscriptionRepository.upsert.providerUpdatedAt");
|
|
148
|
+
const rows = 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) AS id", [
|
|
77
149
|
userId,
|
|
78
150
|
provider,
|
|
79
151
|
providerSubscriptionId,
|
|
80
152
|
providerCustomerId,
|
|
81
153
|
offerId,
|
|
82
|
-
state.status
|
|
154
|
+
state.status,
|
|
83
155
|
state.currentPeriodStart ?? null,
|
|
84
156
|
state.currentPeriodEnd ?? null,
|
|
85
|
-
state.cancelAtPeriodEnd
|
|
157
|
+
state.cancelAtPeriodEnd,
|
|
86
158
|
JSON.stringify(state.metadata ?? {}),
|
|
87
159
|
state.trialEnd ?? null,
|
|
88
160
|
state.cancelAt ?? null,
|
|
89
161
|
state.endedAt ?? null,
|
|
90
|
-
|
|
162
|
+
providerUpdatedAt,
|
|
91
163
|
state.graceEndsAt ?? null,
|
|
92
164
|
]);
|
|
165
|
+
requireResultField(rows, "id", postgresUuid, "BillingSubscriptionRepository.upsert");
|
|
93
166
|
}
|
|
94
167
|
map(row) {
|
|
95
|
-
const r = row;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
168
|
+
const r = record(row, "BillingSubscriptionRepository.map");
|
|
169
|
+
return safeParse(SubscriptionRowSchema, {
|
|
170
|
+
id: r.id,
|
|
171
|
+
user_id: r.subject_id,
|
|
172
|
+
provider: r.provider,
|
|
173
|
+
provider_subscription_id: r.provider_subscription_id,
|
|
174
|
+
provider_customer_id: r.provider_customer_id,
|
|
175
|
+
offer_id: r.offer_id,
|
|
176
|
+
offer_key: r.offer_key,
|
|
177
|
+
plan_id: r.plan_id,
|
|
178
|
+
plan: r.plan,
|
|
179
|
+
status: r.status,
|
|
180
|
+
current_period_start: r.current_period_start,
|
|
181
|
+
current_period_end: r.current_period_end,
|
|
182
|
+
trial_end: r.trial_end,
|
|
183
|
+
cancel_at: r.cancel_at,
|
|
184
|
+
ended_at: r.ended_at,
|
|
185
|
+
grace_ends_at: r.grace_ends_at,
|
|
186
|
+
grace_expired_at: r.grace_expired_at,
|
|
187
|
+
provider_updated_at: r.provider_updated_at,
|
|
188
|
+
cancel_at_period_end: r.cancel_at_period_end,
|
|
189
|
+
interval: r.interval,
|
|
190
|
+
interval_count: r.interval_count,
|
|
191
|
+
metadata: r.metadata,
|
|
192
|
+
}, "BillingSubscriptionRepository");
|
|
99
193
|
}
|
|
100
|
-
async withOfferContext(
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
194
|
+
async withOfferContext(value) {
|
|
195
|
+
const reference = safeParse(CatalogReferenceSchema, { offer_id: value.offer_id, catalog_revision_id: value.catalog_revision_id }, "BillingSubscriptionRepository.withOfferContext.reference");
|
|
196
|
+
const contextRows = await this.query(`SELECT * FROM bursar.get_catalog_offer_context($1::uuid, $2::uuid)`, [reference.offer_id, reference.catalog_revision_id]);
|
|
197
|
+
const rawContext = optionalRecordRow(contextRows, "BillingSubscriptionRepository.withOfferContext");
|
|
198
|
+
if (rawContext === null) {
|
|
199
|
+
throw new StoreError("Billing subscription offer context is missing", {
|
|
200
|
+
details: {
|
|
201
|
+
offerId: reference.offer_id,
|
|
202
|
+
catalogRevisionId: reference.catalog_revision_id,
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const context = safeParse(OfferContextSchema, {
|
|
207
|
+
offer_key: rawContext.offer_key,
|
|
208
|
+
plan_id: rawContext.plan_id,
|
|
209
|
+
plan_key: rawContext.plan_key,
|
|
210
|
+
billing_unit: rawContext.billing_unit,
|
|
211
|
+
billing_count: rawContext.billing_count,
|
|
212
|
+
}, "BillingSubscriptionRepository.withOfferContext.context");
|
|
106
213
|
return {
|
|
107
214
|
...value,
|
|
108
215
|
...context,
|
|
@@ -113,40 +220,43 @@ export class BillingSubscriptionRepository {
|
|
|
113
220
|
}
|
|
114
221
|
async get(provider, providerSubscriptionId) {
|
|
115
222
|
const rows = await this.query(`SELECT * FROM bursar.get_billing_subscription_by_provider($1, $2)`, [provider, providerSubscriptionId]);
|
|
116
|
-
|
|
223
|
+
const row = optionalRecordRow(rows, "BillingSubscriptionRepository.get");
|
|
224
|
+
return row === null
|
|
225
|
+
? null
|
|
226
|
+
: this.map(await this.withOfferContext(persistedSubscription(row, "BillingSubscriptionRepository.get")));
|
|
117
227
|
}
|
|
118
228
|
async getUserSubscription(userId, statuses) {
|
|
229
|
+
const allowed = new Set(safeParse(z.array(SubscriptionStatusSchema), statuses ?? ["active", "trialing"], "BillingSubscriptionRepository.getUserSubscription.statuses"));
|
|
119
230
|
const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [
|
|
120
231
|
userId,
|
|
121
232
|
]);
|
|
122
|
-
const allowed = statuses ?? ["active", "trialing"];
|
|
123
233
|
const candidates = rows
|
|
124
|
-
.
|
|
125
|
-
.
|
|
126
|
-
|
|
234
|
+
.map((row) => persistedSubscription(row, "BillingSubscriptionRepository.getUserSubscription"))
|
|
235
|
+
.filter((row) => allowed.has(row.status))
|
|
236
|
+
.sort((left, right) => Date.parse(right.provider_updated_at) - Date.parse(left.provider_updated_at));
|
|
237
|
+
const selected = candidates[0];
|
|
238
|
+
return selected === undefined ? null : this.map(await this.withOfferContext(selected));
|
|
127
239
|
}
|
|
128
240
|
async getUserSubscriptions(userId) {
|
|
129
241
|
const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [
|
|
130
242
|
userId,
|
|
131
243
|
]);
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
.filter((row) => row !== null);
|
|
244
|
+
const persisted = rows.map((row) => persistedSubscription(row, "BillingSubscriptionRepository.getUserSubscriptions"));
|
|
245
|
+
const enriched = await Promise.all(persisted.map((row) => this.withOfferContext(row)));
|
|
246
|
+
return enriched.map((row) => this.map(row));
|
|
136
247
|
}
|
|
137
248
|
async listExpiredGraceSubscriptions(now, limit = 100) {
|
|
138
249
|
const rows = await this.query(`SELECT * FROM bursar.list_expired_grace_subscriptions($1, $2)`, [
|
|
139
250
|
now,
|
|
140
251
|
limit,
|
|
141
252
|
]);
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
.filter((row) => row !== null);
|
|
253
|
+
const persisted = rows.map((row) => persistedSubscription(row, "BillingSubscriptionRepository.listExpiredGraceSubscriptions"));
|
|
254
|
+
const enriched = await Promise.all(persisted.map((row) => this.withOfferContext(row)));
|
|
255
|
+
return enriched.map((row) => this.map(row));
|
|
146
256
|
}
|
|
147
257
|
async markGraceExpired(subscriptionId, expectedGraceEndsAt, expiredAt) {
|
|
148
258
|
const rows = await this.query(`SELECT bursar.mark_subscription_grace_expired($1::uuid, $2, $3) AS marked`, [subscriptionId, expectedGraceEndsAt, expiredAt]);
|
|
149
|
-
return rows
|
|
259
|
+
return requireResultField(rows, "marked", pgBoolean, "BillingSubscriptionRepository.markGraceExpired");
|
|
150
260
|
}
|
|
151
261
|
async recordConflict(input) {
|
|
152
262
|
const rows = await this.query(`SELECT bursar.record_subscription_conflict(
|
|
@@ -159,27 +269,25 @@ export class BillingSubscriptionRepository {
|
|
|
159
269
|
input.eventId ?? null,
|
|
160
270
|
JSON.stringify(input.metadata ?? {}),
|
|
161
271
|
]);
|
|
162
|
-
|
|
163
|
-
if (id == null)
|
|
164
|
-
throw new Error("subscription conflict audit returned no ID");
|
|
165
|
-
return String(id);
|
|
272
|
+
return requireResultField(rows, "id", z.union([z.string().min(1), z.number().int()]).transform(String), "BillingSubscriptionRepository.recordConflict");
|
|
166
273
|
}
|
|
167
274
|
/** Select a provider subscription without modifying provider-reported state. */
|
|
168
275
|
async selectEntitlementSource(userId, provider, providerSubscriptionId) {
|
|
169
|
-
const rows = (await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [
|
|
170
|
-
userId,
|
|
171
|
-
]));
|
|
172
276
|
const eligibleStatuses = new Set(["trialing", "active", "past_due", "paused"]);
|
|
277
|
+
const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [
|
|
278
|
+
userId,
|
|
279
|
+
]);
|
|
173
280
|
const replacement = rows
|
|
281
|
+
.map((row) => persistedSubscription(row, "BillingSubscriptionRepository.selectEntitlementSource"))
|
|
174
282
|
.filter((row) => row.provider === provider &&
|
|
175
|
-
eligibleStatuses.has(
|
|
283
|
+
eligibleStatuses.has(row.status) &&
|
|
176
284
|
(!providerSubscriptionId || row.provider_subscription_id === providerSubscriptionId))
|
|
177
|
-
.sort(
|
|
285
|
+
.sort((left, right) => Date.parse(right.provider_updated_at) - Date.parse(left.provider_updated_at))[0];
|
|
178
286
|
if (replacement?.id == null)
|
|
179
287
|
return false;
|
|
180
288
|
const selectedRows = await this.query(`SELECT bursar.select_entitlement_source($1::uuid, $2::uuid) AS selected`, [userId, replacement.id]);
|
|
181
|
-
if (selectedRows
|
|
182
|
-
throw new
|
|
289
|
+
if (!requireResultField(selectedRows, "selected", pgBoolean, "BillingSubscriptionRepository.selectEntitlementSource")) {
|
|
290
|
+
throw new StoreError("subscription entitlement source selection was rejected");
|
|
183
291
|
}
|
|
184
292
|
return true;
|
|
185
293
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.js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Q,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC;QAC/E,MAAM,WAAW,GAAG,IAAI;aACrB,MAAM,CACL,CAAC,GAAG,EAAE,EAAE,CACN,GAAG,CAAC,QAAQ,KAAK,QAAQ;YACzB,gBAAgB,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACxC,CAAC,CAAC,sBAAsB,IAAI,GAAG,CAAC,wBAAwB,KAAK,sBAAsB,CAAC,CACvF;aACA,IAAI,CAAC,mCAAmC,CAAC,CAAC,CAAC,CAAC,CAAC;QAEhD,IAAI,WAAW,EAAE,EAAE,IAAI,IAAI;YAAE,OAAO,KAAK,CAAC;QAE1C,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,KAAK,CACnC,yEAAyE,EACzE,CAAC,MAAM,EAAE,WAAW,CAAC,EAAE,CAAC,CACzB,CAAC;QACF,IAAK,YAAY,CAAC,CAAC,CAAyC,EAAE,QAAQ,KAAK,IAAI,EAAE,CAAC;YAChF,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;QAC5E,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EACL,iBAAiB,EACjB,SAAS,EACT,YAAY,EACZ,kBAAkB,EAClB,SAAS,GACV,MAAM,wCAAwC,CAAC;AAEhD,MAAM,SAAS,GAAG,CAAC;KAChB,KAAK,CAAC;IACL,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;IAC1D,CAAC,C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,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC;QAC/E,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,2DAA2D,EAAE;YACzF,MAAM;SACP,CAAC,CAAC;QACH,MAAM,WAAW,GAAG,IAAI;aACrB,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CACX,qBAAqB,CAAC,GAAG,EAAE,uDAAuD,CAAC,CACpF;aACA,MAAM,CACL,CAAC,GAAG,EAAE,EAAE,CACN,GAAG,CAAC,QAAQ,KAAK,QAAQ;YACzB,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC;YAChC,CAAC,CAAC,sBAAsB,IAAI,GAAG,CAAC,wBAAwB,KAAK,sBAAsB,CAAC,CACvF;aACA,IAAI,CACH,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CACd,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,mBAAmB,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAC/E,CAAC,CAAC,CAAC,CAAC;QAEP,IAAI,WAAW,EAAE,EAAE,IAAI,IAAI;YAAE,OAAO,KAAK,CAAC;QAE1C,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,KAAK,CACnC,yEAAyE,EACzE,CAAC,MAAM,EAAE,WAAW,CAAC,EAAE,CAAC,CACzB,CAAC;QACF,IACE,CAAC,kBAAkB,CACjB,YAAY,EACZ,UAAU,EACV,SAAS,EACT,uDAAuD,CACxD,EACD,CAAC;YACD,MAAM,IAAI,UAAU,CAAC,wDAAwD,CAAC,CAAC;QACjF,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import { z } from \"zod\";\nimport type { BillingSubscriptionState } from \"../../types/subscriptions.js\";\nimport type { QueryFn } from \"../../../shared/postgres-types.js\";\nimport { StoreError } from \"../../../errors.js\";\nimport {\n optionalRecordRow,\n pgBoolean,\n postgresUuid,\n requireResultField,\n safeParse,\n} from \"../../../shared/postgres-validation.js\";\n\nconst timestamp = z\n .union([\n z.date().refine((value) => !Number.isNaN(value.getTime())),\n z.string().datetime({ offset: true }),\n ])\n .transform((value) => (value instanceof Date ? value : new Date(value)).toISOString());\nconst SubscriptionStatusSchema = z.enum([\n \"incomplete\",\n \"incomplete_expired\",\n \"trialing\",\n \"active\",\n \"past_due\",\n \"canceled\",\n \"unpaid\",\n \"paused\",\n \"expired\",\n]);\nconst PersistedSubscriptionRowSchema = z\n .object({\n id: postgresUuid,\n subject_id: postgresUuid,\n provider: z.string().min(1),\n provider_subscription_id: z.string().min(1),\n provider_customer_id: z.string().min(1).nullable(),\n offer_id: postgresUuid,\n catalog_revision_id: postgresUuid,\n status: SubscriptionStatusSchema,\n current_period_start: timestamp.nullable(),\n current_period_end: timestamp.nullable(),\n trial_end: timestamp.nullable(),\n cancel_at: timestamp.nullable(),\n ended_at: timestamp.nullable(),\n grace_ends_at: timestamp.nullable(),\n grace_expired_at: timestamp.nullable(),\n provider_updated_at: timestamp,\n cancel_at_period_end: pgBoolean,\n metadata: z.record(z.string(), z.unknown()),\n })\n .strict();\nconst CatalogReferenceSchema = z\n .object({\n offer_id: postgresUuid,\n catalog_revision_id: postgresUuid,\n })\n .strict();\nconst OfferContextSchema = z\n .object({\n offer_key: z.string().min(1),\n plan_id: postgresUuid,\n plan_key: z.string().min(1),\n billing_unit: z.enum([\"day\", \"week\", \"month\", \"year\"]),\n billing_count: z.number().int().positive(),\n })\n .strict();\n\nconst SubscriptionRowSchema = z\n .object({\n id: postgresUuid,\n user_id: postgresUuid,\n provider: z.string().min(1),\n provider_subscription_id: z.string().min(1),\n provider_customer_id: z.string().min(1).nullable(),\n offer_id: postgresUuid,\n offer_key: z.string().min(1),\n plan_id: postgresUuid,\n plan: z.string().min(1),\n status: SubscriptionStatusSchema,\n current_period_start: timestamp.nullable(),\n current_period_end: timestamp.nullable(),\n trial_end: timestamp.nullable(),\n cancel_at: timestamp.nullable(),\n ended_at: timestamp.nullable(),\n grace_ends_at: timestamp.nullable(),\n grace_expired_at: timestamp.nullable(),\n provider_updated_at: timestamp,\n cancel_at_period_end: pgBoolean,\n interval: z.enum([\"day\", \"week\", \"month\", \"year\"]),\n interval_count: z.number().int().positive(),\n metadata: z.record(z.string(), z.unknown()),\n })\n .strict();\n\nexport type SubscriptionRow = z.infer<typeof SubscriptionRowSchema>;\ntype PersistedSubscriptionRow = z.infer<typeof PersistedSubscriptionRowSchema>;\n\nfunction record(value: unknown, context: string): Record<string, unknown> {\n return safeParse(z.record(z.string(), z.unknown()), value, context);\n}\n\nfunction persistedSubscription(row: unknown, context: string): PersistedSubscriptionRow {\n const value = record(row, context);\n return safeParse(\n PersistedSubscriptionRowSchema,\n {\n id: value.id,\n subject_id: value.subject_id,\n provider: value.provider,\n provider_subscription_id: value.provider_subscription_id,\n provider_customer_id: value.provider_customer_id,\n offer_id: value.offer_id,\n catalog_revision_id: value.catalog_revision_id,\n status: value.status,\n current_period_start: value.current_period_start,\n current_period_end: value.current_period_end,\n trial_end: value.trial_end,\n cancel_at: value.cancel_at,\n ended_at: value.ended_at,\n grace_ends_at: value.grace_ends_at,\n grace_expired_at: value.grace_expired_at,\n provider_updated_at: value.provider_updated_at,\n cancel_at_period_end: value.cancel_at_period_end,\n metadata: value.metadata,\n },\n context,\n );\n}\n\nexport class BillingSubscriptionRepository {\n constructor(private query: QueryFn) {}\n\n async upsert(state: BillingSubscriptionState): Promise<void> {\n const provider = state.provider;\n const providerSubscriptionId = state.providerSubscriptionId;\n const userId = state.userId;\n let providerCustomerId = state.providerCustomerId ?? null;\n let offerId = state.offerId ?? null;\n\n if (!provider || !providerSubscriptionId) {\n throw new TypeError(\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 rawExisting = optionalRecordRow(\n existing,\n \"BillingSubscriptionRepository.upsert.existing\",\n );\n const existingRow =\n rawExisting === null\n ? null\n : persistedSubscription(rawExisting, \"BillingSubscriptionRepository.upsert.existing\");\n if (providerCustomerId === null && existingRow !== null) {\n providerCustomerId = existingRow.provider_customer_id;\n }\n if (offerId === null && existingRow !== null) offerId = existingRow.offer_id;\n\n const offerKey = state.offerKey ?? null;\n if (offerId === null && offerKey !== null) {\n const offerRows = await this.query(`SELECT * FROM bursar.resolve_active_catalog_offer($1)`, [\n offerKey,\n ]);\n const offerRow = optionalRecordRow(offerRows, \"BillingSubscriptionRepository.upsert.offer\");\n if (offerRow !== null) {\n offerId = safeParse(\n postgresUuid,\n offerRow.id,\n \"BillingSubscriptionRepository.upsert.offer.id\",\n );\n }\n }\n\n if (!userId || !offerId) {\n throw new TypeError(\n \"subscription.upsert: subject, provider, subscription, and offer are required\",\n );\n }\n const providerUpdatedAt = safeParse(\n timestamp,\n state.providerUpdatedAt,\n \"BillingSubscriptionRepository.upsert.providerUpdatedAt\",\n );\n\n const rows = 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) AS id\",\n [\n userId,\n provider,\n providerSubscriptionId,\n providerCustomerId,\n offerId,\n state.status,\n state.currentPeriodStart ?? null,\n state.currentPeriodEnd ?? null,\n state.cancelAtPeriodEnd,\n JSON.stringify(state.metadata ?? {}),\n state.trialEnd ?? null,\n state.cancelAt ?? null,\n state.endedAt ?? null,\n providerUpdatedAt,\n state.graceEndsAt ?? null,\n ],\n );\n requireResultField(rows, \"id\", postgresUuid, \"BillingSubscriptionRepository.upsert\");\n }\n\n private map(row: unknown): SubscriptionRow {\n const r = record(row, \"BillingSubscriptionRepository.map\");\n return safeParse(\n SubscriptionRowSchema,\n {\n id: r.id,\n user_id: r.subject_id,\n provider: r.provider,\n provider_subscription_id: r.provider_subscription_id,\n provider_customer_id: r.provider_customer_id,\n offer_id: r.offer_id,\n offer_key: r.offer_key,\n plan_id: r.plan_id,\n plan: r.plan,\n status: r.status,\n current_period_start: r.current_period_start,\n current_period_end: r.current_period_end,\n trial_end: r.trial_end,\n cancel_at: r.cancel_at,\n ended_at: r.ended_at,\n grace_ends_at: r.grace_ends_at,\n grace_expired_at: r.grace_expired_at,\n provider_updated_at: r.provider_updated_at,\n cancel_at_period_end: r.cancel_at_period_end,\n interval: r.interval,\n interval_count: r.interval_count,\n metadata: r.metadata,\n },\n \"BillingSubscriptionRepository\",\n );\n }\n\n private async withOfferContext(\n value: PersistedSubscriptionRow,\n ): Promise<Record<string, unknown>> {\n const reference = safeParse(\n CatalogReferenceSchema,\n { offer_id: value.offer_id, catalog_revision_id: value.catalog_revision_id },\n \"BillingSubscriptionRepository.withOfferContext.reference\",\n );\n const contextRows = await this.query(\n `SELECT * FROM bursar.get_catalog_offer_context($1::uuid, $2::uuid)`,\n [reference.offer_id, reference.catalog_revision_id],\n );\n const rawContext = optionalRecordRow(\n contextRows,\n \"BillingSubscriptionRepository.withOfferContext\",\n );\n if (rawContext === null) {\n throw new StoreError(\"Billing subscription offer context is missing\", {\n details: {\n offerId: reference.offer_id,\n catalogRevisionId: reference.catalog_revision_id,\n },\n });\n }\n const context = safeParse(\n OfferContextSchema,\n {\n offer_key: rawContext.offer_key,\n plan_id: rawContext.plan_id,\n plan_key: rawContext.plan_key,\n billing_unit: rawContext.billing_unit,\n billing_count: rawContext.billing_count,\n },\n \"BillingSubscriptionRepository.withOfferContext.context\",\n );\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 const row = optionalRecordRow(rows, \"BillingSubscriptionRepository.get\");\n return row === null\n ? null\n : this.map(\n await this.withOfferContext(\n persistedSubscription(row, \"BillingSubscriptionRepository.get\"),\n ),\n );\n }\n async getUserSubscription(userId: string, statuses?: string[]): Promise<SubscriptionRow | null> {\n const allowed = new Set(\n safeParse(\n z.array(SubscriptionStatusSchema),\n statuses ?? [\"active\", \"trialing\"],\n \"BillingSubscriptionRepository.getUserSubscription.statuses\",\n ),\n );\n const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [\n userId,\n ]);\n const candidates = rows\n .map((row) => persistedSubscription(row, \"BillingSubscriptionRepository.getUserSubscription\"))\n .filter((row) => allowed.has(row.status))\n .sort(\n (left, right) =>\n Date.parse(right.provider_updated_at) - Date.parse(left.provider_updated_at),\n );\n const selected = candidates[0];\n return selected === undefined ? null : this.map(await this.withOfferContext(selected));\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 persisted = rows.map((row) =>\n persistedSubscription(row, \"BillingSubscriptionRepository.getUserSubscriptions\"),\n );\n const enriched = await Promise.all(persisted.map((row) => this.withOfferContext(row)));\n return enriched.map((row) => this.map(row));\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 persisted = rows.map((row) =>\n persistedSubscription(row, \"BillingSubscriptionRepository.listExpiredGraceSubscriptions\"),\n );\n const enriched = await Promise.all(persisted.map((row) => this.withOfferContext(row)));\n return enriched.map((row) => this.map(row));\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 requireResultField(\n rows,\n \"marked\",\n pgBoolean,\n \"BillingSubscriptionRepository.markGraceExpired\",\n );\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 return requireResultField(\n rows,\n \"id\",\n z.union([z.string().min(1), z.number().int()]).transform(String),\n \"BillingSubscriptionRepository.recordConflict\",\n );\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 eligibleStatuses = new Set([\"trialing\", \"active\", \"past_due\", \"paused\"]);\n const rows = await this.query(`SELECT * FROM bursar.list_billing_subscriptions($1::uuid)`, [\n userId,\n ]);\n const replacement = rows\n .map((row) =>\n persistedSubscription(row, \"BillingSubscriptionRepository.selectEntitlementSource\"),\n )\n .filter(\n (row) =>\n row.provider === provider &&\n eligibleStatuses.has(row.status) &&\n (!providerSubscriptionId || row.provider_subscription_id === providerSubscriptionId),\n )\n .sort(\n (left, right) =>\n Date.parse(right.provider_updated_at) - Date.parse(left.provider_updated_at),\n )[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 (\n !requireResultField(\n selectedRows,\n \"selected\",\n pgBoolean,\n \"BillingSubscriptionRepository.selectEntitlementSource\",\n )\n ) {\n throw new StoreError(\"subscription entitlement source selection was rejected\");\n }\n\n return true;\n }\n}\n"]}
|
|
@@ -1,27 +1,25 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
import type { QueryFn } from "../../../shared/postgres-types.js";
|
|
3
4
|
declare const BillingTopupRowSchema: z.ZodObject<{
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
default_quantity: z.ZodOptional<z.ZodNumber>;
|
|
15
|
-
}, z.core.$loose>;
|
|
5
|
+
id: z.ZodString;
|
|
6
|
+
topup_key: z.ZodString;
|
|
7
|
+
credits_per_unit: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodCustom<Decimal, Decimal>]>, z.ZodTransform<Decimal, string | number | Decimal>>;
|
|
8
|
+
bucket_key: z.ZodString;
|
|
9
|
+
amount_minor: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>, z.ZodTransform<number, string | number>>;
|
|
10
|
+
currency: z.ZodString;
|
|
11
|
+
min_quantity: z.ZodNumber;
|
|
12
|
+
max_quantity: z.ZodNumber;
|
|
13
|
+
default_quantity: z.ZodNumber;
|
|
14
|
+
}, z.core.$strict>;
|
|
16
15
|
export type BillingTopupRow = z.infer<typeof BillingTopupRowSchema>;
|
|
17
16
|
/** Repository for credit top-up resolution. */
|
|
18
17
|
export declare class BillingTopupRepository {
|
|
19
|
-
private query;
|
|
18
|
+
private readonly query;
|
|
20
19
|
constructor(query: QueryFn);
|
|
21
|
-
/** Resolve a credit top-up by price ID and product ID. */
|
|
22
20
|
resolveByPrice(provider: string, priceId: string | null, productId: string | null): Promise<BillingTopupRow | null>;
|
|
23
|
-
/** Resolve a credit top-up by provider lookup key. */
|
|
24
21
|
resolveByLookup(provider: string, lookupKey: string): Promise<BillingTopupRow | null>;
|
|
22
|
+
private resolve;
|
|
25
23
|
}
|
|
26
24
|
export {};
|
|
27
25
|
//# sourceMappingURL=topup.d.ts.map
|
|
@@ -1 +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;
|
|
1
|
+
{"version":3,"file":"topup.d.ts","sourceRoot":"","sources":["../../../../src/billing/postgres/repositories/topup.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,YAAY,CAAC;AACjC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAajE,QAAA,MAAM,qBAAqB;;;;;;;;;;kBAuBvB,CAAC;AAEL,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,+CAA+C;AAC/C,qBAAa,sBAAsB;IACrB,OAAO,CAAC,QAAQ,CAAC,KAAK;gBAAL,KAAK,EAAE,OAAO;IAErC,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GAAG,IAAI,EACtB,SAAS,EAAE,MAAM,GAAG,IAAI,GACvB,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAK5B,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;YAI7E,OAAO;CAsCtB"}
|
|
@@ -1,51 +1,82 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
1
2
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
3
|
+
import { StoreError } from "../../../errors.js";
|
|
4
|
+
import { postgresUuid, safeParse, unwrapJsonb } from "../../../shared/postgres-validation.js";
|
|
5
|
+
const PositiveDecimalSchema = z
|
|
6
|
+
.union([z.string(), z.number(), z.instanceof(Decimal)])
|
|
7
|
+
.transform((value) => new Decimal(value))
|
|
8
|
+
.refine((value) => value.isFinite() && value.gt(0), "expected a finite positive decimal");
|
|
9
|
+
const SafeMinorUnitsSchema = z
|
|
10
|
+
.union([z.string().regex(/^\d+$/), z.number().int().nonnegative().safe()])
|
|
11
|
+
.transform(Number)
|
|
12
|
+
.refine(Number.isSafeInteger, "minor-unit amount exceeds JavaScript's safe integer range");
|
|
3
13
|
const BillingTopupRowSchema = z
|
|
4
14
|
.object({
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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(),
|
|
15
|
+
id: postgresUuid,
|
|
16
|
+
topup_key: z.string().min(1),
|
|
17
|
+
credits_per_unit: PositiveDecimalSchema,
|
|
18
|
+
bucket_key: z.string().min(1),
|
|
19
|
+
amount_minor: SafeMinorUnitsSchema,
|
|
20
|
+
currency: z.string().regex(/^[A-Z]{3}$/),
|
|
21
|
+
min_quantity: z.number().int().positive(),
|
|
22
|
+
max_quantity: z.number().int().positive(),
|
|
23
|
+
default_quantity: z.number().int().positive(),
|
|
22
24
|
})
|
|
23
|
-
.
|
|
25
|
+
.strict()
|
|
26
|
+
.superRefine((row, ctx) => {
|
|
27
|
+
if (row.max_quantity < row.min_quantity) {
|
|
28
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "max_quantity is below min_quantity" });
|
|
29
|
+
}
|
|
30
|
+
if (row.default_quantity < row.min_quantity || row.default_quantity > row.max_quantity) {
|
|
31
|
+
ctx.addIssue({
|
|
32
|
+
code: z.ZodIssueCode.custom,
|
|
33
|
+
message: "default_quantity is outside the configured range",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
24
37
|
/** Repository for credit top-up resolution. */
|
|
25
38
|
export class BillingTopupRepository {
|
|
26
39
|
query;
|
|
27
40
|
constructor(query) {
|
|
28
41
|
this.query = query;
|
|
29
42
|
}
|
|
30
|
-
/** Resolve a credit top-up by price ID and product ID. */
|
|
31
43
|
async resolveByPrice(provider, priceId, productId) {
|
|
44
|
+
const lookupType = priceId !== null ? "price_id" : "product_id";
|
|
45
|
+
return this.resolve(provider, lookupType, priceId ?? productId, "resolveByPrice");
|
|
46
|
+
}
|
|
47
|
+
async resolveByLookup(provider, lookupKey) {
|
|
48
|
+
return this.resolve(provider, "external_id", lookupKey, "resolveByLookup");
|
|
49
|
+
}
|
|
50
|
+
async resolve(provider, lookupType, lookupValue, operation) {
|
|
51
|
+
if (!provider.trim())
|
|
52
|
+
throw new TypeError("provider must not be empty");
|
|
53
|
+
if (!lookupValue?.trim())
|
|
54
|
+
return null;
|
|
55
|
+
const context = `BillingTopupRepository.${operation}`;
|
|
32
56
|
const rows = await this.query("SELECT * FROM bursar.resolve_catalog_topup($1, $2, $3)", [
|
|
33
57
|
provider,
|
|
34
|
-
|
|
35
|
-
|
|
58
|
+
lookupType,
|
|
59
|
+
lookupValue,
|
|
36
60
|
]);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
:
|
|
61
|
+
if (rows.length > 1) {
|
|
62
|
+
throw new StoreError(`${context}: expected at most one top-up`, {
|
|
63
|
+
details: { rowCount: rows.length },
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const raw = unwrapJsonb(rows);
|
|
67
|
+
if (raw?.id == null)
|
|
68
|
+
return null;
|
|
69
|
+
return safeParse(BillingTopupRowSchema, {
|
|
70
|
+
id: raw.id,
|
|
71
|
+
topup_key: raw.topup_key,
|
|
72
|
+
credits_per_unit: raw.credits_per_unit,
|
|
73
|
+
bucket_key: raw.bucket_key,
|
|
74
|
+
amount_minor: raw.amount_minor,
|
|
75
|
+
currency: raw.currency,
|
|
76
|
+
min_quantity: raw.min_quantity,
|
|
77
|
+
max_quantity: raw.max_quantity,
|
|
78
|
+
default_quantity: raw.default_quantity,
|
|
79
|
+
}, context);
|
|
49
80
|
}
|
|
50
81
|
}
|
|
51
82
|
//# sourceMappingURL=topup.js.map
|