@noodleseed/one 0.52.1 → 0.54.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.
Files changed (79) hide show
  1. package/node_modules/@noodle-borg/service/dist/billing/active-commercial-entitlement.d.ts +25 -0
  2. package/node_modules/@noodle-borg/service/dist/billing/active-commercial-entitlement.d.ts.map +1 -0
  3. package/node_modules/@noodle-borg/service/dist/billing/active-commercial-entitlement.js +94 -0
  4. package/node_modules/@noodle-borg/service/dist/billing/active-commercial-entitlement.js.map +1 -0
  5. package/node_modules/@noodle-borg/service/dist/billing/commercial-entitlements.d.ts +8 -0
  6. package/node_modules/@noodle-borg/service/dist/billing/commercial-entitlements.d.ts.map +1 -1
  7. package/node_modules/@noodle-borg/service/dist/billing/commercial-entitlements.js +49 -0
  8. package/node_modules/@noodle-borg/service/dist/billing/commercial-entitlements.js.map +1 -1
  9. package/node_modules/@noodle-borg/service/dist/billing/in-memory.d.ts.map +1 -1
  10. package/node_modules/@noodle-borg/service/dist/billing/in-memory.js +3 -2
  11. package/node_modules/@noodle-borg/service/dist/billing/in-memory.js.map +1 -1
  12. package/node_modules/@noodle-borg/service/dist/billing/integration.d.ts.map +1 -1
  13. package/node_modules/@noodle-borg/service/dist/billing/integration.js +0 -3
  14. package/node_modules/@noodle-borg/service/dist/billing/integration.js.map +1 -1
  15. package/node_modules/@noodle-borg/service/dist/billing/model.d.ts +16 -3
  16. package/node_modules/@noodle-borg/service/dist/billing/model.d.ts.map +1 -1
  17. package/node_modules/@noodle-borg/service/dist/billing/model.js +1 -0
  18. package/node_modules/@noodle-borg/service/dist/billing/model.js.map +1 -1
  19. package/node_modules/@noodle-borg/service/dist/billing/paid-plan-catalog.d.ts +26 -0
  20. package/node_modules/@noodle-borg/service/dist/billing/paid-plan-catalog.d.ts.map +1 -0
  21. package/node_modules/@noodle-borg/service/dist/billing/paid-plan-catalog.js +94 -0
  22. package/node_modules/@noodle-borg/service/dist/billing/paid-plan-catalog.js.map +1 -0
  23. package/node_modules/@noodle-borg/service/dist/billing/postgres-authoritative-authority.d.ts +4 -1
  24. package/node_modules/@noodle-borg/service/dist/billing/postgres-authoritative-authority.d.ts.map +1 -1
  25. package/node_modules/@noodle-borg/service/dist/billing/postgres-authoritative-authority.js +21 -7
  26. package/node_modules/@noodle-borg/service/dist/billing/postgres-authoritative-authority.js.map +1 -1
  27. package/node_modules/@noodle-borg/service/dist/billing/postgres-commercial-enforcement-counters.js +4 -5
  28. package/node_modules/@noodle-borg/service/dist/billing/postgres-commercial-enforcement-counters.js.map +1 -1
  29. package/node_modules/@noodle-borg/service/dist/billing/postgres-commercial-enforcement-projection.d.ts +0 -1
  30. package/node_modules/@noodle-borg/service/dist/billing/postgres-commercial-enforcement-projection.d.ts.map +1 -1
  31. package/node_modules/@noodle-borg/service/dist/billing/postgres-commercial-enforcement-projection.js +92 -27
  32. package/node_modules/@noodle-borg/service/dist/billing/postgres-commercial-enforcement-projection.js.map +1 -1
  33. package/node_modules/@noodle-borg/service/dist/billing/postgres-meter-admission.d.ts +0 -1
  34. package/node_modules/@noodle-borg/service/dist/billing/postgres-meter-admission.d.ts.map +1 -1
  35. package/node_modules/@noodle-borg/service/dist/billing/postgres-meter-admission.js +72 -27
  36. package/node_modules/@noodle-borg/service/dist/billing/postgres-meter-admission.js.map +1 -1
  37. package/node_modules/@noodle-borg/service/dist/billing/postgres-production-app-admission.d.ts.map +1 -1
  38. package/node_modules/@noodle-borg/service/dist/billing/postgres-production-app-admission.js +56 -26
  39. package/node_modules/@noodle-borg/service/dist/billing/postgres-production-app-admission.js.map +1 -1
  40. package/node_modules/@noodle-borg/service/dist/billing/postgres-records.d.ts +4 -0
  41. package/node_modules/@noodle-borg/service/dist/billing/postgres-records.d.ts.map +1 -1
  42. package/node_modules/@noodle-borg/service/dist/billing/postgres-records.js +38 -9
  43. package/node_modules/@noodle-borg/service/dist/billing/postgres-records.js.map +1 -1
  44. package/node_modules/@noodle-borg/service/dist/billing/postgres-schema.d.ts.map +1 -1
  45. package/node_modules/@noodle-borg/service/dist/billing/postgres-schema.js +28 -21
  46. package/node_modules/@noodle-borg/service/dist/billing/postgres-schema.js.map +1 -1
  47. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-authority.d.ts +5 -0
  48. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-authority.d.ts.map +1 -0
  49. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-authority.js +97 -0
  50. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-authority.js.map +1 -0
  51. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-persistence.d.ts +50 -0
  52. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-persistence.d.ts.map +1 -0
  53. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-persistence.js +221 -0
  54. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-persistence.js.map +1 -0
  55. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-support.d.ts +8 -0
  56. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-support.d.ts.map +1 -0
  57. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-support.js +42 -0
  58. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation-support.js.map +1 -0
  59. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation.d.ts +15 -0
  60. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation.d.ts.map +1 -0
  61. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation.js +123 -0
  62. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-reconciliation.js.map +1 -0
  63. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-schema.d.ts +4 -0
  64. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-schema.d.ts.map +1 -0
  65. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-schema.js +419 -0
  66. package/node_modules/@noodle-borg/service/dist/billing/postgres-subscription-schema.js.map +1 -0
  67. package/node_modules/@noodle-borg/service/dist/billing/subscription-model.d.ts +40 -0
  68. package/node_modules/@noodle-borg/service/dist/billing/subscription-model.d.ts.map +1 -0
  69. package/node_modules/@noodle-borg/service/dist/billing/subscription-model.js +388 -0
  70. package/node_modules/@noodle-borg/service/dist/billing/subscription-model.js.map +1 -0
  71. package/node_modules/@noodle-borg/service/dist/billing/subscription-reconciliation.d.ts +45 -0
  72. package/node_modules/@noodle-borg/service/dist/billing/subscription-reconciliation.d.ts.map +1 -0
  73. package/node_modules/@noodle-borg/service/dist/billing/subscription-reconciliation.js +165 -0
  74. package/node_modules/@noodle-borg/service/dist/billing/subscription-reconciliation.js.map +1 -0
  75. package/node_modules/@noodle-borg/service/dist/billing-public.d.ts +6 -1
  76. package/node_modules/@noodle-borg/service/dist/billing-public.d.ts.map +1 -1
  77. package/node_modules/@noodle-borg/service/dist/billing-public.js +5 -0
  78. package/node_modules/@noodle-borg/service/dist/billing-public.js.map +1 -1
  79. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"postgres-records.js","sourceRoot":"","sources":["../../src/billing/postgres-records.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAIL,sBAAsB,EAEtB,qBAAqB,EACrB,wBAAwB,EAGxB,wBAAwB,GACzB,MAAM,YAAY,CAAC;AAEpB,MAAM,UAAU,kBAAkB,CAAC,GAAe;IAChD,IAAI,GAAG,CAAC,KAAK,KAAK,QAAQ,IAAI,GAAG,CAAC,KAAK,KAAK,OAAO,IAAI,GAAG,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;QAChF,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACpE,CAAC;IACD,OAAO;QACL,EAAE,EAAE,wBAAwB,CAAC,GAA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AC,MAAM,GAAG,GAAG;QACvB,CAAC,GAAG,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE;YACjC,MAAM,IAAI,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YACrC,OAAO,IAAI,IAAI,IAAI,IAAI,IAAI,KAAK,IAAI,CAAC;QACvC,CAAC,CAAC,EACF,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,wCAAwC,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,SAAS,WAAW,CAAC,KAAoB;IACvC,MAAM,IAAI,GAAG,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAChG,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC;AAC5B,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"postgres-schema.d.ts","sourceRoot":"","sources":["../../src/billing/postgres-schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAQ/B,2FAA2F;AAC3F,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAqSnE"}
1
+ {"version":3,"file":"postgres-schema.d.ts","sourceRoot":"","sources":["../../src/billing/postgres-schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAS/B,2FAA2F;AAC3F,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CA2SnE"}
@@ -1,9 +1,10 @@
1
- import { FREE_PLAN_VERSION } from './model.js';
1
+ import { BILLING_PUBLIC_PLAN_VERSIONS } from './paid-plan-catalog.js';
2
2
  import { ensureBillingEnforcementActivationSchema } from './postgres-enforcement-activation-schema.js';
3
3
  import { ensureBillingEnforcementProfileSchema } from './postgres-enforcement-profile-schema.js';
4
4
  import { ensureAuthoritativeMeterSchema } from './postgres-meter-schema.js';
5
5
  import { ensureBillingMeterWriterHealthSchema } from './postgres-meter-writer-health-schema.js';
6
6
  import { planVersionRowToRecord } from './postgres-records.js';
7
+ import { ensureBillingSubscriptionSchema } from './postgres-subscription-schema.js';
7
8
  /** Additive BA-1 schema. No existing organization is linked or otherwise migrated here. */
8
9
  export async function ensureBillingSchema(pool) {
9
10
  await pool.query(`
@@ -81,12 +82,16 @@ export async function ensureBillingSchema(pool) {
81
82
  billing_account_id text NOT NULL REFERENCES billing_accounts(id) ON DELETE RESTRICT,
82
83
  version integer NOT NULL CHECK (version > 0),
83
84
  plan_version_id text NOT NULL REFERENCES billing_plan_versions(id) ON DELETE RESTRICT,
84
- source_kind text NOT NULL CHECK (source_kind IN ('system-free')),
85
- generation_reason text NOT NULL CHECK (generation_reason IN ('default-account-created')),
86
- verification_status text NOT NULL CHECK (verification_status IN ('verified')),
85
+ schema_version integer NOT NULL DEFAULT 1,
86
+ source_kind text NOT NULL,
87
+ generation_reason text NOT NULL,
88
+ verification_status text NOT NULL,
87
89
  effective_from timestamptz NOT NULL,
90
+ effective_until timestamptz,
88
91
  monthly_usage_anchor timestamptz NOT NULL,
89
92
  entitlements jsonb NOT NULL,
93
+ source_subscription_id text,
94
+ source_subscription_version integer,
90
95
  content_checksum text NOT NULL,
91
96
  verified_at timestamptz NOT NULL,
92
97
  created_at timestamptz NOT NULL,
@@ -102,6 +107,7 @@ export async function ensureBillingSchema(pool) {
102
107
  REFERENCES billing_entitlement_snapshots(billing_account_id, id) ON DELETE RESTRICT
103
108
  )
104
109
  `);
110
+ await ensureBillingSubscriptionSchema(pool);
105
111
  await pool.query(`
106
112
  CREATE TABLE IF NOT EXISTS org_billing_home_accounts (
107
113
  org_slug text PRIMARY KEY REFERENCES orgs(slug) ON DELETE RESTRICT,
@@ -271,23 +277,24 @@ export async function ensureBillingSchema(pool) {
271
277
  PRIMARY KEY (migration_id, org_slug)
272
278
  )
273
279
  `);
274
- await pool.query(`INSERT INTO billing_plan_versions
275
- (id, plan_code, version, effective_from, entitlements, content_checksum, created_at)
276
- VALUES ($1, $2, $3, $4, $5::jsonb, $6, $7)
277
- ON CONFLICT (id) DO NOTHING`, [
278
- FREE_PLAN_VERSION.id,
279
- FREE_PLAN_VERSION.planCode,
280
- FREE_PLAN_VERSION.version,
281
- FREE_PLAN_VERSION.effectiveFrom,
282
- JSON.stringify(FREE_PLAN_VERSION.entitlements),
283
- FREE_PLAN_VERSION.contentChecksum,
284
- FREE_PLAN_VERSION.createdAt,
285
- ]);
286
- const { rows } = await pool.query('SELECT * FROM billing_plan_versions WHERE id = $1', [FREE_PLAN_VERSION.id]);
287
- const persistedFree = rows[0] ? planVersionRowToRecord(rows[0]) : undefined;
288
- if (persistedFree === undefined ||
289
- persistedFree.contentChecksum !== FREE_PLAN_VERSION.contentChecksum) {
290
- throw new Error('persisted Free plan version does not match the immutable platform definition');
280
+ for (const plan of BILLING_PUBLIC_PLAN_VERSIONS) {
281
+ await pool.query(`INSERT INTO billing_plan_versions
282
+ (id, plan_code, version, effective_from, entitlements, content_checksum, created_at)
283
+ VALUES ($1, $2, $3, $4, $5::jsonb, $6, $7)
284
+ ON CONFLICT (id) DO NOTHING`, [
285
+ plan.id,
286
+ plan.planCode,
287
+ plan.version,
288
+ plan.effectiveFrom,
289
+ JSON.stringify(plan.entitlements),
290
+ plan.contentChecksum,
291
+ plan.createdAt,
292
+ ]);
293
+ const { rows } = await pool.query('SELECT * FROM billing_plan_versions WHERE id = $1', [plan.id]);
294
+ const persisted = rows[0] ? planVersionRowToRecord(rows[0]) : undefined;
295
+ if (persisted === undefined || persisted.contentChecksum !== plan.contentChecksum) {
296
+ throw new Error(`persisted ${plan.planCode} plan version does not match the immutable platform definition`);
297
+ }
291
298
  }
292
299
  }
293
300
  //# sourceMappingURL=postgres-schema.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"postgres-schema.js","sourceRoot":"","sources":["../../src/billing/postgres-schema.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,wCAAwC,EAAE,MAAM,6CAA6C,CAAC;AACvG,OAAO,EAAE,qCAAqC,EAAE,MAAM,0CAA0C,CAAC;AACjG,OAAO,EAAE,8BAA8B,EAAE,MAAM,4BAA4B,CAAC;AAC5E,OAAO,EAAE,oCAAoC,EAAE,MAAM,0CAA0C,CAAC;AAChG,OAAO,EAAuB,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAEpF,2FAA2F;AAC3F,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,IAAU;IAClD,MAAM,IAAI,CAAC,KAAK,CAAC;;;;;;;;;;;GAWhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;;;;;GAOhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;;;;;GAOhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;;;;;;;;GAUhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;GAGhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;GAGhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;;;;;;;;;;;;;;;;;;;GAqBhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;;;;;;;;;;;;;;;;GAkBhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;;;;;GAOhB,CAAC,CAAC;IACH,MAAM,IAAI,CAAC,KAAK,CAAC;;;;;;;;;;;GAWhB,CAAC,CAAC;IACH,MAAM,IA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export declare function requirePaidAuthority(client: PoolClient, key: string): Promise<void>;
3
+ export declare function requireActiveBillingAccount(client: PoolClient, billingAccountId: string): Promise<void>;
4
+ export declare function requireEnforcedBillingAccount(client: PoolClient, billingAccountId: string): Promise<void>;
5
+ //# sourceMappingURL=postgres-subscription-reconciliation-authority.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres-subscription-reconciliation-authority.d.ts","sourceRoot":"","sources":["../../src/billing/postgres-subscription-reconciliation-authority.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAyBrC,wBAAsB,oBAAoB,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBzF;AAED,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,UAAU,EAClB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,IAAI,CAAC,CAWf;AAED,wBAAsB,6BAA6B,CACjD,MAAM,EAAE,UAAU,EAClB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,IAAI,CAAC,CA6Ef"}
@@ -0,0 +1,97 @@
1
+ import { BillingAuthoritativeAuthorityInconsistentError, readBillingAuthoritativeAuthorityTx, } from './postgres-authoritative-authority.js';
2
+ import { conflict } from './postgres-subscription-reconciliation-support.js';
3
+ import { BILLING_PAID_ADMISSION_CONTRACT_VERSION } from './subscription-reconciliation.js';
4
+ export async function requirePaidAuthority(client, key) {
5
+ try {
6
+ const authority = await readBillingAuthoritativeAuthorityTx(client, { keyGeneration: key });
7
+ if (authority.state !== 'active' ||
8
+ authority.admissionContractVersion !== BILLING_PAID_ADMISSION_CONTRACT_VERSION) {
9
+ throw conflict('paid_admission_contract_unavailable', 'paid subscription activation requires active commercial admission contract v2');
10
+ }
11
+ }
12
+ catch (error) {
13
+ if (error instanceof BillingAuthoritativeAuthorityInconsistentError) {
14
+ throw conflict('paid_admission_contract_unavailable', 'paid subscription activation requires coherent commercial admission authority');
15
+ }
16
+ throw error;
17
+ }
18
+ }
19
+ export async function requireActiveBillingAccount(client, billingAccountId) {
20
+ const { rows } = await client.query(`SELECT state FROM billing_accounts WHERE id = $1 FOR UPDATE`, [billingAccountId]);
21
+ if (rows[0]?.state !== 'active') {
22
+ throw conflict('billing_account_conflict', 'paid subscription reconciliation requires an active billing account');
23
+ }
24
+ }
25
+ export async function requireEnforcedBillingAccount(client, billingAccountId) {
26
+ const { rows } = await client.query(`SELECT seal.id AS seal_id,
27
+ seal.schema_version AS seal_schema_version,
28
+ seal.cohort_kind AS seal_cohort_kind,
29
+ seal.state AS seal_state,
30
+ seal.post_seal_account_profile AS seal_post_profile,
31
+ seal.sealed_at,
32
+ active.profile_version AS active_profile_version,
33
+ profile.schema_version AS profile_schema_version,
34
+ profile.mode AS profile_mode,
35
+ profile.cohort_kind AS profile_cohort_kind,
36
+ profile.cohort_seal_id AS profile_cohort_seal_id,
37
+ profile.source AS profile_source,
38
+ profile.effective_from AS profile_effective_from,
39
+ profile.created_at AS profile_created_at
40
+ FROM billing_enforcement_cohort_seals AS seal
41
+ LEFT JOIN billing_account_active_enforcement_profiles AS active
42
+ ON active.billing_account_id = $1
43
+ LEFT JOIN billing_account_enforcement_profiles AS profile
44
+ ON profile.billing_account_id = active.billing_account_id
45
+ AND profile.version = active.profile_version
46
+ WHERE seal.singleton = true`, [billingAccountId]);
47
+ const profile = rows[0];
48
+ if (profile === undefined || !profileBindingIsComplete(profile)) {
49
+ throw conflict('billing_account_enforcement_unavailable', 'paid subscription activation requires a coherent active enforcement profile');
50
+ }
51
+ const sharedProfileIsCoherent = profile.seal_schema_version === 1 &&
52
+ profile.seal_cohort_kind === 'legacy_internal_v1' &&
53
+ profile.seal_state === 'sealed' &&
54
+ profile.seal_post_profile === 'enforced' &&
55
+ profile.profile_schema_version === 1 &&
56
+ profile.profile_cohort_kind === profile.seal_cohort_kind &&
57
+ profile.profile_cohort_seal_id === profile.seal_id;
58
+ if (!sharedProfileIsCoherent) {
59
+ throw conflict('billing_account_enforcement_unavailable', 'paid subscription activation requires a coherent active enforcement profile');
60
+ }
61
+ const sealedAt = toIso(profile.sealed_at);
62
+ const effectiveFrom = toIso(profile.profile_effective_from);
63
+ const createdAt = toIso(profile.profile_created_at);
64
+ if (profile.profile_mode === 'legacy_internal_exempt') {
65
+ if (profile.profile_source !== 'legacy_cohort_seal' ||
66
+ effectiveFrom !== sealedAt ||
67
+ createdAt !== sealedAt) {
68
+ throw conflict('billing_account_enforcement_unavailable', 'paid subscription activation requires a coherent active enforcement profile');
69
+ }
70
+ throw conflict('billing_account_not_enforced', 'legacy internal exempt billing accounts cannot activate paid subscriptions');
71
+ }
72
+ if (profile.profile_mode !== 'enforced' ||
73
+ profile.profile_source !== 'post_seal_account' ||
74
+ effectiveFrom < sealedAt ||
75
+ createdAt !== effectiveFrom) {
76
+ throw conflict('billing_account_enforcement_unavailable', 'paid subscription activation requires a coherent active enforcement profile');
77
+ }
78
+ }
79
+ function profileBindingIsComplete(profile) {
80
+ return (Number.isSafeInteger(profile.active_profile_version) &&
81
+ (profile.active_profile_version ?? 0) > 0 &&
82
+ profile.profile_schema_version !== null &&
83
+ profile.profile_mode !== null &&
84
+ profile.profile_cohort_kind !== null &&
85
+ profile.profile_cohort_seal_id !== null &&
86
+ profile.profile_source !== null &&
87
+ profile.profile_effective_from !== null &&
88
+ profile.profile_created_at !== null);
89
+ }
90
+ function toIso(value) {
91
+ const date = value instanceof Date ? value : new Date(value);
92
+ if (!Number.isFinite(date.getTime())) {
93
+ throw conflict('billing_account_enforcement_unavailable', 'paid subscription activation requires valid enforcement profile timestamps');
94
+ }
95
+ return date.toISOString();
96
+ }
97
+ //# sourceMappingURL=postgres-subscription-reconciliation-authority.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres-subscription-reconciliation-authority.js","sourceRoot":"","sources":["../../src/billing/postgres-subscription-reconciliation-authority.ts"],"names":[],"mappings":"AACA,OAAO,EACL,8CAA8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AAc,KAAK,mBAAmB;QAC9C,aAAa,GAAG,QAAQ;QACxB,SAAS,KAAK,aAAa,EAC3B,CAAC;QACD,MAAM,QAAQ,CACZ,yCAAyC,EACzC,6EAA6E,CAC9E,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,wBAAwB,CAC/B,OAA8B;IAW9B,OAAO,CACL,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,sBAAsB,CAAC;QACpD,CAAC,OAAO,CAAC,sBAAsB,IAAI,CAAC,CAAC,GAAG,CAAC;QACzC,OAAO,CAAC,sBAAsB,KAAK,IAAI;QACvC,OAAO,CAAC,YAAY,KAAK,IAAI;QAC7B,OAAO,CAAC,mBAAmB,KAAK,IAAI;QACpC,OAAO,CAAC,sBAAsB,KAAK,IAAI;QACvC,OAAO,CAAC,cAAc,KAAK,IAAI;QAC/B,OAAO,CAAC,sBAAsB,KAAK,IAAI;QACvC,OAAO,CAAC,kBAAkB,KAAK,IAAI,CACpC,CAAC;AACJ,CAAC;AAED,SAAS,KAAK,CAAC,KAAoB;IACjC,MAAM,IAAI,GAAG,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;QACrC,MAAM,QAAQ,CACZ,yCAAyC,EACzC,4EAA4E,CAC7E,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,50 @@
1
+ import type { PoolClient } from 'pg';
2
+ import { entitlementSnapshotRowToRecord } from './postgres-records.js';
3
+ import type { createPaidEntitlementSnapshot } from './subscription-model.js';
4
+ import { type BillingSubscriptionReconciliationRequest, type BillingSubscriptionReconciliationResult, type normalizeBillingSubscriptionReconciliationRequest } from './subscription-reconciliation.js';
5
+ export interface OperationRow {
6
+ readonly request_fingerprint: string;
7
+ readonly billing_account_id: string;
8
+ readonly subscription_id: string;
9
+ readonly revision_version: number;
10
+ readonly snapshot_id: string;
11
+ readonly created_at: Date | string;
12
+ readonly result: unknown;
13
+ readonly persisted_snapshot_id: string | null;
14
+ readonly snapshot_version: number | null;
15
+ readonly plan_version_id: string | null;
16
+ readonly source_subscription_id: string | null;
17
+ readonly source_subscription_version: number | null;
18
+ }
19
+ export interface CurrentSubscriptionRow {
20
+ readonly subscription_id: string;
21
+ readonly revision_version: number;
22
+ }
23
+ export declare function findOperation(client: PoolClient, idempotencyHash: string): Promise<OperationRow | undefined>;
24
+ export declare function replayOperation(row: OperationRow, fingerprint: string): {
25
+ replayed: boolean;
26
+ schemaVersion: 1;
27
+ billingAccountId: string;
28
+ subscriptionId: string;
29
+ subscriptionRevisionVersion: number;
30
+ snapshotId: string;
31
+ snapshotVersion: number;
32
+ planVersionId: string;
33
+ reconciledAt: string;
34
+ };
35
+ export declare function readActiveSnapshotForUpdate(client: PoolClient, billingAccountId: string): Promise<import("./model.js").EntitlementSnapshotRecord | undefined>;
36
+ export declare function requireNoLaterSnapshot(client: PoolClient, billingAccountId: string, activeVersion: number): Promise<void>;
37
+ export declare function readCurrentSubscriptionForUpdate(client: PoolClient, billingAccountId: string): Promise<CurrentSubscriptionRow | undefined>;
38
+ export declare function validateCurrentSubscription(current: CurrentSubscriptionRow | undefined, expected: BillingSubscriptionReconciliationRequest['expectedCurrentBaseSubscription'], previousSnapshot: ReturnType<typeof entitlementSnapshotRowToRecord>): void;
39
+ export declare function validateSubscriptionHistory(client: PoolClient, request: ReturnType<typeof normalizeBillingSubscriptionReconciliationRequest>, current: CurrentSubscriptionRow | undefined): Promise<void>;
40
+ export declare function insertRevision(client: PoolClient, revision: ReturnType<typeof normalizeBillingSubscriptionReconciliationRequest>['revision']): Promise<void>;
41
+ export declare function insertPaidSnapshot(client: PoolClient, snapshot: ReturnType<typeof createPaidEntitlementSnapshot>): Promise<void>;
42
+ export declare function compareAndSwapPointers(client: PoolClient, request: ReturnType<typeof normalizeBillingSubscriptionReconciliationRequest>, previousSnapshotId: string, current: CurrentSubscriptionRow | undefined): Promise<void>;
43
+ export declare function insertOperation(client: PoolClient, input: {
44
+ readonly operationId: string;
45
+ readonly idempotencyHash: string;
46
+ readonly fingerprint: string;
47
+ readonly result: BillingSubscriptionReconciliationResult;
48
+ readonly createdAt: string;
49
+ }): Promise<void>;
50
+ //# sourceMappingURL=postgres-subscription-reconciliation-persistence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres-subscription-reconciliation-persistence.d.ts","sourceRoot":"","sources":["../../src/billing/postgres-subscription-reconciliation-persistence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AACrC,OAAO,EAA+B,8BAA8B,EAAE,MAAM,uBAAuB,CAAC;AAEpG,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EACL,KAAK,wCAAwC,EAC7C,KAAK,uCAAuC,EAC5C,KAAK,iDAAiD,EAEvD,MAAM,kCAAkC,CAAC;AAE1C,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,mBAAmB,EAAE,MAAM,CAAC;IACrC,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;IACpC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,IAAI,GAAG,MAAM,CAAC;IACnC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,qBAAqB,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9C,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IACzC,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;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D,CAAC,CAAC,UAAU,CAAC,GACzF,OAAO,CAAC,IAAI,CAAC,CA4Bf;AAED,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,UAAU,EAClB,QAAQ,EAAE,UAAU,CAAC,OAAO,6BAA6B,CAAC,GACzD,OAAO,CAAC,IAAI,CAAC,CA6Bf;AAED,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,UAAU,EAClB,OAAO,EAAE,UAAU,CAAC,OAAO,iDAAiD,CAAC,EAC7E,kBAAkB,EAAE,MAAM,EAC1B,OAAO,EAAE,sBAAsB,GAAG,SAAS,GAC1C,OAAO,CAAC,IAAI,CAAC,CAqCf;AAED,wBAAsB,eAAe,CACnC,MAAM,EAAE,UAAU,EAClB,KAAK,EAAE;IACL,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,uCAAuC,CAAC;IACzD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B,GACA,OAAO,CAAC,IAAI,CAAC,CAkBf"}
@@ -0,0 +1,221 @@
1
+ import { entitlementSnapshotRowToRecord } from './postgres-records.js';
2
+ import { conflict } from './postgres-subscription-reconciliation-support.js';
3
+ import { parseBillingSubscriptionReconciliationResult, } from './subscription-reconciliation.js';
4
+ export async function findOperation(client, idempotencyHash) {
5
+ const { rows } = await client.query(`SELECT operation.request_fingerprint,
6
+ operation.billing_account_id,
7
+ operation.subscription_id,
8
+ operation.revision_version,
9
+ operation.snapshot_id,
10
+ operation.created_at,
11
+ operation.result,
12
+ snapshot.id AS persisted_snapshot_id,
13
+ snapshot.version AS snapshot_version,
14
+ snapshot.plan_version_id,
15
+ snapshot.source_subscription_id,
16
+ snapshot.source_subscription_version
17
+ FROM billing_subscription_reconciliation_operations AS operation
18
+ LEFT JOIN billing_entitlement_snapshots AS snapshot
19
+ ON snapshot.billing_account_id = operation.billing_account_id
20
+ AND snapshot.id = operation.snapshot_id
21
+ WHERE operation.idempotency_key_hash = $1`, [idempotencyHash]);
22
+ return rows[0];
23
+ }
24
+ export function replayOperation(row, fingerprint) {
25
+ if (row.request_fingerprint !== fingerprint) {
26
+ throw conflict('idempotency_key_conflict', 'idempotency key was already used by a different subscription reconciliation request');
27
+ }
28
+ let result;
29
+ try {
30
+ result = parseBillingSubscriptionReconciliationResult(row.result);
31
+ }
32
+ catch {
33
+ throw invalidIdempotencyEvidence();
34
+ }
35
+ const createdAt = row.created_at instanceof Date ? row.created_at.toISOString() : row.created_at;
36
+ if (result.replayed ||
37
+ result.billingAccountId !== row.billing_account_id ||
38
+ result.subscriptionId !== row.subscription_id ||
39
+ result.subscriptionRevisionVersion !== row.revision_version ||
40
+ result.snapshotId !== row.snapshot_id ||
41
+ result.reconciledAt !== createdAt ||
42
+ row.persisted_snapshot_id !== row.snapshot_id ||
43
+ row.source_subscription_id !== row.subscription_id ||
44
+ row.source_subscription_version !== row.revision_version ||
45
+ result.snapshotVersion !== row.snapshot_version ||
46
+ result.planVersionId !== row.plan_version_id) {
47
+ throw invalidIdempotencyEvidence();
48
+ }
49
+ return { ...result, replayed: true };
50
+ }
51
+ function invalidIdempotencyEvidence() {
52
+ return conflict('idempotency_evidence_invalid', 'persisted subscription reconciliation evidence is inconsistent');
53
+ }
54
+ export async function readActiveSnapshotForUpdate(client, billingAccountId) {
55
+ const { rows } = await client.query(`SELECT snapshot.*,
56
+ plan.plan_code,
57
+ plan.version AS plan_version,
58
+ plan.effective_from AS plan_effective_from,
59
+ plan.entitlements AS plan_entitlements,
60
+ plan.content_checksum AS plan_content_checksum,
61
+ plan.created_at AS plan_created_at
62
+ FROM billing_account_active_snapshots AS active
63
+ JOIN billing_entitlement_snapshots AS snapshot
64
+ ON snapshot.billing_account_id = active.billing_account_id
65
+ AND snapshot.id = active.snapshot_id
66
+ JOIN billing_plan_versions AS plan ON plan.id = snapshot.plan_version_id
67
+ WHERE active.billing_account_id = $1
68
+ FOR UPDATE OF active, snapshot`, [billingAccountId]);
69
+ return rows[0] === undefined ? undefined : entitlementSnapshotRowToRecord(rows[0]);
70
+ }
71
+ export async function requireNoLaterSnapshot(client, billingAccountId, activeVersion) {
72
+ const { rows } = await client.query(`SELECT max(version)::integer AS latest_version
73
+ FROM billing_entitlement_snapshots
74
+ WHERE billing_account_id = $1`, [billingAccountId]);
75
+ if (rows[0]?.latest_version !== activeVersion) {
76
+ throw conflict('active_snapshot_conflict', 'the billing account has unapplied entitlement history');
77
+ }
78
+ }
79
+ export async function readCurrentSubscriptionForUpdate(client, billingAccountId) {
80
+ const { rows } = await client.query(`SELECT subscription_id, revision_version
81
+ FROM billing_account_current_base_subscriptions
82
+ WHERE billing_account_id = $1
83
+ FOR UPDATE`, [billingAccountId]);
84
+ return rows[0];
85
+ }
86
+ export function validateCurrentSubscription(current, expected, previousSnapshot) {
87
+ if (expected === null) {
88
+ if (current !== undefined || previousSnapshot.schemaVersion !== 1) {
89
+ throw conflict('base_subscription_conflict', 'the billing account already has paid subscription state');
90
+ }
91
+ return;
92
+ }
93
+ if (current === undefined ||
94
+ current.subscription_id !== expected.subscriptionId ||
95
+ current.revision_version !== expected.revisionVersion ||
96
+ previousSnapshot.schemaVersion !== 2 ||
97
+ previousSnapshot.sourceSubscriptionId !== expected.subscriptionId ||
98
+ previousSnapshot.sourceSubscriptionVersion !== expected.revisionVersion) {
99
+ throw conflict('base_subscription_conflict', 'the current base subscription changed; retry from the current billing account state');
100
+ }
101
+ }
102
+ export async function validateSubscriptionHistory(client, request, current) {
103
+ const base = await client.query(`SELECT billing_account_id
104
+ FROM billing_base_subscriptions
105
+ WHERE id = $1
106
+ FOR UPDATE`, [request.revision.subscriptionId]);
107
+ if (current === undefined) {
108
+ if (base.rows[0] !== undefined) {
109
+ throw conflict('base_subscription_conflict', 'the subscription id is already in use');
110
+ }
111
+ return;
112
+ }
113
+ if (base.rows[0]?.billing_account_id !== request.billingAccountId) {
114
+ throw conflict('base_subscription_conflict', 'the current base subscription is incomplete');
115
+ }
116
+ const revisions = await client.query(`SELECT max(version)::integer AS latest_version
117
+ FROM billing_base_subscription_revisions
118
+ WHERE subscription_id = $1`, [request.revision.subscriptionId]);
119
+ if (revisions.rows[0]?.latest_version !== current.revision_version) {
120
+ throw conflict('base_subscription_conflict', 'the subscription has unapplied revision history');
121
+ }
122
+ }
123
+ export async function insertRevision(client, revision) {
124
+ await client.query(`INSERT INTO billing_base_subscription_revisions
125
+ (schema_version, subscription_id, billing_account_id, version, plan_version_id, state,
126
+ billing_interval, currency, base_amount_minor, current_period_start, current_period_end,
127
+ effective_from, effective_until, renews, content_checksum, verified_at, created_at)
128
+ VALUES
129
+ ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)`, [
130
+ revision.schemaVersion,
131
+ revision.subscriptionId,
132
+ revision.billingAccountId,
133
+ revision.version,
134
+ revision.planVersionId,
135
+ revision.state,
136
+ revision.billingInterval,
137
+ revision.currency,
138
+ revision.baseAmountMinor,
139
+ revision.currentPeriodStart,
140
+ revision.currentPeriodEnd,
141
+ revision.effectiveFrom,
142
+ revision.effectiveUntil,
143
+ revision.renews,
144
+ revision.contentChecksum,
145
+ revision.verifiedAt,
146
+ revision.createdAt,
147
+ ]);
148
+ }
149
+ export async function insertPaidSnapshot(client, snapshot) {
150
+ await client.query(`INSERT INTO billing_entitlement_snapshots
151
+ (id, billing_account_id, version, plan_version_id, schema_version, source_kind,
152
+ generation_reason, verification_status, effective_from, effective_until,
153
+ monthly_usage_anchor, entitlements, source_subscription_id, source_subscription_version,
154
+ content_checksum, verified_at, created_at)
155
+ VALUES
156
+ ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12::jsonb, $13, $14, $15, $16, $17)`, [
157
+ snapshot.id,
158
+ snapshot.billingAccountId,
159
+ snapshot.version,
160
+ snapshot.planVersionId,
161
+ snapshot.schemaVersion,
162
+ snapshot.sourceKind,
163
+ snapshot.generationReason,
164
+ snapshot.verificationStatus,
165
+ snapshot.effectiveFrom,
166
+ snapshot.effectiveUntil,
167
+ snapshot.monthlyUsageAnchor,
168
+ JSON.stringify(snapshot.entitlements),
169
+ snapshot.sourceSubscriptionId,
170
+ snapshot.sourceSubscriptionVersion,
171
+ snapshot.contentChecksum,
172
+ snapshot.verifiedAt,
173
+ snapshot.createdAt,
174
+ ]);
175
+ }
176
+ export async function compareAndSwapPointers(client, request, previousSnapshotId, current) {
177
+ const activeSnapshot = await client.query(`UPDATE billing_account_active_snapshots
178
+ SET snapshot_id = $3
179
+ WHERE billing_account_id = $1 AND snapshot_id = $2`, [request.billingAccountId, previousSnapshotId, request.snapshotId]);
180
+ if (activeSnapshot.rowCount !== 1) {
181
+ throw conflict('active_snapshot_conflict', 'the active entitlement snapshot changed');
182
+ }
183
+ if (current === undefined) {
184
+ const inserted = await client.query(`INSERT INTO billing_account_current_base_subscriptions
185
+ (billing_account_id, subscription_id, revision_version)
186
+ VALUES ($1, $2, $3)
187
+ ON CONFLICT (billing_account_id) DO NOTHING`, [request.billingAccountId, request.revision.subscriptionId, request.revision.version]);
188
+ if (inserted.rowCount !== 1) {
189
+ throw conflict('base_subscription_conflict', 'the current base subscription changed');
190
+ }
191
+ return;
192
+ }
193
+ const updated = await client.query(`UPDATE billing_account_current_base_subscriptions
194
+ SET revision_version = $4
195
+ WHERE billing_account_id = $1 AND subscription_id = $2 AND revision_version = $3`, [
196
+ request.billingAccountId,
197
+ current.subscription_id,
198
+ current.revision_version,
199
+ request.revision.version,
200
+ ]);
201
+ if (updated.rowCount !== 1) {
202
+ throw conflict('base_subscription_conflict', 'the current base subscription changed');
203
+ }
204
+ }
205
+ export async function insertOperation(client, input) {
206
+ await client.query(`INSERT INTO billing_subscription_reconciliation_operations
207
+ (operation_id, schema_version, action, billing_account_id, idempotency_key_hash,
208
+ request_fingerprint, subscription_id, revision_version, snapshot_id, result, created_at)
209
+ VALUES ($1, 1, 'paid_reconcile', $2, $3, $4, $5, $6, $7, $8::jsonb, $9)`, [
210
+ input.operationId,
211
+ input.result.billingAccountId,
212
+ input.idempotencyHash,
213
+ input.fingerprint,
214
+ input.result.subscriptionId,
215
+ input.result.subscriptionRevisionVersion,
216
+ input.result.snapshotId,
217
+ JSON.stringify(input.result),
218
+ input.createdAt,
219
+ ]);
220
+ }
221
+ //# sourceMappingURL=postgres-subscription-reconciliation-persistence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres-subscription-reconciliation-persistence.js","sourceRoot":"","sources":["../../src/billing/postgres-subscription-reconciliation-persistence.ts"],"names":[],"mappings":"AACA,OAAO,EAA+B,8BAA8B,EAAE,MAAM,uBAAuB,CAAC;AACpG,OAAO,EAAE,QAAQ,EAAE,MAAM,mDAAmD,CAAC;AAE7E,OAAO,EAIL,4CAA4C,GAC7C,MAAM,kCAAkC,CAAC;AAsB1C,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,MAAkB,EAClB,eAAuB;IAEvB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,KAAK,CACjC;;;;;;;;;;;;;;;;+CAgB2C,EAC3C,CAAC,eAAe,CAAC,CAClB,CAAC;IACF,OAAO,IAAI,CAAC,CAAC,CAAC,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,GAAiB,EAAE,WAAmB;IACpE,IAAI,GAAG,CAAC,mBAAmB,KAAK,WAAW,EAAE,CAAC;QAC5C,MAAM,QAAQ,CACZ,0BAA0B,EAC1B,qFAAqF,CACtF,CAAC;IACJ,CAAC;IACD,IAAI,MAA+C,CAAC;IACpD,IAAI,CAAC;QACH,MAAM,GAAG,4CAA4C,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACpE,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,0BAA0B,EAAE,CAAC;IACrC,CAAC;IACD,MAAM,SAAS,GAAG,GAAG,CAAC,UAAU,YAAY,IAAI,CAAC,CAAC,CAAC,G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CAAC;QAC1B,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,KAAK,CACjC;;;mDAG6C,EAC7C,CAAC,OAAO,CAAC,gBAAgB,EAAE,OAAO,CAAC,QAAQ,CAAC,cAAc,EAAE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CACtF,CAAC;QACF,IAAI,QAAQ,CAAC,QAAQ,KAAK,CAAC,EAAE,CAAC;YAC5B,MAAM,QAAQ,CAAC,4BAA4B,EAAE,uCAAuC,CAAC,CAAC;QACxF,CAAC;QACD,OAAO;IACT,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,KAAK,CAChC;;sFAEkF,EAClF;QACE,OAAO,CAAC,gBAAgB;QACxB,OAAO,CAAC,eAAe;QACvB,OAAO,CAAC,gBAAgB;QACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;KACzB,CACF,CAAC;IACF,IAAI,OAAO,CAAC,QAAQ,KAAK,CAAC,EAAE,CAAC;QAC3B,MAAM,QAAQ,CAAC,4BAA4B,EAAE,uCAAuC,CAAC,CAAC;IACxF,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,MAAkB,EAClB,KAMC;IAED,MAAM,MAAM,CAAC,KAAK,CAChB;;;6EAGyE,EACzE;QACE,KAAK,CAAC,WAAW;QACjB,KAAK,CAAC,MAAM,CAAC,gBAAgB;QAC7B,KAAK,CAAC,eAAe;QACrB,KAAK,CAAC,WAAW;QACjB,KAAK,CAAC,MAAM,CAAC,cAAc;QAC3B,KAAK,CAAC,MAAM,CAAC,2BAA2B;QACxC,KAAK,CAAC,MAAM,CAAC,UAAU;QACvB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC;QAC5B,KAAK,CAAC,SAAS;KAChB,CACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { PoolClient } from 'pg';
2
+ import { BillingSubscriptionReconciliationConflictError } from './subscription-reconciliation.js';
3
+ export declare function translateTransactionError(error: unknown): unknown;
4
+ export declare function conflict(code: ConstructorParameters<typeof BillingSubscriptionReconciliationConflictError>[0], message: string): BillingSubscriptionReconciliationConflictError;
5
+ export declare function keyGeneration(value: string): string;
6
+ export declare function timeout(name: string, value: number): number;
7
+ export declare function rollbackQuietly(client: PoolClient): Promise<void>;
8
+ //# sourceMappingURL=postgres-subscription-reconciliation-support.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres-subscription-reconciliation-support.d.ts","sourceRoot":"","sources":["../../src/billing/postgres-subscription-reconciliation-support.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AACrC,OAAO,EAAE,8CAA8C,EAAE,MAAM,kCAAkC,CAAC;AAElG,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAgBjE;AAQD,wBAAgB,QAAQ,CACtB,IAAI,EAAE,qBAAqB,CAAC,OAAO,8CAA8C,CAAC,CAAC,CAAC,CAAC,EACrF,OAAO,EAAE,MAAM,kDAGhB;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKnD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAK3D;AAED,wBAAsB,eAAe,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAMvE"}
@@ -0,0 +1,42 @@
1
+ import { BillingSubscriptionReconciliationConflictError } from './subscription-reconciliation.js';
2
+ export function translateTransactionError(error) {
3
+ if (error instanceof BillingSubscriptionReconciliationConflictError)
4
+ return error;
5
+ const code = postgresErrorCode(error);
6
+ if (code === '40001' || code === '40P01' || code === '55P03' || code === '57014') {
7
+ return conflict('serialization_conflict', 'billing subscription state changed concurrently; retry the exact request');
8
+ }
9
+ if (code === '23505' || code === '23503') {
10
+ return conflict('base_subscription_conflict', 'billing subscription history conflicts with the current account state');
11
+ }
12
+ return error;
13
+ }
14
+ function postgresErrorCode(error) {
15
+ return typeof error === 'object' && error !== null && 'code' in error
16
+ ? String(error.code)
17
+ : undefined;
18
+ }
19
+ export function conflict(code, message) {
20
+ return new BillingSubscriptionReconciliationConflictError(code, message);
21
+ }
22
+ export function keyGeneration(value) {
23
+ if (!/^[0-9a-f]{64}$/.test(value)) {
24
+ throw new Error('authoritative billing key generation must be lowercase SHA-256');
25
+ }
26
+ return value;
27
+ }
28
+ export function timeout(name, value) {
29
+ if (!Number.isSafeInteger(value) || value < 1 || value > 60_000) {
30
+ throw new Error(`${name} must be 1..60000ms`);
31
+ }
32
+ return value;
33
+ }
34
+ export async function rollbackQuietly(client) {
35
+ try {
36
+ await client.query('ROLLBACK');
37
+ }
38
+ catch {
39
+ // Preserve the original transaction failure.
40
+ }
41
+ }
42
+ //# sourceMappingURL=postgres-subscription-reconciliation-support.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres-subscription-reconciliation-support.js","sourceRoot":"","sources":["../../src/billing/postgres-subscription-reconciliation-support.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,8CAA8C,EAAE,MAAM,kCAAkC,CAAC;AAElG,MAAM,UAAU,yBAAyB,CAAC,KAAc;IACtD,IAAI,KAAK,YAAY,8CAA8C;QAAE,OAAO,KAAK,CAAC;IAClF,MAAM,IAAI,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACtC,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC;QACjF,OAAO,QAAQ,CACb,wBAAwB,EACxB,0EAA0E,CAC3E,CAAC;IACJ,CAAC;IACD,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC;QACzC,OAAO,QAAQ,CACb,4BAA4B,EAC5B,uEAAuE,CACxE,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,iBAAiB,CAAC,KAAc;IACvC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK;QACnE,CAAC,CAAC,MAAM,CAAE,KAAoC,CAAC,IAAI,CAAC;QACpD,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,QAAQ,CACtB,IAAqF,EACrF,OAAe;IAEf,OAAO,IAAI,8CAA8C,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC3E,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAa;IACzC,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;IACpF,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,IAAY,EAAE,KAAa;IACjD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,MAAM,EAAE,CAAC;QAChE,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,qBAAqB,CAAC,CAAC;IAChD,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,MAAkB;IACtD,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IACjC,CAAC;IAAC,MAAM,CAAC;QACP,6CAA6C;IAC/C,CAAC;AACH,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { Pool } from 'pg';
2
+ import { type BillingSubscriptionReconciliationRequest, type BillingSubscriptionReconciliationResult, type BillingSubscriptionReconciliationStore } from './subscription-reconciliation.js';
3
+ export interface PostgresBillingSubscriptionReconciliationStoreOptions {
4
+ readonly authoritativeKeyGeneration: string;
5
+ readonly lockTimeoutMs?: number;
6
+ readonly statementTimeoutMs?: number;
7
+ readonly id?: () => string;
8
+ }
9
+ /** Atomic provider-neutral paid history and entitlement-pointer reconciliation. */
10
+ export declare class PostgresBillingSubscriptionReconciliationStore implements BillingSubscriptionReconciliationStore {
11
+ #private;
12
+ constructor(pool: Pool, options: PostgresBillingSubscriptionReconciliationStoreOptions);
13
+ reconcilePaidSubscription(rawRequest: BillingSubscriptionReconciliationRequest): Promise<BillingSubscriptionReconciliationResult>;
14
+ }
15
+ //# sourceMappingURL=postgres-subscription-reconciliation.d.ts.map