@voltro/plugin-billing 0.89.2 → 0.91.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/CHANGELOG.md +1943 -0
- package/README.md +0 -435
- package/THIRD-PARTY-NOTICES.md +22 -646
- package/dist/catalog.js +169 -70
- package/dist/custodyInventory.js +101 -65
- package/dist/dunning.js +14 -6
- package/dist/eventDelivery.js +14 -5
- package/dist/historicalUsage.js +94 -0
- package/dist/index.d.ts +777 -68
- package/dist/index.js +354 -233
- package/dist/invoiceHistoryJournal.js +204 -179
- package/dist/invoiceHistoryProgress.js +130 -106
- package/dist/invoiceHistoryWork.js +188 -186
- package/dist/invoicePaymentEpoch.js +145 -0
- package/dist/invoicePaymentObservation.js +64 -0
- package/dist/invoiceSnapshot.js +56 -46
- package/dist/invoiceStore.js +124 -90
- package/dist/paidInvoiceCoverage.js +84 -0
- package/dist/plans.js +15 -2
- package/dist/providers.js +149 -92
- package/dist/provision.js +84 -73
- package/dist/rpc.d.ts +53 -13
- package/dist/rpc.js +141 -78
- package/dist/service.js +1208 -722
- package/dist/stores.js +240 -148
- package/dist/stripeIdentity.js +17 -17
- package/dist/stripeInvoiceHistory.js +21 -21
- package/dist/stripeInvoicePayments.js +129 -0
- package/dist/stripeInvoiceSnapshot.js +65 -46
- package/dist/stripeProvider.js +570 -201
- package/dist/stripeRest.js +10 -10
- package/dist/subscriptionObservation.js +93 -0
- package/dist/subscriptionWriteStore.js +325 -0
- package/dist/table.js +75 -66
- package/dist/types.d.ts +415 -27
- package/dist/types.js +21 -4
- package/dist/web/index.d.ts +93 -3
- package/dist/web/index.js +76 -33
- package/dist/web/writeRequest.js +65 -0
- package/package.json +9 -9
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { dataStoreCustomerStore as e } from "./customerStore.js";
|
|
2
|
+
import { captureInvoiceHistoryInput as t, captureInvoiceHistoryPage as n, historyError as r } from "./invoiceHistory.js";
|
|
3
|
+
import { captureProviderAccount as i, exactProviderIdentifier as a, sameProviderAccount as o } from "./providerIdentity.js";
|
|
4
|
+
import { BillingError as s } from "./types.js";
|
|
5
5
|
import { Effect as c } from "effect";
|
|
6
6
|
import { and as l, count as u, eq as d, gt as f, id as p, inSet as m, integer as h, lt as g, lte as _, neq as v, or as y, sum as b, table as x, text as S, timestamp as C } from "@voltro/database";
|
|
7
7
|
// src/invoiceHistoryJournal.ts
|
|
@@ -20,7 +20,8 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
20
20
|
round: h(),
|
|
21
21
|
pageOrdinal: h(),
|
|
22
22
|
cursorAfter: S().maxLength(191).nullable(),
|
|
23
|
-
exhausted: h()
|
|
23
|
+
exhausted: h(),
|
|
24
|
+
headCheckedAt: C().nullable()
|
|
24
25
|
}).unique("byBillingInvoiceHistoryOwner", ["identityKey"]).index("byBillingInvoiceHistoryTenant", ["tenantId"]), k = x(T, {
|
|
25
26
|
id: p({ prefix: "bihpage" }),
|
|
26
27
|
...D(),
|
|
@@ -49,8 +50,8 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
49
50
|
"walkId",
|
|
50
51
|
"pageOrdinal",
|
|
51
52
|
"position"
|
|
52
|
-
]).index("byBillingInvoiceHistoryReferenceTenant", ["tenantId"]), j = 2147483647, M = (e, t = 191) => {
|
|
53
|
-
if (!
|
|
53
|
+
]).index("byBillingInvoiceHistoryReferenceInvoice", ["walkId", "providerInvoiceId"]).index("byBillingInvoiceHistoryReferenceTenant", ["tenantId"]), j = 2147483647, M = (e, t = 191) => {
|
|
54
|
+
if (!a(e) || e.length > t || !e.isWellFormed()) throw F();
|
|
54
55
|
return e;
|
|
55
56
|
}, N = (e, t = 0) => {
|
|
56
57
|
if (typeof e != "number" || !Number.isSafeInteger(e) || e < t || e > j) throw F();
|
|
@@ -59,18 +60,18 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
59
60
|
let t = e instanceof Date ? new Date(e) : new Date(typeof e == "string" ? e : NaN);
|
|
60
61
|
if (!Number.isFinite(t.getTime())) throw F();
|
|
61
62
|
return t;
|
|
62
|
-
}, F = (e = "The durable invoice history journal requires reconciliation.", t = !1) =>
|
|
63
|
-
try:
|
|
64
|
-
catch: (
|
|
63
|
+
}, F = (e = "The durable invoice history journal requires reconciliation.", t = !1) => r(e, t), I = (e) => c.tryPromise({
|
|
64
|
+
try: e,
|
|
65
|
+
catch: (e) => e instanceof s ? e : F("The native invoice history transaction could not be confirmed.", !0)
|
|
65
66
|
}), L = async (e) => {
|
|
66
67
|
let t = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(e)));
|
|
67
68
|
return [...new Uint8Array(t)].map((e) => e.toString(16).padStart(2, "0")).join("");
|
|
68
69
|
}, R = (e) => {
|
|
69
|
-
let
|
|
70
|
-
return M(
|
|
70
|
+
let t = i(e.account);
|
|
71
|
+
return M(t.providerAccountId), Object.freeze({
|
|
71
72
|
tenantId: M(e.tenantId),
|
|
72
73
|
provider: M(e.provider, 64),
|
|
73
|
-
account: Object.freeze(
|
|
74
|
+
account: Object.freeze(t),
|
|
74
75
|
providerCustomerId: M(e.providerCustomerId),
|
|
75
76
|
custodyId: M(e.custodyId ?? e.id)
|
|
76
77
|
});
|
|
@@ -94,27 +95,28 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
94
95
|
custodyId: M(e.custodyId)
|
|
95
96
|
});
|
|
96
97
|
}, V = (e, t) => {
|
|
97
|
-
if (e.tenantId !== t.tenantId || e.provider !== t.provider || e.custodyId !== t.custodyId || e.providerCustomerId !== t.providerCustomerId || !
|
|
98
|
+
if (e.tenantId !== t.tenantId || e.provider !== t.provider || e.custodyId !== t.custodyId || e.providerCustomerId !== t.providerCustomerId || !o(e.account, t.account)) throw F();
|
|
98
99
|
}, H = (e) => {
|
|
99
|
-
let
|
|
100
|
+
let n = R(e);
|
|
100
101
|
if (typeof e.exhausted != "boolean" || e.exhausted && e.cursor !== null) throw F();
|
|
101
|
-
let
|
|
102
|
-
account:
|
|
103
|
-
providerCustomerId:
|
|
102
|
+
let r = e.headCheckedAt === null ? null : P(e.headCheckedAt), i = e.cursor === null ? null : t({
|
|
103
|
+
account: n.account,
|
|
104
|
+
providerCustomerId: n.providerCustomerId,
|
|
104
105
|
cursor: e.cursor
|
|
105
106
|
}).cursor;
|
|
106
|
-
if (
|
|
107
|
+
if (i === void 0 || e.revision !== e.pageOrdinal + e.round || e.pageOrdinal === 0 && (e.cursor !== null || e.exhausted)) throw F();
|
|
107
108
|
return Object.freeze({
|
|
108
|
-
...
|
|
109
|
+
...n,
|
|
109
110
|
id: M(e.id),
|
|
110
111
|
revision: N(e.revision, 1),
|
|
111
112
|
round: N(e.round, 1),
|
|
112
113
|
pageOrdinal: N(e.pageOrdinal),
|
|
113
|
-
cursor:
|
|
114
|
-
exhausted: e.exhausted
|
|
114
|
+
cursor: i,
|
|
115
|
+
exhausted: e.exhausted,
|
|
116
|
+
headCheckedAt: r
|
|
115
117
|
});
|
|
116
118
|
}, U = (e) => {
|
|
117
|
-
let t = B(e), n = e.cursorAfter;
|
|
119
|
+
let t = B(e), n = e.cursorAfter, r = e.headCheckedAt;
|
|
118
120
|
if (e.exhausted !== 0 && e.exhausted !== 1 || n !== null && typeof n != "string") throw F();
|
|
119
121
|
return H({
|
|
120
122
|
...t,
|
|
@@ -123,6 +125,7 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
123
125
|
round: N(e.round, 1),
|
|
124
126
|
pageOrdinal: N(e.pageOrdinal),
|
|
125
127
|
exhausted: e.exhausted === 1,
|
|
128
|
+
headCheckedAt: r == null ? null : P(r),
|
|
126
129
|
cursor: n === null ? null : {
|
|
127
130
|
...t.account,
|
|
128
131
|
providerCustomerId: t.providerCustomerId,
|
|
@@ -143,15 +146,15 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
143
146
|
skip: void 0,
|
|
144
147
|
projection: void 0,
|
|
145
148
|
...r ? { lock: "update" } : {}
|
|
146
|
-
}), K = (e, t) => e.id === t.id && e.revision === t.revision && e.round === t.round && e.pageOrdinal === t.pageOrdinal && e.exhausted === t.exhausted && e.cursor?.afterInvoiceId === t.cursor?.afterInvoiceId, q = (
|
|
147
|
-
let
|
|
148
|
-
let
|
|
149
|
-
if (
|
|
150
|
-
V(R(
|
|
151
|
-
},
|
|
152
|
-
for (let
|
|
153
|
-
let
|
|
154
|
-
if ((await e.query(G(
|
|
149
|
+
}), K = (e, t) => e.id === t.id && e.revision === t.revision && e.round === t.round && e.pageOrdinal === t.pageOrdinal && e.exhausted === t.exhausted && e.cursor?.afterInvoiceId === t.cursor?.afterInvoiceId && (e.headCheckedAt?.getTime() ?? null) === (t.headCheckedAt?.getTime() ?? null), q = (r) => {
|
|
150
|
+
let i = async (t, n) => {
|
|
151
|
+
let r = await c.runPromise(e(t).getByProviderCustomerId(n.providerCustomerId, n));
|
|
152
|
+
if (r === null) throw F("Positive retained customer custody is required before invoice history admission.");
|
|
153
|
+
V(R(r), n);
|
|
154
|
+
}, a = (e) => l(d("provider", e.provider), d("providerAccountId", e.account.providerAccountId), d("livemode", +!!e.account.livemode), d("providerCustomerId", e.providerCustomerId)), o = async (e, t) => {
|
|
155
|
+
for (let n of [T, E]) {
|
|
156
|
+
let r = y(v("walkId", t.id), v("custodyId", t.custodyId), v("tenantId", t.tenantId), f("pageOrdinal", t.pageOrdinal), f("round", t.round), g("pageOrdinal", 1), g("round", 1)), i = y(v("provider", t.provider), v("providerAccountId", t.account.providerAccountId), v("livemode", +!!t.account.livemode), v("providerCustomerId", t.providerCustomerId)), o = y(l(a(t), r), l(d("walkId", t.id), y(r, i)));
|
|
157
|
+
if ((await e.query(G(n, o, 1))).length) throw F("Invoice history receipts do not belong to the retained walk checkpoint; explicit reconciliation is required.", !0);
|
|
155
158
|
}
|
|
156
159
|
}, s = (e) => {
|
|
157
160
|
if (typeof e == "bigint") {
|
|
@@ -181,13 +184,13 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
181
184
|
let r = N(n[0].round, 1), i = n[0].nextCursorAfter;
|
|
182
185
|
if (i !== null && typeof i != "string" || r > t.round || r === t.round && (i !== (t.cursor?.afterInvoiceId ?? null) || t.exhausted !== (i === null)) || r < t.round && (t.cursor !== null || t.exhausted)) throw F("The retained provider cursor does not match the admitted history head.");
|
|
183
186
|
}
|
|
184
|
-
}, h = async (e, t, n,
|
|
185
|
-
let
|
|
186
|
-
if (
|
|
187
|
-
if (!
|
|
188
|
-
let
|
|
189
|
-
if (V(
|
|
190
|
-
return await
|
|
187
|
+
}, h = async (e, t, n, r = !1) => {
|
|
188
|
+
let i = await e.query(G(w, d("identityKey", n), 2, r));
|
|
189
|
+
if (i.length > 1) throw F();
|
|
190
|
+
if (!i[0]) return null;
|
|
191
|
+
let a = U(i[0]);
|
|
192
|
+
if (V(a, t), i[0].identityKey !== n || await W(a) !== n) throw F();
|
|
193
|
+
return await o(e, a), await p(e, a), a;
|
|
191
194
|
}, x = async (e, t, n, r) => {
|
|
192
195
|
if (t.revision >= j) throw F("The invoice history revision is exhausted.");
|
|
193
196
|
if (await e.updateMany("_voltro_billing_invoice_history_walks", {
|
|
@@ -195,7 +198,8 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
195
198
|
round: n.round,
|
|
196
199
|
pageOrdinal: n.pageOrdinal,
|
|
197
200
|
cursorAfter: n.cursor?.afterInvoiceId ?? null,
|
|
198
|
-
exhausted: +!!n.exhausted
|
|
201
|
+
exhausted: +!!n.exhausted,
|
|
202
|
+
headCheckedAt: n.headCheckedAt
|
|
199
203
|
}, { where: l(d("id", t.id), d("identityKey", r), d("revision", t.revision), d("custodyId", t.custodyId)) }) !== 1) throw F("Invoice history changed during the native page transaction.", !0);
|
|
200
204
|
let i = await h(e, n, r, !0);
|
|
201
205
|
if (i === null || !K(i, n)) throw F("The invoice history checkpoint was not read back.", !0);
|
|
@@ -279,28 +283,112 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
279
283
|
let t = s.get(M(e.pageId));
|
|
280
284
|
if (t === void 0 || !t.some((t) => t.id === e.id && t.referenceKey === e.referenceKey)) throw F();
|
|
281
285
|
}
|
|
282
|
-
}, D = async (e,
|
|
283
|
-
if (
|
|
284
|
-
await
|
|
285
|
-
let
|
|
286
|
-
if (
|
|
287
|
-
if (
|
|
288
|
-
let a = await e.query(G(E, m("id",
|
|
289
|
-
if (a.length >
|
|
290
|
-
await C(e,
|
|
286
|
+
}, D = async (e, t, n) => {
|
|
287
|
+
if (n.length > 101 || new Set(n).size !== n.length) throw F();
|
|
288
|
+
await i(e, t);
|
|
289
|
+
let r = await h(e, t, await W(t), !0);
|
|
290
|
+
if (r === null) throw F("No retained owned invoice history walk exists.");
|
|
291
|
+
if (n.length === 0) return /* @__PURE__ */ new Map();
|
|
292
|
+
let a = await e.query(G(E, m("id", n), n.length + 1));
|
|
293
|
+
if (a.length > n.length) throw F();
|
|
294
|
+
await C(e, r, a);
|
|
291
295
|
let o = /* @__PURE__ */ new Map();
|
|
292
296
|
for (let e of a) {
|
|
293
|
-
let t = S(e,
|
|
294
|
-
if (!
|
|
297
|
+
let t = S(e, r);
|
|
298
|
+
if (!n.includes(t.id) || o.has(t.id)) throw F();
|
|
295
299
|
o.set(t.id, t);
|
|
296
300
|
}
|
|
297
301
|
return o;
|
|
302
|
+
}, O = async (e, t, n, r) => {
|
|
303
|
+
let i = r.invoices.map((e) => e.providerInvoiceId);
|
|
304
|
+
if (i.length === 0) return /* @__PURE__ */ new Set();
|
|
305
|
+
let a = await e.query({
|
|
306
|
+
...G(E, l(d("walkId", t.id), m("providerInvoiceId", i)), 1),
|
|
307
|
+
take: void 0
|
|
308
|
+
}), o = /* @__PURE__ */ new Set();
|
|
309
|
+
for (let e of a) {
|
|
310
|
+
let r = S(e, t);
|
|
311
|
+
if (r.round >= n) throw F("The invoice history cursor or invoice repeated within this round; the page remains uncommitted.");
|
|
312
|
+
o.add(r.providerInvoiceId);
|
|
313
|
+
}
|
|
314
|
+
return o;
|
|
315
|
+
}, k = async (e, t, n, r, i, a) => {
|
|
316
|
+
let o = a ?? await O(e, t, n, r), s = r.invoices.filter((e) => !o.has(e.providerInvoiceId)), c = o.size > 0 ? null : r.nextCursor, l = n === t.round ? t.cursor?.afterInvoiceId ?? null : null, u = c?.afterInvoiceId ?? null, f = await L([
|
|
317
|
+
"voltro.invoice-history-page.v1",
|
|
318
|
+
t.id,
|
|
319
|
+
n,
|
|
320
|
+
l
|
|
321
|
+
]), p = await Promise.all(s.map((e) => L([
|
|
322
|
+
"voltro.invoice-history-reference.v1",
|
|
323
|
+
t.id,
|
|
324
|
+
n,
|
|
325
|
+
e.providerInvoiceId
|
|
326
|
+
]))), h = await L([
|
|
327
|
+
f,
|
|
328
|
+
u,
|
|
329
|
+
r.observedAt.toISOString(),
|
|
330
|
+
s.map((e) => [
|
|
331
|
+
e.providerInvoiceId,
|
|
332
|
+
e.status,
|
|
333
|
+
e.createdAt.toISOString()
|
|
334
|
+
])
|
|
335
|
+
]);
|
|
336
|
+
if ((await e.query(G("_voltro_billing_invoice_history_pages", d("pageKey", f), 1))).length || p.length > 0 && (await e.query(G("_voltro_billing_invoice_history_references", m("referenceKey", p), 1))).length) throw F("The invoice history cursor or invoice repeated within this round; the page remains uncommitted.");
|
|
337
|
+
let g = "bihpage_" + globalThis.crypto.randomUUID(), _ = t.pageOrdinal + 1;
|
|
338
|
+
await e.insert(T, {
|
|
339
|
+
id: g,
|
|
340
|
+
...z(t),
|
|
341
|
+
walkId: t.id,
|
|
342
|
+
round: n,
|
|
343
|
+
pageOrdinal: _,
|
|
344
|
+
pageKey: f,
|
|
345
|
+
bodyDigest: h,
|
|
346
|
+
cursorAfter: l,
|
|
347
|
+
nextCursorAfter: u,
|
|
348
|
+
referenceCount: s.length,
|
|
349
|
+
observedAt: r.observedAt
|
|
350
|
+
}), await e.insertMany(E, s.map((e, r) => ({
|
|
351
|
+
id: "bihref_" + globalThis.crypto.randomUUID(),
|
|
352
|
+
...z(t),
|
|
353
|
+
walkId: t.id,
|
|
354
|
+
round: n,
|
|
355
|
+
pageId: g,
|
|
356
|
+
pageOrdinal: _,
|
|
357
|
+
position: r,
|
|
358
|
+
referenceKey: p[r],
|
|
359
|
+
providerInvoiceId: e.providerInvoiceId,
|
|
360
|
+
discoveryStatus: e.status,
|
|
361
|
+
providerCreatedAt: e.createdAt
|
|
362
|
+
})));
|
|
363
|
+
let v = H({
|
|
364
|
+
...t,
|
|
365
|
+
revision: t.revision + 1 + (n - t.round),
|
|
366
|
+
round: n,
|
|
367
|
+
pageOrdinal: _,
|
|
368
|
+
cursor: c,
|
|
369
|
+
exhausted: c === null,
|
|
370
|
+
headCheckedAt: c === null ? r.observedAt : t.headCheckedAt
|
|
371
|
+
}), y = await e.query(G(T, d("pageKey", f), 2));
|
|
372
|
+
if (y.length !== 1 || y[0]?.id !== g || y[0]?.bodyDigest !== h || y[0]?.referenceCount !== s.length || y[0]?.walkId !== t.id || y[0]?.round !== n || y[0]?.pageOrdinal !== _ || y[0]?.cursorAfter !== l || y[0]?.nextCursorAfter !== u || P(y[0]?.observedAt).getTime() !== r.observedAt.getTime()) throw F("The admitted page receipt was not read back.", !0);
|
|
373
|
+
V(B(y[0]), t);
|
|
374
|
+
let b = await e.query({
|
|
375
|
+
...G(E, d("pageId", g), 101),
|
|
376
|
+
order: [{
|
|
377
|
+
column: "position",
|
|
378
|
+
direction: "asc"
|
|
379
|
+
}]
|
|
380
|
+
});
|
|
381
|
+
if (b.length !== s.length) throw F("Not every invoice reference was read back; the page transaction must roll back.", !0);
|
|
382
|
+
return b.forEach((e, t) => {
|
|
383
|
+
let r = S(e, v), i = s[t];
|
|
384
|
+
if (!i || r.round !== n || r.pageOrdinal !== _ || r.pageId !== g || r.position !== t || e.referenceKey !== p[t] || r.providerInvoiceId !== i.providerInvoiceId || r.discoveryStatus !== i.status || r.providerCreatedAt.getTime() !== i.createdAt.getTime()) throw F("The persisted invoice reference differs from its admitted page.", !0);
|
|
385
|
+
}), x(e, t, v, i);
|
|
298
386
|
};
|
|
299
387
|
return {
|
|
300
|
-
referencesByIds: (
|
|
301
|
-
let
|
|
302
|
-
return
|
|
303
|
-
let t = await D(e,
|
|
388
|
+
referencesByIds: (e, t) => I(async () => {
|
|
389
|
+
let n = R(e), i = t.map((e) => M(e));
|
|
390
|
+
return r(async (e) => {
|
|
391
|
+
let t = await D(e, n, i);
|
|
304
392
|
return i.map((e) => {
|
|
305
393
|
let n = t.get(e);
|
|
306
394
|
if (n === void 0) throw F("A selected invoice history reference is missing.");
|
|
@@ -308,146 +396,83 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
308
396
|
});
|
|
309
397
|
});
|
|
310
398
|
}),
|
|
311
|
-
begin: (
|
|
312
|
-
let
|
|
313
|
-
return
|
|
314
|
-
await
|
|
315
|
-
let r = await h(e,
|
|
399
|
+
begin: (e) => I(async () => {
|
|
400
|
+
let t = R(e), n = await W(t);
|
|
401
|
+
return r(async (e) => {
|
|
402
|
+
await i(e, t);
|
|
403
|
+
let r = await h(e, t, n, !0);
|
|
316
404
|
if (r !== null) return r;
|
|
317
|
-
for (let
|
|
405
|
+
for (let n of [T, E]) if ((await e.query(G(n, a(t), 1))).length) throw F("Retained invoice history receipts require their original walk root; no replacement root is fabricated.");
|
|
318
406
|
await e.insertIgnore(w, {
|
|
319
407
|
id: "bihwalk_" + globalThis.crypto.randomUUID(),
|
|
320
|
-
...z(
|
|
321
|
-
identityKey:
|
|
408
|
+
...z(t),
|
|
409
|
+
identityKey: n,
|
|
322
410
|
revision: 1,
|
|
323
411
|
round: 1,
|
|
324
412
|
pageOrdinal: 0,
|
|
325
413
|
cursorAfter: null,
|
|
326
|
-
exhausted: 0
|
|
414
|
+
exhausted: 0,
|
|
415
|
+
headCheckedAt: null
|
|
327
416
|
}, { conflictColumns: ["identityKey"] });
|
|
328
|
-
let o = await h(e,
|
|
417
|
+
let o = await h(e, t, n, !0);
|
|
329
418
|
if (o === null) throw F("The invoice history owner reservation was not read back.", !0);
|
|
330
419
|
return o;
|
|
331
420
|
});
|
|
332
421
|
}),
|
|
333
|
-
read: (
|
|
334
|
-
let
|
|
335
|
-
return
|
|
422
|
+
read: (e) => I(async () => {
|
|
423
|
+
let t = R(e), n = await W(t);
|
|
424
|
+
return r(async (e) => (await i(e, t), h(e, t, n)));
|
|
336
425
|
}),
|
|
337
|
-
commitPage: (
|
|
338
|
-
let
|
|
339
|
-
account:
|
|
340
|
-
providerCustomerId:
|
|
341
|
-
...
|
|
342
|
-
}), c =
|
|
343
|
-
if (
|
|
344
|
-
let l = await W(
|
|
345
|
-
|
|
346
|
-
i
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
]), p = await Promise.all(c.invoices.map((e) => L([
|
|
350
|
-
"voltro.invoice-history-reference.v1",
|
|
351
|
-
i.id,
|
|
352
|
-
i.round,
|
|
353
|
-
e.providerInvoiceId
|
|
354
|
-
]))), g = await L([
|
|
355
|
-
f,
|
|
356
|
-
c.nextCursor?.afterInvoiceId ?? null,
|
|
357
|
-
c.observedAt.toISOString(),
|
|
358
|
-
c.invoices.map((e) => [
|
|
359
|
-
e.providerInvoiceId,
|
|
360
|
-
e.status,
|
|
361
|
-
e.createdAt.toISOString()
|
|
362
|
-
])
|
|
363
|
-
]);
|
|
364
|
-
return e(async (e) => {
|
|
365
|
-
await t(e, i);
|
|
366
|
-
let n = await h(e, i, l, !0);
|
|
367
|
-
if (n === null || !K(n, i)) return null;
|
|
368
|
-
if ((await e.query(G("_voltro_billing_invoice_history_pages", d("pageKey", f), 1))).length || p.length > 0 && (await e.query(G("_voltro_billing_invoice_history_references", m("referenceKey", p), 1))).length) throw F("The invoice history cursor or invoice repeated within this round; the page remains uncommitted.");
|
|
369
|
-
let r = "bihpage_" + globalThis.crypto.randomUUID(), a = n.pageOrdinal + 1;
|
|
370
|
-
await e.insert(T, {
|
|
371
|
-
id: r,
|
|
372
|
-
...z(n),
|
|
373
|
-
walkId: n.id,
|
|
374
|
-
round: n.round,
|
|
375
|
-
pageOrdinal: a,
|
|
376
|
-
pageKey: f,
|
|
377
|
-
bodyDigest: g,
|
|
378
|
-
cursorAfter: u,
|
|
379
|
-
nextCursorAfter: c.nextCursor?.afterInvoiceId ?? null,
|
|
380
|
-
referenceCount: c.invoices.length,
|
|
381
|
-
observedAt: c.observedAt
|
|
382
|
-
}), await e.insertMany(E, c.invoices.map((e, t) => ({
|
|
383
|
-
id: "bihref_" + globalThis.crypto.randomUUID(),
|
|
384
|
-
...z(n),
|
|
385
|
-
walkId: n.id,
|
|
386
|
-
round: n.round,
|
|
387
|
-
pageId: r,
|
|
388
|
-
pageOrdinal: a,
|
|
389
|
-
position: t,
|
|
390
|
-
referenceKey: p[t],
|
|
391
|
-
providerInvoiceId: e.providerInvoiceId,
|
|
392
|
-
discoveryStatus: e.status,
|
|
393
|
-
providerCreatedAt: e.createdAt
|
|
394
|
-
})));
|
|
395
|
-
let o = H({
|
|
396
|
-
...n,
|
|
397
|
-
revision: n.revision + 1,
|
|
398
|
-
pageOrdinal: a,
|
|
399
|
-
cursor: c.nextCursor,
|
|
400
|
-
exhausted: c.nextCursor === null
|
|
401
|
-
}), s = await e.query(G(T, d("pageKey", f), 2));
|
|
402
|
-
if (s.length !== 1 || s[0]?.id !== r || s[0]?.bodyDigest !== g || s[0]?.referenceCount !== c.invoices.length || s[0]?.walkId !== n.id || s[0]?.round !== n.round || s[0]?.pageOrdinal !== a || s[0]?.cursorAfter !== u || s[0]?.nextCursorAfter !== (c.nextCursor?.afterInvoiceId ?? null) || P(s[0]?.observedAt).getTime() !== c.observedAt.getTime()) throw F("The admitted page receipt was not read back.", !0);
|
|
403
|
-
V(B(s[0]), n);
|
|
404
|
-
let _ = await e.query({
|
|
405
|
-
...G(E, d("pageId", r), 101),
|
|
406
|
-
order: [{
|
|
407
|
-
column: "position",
|
|
408
|
-
direction: "asc"
|
|
409
|
-
}]
|
|
410
|
-
});
|
|
411
|
-
if (_.length !== c.invoices.length) throw F("Not every invoice reference was read back; the page transaction must roll back.", !0);
|
|
412
|
-
return _.forEach((e, t) => {
|
|
413
|
-
let i = S(e, o), s = c.invoices[t];
|
|
414
|
-
if (!s || i.round !== n.round || i.pageOrdinal !== a || i.pageId !== r || i.position !== t || e.referenceKey !== p[t] || i.providerInvoiceId !== s.providerInvoiceId || i.discoveryStatus !== s.status || i.providerCreatedAt.getTime() !== s.createdAt.getTime()) throw F("The persisted invoice reference differs from its admitted page.", !0);
|
|
415
|
-
}), x(e, n, o, l);
|
|
426
|
+
commitPage: (e, a) => I(async () => {
|
|
427
|
+
let o = H(e), s = t({
|
|
428
|
+
account: o.account,
|
|
429
|
+
providerCustomerId: o.providerCustomerId,
|
|
430
|
+
...o.cursor === null ? {} : { cursor: o.cursor }
|
|
431
|
+
}), c = n(s, a);
|
|
432
|
+
if (o.exhausted || o.revision >= j || o.pageOrdinal >= j) throw F();
|
|
433
|
+
let l = await W(o);
|
|
434
|
+
return r(async (e) => {
|
|
435
|
+
await i(e, o);
|
|
436
|
+
let t = await h(e, o, l, !0);
|
|
437
|
+
return t === null || !K(t, o) ? null : k(e, t, t.round, c, l);
|
|
416
438
|
});
|
|
417
439
|
}),
|
|
418
|
-
|
|
419
|
-
let
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
440
|
+
scanHead: (e, a) => I(async () => {
|
|
441
|
+
let o = H(e), s = n(t({
|
|
442
|
+
account: o.account,
|
|
443
|
+
providerCustomerId: o.providerCustomerId
|
|
444
|
+
}), a);
|
|
445
|
+
if (!o.exhausted || o.round >= j || o.revision >= 2147483646 || o.pageOrdinal >= j) throw F();
|
|
446
|
+
let c = await W(o);
|
|
447
|
+
return r(async (e) => {
|
|
448
|
+
await i(e, o);
|
|
449
|
+
let t = await h(e, o, c, !0);
|
|
450
|
+
if (t === null || !K(t, o)) return null;
|
|
451
|
+
let n = await O(e, t, t.round + 1, s);
|
|
452
|
+
return s.invoices.some((e) => !n.has(e.providerInvoiceId)) ? k(e, t, t.round + 1, s, c, n) : t.headCheckedAt?.getTime() === s.observedAt.getTime() ? t : x(e, t, H({
|
|
453
|
+
...t,
|
|
454
|
+
headCheckedAt: s.observedAt
|
|
455
|
+
}), c);
|
|
431
456
|
});
|
|
432
457
|
}),
|
|
433
|
-
reference: (
|
|
434
|
-
let
|
|
435
|
-
return
|
|
458
|
+
reference: (e, t) => I(async () => {
|
|
459
|
+
let n = R(e), i = M(t);
|
|
460
|
+
return r(async (e) => (await D(e, n, [i])).get(i) ?? null);
|
|
436
461
|
}),
|
|
437
|
-
references: (
|
|
438
|
-
let
|
|
462
|
+
references: (e, t) => I(async () => {
|
|
463
|
+
let n = R(e), a = t?.limit ?? 100, o = t?.afterReferenceId === void 0 ? null : M(t.afterReferenceId);
|
|
439
464
|
if (!Number.isSafeInteger(a) || a < 1 || a > 100) throw F();
|
|
440
|
-
let s = await W(
|
|
441
|
-
return
|
|
442
|
-
await
|
|
443
|
-
let
|
|
444
|
-
if (
|
|
445
|
-
let r = [d("walkId",
|
|
465
|
+
let s = await W(n);
|
|
466
|
+
return r(async (e) => {
|
|
467
|
+
await i(e, n);
|
|
468
|
+
let t = await h(e, n, s);
|
|
469
|
+
if (t === null) throw F("No owned invoice history walk exists.");
|
|
470
|
+
let r = [d("walkId", t.id), _("pageOrdinal", t.pageOrdinal)];
|
|
446
471
|
if (o !== null) {
|
|
447
|
-
let
|
|
448
|
-
if (
|
|
449
|
-
await C(e,
|
|
450
|
-
let i = S(
|
|
472
|
+
let n = await e.query(G(E, d("id", o), 2));
|
|
473
|
+
if (n.length !== 1 || n[0]?.id !== o) throw F("The invoice reference cursor is not retained by this walk.");
|
|
474
|
+
await C(e, t, n);
|
|
475
|
+
let i = S(n[0], t);
|
|
451
476
|
r.push(y(f("pageOrdinal", i.pageOrdinal), l(d("pageOrdinal", i.pageOrdinal), f("position", i.position))));
|
|
452
477
|
}
|
|
453
478
|
let c = await e.query({
|
|
@@ -460,13 +485,13 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
|
|
|
460
485
|
direction: "asc"
|
|
461
486
|
}]
|
|
462
487
|
});
|
|
463
|
-
await C(e,
|
|
464
|
-
let u = c.map((e) => S(e,
|
|
488
|
+
await C(e, t, c);
|
|
489
|
+
let u = c.map((e) => S(e, t)), p = u.slice(0, a);
|
|
465
490
|
return {
|
|
466
491
|
references: p,
|
|
467
492
|
nextReferenceId: u.length > a ? p.at(-1).id : null,
|
|
468
|
-
watermarkPageOrdinal:
|
|
469
|
-
walkRevision:
|
|
493
|
+
watermarkPageOrdinal: t.pageOrdinal,
|
|
494
|
+
walkRevision: t.revision
|
|
470
495
|
};
|
|
471
496
|
});
|
|
472
497
|
})
|