@voltro/plugin-billing 0.85.0 → 0.87.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.
@@ -0,0 +1,125 @@
1
+ import { BillingError as e } from "./types.js";
2
+ import { Effect as t } from "effect";
3
+ import { and as n, eq as r, id as i, integer as a, table as o, text as s, timestamp as c } from "@voltro/database";
4
+ //#region src/closeStore.ts
5
+ var l = "_voltro_billing_close_operations", u = o(l, {
6
+ id: i({ prefix: "bclose" }),
7
+ tenantId: s().maxLength(191),
8
+ provider: s().maxLength(64),
9
+ scope: s().maxLength(64),
10
+ providerSubscriptionId: s().nullable(),
11
+ cancelAt: c().nullable(),
12
+ createdAt: c(),
13
+ usageSealed: a().default(0)
14
+ }).unique("byBillingCloseTenantScope", ["tenantId", "scope"]), d = () => new e({
15
+ source: "closeStore",
16
+ message: "Billing closure coordination requires reconciliation.",
17
+ transient: !0
18
+ }), f = (e) => {
19
+ let t = e instanceof Date ? new Date(e) : new Date(typeof e == "string" || typeof e == "number" ? e : NaN);
20
+ if (!Number.isFinite(t.getTime())) throw d();
21
+ return t;
22
+ }, p = (e) => {
23
+ for (let t of [
24
+ "id",
25
+ "tenantId",
26
+ "provider",
27
+ "scope"
28
+ ]) if (typeof e[t] != "string" || e[t] === "") throw d();
29
+ if (String(e.tenantId).length > 191 || String(e.provider).length > 64 || e.scope !== "account" && !/^[a-f0-9]{64}$/.test(String(e.scope))) throw d();
30
+ let t = e.scope === "account";
31
+ if (e.usageSealed !== 0 && e.usageSealed !== 1 || (t ? e.providerSubscriptionId !== null || e.cancelAt !== null : typeof e.providerSubscriptionId != "string" || e.providerSubscriptionId === "" || e.cancelAt === null)) throw d();
32
+ return {
33
+ id: String(e.id),
34
+ tenantId: String(e.tenantId),
35
+ provider: String(e.provider),
36
+ scope: String(e.scope),
37
+ providerSubscriptionId: t ? null : String(e.providerSubscriptionId),
38
+ cancelAt: t ? null : f(e.cancelAt),
39
+ createdAt: f(e.createdAt),
40
+ usageSealed: Number(e.usageSealed)
41
+ };
42
+ }, m = async (e) => {
43
+ if (e.length === 0) throw d();
44
+ let t = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(e));
45
+ return [...new Uint8Array(t)].map((e) => e.toString(16).padStart(2, "0")).join("");
46
+ }, h = (e, n, r) => {
47
+ let i = async (t) => {
48
+ let r = p({
49
+ ...t,
50
+ id: "bclose_" + globalThis.crypto.randomUUID()
51
+ });
52
+ for (let i = 0; i < 8; i++) {
53
+ let i = await e(t.tenantId, t.scope);
54
+ if (i !== null) {
55
+ if (i.provider !== t.provider || i.providerSubscriptionId !== t.providerSubscriptionId) throw d();
56
+ return i;
57
+ }
58
+ await n(r);
59
+ }
60
+ throw d();
61
+ }, a = (e) => t.tryPromise({
62
+ try: e,
63
+ catch: d
64
+ });
65
+ return {
66
+ getAccount: (t) => a(() => e(t, "account")),
67
+ sealUsage: (t) => a(async () => {
68
+ let n = await e(t, "account");
69
+ if (n === null) throw d();
70
+ if (n.usageSealed === 1) return n;
71
+ await r(n);
72
+ let i = await e(t, "account");
73
+ if (i?.usageSealed !== 1) throw d();
74
+ return i;
75
+ }),
76
+ reserveAccount: (e, t, n) => a(() => i({
77
+ tenantId: e,
78
+ provider: t,
79
+ scope: "account",
80
+ providerSubscriptionId: null,
81
+ cancelAt: null,
82
+ createdAt: n,
83
+ usageSealed: 0
84
+ })),
85
+ getSubscription: (t, n) => a(async () => e(t, await m(n))),
86
+ reserveSubscription: (e) => a(async () => i({
87
+ ...e,
88
+ scope: await m(e.providerSubscriptionId),
89
+ usageSealed: 0
90
+ }))
91
+ };
92
+ }, g = () => {
93
+ let e = /* @__PURE__ */ new Map(), t = (e, t) => JSON.stringify([e, t]);
94
+ return h(async (n, r) => {
95
+ let i = e.get(t(n, r));
96
+ return i ? p({ ...i }) : null;
97
+ }, async (n) => {
98
+ let r = t(n.tenantId, n.scope);
99
+ e.has(r) || e.set(r, p({ ...n }));
100
+ }, async (n) => {
101
+ let r = t(n.tenantId, "account"), i = e.get(r);
102
+ if (!i || i.id !== n.id) throw d();
103
+ e.set(r, p({
104
+ ...i,
105
+ usageSealed: 1
106
+ }));
107
+ });
108
+ }, _ = (e) => h(async (t, i) => {
109
+ let a = {
110
+ table: l,
111
+ predicate: n(r("tenantId", t), r("scope", i)),
112
+ order: [],
113
+ take: 2,
114
+ skip: void 0,
115
+ projection: void 0
116
+ }, o = await e.query(a);
117
+ if (o.length > 1) throw d();
118
+ return o[0] ? p(o[0]) : null;
119
+ }, async (t) => {
120
+ await e.insertIgnore(l, { ...t }, { conflictColumns: ["tenantId", "scope"] });
121
+ }, async (t) => {
122
+ await e.updateMany(l, { usageSealed: 1 }, { where: n(r("id", t.id), r("tenantId", t.tenantId), r("scope", "account"), r("usageSealed", 0)) });
123
+ });
124
+ //#endregion
125
+ export { l as BILLING_CLOSE_OPERATIONS_TABLE, u as billingCloseOperationsTable, _ as dataStoreBillingCloseStore, g as memoryBillingCloseStore };
@@ -0,0 +1,155 @@
1
+ import { BillingError as e } from "./types.js";
2
+ import { exactProviderIdentifier as t } from "./providerIdentity.js";
3
+ import { Effect as n } from "effect";
4
+ import { canonicalJson as r } from "@voltro/protocol";
5
+ import { and as i, eq as a, gt as o } from "@voltro/database";
6
+ import { createHash as s } from "node:crypto";
7
+ //#region src/custodyInventory.ts
8
+ var c = Object.freeze([
9
+ "_voltro_billing_customer_accounts",
10
+ "_voltro_billing_customer_custody",
11
+ "_voltro_billing_account_invoices",
12
+ "_voltro_billing_invoice_history_walks",
13
+ "_voltro_billing_invoice_history_pages",
14
+ "_voltro_billing_invoice_history_references",
15
+ "_voltro_billing_invoice_history_work",
16
+ "_voltro_billing_invoice_history_dispatch",
17
+ "_voltro_billing_customers",
18
+ "_voltro_billing_subscriptions",
19
+ "_voltro_billing_invoices",
20
+ "_voltro_billing_invoice_observations",
21
+ "_voltro_billing_usage",
22
+ "_voltro_billing_usage_events",
23
+ "_voltro_billing_checkout_operations",
24
+ "_voltro_billing_close_operations",
25
+ "_voltro_billing_dunning_notices"
26
+ ]), l = (r, i) => n.tryPromise({
27
+ try: async () => {
28
+ let e = i.tenantId, n = i.pageSize ?? 200, a = i.maxRowsPerTable ?? 5e3;
29
+ if (!t(e) || e.length > 191 || !e.isWellFormed() || !Number.isSafeInteger(n) || n < 1 || n > 500 || !Number.isSafeInteger(a) || a < 1 || a > 5e4) throw v("Inventory tenant and bounds must be valid.", !1);
30
+ if (r.integrityQuery === void 0) throw v("Complete native integrity reads are required.", !1);
31
+ let o = {
32
+ format: "voltro.billing-custody-inventory.v1",
33
+ tenantId: e,
34
+ tables: await r.transactional(async (t) => {
35
+ if (t.integrityQuery === void 0) throw v("Transactional native integrity reads are required.", !1);
36
+ let r = await u(t, e, n, a), i = await u(t, e, n, a);
37
+ if (g(r) !== g(i)) throw v("Billing state changed during inventory. Quiesce writers and retry.", !0);
38
+ return i;
39
+ })
40
+ };
41
+ return Object.freeze({
42
+ ...o,
43
+ digest: g(o),
44
+ observedAt: /* @__PURE__ */ new Date(),
45
+ purpose: "review-only"
46
+ });
47
+ },
48
+ catch: (t) => t instanceof e ? t : v("The native billing inventory could not be completed.", !0)
49
+ }), u = async (e, n, s, l) => {
50
+ let u = e.integrityQuery;
51
+ if (u === void 0) throw v("Complete native integrity reads are required.", !1);
52
+ let y = [], b = 0;
53
+ for (let x of c) {
54
+ let c = [], S = /* @__PURE__ */ new Set(), C;
55
+ for (;;) {
56
+ let g = Math.min(s, l - c.length + 1), y = {
57
+ table: x,
58
+ predicate: C === void 0 ? a("tenantId", n) : i(a("tenantId", n), o("id", C)),
59
+ crossTenant: !0,
60
+ includeDeleted: !0,
61
+ includeExpired: !0,
62
+ order: [{
63
+ column: "id",
64
+ direction: "asc"
65
+ }],
66
+ take: g,
67
+ skip: void 0,
68
+ projection: void 0
69
+ }, w = await u.call(e, y);
70
+ if (!Array.isArray(w) || w.length > g) throw v(`Invalid inventory page for ${x}.`, !1);
71
+ for (let e of w) {
72
+ let i = h(e);
73
+ if (!m(i)) throw v(`Invalid inventory row for ${x}.`, !1);
74
+ let a = i.id;
75
+ if (!t(a) || i.tenantId !== n || S.has(a)) throw v(`Invalid inventory identity for ${x}.`, !1);
76
+ if (c.length === l) throw v(`Inventory bound exceeded for ${x}; no partial inventory is returned.`, !1);
77
+ S.add(a);
78
+ let o = {};
79
+ for (let e of d) {
80
+ let n = i[e];
81
+ if (n != null) {
82
+ if (!t(n)) throw v(`Invalid inventory reference for ${x}.`, !1);
83
+ o[e] = n;
84
+ }
85
+ }
86
+ let s = r(i), u = Buffer.byteLength(s, "utf8");
87
+ if (b += u, u > f || b > p) throw v("Native billing inventory byte bound exceeded; no partial inventory is returned.", !1);
88
+ c.push(Object.freeze({
89
+ id: a,
90
+ digest: _(s),
91
+ references: Object.freeze(o)
92
+ })), C = a;
93
+ }
94
+ if (w.length < g) break;
95
+ }
96
+ c.sort((e, t) => e.id < t.id ? -1 : +(e.id > t.id)), y.push(Object.freeze({
97
+ tableName: x,
98
+ rowCount: c.length,
99
+ digest: g(c),
100
+ rows: Object.freeze(c)
101
+ }));
102
+ }
103
+ return Object.freeze(y);
104
+ }, d = [
105
+ "provider",
106
+ "providerAccountId",
107
+ "providerCustomerId",
108
+ "providerSubscriptionId",
109
+ "providerInvoiceId"
110
+ ], f = 4194304, p = 67108864, m = (e) => typeof e == "object" && !!e && !Array.isArray(e), h = (e, t = /* @__PURE__ */ new Set(), n = 0) => {
111
+ if (n > 64) throw v("Native inventory value exceeds supported depth.", !1);
112
+ if (e === null || typeof e == "boolean") return e;
113
+ if (typeof e == "string") {
114
+ if (!e.isWellFormed()) throw v("Native inventory contains malformed text.", !1);
115
+ return e;
116
+ }
117
+ if (typeof e == "number") {
118
+ if (!Number.isFinite(e) || !Number.isSafeInteger(e) && Math.abs(e) > 2 ** 53 - 1 || Object.is(e, -0)) throw v("Native inventory numeric evidence cannot be encoded exactly.", !1);
119
+ return e;
120
+ }
121
+ if (e instanceof Date) {
122
+ if (!Number.isFinite(e.getTime())) throw v("Native inventory contains an invalid date.", !1);
123
+ return e.toISOString();
124
+ }
125
+ if (typeof e != "object" || t.has(e)) throw v("Native inventory contains a non-JSON value.", !1);
126
+ let r = Object.getPrototypeOf(e);
127
+ if (!Array.isArray(e) && r !== Object.prototype && r !== null) throw v("Native inventory value has an unsupported prototype.", !1);
128
+ if (Object.getOwnPropertySymbols(e).length !== 0) throw v("Native inventory contains unsupported symbol fields.", !1);
129
+ t.add(e);
130
+ try {
131
+ if (Array.isArray(e)) {
132
+ if (Object.keys(e).length !== e.length || Object.getOwnPropertyNames(e).length !== e.length + 1) throw v("Native inventory contains a sparse or extended array.", !1);
133
+ return Array.from({ length: e.length }, (r, i) => {
134
+ let a = Object.getOwnPropertyDescriptor(e, String(i));
135
+ if (a === void 0 || !("value" in a)) throw v("Native inventory contains a sparse or accessor array.", !1);
136
+ return h(a.value, t, n + 1);
137
+ });
138
+ }
139
+ if (Object.getOwnPropertyNames(e).length !== Object.keys(e).length) throw v("Native inventory contains non-enumerable fields.", !1);
140
+ return Object.fromEntries(Object.keys(e).map((r) => {
141
+ if (!r.isWellFormed()) throw v("Native inventory contains a malformed field.", !1);
142
+ let i = Object.getOwnPropertyDescriptor(e, r);
143
+ if (i === void 0 || !("value" in i)) throw v("Native inventory contains an accessor field.", !1);
144
+ return [r, h(i.value, t, n + 1)];
145
+ }));
146
+ } finally {
147
+ t.delete(e);
148
+ }
149
+ }, g = (e) => _(r(e)), _ = (e) => s("sha256").update(e, "utf8").digest("hex"), v = (t, n) => new e({
150
+ source: "billingCustodyInventory",
151
+ message: t,
152
+ transient: n
153
+ });
154
+ //#endregion
155
+ export { c as BILLING_CUSTODY_INVENTORY_TABLES, l as inspectBillingCustody };
@@ -0,0 +1,259 @@
1
+ import { BillingError as e } from "./types.js";
2
+ import { captureProviderAccount as t, captureProviderCustomer as n, exactProviderIdentifier as r, sameProviderAccount as i } from "./providerIdentity.js";
3
+ import { BILLING_CUSTODY_INVENTORY_TABLES as a } from "./custodyInventory.js";
4
+ import { Effect as o } from "effect";
5
+ import { and as s, eq as c, generateId as l, gt as u, id as d, integer as f, table as p, text as m, timestamp as h } from "@voltro/database";
6
+ //#region src/customerStore.ts
7
+ var g = "_voltro_billing_customer_accounts", _ = "_voltro_billing_customer_custody", v = p(g, {
8
+ id: d({ prefix: "bcacct" }),
9
+ tenantId: m().maxLength(191),
10
+ provider: m().maxLength(64),
11
+ providerAccountId: m().maxLength(191),
12
+ livemode: f()
13
+ }).unique("byBillingCustomerAccountTenant", ["tenantId"]), y = p(_, {
14
+ id: d({ prefix: "bcustody" }),
15
+ tenantId: m().maxLength(191),
16
+ provider: m().maxLength(64),
17
+ providerAccountId: m().maxLength(191),
18
+ livemode: f(),
19
+ providerCustomerId: m().maxLength(191),
20
+ identityKey: m().maxLength(64),
21
+ slot: m().maxLength(191),
22
+ observedAt: h(),
23
+ retiredAt: h().nullable()
24
+ }).unique("byBillingCustomerCustodyIdentity", ["identityKey"]).unique("byBillingCustomerCustodySlot", ["tenantId", "slot"]).index("byBillingCustomerCustodyInventory", [
25
+ "provider",
26
+ "providerAccountId",
27
+ "livemode",
28
+ "id"
29
+ ]), b = () => new e({
30
+ source: "customerStore",
31
+ message: "Billing customer custody or retained unqualified history requires reconciliation.",
32
+ transient: !0
33
+ }), x = (e, t) => {
34
+ let n = l({
35
+ kind: "typeid",
36
+ prefix: t
37
+ }, e);
38
+ if (typeof n != "string" || n.length === 0) throw b();
39
+ return n;
40
+ }, S = (e) => {
41
+ if (!r(e.provider) || e.provider.length > 64) throw b();
42
+ let n = t(e.account);
43
+ if (n.providerAccountId.length > 191) throw b();
44
+ return Object.freeze({
45
+ provider: e.provider,
46
+ account: n
47
+ });
48
+ }, C = (e) => {
49
+ if (!r(e) || e.length > 191) throw b();
50
+ return e;
51
+ }, w = (e, t) => e.provider === t.provider && i(e.account, t.account), T = (e, t) => {
52
+ if (!w(e, t)) throw b();
53
+ }, E = (e) => {
54
+ let t = S(e);
55
+ if (!r(e.providerCustomerId) || e.providerCustomerId.length > 191) throw b();
56
+ return Object.freeze({
57
+ ...t,
58
+ tenantId: C(e.tenantId),
59
+ providerCustomerId: e.providerCustomerId
60
+ });
61
+ }, D = async (e) => {
62
+ let t = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify([
63
+ "voltro.customer-custody.v1",
64
+ e.provider,
65
+ e.account.providerAccountId,
66
+ e.account.livemode,
67
+ e.providerCustomerId
68
+ ])));
69
+ return [...new Uint8Array(t)].map((e) => e.toString(16).padStart(2, "0")).join("");
70
+ }, O = (e, t, n) => ({
71
+ table: e,
72
+ predicate: t,
73
+ order: [],
74
+ take: n,
75
+ skip: void 0,
76
+ projection: void 0
77
+ }), k = (t) => o.tryPromise({
78
+ try: t,
79
+ catch: (t) => t instanceof e ? t : b()
80
+ }), A = (e) => {
81
+ if (e.livemode !== 0 && e.livemode !== 1 || !r(e.provider) || !r(e.providerAccountId)) throw b();
82
+ return S({
83
+ provider: e.provider,
84
+ account: {
85
+ providerAccountId: e.providerAccountId,
86
+ livemode: e.livemode === 1
87
+ }
88
+ });
89
+ }, j = (e) => {
90
+ let t = e instanceof Date || typeof e == "string" || typeof e == "number" ? new Date(e) : /* @__PURE__ */ new Date(NaN);
91
+ if (!Number.isFinite(t.getTime())) throw b();
92
+ return t;
93
+ }, M = (e) => {
94
+ let t = A(e), n = e.id, i = e.tenantId, a = e.providerCustomerId;
95
+ if (!r(n) || !r(i) || !r(a) || typeof e.identityKey != "string" || !/^[0-9a-f]{64}$/.test(e.identityKey)) throw b();
96
+ let o = e.retiredAt === null ? null : j(e.retiredAt);
97
+ if (j(e.observedAt), e.slot !== (o === null ? "current" : n)) throw b();
98
+ return {
99
+ ...E({
100
+ ...t,
101
+ tenantId: i,
102
+ providerCustomerId: a
103
+ }),
104
+ id: n,
105
+ retiredAt: o
106
+ };
107
+ }, N = (e, t) => {
108
+ if (T(e, t), e.tenantId !== t.tenantId || e.providerCustomerId !== t.providerCustomerId) throw b();
109
+ }, P = (e, t, n) => ({
110
+ id: x(_, "bcustody"),
111
+ tenantId: e.tenantId,
112
+ provider: e.provider,
113
+ providerAccountId: e.account.providerAccountId,
114
+ livemode: +!!e.account.livemode,
115
+ providerCustomerId: e.providerCustomerId,
116
+ identityKey: t,
117
+ slot: "current",
118
+ observedAt: n.observedAt,
119
+ retiredAt: null
120
+ }), F = a, I = (e) => {
121
+ let t = async (t) => {
122
+ let n = await e.query(O(g, c("tenantId", C(t)), 2));
123
+ if (n.length > 1 || n[0] && (n[0].tenantId !== t || !r(n[0].id))) throw b();
124
+ return n[0] ? A(n[0]) : null;
125
+ }, a = async (t) => {
126
+ for (let n of F) if ((await e.query(O(n, c("tenantId", t), 1))).length) throw b();
127
+ }, o = async (n, r) => {
128
+ let i = await t(n);
129
+ if (i !== null) {
130
+ T(i, r);
131
+ return;
132
+ }
133
+ await a(n), await e.insertIgnore(g, {
134
+ id: x(g, "bcacct"),
135
+ tenantId: n,
136
+ provider: r.provider,
137
+ providerAccountId: r.account.providerAccountId,
138
+ livemode: +!!r.account.livemode
139
+ }, { conflictColumns: ["tenantId"] });
140
+ let o = await t(n);
141
+ if (o === null) throw b();
142
+ T(o, r);
143
+ }, l = async (e) => {
144
+ if (e.length > 1) throw b();
145
+ if (!e[0]) return null;
146
+ let n = M(e[0]);
147
+ if (await D(n) !== e[0].identityKey) throw b();
148
+ let r = await t(n.tenantId);
149
+ if (r === null) throw b();
150
+ return T(r, n), n;
151
+ }, d = async (t) => {
152
+ let n = await D(t), r = await l(await e.query(O(_, c("identityKey", n), 2)));
153
+ if (r !== null && await D(r) !== n || r === null && (await e.query(O("_voltro_billing_customers", s(c("provider", t.provider), c("providerCustomerId", t.providerCustomerId)), 1))).length) throw b();
154
+ return r;
155
+ };
156
+ return {
157
+ accountForTenant: (e) => k(() => t(e)),
158
+ assertAccount: (e, n) => k(async () => {
159
+ let r = S(n), i = C(e), o = await t(i);
160
+ return o === null ? (await a(i), !1) : (T(o, r), !0);
161
+ }),
162
+ reserveAccount: (e, t) => k(async () => {
163
+ let n = S(t);
164
+ await o(C(e), n);
165
+ }),
166
+ bind: (t, r) => k(async () => {
167
+ let a = E(t), s = n(r);
168
+ if (s.deleted || s.providerCustomerId !== a.providerCustomerId || !i(s.account, a.account)) throw b();
169
+ let c = await d(a);
170
+ if (c !== null) {
171
+ if (N(c, a), c.retiredAt !== null) throw b();
172
+ return c;
173
+ }
174
+ if (await f(a.tenantId) !== null) throw b();
175
+ await o(a.tenantId, a);
176
+ let l = P(a, await D(a), s);
177
+ await e.insertIgnore(_, l, { conflictColumns: ["identityKey"] });
178
+ let u = await d(a);
179
+ if (u === null || u.retiredAt !== null) throw b();
180
+ N(u, a);
181
+ let p = await f(a.tenantId);
182
+ if (p === null || p.id !== u.id) throw b();
183
+ return u;
184
+ }),
185
+ getByTenant: (e) => k(() => f(e)),
186
+ getByProviderCustomerId: (e, t) => k(async () => {
187
+ let n = S(t);
188
+ if (!r(e)) throw b();
189
+ return d({
190
+ ...n,
191
+ tenantId: "lookup",
192
+ providerCustomerId: e
193
+ });
194
+ }),
195
+ inventory: (t, n) => k(async () => {
196
+ let i = S(t), a = n?.limit ?? 25, o = n?.afterCustodyId ?? null;
197
+ if (n?.afterCustodyId !== void 0 && typeof n.afterCustodyId != "string" || !Number.isInteger(a) || a < 1 || a > 100 || o !== null && (!r(o) || o.length > 191 || !o.isWellFormed()) || (await e.query(O("_voltro_billing_customers", void 0, 1))).length) throw b();
198
+ let d = s(c("provider", i.provider), c("providerAccountId", i.account.providerAccountId), c("livemode", +!!i.account.livemode));
199
+ if (o !== null) {
200
+ let t = await l(await e.query(O(_, s(d, c("id", o)), 2)));
201
+ if (t === null || t.id !== o) throw b();
202
+ T(t, i);
203
+ }
204
+ let f = await e.query({
205
+ ...O(_, o === null ? d : s(d, u("id", o)), a + 1),
206
+ order: [{
207
+ column: "id",
208
+ direction: "asc"
209
+ }]
210
+ });
211
+ if (f.length > a + 1) throw b();
212
+ let p = [], m = o;
213
+ for (let e of f) {
214
+ let t = await l([e]);
215
+ if (t === null || m !== null && t.id <= m) throw b();
216
+ T(t, i), p.push(t), m = t.id;
217
+ }
218
+ return {
219
+ customers: p.slice(0, a),
220
+ nextCustodyId: p.length > a ? p[a - 1].id : null
221
+ };
222
+ }),
223
+ list: (t) => k(async () => {
224
+ let n = S(t);
225
+ if ((await e.query(O("_voltro_billing_customers", void 0, 1))).length) throw b();
226
+ let r = await e.query(O(_, s(c("provider", n.provider), c("providerAccountId", n.account.providerAccountId), c("livemode", +!!n.account.livemode), c("slot", "current")))), i = [];
227
+ for (let e of r) {
228
+ let t = await l([e]);
229
+ t && i.push(t);
230
+ }
231
+ return i;
232
+ }),
233
+ retire: (t, r) => k(async () => {
234
+ let a = S(t), o = n(r);
235
+ if (!o.deleted || !i(a.account, o.account)) throw b();
236
+ let l = {
237
+ ...a,
238
+ tenantId: "lookup",
239
+ providerCustomerId: o.providerCustomerId
240
+ }, u = await d(l);
241
+ if (u === null || u.retiredAt !== null) return u;
242
+ let f = await D(u);
243
+ await e.updateMany(_, {
244
+ slot: u.id,
245
+ retiredAt: o.observedAt
246
+ }, { where: s(c("id", u.id), c("identityKey", f), c("slot", "current"), c("tenantId", u.tenantId), c("provider", a.provider), c("providerAccountId", a.account.providerAccountId), c("livemode", +!!a.account.livemode), c("providerCustomerId", o.providerCustomerId)) });
247
+ let p = await d(l);
248
+ if (p === null || p.id !== u.id || p.retiredAt === null) throw b();
249
+ return p;
250
+ })
251
+ };
252
+ async function f(n) {
253
+ let r = C(n), i = await l(await e.query(O(_, s(c("tenantId", r), c("slot", "current")), 2)));
254
+ if (i !== null && (i.tenantId !== r || i.retiredAt !== null)) throw b();
255
+ return i === null && await t(r) === null && await a(r), i;
256
+ }
257
+ };
258
+ //#endregion
259
+ export { g as BILLING_CUSTOMER_ACCOUNTS_TABLE, _ as BILLING_CUSTOMER_CUSTODY_TABLE, v as billingCustomerAccountsTable, y as billingCustomerCustodyTable, I as dataStoreCustomerStore };