@zonastery/bursar 1.0.1 → 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 +86 -3
- 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 +8 -7
- 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 +31 -9
- package/dist/billing/event-handlers.js.map +1 -1
- package/dist/billing/event-processor.d.ts.map +1 -1
- package/dist/billing/event-processor.js +2 -1
- 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 +1 -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/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 +202 -59
- 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 +12 -5
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/event.js +18 -15
- 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 +18 -11
- package/dist/billing/postgres/store.d.ts.map +1 -1
- package/dist/billing/postgres/store.js +260 -238
- 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/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/bursar.d.ts +27 -9
- package/dist/bursar.d.ts.map +1 -1
- package/dist/bursar.js +48 -20
- package/dist/bursar.js.map +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +1 -1
- package/dist/catalog.js.map +1 -1
- package/dist/commerce/errors.d.ts +1 -10
- package/dist/commerce/errors.d.ts.map +1 -1
- package/dist/commerce/errors.js +1 -16
- 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/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 +85 -67
- package/dist/commerce/service.js.map +1 -1
- package/dist/commerce/types.d.ts +27 -15
- package/dist/commerce/types.d.ts.map +1 -1
- package/dist/commerce/types.js.map +1 -1
- package/dist/config/parse-plans.d.ts.map +1 -1
- package/dist/config/parse-plans.js +16 -15
- 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/types.d.ts +2 -2
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js.map +1 -1
- package/dist/credits/{pricing-runtime.d.ts → catalog-runtime.d.ts} +8 -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 +34 -34
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/analytics.js +52 -52
- 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 +28 -26
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/deduction.js +153 -68
- 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 +90 -43
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/plan.js +126 -108
- 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 +23 -18
- package/dist/credits/postgres/store.d.ts.map +1 -1
- package/dist/credits/postgres/store.js +282 -265
- 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 +36 -61
- 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-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 +42 -62
- package/dist/credits/service.d.ts.map +1 -1
- package/dist/credits/service.js +140 -121
- package/dist/credits/service.js.map +1 -1
- package/dist/credits/store.d.ts +20 -22
- package/dist/credits/store.d.ts.map +1 -1
- package/dist/credits/store.js +5 -5
- package/dist/credits/store.js.map +1 -1
- package/dist/credits/types/account.d.ts +49 -11
- 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 +20 -15
- 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/ledger.d.ts +12 -3
- package/dist/credits/types/ledger.d.ts.map +1 -1
- package/dist/credits/types/ledger.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 +52 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +76 -4
- 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/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 +5 -7
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +5 -12
- package/dist/generated/pricing-config.types.d.ts +3 -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 +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- 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} +15 -10
- package/dist/load-config-file.js.map +1 -0
- 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 +36 -0
- 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/shared/postgres-client.d.ts +5 -0
- package/dist/shared/postgres-client.d.ts.map +1 -1
- package/dist/shared/postgres-client.js +23 -7
- package/dist/shared/postgres-client.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 +2 -5
- 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/ports.d.ts +0 -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 +2 -2
- package/dist/storage/postgres-repository.js.map +1 -1
- package/dist/storage/runtime.d.ts +24 -6
- package/dist/storage/runtime.d.ts.map +1 -1
- package/dist/storage/runtime.js +87 -30
- package/dist/storage/runtime.js.map +1 -1
- package/package.json +56 -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 -76
- 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,47 +1,113 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { DeductionRowSchema } from "./deduction.js";
|
|
3
|
-
import {
|
|
3
|
+
import { optionalRecordRow, postgresUuid, requireRow, safeParse, } from "../../../shared/postgres-validation.js";
|
|
4
|
+
import { StoreError } from "../../../errors.js";
|
|
5
|
+
import Decimal from "decimal.js";
|
|
6
|
+
const decimal = z.union([z.string().min(1), z.number().finite()]);
|
|
7
|
+
const timestamp = z.union([z.string().min(1), z.date()]).transform((value, context) => {
|
|
8
|
+
const parsed = value instanceof Date ? value : new Date(value);
|
|
9
|
+
if (Number.isNaN(parsed.getTime())) {
|
|
10
|
+
context.addIssue({ code: "custom", message: "expected a valid timestamp" });
|
|
11
|
+
return z.NEVER;
|
|
12
|
+
}
|
|
13
|
+
return parsed.toISOString();
|
|
14
|
+
});
|
|
15
|
+
const safeInteger = z
|
|
16
|
+
.union([z.number().int(), z.string().regex(/^\d+$/)])
|
|
17
|
+
.transform(Number)
|
|
18
|
+
.refine(Number.isSafeInteger, "expected a safe integer");
|
|
19
|
+
const leaseStatus = z.enum(["active", "settling", "settled", "released", "expired"]);
|
|
20
|
+
const LeaseMutationRpcRowSchema = z
|
|
21
|
+
.object({
|
|
22
|
+
lease_id: postgresUuid.nullable(),
|
|
23
|
+
status: leaseStatus,
|
|
24
|
+
reserved_amount: decimal,
|
|
25
|
+
error_code: z.string().min(1).nullable(),
|
|
26
|
+
})
|
|
27
|
+
.strict()
|
|
28
|
+
.superRefine((row, context) => {
|
|
29
|
+
if (row.error_code === null && (row.lease_id === null || row.status !== "active")) {
|
|
30
|
+
context.addIssue({
|
|
31
|
+
code: "custom",
|
|
32
|
+
message: "successful lease mutations require an active lease",
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const SettleLeaseRpcRowSchema = z
|
|
37
|
+
.object({
|
|
38
|
+
ledger_entry_id: postgresUuid.nullable(),
|
|
39
|
+
charge_id: postgresUuid.nullable(),
|
|
40
|
+
settled_amount: decimal,
|
|
41
|
+
replayed: z.boolean(),
|
|
42
|
+
error_code: z.string().min(1).nullable(),
|
|
43
|
+
})
|
|
44
|
+
.strict()
|
|
45
|
+
.superRefine((row, context) => {
|
|
46
|
+
if (row.error_code === null && row.charge_id === null) {
|
|
47
|
+
context.addIssue({
|
|
48
|
+
code: "custom",
|
|
49
|
+
message: "successful lease settlement requires a charge receipt",
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
if (row.error_code !== null &&
|
|
53
|
+
(row.ledger_entry_id !== null || row.charge_id !== null || row.replayed)) {
|
|
54
|
+
context.addIssue({
|
|
55
|
+
code: "custom",
|
|
56
|
+
message: "failed lease settlement cannot expose committed fields",
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
4
60
|
const LeaseRowSchema = z
|
|
5
61
|
.object({
|
|
6
|
-
lease_id:
|
|
7
|
-
user_id:
|
|
8
|
-
amount:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
available: z
|
|
13
|
-
.union([z.string(), z.number()])
|
|
14
|
-
.nullable()
|
|
15
|
-
.optional(),
|
|
16
|
-
reserved: z
|
|
17
|
-
.union([z.string(), z.number()])
|
|
18
|
-
.nullable()
|
|
19
|
-
.optional(),
|
|
20
|
-
billing_mode: z.string().optional(),
|
|
21
|
-
minimum_balance: z
|
|
22
|
-
.union([z.string(), z.number()])
|
|
23
|
-
.nullable()
|
|
24
|
-
.optional(),
|
|
25
|
-
expires_at: z
|
|
26
|
-
.union([z.string(), z.date().transform((value) => value.toISOString())])
|
|
27
|
-
.optional(),
|
|
28
|
-
error: z.string().nullable().optional(),
|
|
62
|
+
lease_id: postgresUuid.nullable(),
|
|
63
|
+
user_id: postgresUuid,
|
|
64
|
+
amount: decimal.nullable(),
|
|
65
|
+
minimum_balance: decimal.nullable(),
|
|
66
|
+
expires_at: timestamp.nullable(),
|
|
67
|
+
error: z.string().min(1).nullable(),
|
|
29
68
|
})
|
|
30
|
-
.
|
|
69
|
+
.strict()
|
|
70
|
+
.superRefine((row, context) => {
|
|
71
|
+
if (row.error === null &&
|
|
72
|
+
(row.lease_id === null ||
|
|
73
|
+
row.amount === null ||
|
|
74
|
+
row.minimum_balance === null ||
|
|
75
|
+
row.expires_at === null)) {
|
|
76
|
+
context.addIssue({
|
|
77
|
+
code: "custom",
|
|
78
|
+
message: "successful lease acquisition requires lease and policy fields",
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (row.error !== null &&
|
|
82
|
+
(row.lease_id !== null ||
|
|
83
|
+
row.amount !== null ||
|
|
84
|
+
row.minimum_balance !== null ||
|
|
85
|
+
row.expires_at !== null)) {
|
|
86
|
+
context.addIssue({
|
|
87
|
+
code: "custom",
|
|
88
|
+
message: "failed lease mutations cannot expose committed fields",
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
});
|
|
31
92
|
const ReleaseRowSchema = z
|
|
32
93
|
.object({
|
|
33
|
-
released:
|
|
34
|
-
reason: z.
|
|
94
|
+
released: z.boolean(),
|
|
95
|
+
reason: leaseStatus.or(z.literal("missing_lease")).nullable(),
|
|
35
96
|
})
|
|
36
|
-
.
|
|
97
|
+
.strict();
|
|
37
98
|
const LeasePricingContextRowSchema = z
|
|
38
99
|
.object({
|
|
39
|
-
catalog_revision_no:
|
|
40
|
-
plan_id:
|
|
41
|
-
plan_key: z.string().
|
|
42
|
-
rate_card: z.string().
|
|
100
|
+
catalog_revision_no: safeInteger,
|
|
101
|
+
plan_id: postgresUuid.nullable(),
|
|
102
|
+
plan_key: z.string().min(1).nullable(),
|
|
103
|
+
rate_card: z.string().min(1).nullable(),
|
|
43
104
|
})
|
|
44
|
-
.
|
|
105
|
+
.strict()
|
|
106
|
+
.superRefine((row, context) => {
|
|
107
|
+
if ((row.plan_id === null) !== (row.plan_key === null)) {
|
|
108
|
+
context.addIssue({ code: "custom", message: "lease plan identity fields are inconsistent" });
|
|
109
|
+
}
|
|
110
|
+
});
|
|
45
111
|
/** Repository for lease lifecycle operations (admission control). */
|
|
46
112
|
export class LeaseRepository {
|
|
47
113
|
callproc;
|
|
@@ -63,17 +129,25 @@ export class LeaseRepository {
|
|
|
63
129
|
params.minimumBalance,
|
|
64
130
|
params.maxConcurrent,
|
|
65
131
|
]);
|
|
66
|
-
const row = (rows
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
132
|
+
const row = safeParse(LeaseMutationRpcRowSchema, requireRow(rows, "LeaseRepository.createLease"), "LeaseRepository.createLease");
|
|
133
|
+
if (row.error_code !== null) {
|
|
134
|
+
return safeParse(LeaseRowSchema, {
|
|
135
|
+
lease_id: null,
|
|
136
|
+
user_id: params.userId,
|
|
137
|
+
amount: null,
|
|
138
|
+
expires_at: null,
|
|
139
|
+
minimum_balance: null,
|
|
140
|
+
error: row.error_code,
|
|
141
|
+
}, "LeaseRepository.createLease");
|
|
142
|
+
}
|
|
143
|
+
const lease = optionalRecordRow(await this.callproc("get_credit_lease", [params.userId, row.lease_id]), "LeaseRepository.createLease.details");
|
|
70
144
|
return safeParse(LeaseRowSchema, {
|
|
71
|
-
|
|
145
|
+
lease_id: row.lease_id,
|
|
72
146
|
user_id: params.userId,
|
|
73
147
|
amount: row.reserved_amount,
|
|
74
148
|
expires_at: lease?.expires_at,
|
|
75
149
|
minimum_balance: lease?.minimum_balance,
|
|
76
|
-
error:
|
|
150
|
+
error: null,
|
|
77
151
|
}, "LeaseRepository.createLease");
|
|
78
152
|
}
|
|
79
153
|
/** Charge the actual cost against a lease and mark it settled. */
|
|
@@ -90,18 +164,27 @@ export class LeaseRepository {
|
|
|
90
164
|
params.dimensions,
|
|
91
165
|
params.metadata,
|
|
92
166
|
]);
|
|
93
|
-
const row = (rows
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
167
|
+
const row = safeParse(SettleLeaseRpcRowSchema, requireRow(rows, "LeaseRepository.settleLease"), "LeaseRepository.settleLease");
|
|
168
|
+
if (row.error_code === null && !new Decimal(row.settled_amount).equals(params.amount)) {
|
|
169
|
+
throw new StoreError("LeaseRepository.settleLease: committed amount differs from the request", {
|
|
170
|
+
indeterminate: true,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
const charge = row.error_code == null
|
|
174
|
+
? optionalRecordRow(await this.callproc("get_credit_operation_details", [
|
|
175
|
+
params.userId,
|
|
176
|
+
row.ledger_entry_id ?? null,
|
|
177
|
+
params.idempotencyKey,
|
|
178
|
+
]), "LeaseRepository.settleLease.details")
|
|
179
|
+
: null;
|
|
99
180
|
return safeParse(DeductionRowSchema, {
|
|
100
|
-
|
|
181
|
+
charge_id: row.charge_id,
|
|
182
|
+
user_id: params.userId,
|
|
101
183
|
entry_id: row.ledger_entry_id,
|
|
102
|
-
amount: row.settled_amount,
|
|
103
|
-
allowance_consumed: charge?.allowance_covered,
|
|
104
|
-
balance_after: charge?.balance_after,
|
|
184
|
+
amount: row.error_code === null ? row.settled_amount : params.amount,
|
|
185
|
+
allowance_consumed: row.error_code === null ? charge?.allowance_covered : "0",
|
|
186
|
+
balance_after: row.error_code === null ? charge?.balance_after : null,
|
|
187
|
+
bucket_breakdown: row.error_code === null ? charge?.bucket_breakdown : null,
|
|
105
188
|
idempotent: row.replayed,
|
|
106
189
|
error: row.error_code,
|
|
107
190
|
}, "LeaseRepository.settleLease");
|
|
@@ -109,40 +192,49 @@ export class LeaseRepository {
|
|
|
109
192
|
/** Return the immutable pricing context captured at lease admission. */
|
|
110
193
|
async getPricingContext(userId, leaseId) {
|
|
111
194
|
const rows = await this.callproc("get_credit_lease_pricing_context", [userId, leaseId]);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
195
|
+
const row = optionalRecordRow(rows, "LeaseRepository.getPricingContext");
|
|
196
|
+
return row === null
|
|
197
|
+
? null
|
|
198
|
+
: safeParse(LeasePricingContextRowSchema, row, "LeaseRepository.getPricingContext");
|
|
115
199
|
}
|
|
116
200
|
/** Release a lease without charging — idempotent. */
|
|
117
201
|
async releaseLease(userId, leaseId) {
|
|
118
202
|
const rows = await this.callproc("release_lease", [userId, leaseId]);
|
|
119
|
-
const
|
|
120
|
-
const reason = typeof rows?.[0] === "string" ? rows[0] : result?.released === true ? "released" : null;
|
|
203
|
+
const status = safeParse(leaseStatus.or(z.literal("missing_lease")), requireRow(rows, "LeaseRepository.releaseLease"), "LeaseRepository.releaseLease");
|
|
121
204
|
return safeParse(ReleaseRowSchema, {
|
|
122
|
-
released:
|
|
123
|
-
reason,
|
|
205
|
+
released: status === "released",
|
|
206
|
+
reason: status === "released" ? null : status,
|
|
124
207
|
}, "LeaseRepository.releaseLease");
|
|
125
208
|
}
|
|
126
209
|
/** Extend an active lease without changing its captured policy snapshot. */
|
|
127
210
|
async renewLease(userId, leaseId, ttlSeconds) {
|
|
128
211
|
const rows = await this.callproc("renew_lease", [userId, leaseId, `${ttlSeconds} seconds`]);
|
|
129
|
-
const row = (rows
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
212
|
+
const row = safeParse(LeaseMutationRpcRowSchema, requireRow(rows, "LeaseRepository.renewLease"), "LeaseRepository.renewLease");
|
|
213
|
+
if (row.error_code !== null) {
|
|
214
|
+
return safeParse(LeaseRowSchema, {
|
|
215
|
+
lease_id: null,
|
|
216
|
+
user_id: userId,
|
|
217
|
+
amount: null,
|
|
218
|
+
expires_at: null,
|
|
219
|
+
minimum_balance: null,
|
|
220
|
+
error: row.error_code,
|
|
221
|
+
}, "LeaseRepository.renewLease");
|
|
222
|
+
}
|
|
223
|
+
const lease = optionalRecordRow(await this.callproc("get_credit_lease", [userId, row.lease_id]), "LeaseRepository.renewLease.details");
|
|
133
224
|
return safeParse(LeaseRowSchema, {
|
|
134
|
-
|
|
135
|
-
...lease,
|
|
225
|
+
lease_id: row.lease_id,
|
|
136
226
|
user_id: userId,
|
|
137
227
|
amount: row.reserved_amount,
|
|
138
|
-
|
|
228
|
+
expires_at: lease?.expires_at,
|
|
229
|
+
minimum_balance: lease?.minimum_balance,
|
|
230
|
+
error: null,
|
|
139
231
|
}, "LeaseRepository.renewLease");
|
|
140
232
|
}
|
|
141
233
|
/** Expire a bounded batch of leases and release their reservations. */
|
|
142
234
|
async expireLeases(limit) {
|
|
143
235
|
const rows = await this.callproc("expire_leases", [limit]);
|
|
144
|
-
const row = rows
|
|
145
|
-
return
|
|
236
|
+
const row = safeParse(z.object({ expired: z.number().int().nonnegative() }).strict(), requireRow(rows, "LeaseRepository.expireLeases"), "LeaseRepository.expireLeases");
|
|
237
|
+
return row.expired;
|
|
146
238
|
}
|
|
147
239
|
}
|
|
148
240
|
//# sourceMappingURL=lease.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lease.js","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/lease.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAE9E,MAAM,cAAc,GAAG,CAAC;KACrB,MAAM,CAAC;IACN,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;IAC1C,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC9B,MAAM,EAAE,CAAC;SACN,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAU,CAAC;SACxC,QAAQ,EAAE;SACV,QA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,CAAC,kBAAkB,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAC9B;YACxC,CAAC,CAAC,SAAS,CAAC;QAChB,OAAO,SAAS,CACd,cAAc,EACd;YACE,GAAG,GAAG;YACN,GAAG,KAAK;YACR,OAAO,EAAE,MAAM;YACf,MAAM,EAAE,GAAG,CAAC,eAAe;YAC3B,KAAK,EAAE,GAAG,CAAC,UAAU;SACtB,EACD,4BAA4B,CAC7B,CAAC;IACJ,CAAC;IAED,uEAAuE;IACvE,KAAK,CAAC,YAAY,CAAC,KAAa;QAC9B,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,eAAe,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QAC3D,MAAM,GAAG,GAAG,IAAI,EAAE,CAAC,CAAC,CAAwC,CAAC;QAC7D,OAAO,MAAM,CAAC,GAAG,EAAE,OAAO,IAAI,CAAC,CAAC,CAAC;IACnC,CAAC;CACF","sourcesContent":["import { z } from \"zod\";\nimport type { CallProc } from \"../../../shared/postgres-types.js\";\nimport { DeductionRowSchema } from \"./deduction.js\";\nimport type { DeductionRow } from \"./deduction.js\";\nimport { pgBoolean, safeParse } from \"../../../shared/postgres-validation.js\";\n\nconst LeaseRowSchema = z\n .object({\n lease_id: z.string().nullable().optional(),\n user_id: z.string().optional(),\n amount: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n available: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n reserved: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n billing_mode: z.string().optional(),\n minimum_balance: z\n .union([z.string(), z.number()] as const)\n .nullable()\n .optional(),\n expires_at: z\n .union([z.string(), z.date().transform((value) => value.toISOString())])\n .optional(),\n error: z.string().nullable().optional(),\n })\n .passthrough();\n\nconst ReleaseRowSchema = z\n .object({\n released: pgBoolean.nullable().optional(),\n reason: z.string().nullable().optional(),\n })\n .passthrough();\n\nconst LeasePricingContextRowSchema = z\n .object({\n catalog_revision_no: z.coerce.number(),\n plan_id: z.string().nullable().optional(),\n plan_key: z.string().nullable().optional(),\n rate_card: z.string().nullable().optional(),\n })\n .passthrough();\n\nexport type LeaseRow = z.infer<typeof LeaseRowSchema>;\nexport type ReleaseRow = z.infer<typeof ReleaseRowSchema>;\nexport type LeasePricingContextRow = z.infer<typeof LeasePricingContextRowSchema>;\n\n/** Repository for lease lifecycle operations (admission control). */\nexport class LeaseRepository {\n constructor(private callproc: CallProc) {}\n\n /** Atomically acquire a lease (hold) — admission control. */\n async createLease(params: {\n userId: string;\n amount: string;\n operationType: string;\n idempotencyKey: string;\n ttlSeconds: number;\n metadata: string;\n feature: string | null;\n measures: string;\n dimensions: string;\n minimumBalance: string | null;\n maxConcurrent: number | null;\n }): Promise<LeaseRow> {\n const rows = await this.callproc(\"create_lease_for_operation\", [\n params.userId,\n params.operationType,\n params.amount,\n params.idempotencyKey,\n `${params.ttlSeconds} seconds`,\n params.metadata,\n params.feature,\n params.measures,\n params.dimensions,\n params.minimumBalance,\n params.maxConcurrent,\n ]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n const lease =\n row.lease_id == null\n ? null\n : ((await this.callproc(\"get_credit_lease\", [params.userId, row.lease_id]))[0] as\n Record<string, unknown> | undefined);\n return safeParse(\n LeaseRowSchema,\n {\n ...row,\n user_id: params.userId,\n amount: row.reserved_amount,\n expires_at: lease?.expires_at,\n minimum_balance: lease?.minimum_balance,\n error: row.error_code,\n },\n \"LeaseRepository.createLease\",\n );\n }\n\n /** Charge the actual cost against a lease and mark it settled. */\n async settleLease(params: {\n userId: string;\n leaseId: string;\n amount: string;\n idempotencyKey: string;\n feature: string | null;\n model: string | null;\n region: string | null;\n measures: string;\n dimensions: string;\n metadata: string;\n }): Promise<DeductionRow> {\n const rows = await this.callproc(\"settle_lease\", [\n params.userId,\n params.leaseId,\n params.amount,\n params.idempotencyKey,\n params.feature,\n params.model,\n params.region,\n params.measures,\n params.dimensions,\n params.metadata,\n ]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n const charge = (\n await this.callproc(\"get_credit_operation_details\", [\n params.userId,\n row.ledger_entry_id ?? null,\n params.idempotencyKey,\n ])\n )[0] as Record<string, unknown> | undefined;\n return safeParse(\n DeductionRowSchema,\n {\n ...row,\n entry_id: row.ledger_entry_id,\n amount: row.settled_amount,\n allowance_consumed: charge?.allowance_covered,\n balance_after: charge?.balance_after,\n idempotent: row.replayed,\n error: row.error_code,\n },\n \"LeaseRepository.settleLease\",\n );\n }\n\n /** Return the immutable pricing context captured at lease admission. */\n async getPricingContext(userId: string, leaseId: string): Promise<LeasePricingContextRow | null> {\n const rows = await this.callproc(\"get_credit_lease_pricing_context\", [userId, leaseId]);\n if (!rows?.length) return null;\n return safeParse(LeasePricingContextRowSchema, rows[0], \"LeaseRepository.getPricingContext\");\n }\n\n /** Release a lease without charging — idempotent. */\n async releaseLease(userId: string, leaseId: string): Promise<ReleaseRow> {\n const rows = await this.callproc(\"release_lease\", [userId, leaseId]);\n const result = rows?.[0] as { released?: boolean } | undefined;\n const reason =\n typeof rows?.[0] === \"string\" ? rows[0] : result?.released === true ? \"released\" : null;\n return safeParse(\n ReleaseRowSchema,\n {\n released: reason === \"released\",\n reason,\n },\n \"LeaseRepository.releaseLease\",\n );\n }\n\n /** Extend an active lease without changing its captured policy snapshot. */\n async renewLease(userId: string, leaseId: string, ttlSeconds: number): Promise<LeaseRow> {\n const rows = await this.callproc(\"renew_lease\", [userId, leaseId, `${ttlSeconds} seconds`]);\n const row = (rows?.[0] ?? {}) as Record<string, unknown>;\n const lease =\n row.error_code == null && row.lease_id != null\n ? ((await this.callproc(\"get_credit_lease\", [userId, row.lease_id]))[0] as\n Record<string, unknown> | undefined)\n : undefined;\n return safeParse(\n LeaseRowSchema,\n {\n ...row,\n ...lease,\n user_id: userId,\n amount: row.reserved_amount,\n error: row.error_code,\n },\n \"LeaseRepository.renewLease\",\n );\n }\n\n /** Expire a bounded batch of leases and release their reservations. */\n async expireLeases(limit: number): Promise<number> {\n const rows = await this.callproc(\"expire_leases\", [limit]);\n const row = rows?.[0] as Record<string, unknown> | undefined;\n return Number(row?.expired ?? 0);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"lease.js","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/lease.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,OAAO,EACL,iBAAiB,EACjB,YAAY,EACZ,UAAU,EACV,SAAS,GACV,MAAM,wCAAwC,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,OAAO,MAAM,YAAY,CAAC;AAEjC,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,CAAU,CAAC,CAAC;AAC3E,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAU,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;IAC7F,MAAM,MAAM,GAAG,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IAC/D,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;QACnC,OAAO,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,4BAA4B,EAAE,CAAC,CAAC;QAC5E,OAAO,CAAC,CAAC,KAAK,CAAC;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MAAM,IAAI,CAAC,QAAQ,CAAC,kBAAkB,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,EAC/D,oCAAoC,CACrC,CAAC;QACF,OAAO,SAAS,CACd,cAAc,EACd;YACE,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,OAAO,EAAE,MAAM;YACf,MAAM,EAAE,GAAG,CAAC,eAAe;YAC3B,UAAU,EAAE,KAAK,EAAE,UAAU;YAC7B,eAAe,EAAE,KAAK,EAAE,eAAe;YACvC,KAAK,EAAE,IAAI;SACZ,EACD,4BAA4B,CAC7B,CAAC;IACJ,CAAC;IAED,uEAAuE;IACvE,KAAK,CAAC,YAAY,CAAC,KAAa;QAC9B,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,eAAe,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QAC3D,MAAM,GAAG,GAAG,SAAS,CACnB,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,EAC9D,UAAU,CAAC,IAAI,EAAE,8BAA8B,CAAC,EAChD,8BAA8B,CAC/B,CAAC;QACF,OAAO,GAAG,CAAC,OAAO,CAAC;IACrB,CAAC;CACF","sourcesContent":["import { z } from \"zod\";\nimport type { CallProc } from \"../../../shared/postgres-types.js\";\nimport { DeductionRowSchema } from \"./deduction.js\";\nimport type { DeductionRow } from \"./deduction.js\";\nimport {\n optionalRecordRow,\n postgresUuid,\n requireRow,\n safeParse,\n} from \"../../../shared/postgres-validation.js\";\nimport { StoreError } from \"../../../errors.js\";\nimport Decimal from \"decimal.js\";\n\nconst decimal = z.union([z.string().min(1), z.number().finite()] as const);\nconst timestamp = z.union([z.string().min(1), z.date()] as const).transform((value, context) => {\n const parsed = value instanceof Date ? value : new Date(value);\n if (Number.isNaN(parsed.getTime())) {\n context.addIssue({ code: \"custom\", message: \"expected a valid timestamp\" });\n return z.NEVER;\n }\n return parsed.toISOString();\n});\nconst safeInteger = z\n .union([z.number().int(), z.string().regex(/^\\d+$/)] as const)\n .transform(Number)\n .refine(Number.isSafeInteger, \"expected a safe integer\");\nconst leaseStatus = z.enum([\"active\", \"settling\", \"settled\", \"released\", \"expired\"]);\n\nconst LeaseMutationRpcRowSchema = z\n .object({\n lease_id: postgresUuid.nullable(),\n status: leaseStatus,\n reserved_amount: decimal,\n error_code: z.string().min(1).nullable(),\n })\n .strict()\n .superRefine((row, context) => {\n if (row.error_code === null && (row.lease_id === null || row.status !== \"active\")) {\n context.addIssue({\n code: \"custom\",\n message: \"successful lease mutations require an active lease\",\n });\n }\n });\n\nconst SettleLeaseRpcRowSchema = z\n .object({\n ledger_entry_id: postgresUuid.nullable(),\n charge_id: postgresUuid.nullable(),\n settled_amount: decimal,\n replayed: z.boolean(),\n error_code: z.string().min(1).nullable(),\n })\n .strict()\n .superRefine((row, context) => {\n if (row.error_code === null && row.charge_id === null) {\n context.addIssue({\n code: \"custom\",\n message: \"successful lease settlement requires a charge receipt\",\n });\n }\n if (\n row.error_code !== null &&\n (row.ledger_entry_id !== null || row.charge_id !== null || row.replayed)\n ) {\n context.addIssue({\n code: \"custom\",\n message: \"failed lease settlement cannot expose committed fields\",\n });\n }\n });\n\nconst LeaseRowSchema = z\n .object({\n lease_id: postgresUuid.nullable(),\n user_id: postgresUuid,\n amount: decimal.nullable(),\n minimum_balance: decimal.nullable(),\n expires_at: timestamp.nullable(),\n error: z.string().min(1).nullable(),\n })\n .strict()\n .superRefine((row, context) => {\n if (\n row.error === null &&\n (row.lease_id === null ||\n row.amount === null ||\n row.minimum_balance === null ||\n row.expires_at === null)\n ) {\n context.addIssue({\n code: \"custom\",\n message: \"successful lease acquisition requires lease and policy fields\",\n });\n }\n if (\n row.error !== null &&\n (row.lease_id !== null ||\n row.amount !== null ||\n row.minimum_balance !== null ||\n row.expires_at !== null)\n ) {\n context.addIssue({\n code: \"custom\",\n message: \"failed lease mutations cannot expose committed fields\",\n });\n }\n });\n\nconst ReleaseRowSchema = z\n .object({\n released: z.boolean(),\n reason: leaseStatus.or(z.literal(\"missing_lease\")).nullable(),\n })\n .strict();\n\nconst LeasePricingContextRowSchema = z\n .object({\n catalog_revision_no: safeInteger,\n plan_id: postgresUuid.nullable(),\n plan_key: z.string().min(1).nullable(),\n rate_card: z.string().min(1).nullable(),\n })\n .strict()\n .superRefine((row, context) => {\n if ((row.plan_id === null) !== (row.plan_key === null)) {\n context.addIssue({ code: \"custom\", message: \"lease plan identity fields are inconsistent\" });\n }\n });\n\nexport type LeaseRow = z.infer<typeof LeaseRowSchema>;\nexport type ReleaseRow = z.infer<typeof ReleaseRowSchema>;\nexport type LeasePricingContextRow = z.infer<typeof LeasePricingContextRowSchema>;\n\n/** Repository for lease lifecycle operations (admission control). */\nexport class LeaseRepository {\n constructor(private callproc: CallProc) {}\n\n /** Atomically acquire a lease (hold) — admission control. */\n async createLease(params: {\n userId: string;\n amount: string;\n operationType: string;\n idempotencyKey: string;\n ttlSeconds: number;\n metadata: string;\n feature: string | null;\n measures: string;\n dimensions: string;\n minimumBalance: string | null;\n maxConcurrent: number | null;\n }): Promise<LeaseRow> {\n const rows = await this.callproc(\"create_lease_for_operation\", [\n params.userId,\n params.operationType,\n params.amount,\n params.idempotencyKey,\n `${params.ttlSeconds} seconds`,\n params.metadata,\n params.feature,\n params.measures,\n params.dimensions,\n params.minimumBalance,\n params.maxConcurrent,\n ]);\n const row = safeParse(\n LeaseMutationRpcRowSchema,\n requireRow(rows, \"LeaseRepository.createLease\"),\n \"LeaseRepository.createLease\",\n );\n if (row.error_code !== null) {\n return safeParse(\n LeaseRowSchema,\n {\n lease_id: null,\n user_id: params.userId,\n amount: null,\n expires_at: null,\n minimum_balance: null,\n error: row.error_code,\n },\n \"LeaseRepository.createLease\",\n );\n }\n const lease = optionalRecordRow(\n await this.callproc(\"get_credit_lease\", [params.userId, row.lease_id]),\n \"LeaseRepository.createLease.details\",\n );\n return safeParse(\n LeaseRowSchema,\n {\n lease_id: row.lease_id,\n user_id: params.userId,\n amount: row.reserved_amount,\n expires_at: lease?.expires_at,\n minimum_balance: lease?.minimum_balance,\n error: null,\n },\n \"LeaseRepository.createLease\",\n );\n }\n\n /** Charge the actual cost against a lease and mark it settled. */\n async settleLease(params: {\n userId: string;\n leaseId: string;\n amount: string;\n idempotencyKey: string;\n feature: string | null;\n model: string | null;\n region: string | null;\n measures: string;\n dimensions: string;\n metadata: string;\n }): Promise<DeductionRow> {\n const rows = await this.callproc(\"settle_lease\", [\n params.userId,\n params.leaseId,\n params.amount,\n params.idempotencyKey,\n params.feature,\n params.model,\n params.region,\n params.measures,\n params.dimensions,\n params.metadata,\n ]);\n const row = safeParse(\n SettleLeaseRpcRowSchema,\n requireRow(rows, \"LeaseRepository.settleLease\"),\n \"LeaseRepository.settleLease\",\n );\n if (row.error_code === null && !new Decimal(row.settled_amount).equals(params.amount)) {\n throw new StoreError(\n \"LeaseRepository.settleLease: committed amount differs from the request\",\n {\n indeterminate: true,\n },\n );\n }\n const charge =\n row.error_code == null\n ? optionalRecordRow(\n await this.callproc(\"get_credit_operation_details\", [\n params.userId,\n row.ledger_entry_id ?? null,\n params.idempotencyKey,\n ]),\n \"LeaseRepository.settleLease.details\",\n )\n : null;\n return safeParse(\n DeductionRowSchema,\n {\n charge_id: row.charge_id,\n user_id: params.userId,\n entry_id: row.ledger_entry_id,\n amount: row.error_code === null ? row.settled_amount : params.amount,\n allowance_consumed: row.error_code === null ? charge?.allowance_covered : \"0\",\n balance_after: row.error_code === null ? charge?.balance_after : null,\n bucket_breakdown: row.error_code === null ? charge?.bucket_breakdown : null,\n idempotent: row.replayed,\n error: row.error_code,\n },\n \"LeaseRepository.settleLease\",\n );\n }\n\n /** Return the immutable pricing context captured at lease admission. */\n async getPricingContext(userId: string, leaseId: string): Promise<LeasePricingContextRow | null> {\n const rows = await this.callproc(\"get_credit_lease_pricing_context\", [userId, leaseId]);\n const row = optionalRecordRow(rows, \"LeaseRepository.getPricingContext\");\n return row === null\n ? null\n : safeParse(LeasePricingContextRowSchema, row, \"LeaseRepository.getPricingContext\");\n }\n\n /** Release a lease without charging — idempotent. */\n async releaseLease(userId: string, leaseId: string): Promise<ReleaseRow> {\n const rows = await this.callproc(\"release_lease\", [userId, leaseId]);\n const status = safeParse(\n leaseStatus.or(z.literal(\"missing_lease\")),\n requireRow(rows, \"LeaseRepository.releaseLease\"),\n \"LeaseRepository.releaseLease\",\n );\n return safeParse(\n ReleaseRowSchema,\n {\n released: status === \"released\",\n reason: status === \"released\" ? null : status,\n },\n \"LeaseRepository.releaseLease\",\n );\n }\n\n /** Extend an active lease without changing its captured policy snapshot. */\n async renewLease(userId: string, leaseId: string, ttlSeconds: number): Promise<LeaseRow> {\n const rows = await this.callproc(\"renew_lease\", [userId, leaseId, `${ttlSeconds} seconds`]);\n const row = safeParse(\n LeaseMutationRpcRowSchema,\n requireRow(rows, \"LeaseRepository.renewLease\"),\n \"LeaseRepository.renewLease\",\n );\n if (row.error_code !== null) {\n return safeParse(\n LeaseRowSchema,\n {\n lease_id: null,\n user_id: userId,\n amount: null,\n expires_at: null,\n minimum_balance: null,\n error: row.error_code,\n },\n \"LeaseRepository.renewLease\",\n );\n }\n const lease = optionalRecordRow(\n await this.callproc(\"get_credit_lease\", [userId, row.lease_id]),\n \"LeaseRepository.renewLease.details\",\n );\n return safeParse(\n LeaseRowSchema,\n {\n lease_id: row.lease_id,\n user_id: userId,\n amount: row.reserved_amount,\n expires_at: lease?.expires_at,\n minimum_balance: lease?.minimum_balance,\n error: null,\n },\n \"LeaseRepository.renewLease\",\n );\n }\n\n /** Expire a bounded batch of leases and release their reservations. */\n async expireLeases(limit: number): Promise<number> {\n const rows = await this.callproc(\"expire_leases\", [limit]);\n const row = safeParse(\n z.object({ expired: z.number().int().nonnegative() }).strict(),\n requireRow(rows, \"LeaseRepository.expireLeases\"),\n \"LeaseRepository.expireLeases\",\n );\n return row.expired;\n }\n}\n"]}
|
|
@@ -1,49 +1,95 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import type { CallProc } from "../../../shared/postgres-types.js";
|
|
3
3
|
declare const UserPlanRowSchema: z.ZodObject<{
|
|
4
|
-
user_id: z.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
4
|
+
user_id: z.ZodString;
|
|
5
|
+
plan_assigned_at: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodDate]>, z.ZodTransform<Date, string | Date>>;
|
|
6
|
+
plan_assignment_ends_at: z.ZodNullable<z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodDate]>, z.ZodTransform<Date, string | Date>>>;
|
|
7
|
+
assignment_source_type: z.ZodEnum<{
|
|
8
|
+
subscription: "subscription";
|
|
9
|
+
manual: "manual";
|
|
10
|
+
migration: "migration";
|
|
11
|
+
system: "system";
|
|
12
|
+
}>;
|
|
13
|
+
assignment_source_id: z.ZodNullable<z.ZodString>;
|
|
14
|
+
catalog_revision_pinned: z.ZodBoolean;
|
|
15
|
+
plan_id: z.ZodString;
|
|
16
|
+
plan_key: z.ZodString;
|
|
17
|
+
plan_label: z.ZodString;
|
|
18
|
+
rate_card: z.ZodNullable<z.ZodString>;
|
|
19
|
+
allowed_operations: z.ZodArray<z.ZodString>;
|
|
20
|
+
credit_allowance_amount: z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
|
|
21
|
+
credit_allowance_priority: z.ZodNullable<z.ZodNumber>;
|
|
22
|
+
credit_allowance_reset_unit: z.ZodNullable<z.ZodEnum<{
|
|
23
|
+
day: "day";
|
|
24
|
+
week: "week";
|
|
25
|
+
month: "month";
|
|
26
|
+
year: "year";
|
|
27
|
+
second: "second";
|
|
28
|
+
minute: "minute";
|
|
29
|
+
hour: "hour";
|
|
30
|
+
}>>;
|
|
31
|
+
credit_allowance_reset_count: z.ZodNullable<z.ZodNumber>;
|
|
32
|
+
credit_allowance_reset_anchor: z.ZodNullable<z.ZodEnum<{
|
|
33
|
+
calendar: "calendar";
|
|
34
|
+
rolling: "rolling";
|
|
35
|
+
plan_assignment: "plan_assignment";
|
|
36
|
+
}>>;
|
|
37
|
+
credit_allowance_reset_timezone: z.ZodNullable<z.ZodString>;
|
|
38
|
+
entitlements: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
39
|
+
value: z.ZodJSONSchema;
|
|
40
|
+
}, z.core.$strict>>;
|
|
41
|
+
credit_policy_type: z.ZodNullable<z.ZodEnum<{
|
|
42
|
+
prepaid: "prepaid";
|
|
43
|
+
credit_line: "credit_line";
|
|
44
|
+
}>>;
|
|
45
|
+
credit_limit: z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
|
|
46
|
+
admission_max_in_flight: z.ZodNullable<z.ZodNumber>;
|
|
47
|
+
operation_admission: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
48
|
+
max_in_flight: z.ZodNullable<z.ZodNumber>;
|
|
49
|
+
}, z.core.$strict>>;
|
|
50
|
+
catalog_revision_no: z.ZodPipe<z.ZodUnion<readonly [z.ZodNumber, z.ZodString]>, z.ZodTransform<number, string | number>>;
|
|
51
|
+
}, z.core.$strict>;
|
|
27
52
|
declare const SetUserPlanRowSchema: z.ZodObject<{
|
|
28
|
-
user_id: z.
|
|
29
|
-
plan_id: z.
|
|
30
|
-
|
|
31
|
-
|
|
53
|
+
user_id: z.ZodString;
|
|
54
|
+
plan_id: z.ZodString;
|
|
55
|
+
plan_key: z.ZodString;
|
|
56
|
+
plan_assigned_at: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodDate]>, z.ZodTransform<Date, string | Date>>;
|
|
57
|
+
assignment_state: z.ZodEnum<{
|
|
58
|
+
scheduled: "scheduled";
|
|
59
|
+
applied: "applied";
|
|
60
|
+
}>;
|
|
61
|
+
}, z.core.$strict>;
|
|
62
|
+
declare const EntitlementRowSchema: z.ZodObject<{
|
|
63
|
+
feature_key: z.ZodString;
|
|
64
|
+
feature_type: z.ZodEnum<{
|
|
65
|
+
string: "string";
|
|
66
|
+
boolean: "boolean";
|
|
67
|
+
enum: "enum";
|
|
68
|
+
integer: "integer";
|
|
69
|
+
}>;
|
|
70
|
+
feature_value: z.ZodJSONSchema;
|
|
71
|
+
catalog_revision_id: z.ZodString;
|
|
72
|
+
plan_key: z.ZodNullable<z.ZodString>;
|
|
73
|
+
value_source: z.ZodEnum<{
|
|
74
|
+
default: "default";
|
|
75
|
+
plan: "plan";
|
|
76
|
+
}>;
|
|
77
|
+
}, z.core.$strict>;
|
|
32
78
|
declare const AllowanceRowSchema: z.ZodObject<{
|
|
33
|
-
plan_id: z.
|
|
34
|
-
allowance_remaining: z.
|
|
35
|
-
period_start: z.
|
|
36
|
-
period_end: z.
|
|
37
|
-
}, z.core.$
|
|
79
|
+
plan_id: z.ZodString;
|
|
80
|
+
allowance_remaining: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
|
|
81
|
+
period_start: z.ZodPipe<z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodDate]>, z.ZodTransform<Date, string | Date>>, z.ZodTransform<string, Date>>;
|
|
82
|
+
period_end: z.ZodPipe<z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodDate]>, z.ZodTransform<Date, string | Date>>, z.ZodTransform<string, Date>>;
|
|
83
|
+
}, z.core.$strict>;
|
|
38
84
|
declare const UnsetPlanRowSchema: z.ZodObject<{
|
|
39
|
-
user_id: z.
|
|
40
|
-
}, z.core.$
|
|
41
|
-
declare const PlanMigrationStartRowSchema: z.
|
|
85
|
+
user_id: z.ZodString;
|
|
86
|
+
}, z.core.$strict>;
|
|
87
|
+
declare const PlanMigrationStartRowSchema: z.ZodString;
|
|
42
88
|
declare const PlanMigrationBatchRowSchema: z.ZodObject<{
|
|
43
89
|
migrated: z.ZodNumber;
|
|
44
90
|
done: z.ZodBoolean;
|
|
45
91
|
next_cursor: z.ZodNullable<z.ZodString>;
|
|
46
|
-
}, z.core.$
|
|
92
|
+
}, z.core.$strict>;
|
|
47
93
|
declare const QuotaStateRowSchema: z.ZodObject<{
|
|
48
94
|
user_id: z.ZodString;
|
|
49
95
|
quota_key: z.ZodString;
|
|
@@ -58,10 +104,10 @@ declare const QuotaStateRowSchema: z.ZodObject<{
|
|
|
58
104
|
block: "block";
|
|
59
105
|
allow: "allow";
|
|
60
106
|
}>;
|
|
61
|
-
window_start: z.ZodUnion<readonly [z.ZodString, z.
|
|
62
|
-
window_end: z.ZodUnion<readonly [z.ZodString, z.
|
|
107
|
+
window_start: z.ZodPipe<z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodDate]>, z.ZodTransform<Date, string | Date>>, z.ZodTransform<string, Date>>;
|
|
108
|
+
window_end: z.ZodPipe<z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodDate]>, z.ZodTransform<Date, string | Date>>, z.ZodTransform<string, Date>>;
|
|
63
109
|
emit_at_percent: z.ZodArray<z.ZodNumber>;
|
|
64
|
-
}, z.core.$
|
|
110
|
+
}, z.core.$strict>;
|
|
65
111
|
declare const QuotaEventRowSchema: z.ZodObject<{
|
|
66
112
|
event_id: z.ZodString;
|
|
67
113
|
quota_key: z.ZodString;
|
|
@@ -71,14 +117,15 @@ declare const QuotaEventRowSchema: z.ZodObject<{
|
|
|
71
117
|
threshold: "threshold";
|
|
72
118
|
blocked: "blocked";
|
|
73
119
|
}>;
|
|
74
|
-
threshold_percent: z.ZodNullable<z.
|
|
120
|
+
threshold_percent: z.ZodNullable<z.ZodNumber>;
|
|
75
121
|
idempotency_key: z.ZodString;
|
|
76
122
|
usage_charge_id: z.ZodNullable<z.ZodString>;
|
|
77
|
-
created_at: z.ZodUnion<readonly [z.ZodString, z.
|
|
78
|
-
}, z.core.$
|
|
123
|
+
created_at: z.ZodPipe<z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodDate]>, z.ZodTransform<Date, string | Date>>, z.ZodTransform<string, Date>>;
|
|
124
|
+
}, z.core.$strict>;
|
|
79
125
|
export type UnsetPlanRow = z.infer<typeof UnsetPlanRowSchema>;
|
|
80
126
|
export type UserPlanRow = z.infer<typeof UserPlanRowSchema>;
|
|
81
127
|
export type SetUserPlanRow = z.infer<typeof SetUserPlanRowSchema>;
|
|
128
|
+
export type EntitlementRow = z.infer<typeof EntitlementRowSchema>;
|
|
82
129
|
export type AllowanceRow = z.infer<typeof AllowanceRowSchema>;
|
|
83
130
|
export type PlanMigrationStartRow = z.infer<typeof PlanMigrationStartRowSchema>;
|
|
84
131
|
export type PlanMigrationBatchRow = z.infer<typeof PlanMigrationBatchRowSchema>;
|
|
@@ -90,7 +137,7 @@ export declare class PlanRepository {
|
|
|
90
137
|
constructor(callproc: CallProc);
|
|
91
138
|
/** Fetch a user's current plan. Returns null if user has no plan. */
|
|
92
139
|
getUserPlan(userId: string): Promise<UserPlanRow | null>;
|
|
93
|
-
getEntitlement(userId: string, feature: string): Promise<
|
|
140
|
+
getEntitlement(userId: string, feature: string): Promise<EntitlementRow | null>;
|
|
94
141
|
/** Assign a plan to a user. */
|
|
95
142
|
setUserPlan(userId: string, planKey: string, planAssignedAt: string | null): Promise<SetUserPlanRow>;
|
|
96
143
|
/** Remove a user's plan assignment. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../../../../src/credits/postgres/repositories/plan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;
|
|
1
|
+
{"version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