@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.
Files changed (40) hide show
  1. package/CHANGELOG.md +1943 -0
  2. package/README.md +0 -435
  3. package/THIRD-PARTY-NOTICES.md +22 -646
  4. package/dist/catalog.js +169 -70
  5. package/dist/custodyInventory.js +101 -65
  6. package/dist/dunning.js +14 -6
  7. package/dist/eventDelivery.js +14 -5
  8. package/dist/historicalUsage.js +94 -0
  9. package/dist/index.d.ts +777 -68
  10. package/dist/index.js +354 -233
  11. package/dist/invoiceHistoryJournal.js +204 -179
  12. package/dist/invoiceHistoryProgress.js +130 -106
  13. package/dist/invoiceHistoryWork.js +188 -186
  14. package/dist/invoicePaymentEpoch.js +145 -0
  15. package/dist/invoicePaymentObservation.js +64 -0
  16. package/dist/invoiceSnapshot.js +56 -46
  17. package/dist/invoiceStore.js +124 -90
  18. package/dist/paidInvoiceCoverage.js +84 -0
  19. package/dist/plans.js +15 -2
  20. package/dist/providers.js +149 -92
  21. package/dist/provision.js +84 -73
  22. package/dist/rpc.d.ts +53 -13
  23. package/dist/rpc.js +141 -78
  24. package/dist/service.js +1208 -722
  25. package/dist/stores.js +240 -148
  26. package/dist/stripeIdentity.js +17 -17
  27. package/dist/stripeInvoiceHistory.js +21 -21
  28. package/dist/stripeInvoicePayments.js +129 -0
  29. package/dist/stripeInvoiceSnapshot.js +65 -46
  30. package/dist/stripeProvider.js +570 -201
  31. package/dist/stripeRest.js +10 -10
  32. package/dist/subscriptionObservation.js +93 -0
  33. package/dist/subscriptionWriteStore.js +325 -0
  34. package/dist/table.js +75 -66
  35. package/dist/types.d.ts +415 -27
  36. package/dist/types.js +21 -4
  37. package/dist/web/index.d.ts +93 -3
  38. package/dist/web/index.js +76 -33
  39. package/dist/web/writeRequest.js +65 -0
  40. package/package.json +9 -9
@@ -1,7 +1,7 @@
1
- import { BillingError as e } from "./types.js";
2
- import { captureProviderAccount as t, exactProviderIdentifier as n, sameProviderAccount as r } from "./providerIdentity.js";
3
- import { dataStoreCustomerStore as i } from "./customerStore.js";
4
- import { captureInvoiceHistoryInput as a, captureInvoiceHistoryPage as o, historyError as s } from "./invoiceHistory.js";
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 (!n(e) || e.length > t || !e.isWellFormed()) throw F();
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) => s(e, t), I = (t) => c.tryPromise({
63
- try: t,
64
- catch: (t) => t instanceof e ? t : F("The native invoice history transaction could not be confirmed.", !0)
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 n = t(e.account);
70
- return M(n.providerAccountId), Object.freeze({
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(n),
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 || !r(e.account, t.account)) throw F();
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 t = R(e);
100
+ let n = R(e);
100
101
  if (typeof e.exhausted != "boolean" || e.exhausted && e.cursor !== null) throw F();
101
- let n = e.cursor === null ? null : a({
102
- account: t.account,
103
- providerCustomerId: t.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 (n === void 0 || e.revision !== e.pageOrdinal + e.round || e.pageOrdinal === 0 && (e.cursor !== null || e.exhausted)) throw F();
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
- ...t,
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: n,
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 = (e) => {
147
- let t = async (e, t) => {
148
- let n = await c.runPromise(i(e).getByProviderCustomerId(t.providerCustomerId, t));
149
- if (n === null) throw F("Positive retained customer custody is required before invoice history admission.");
150
- V(R(n), t);
151
- }, n = (e) => l(d("provider", e.provider), d("providerAccountId", e.account.providerAccountId), d("livemode", +!!e.account.livemode), d("providerCustomerId", e.providerCustomerId)), r = async (e, t) => {
152
- for (let r of [T, E]) {
153
- let i = 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)), a = y(v("provider", t.provider), v("providerAccountId", t.account.providerAccountId), v("livemode", +!!t.account.livemode), v("providerCustomerId", t.providerCustomerId)), o = y(l(n(t), i), l(d("walkId", t.id), y(i, a)));
154
- if ((await e.query(G(r, o, 1))).length) throw F("Invoice history receipts do not belong to the retained walk checkpoint; explicit reconciliation is required.", !0);
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, i = !1) => {
185
- let a = await e.query(G(w, d("identityKey", n), 2, i));
186
- if (a.length > 1) throw F();
187
- if (!a[0]) return null;
188
- let o = U(a[0]);
189
- if (V(o, t), a[0].identityKey !== n || await W(o) !== n) throw F();
190
- return await r(e, o), await p(e, o), o;
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, n, r) => {
283
- if (r.length > 101 || new Set(r).size !== r.length) throw F();
284
- await t(e, n);
285
- let i = await h(e, n, await W(n), !0);
286
- if (i === null) throw F("No retained owned invoice history walk exists.");
287
- if (r.length === 0) return /* @__PURE__ */ new Map();
288
- let a = await e.query(G(E, m("id", r), r.length + 1));
289
- if (a.length > r.length) throw F();
290
- await C(e, i, a);
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, i);
294
- if (!r.includes(t.id) || o.has(t.id)) throw F();
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: (t, n) => I(async () => {
301
- let r = R(t), i = n.map((e) => M(e));
302
- return e(async (e) => {
303
- let t = await D(e, r, i);
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: (r) => I(async () => {
312
- let i = R(r), a = await W(i);
313
- return e(async (e) => {
314
- await t(e, i);
315
- let r = await h(e, i, a, !0);
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 t of [T, E]) if ((await e.query(G(t, n(i), 1))).length) throw F("Retained invoice history receipts require their original walk root; no replacement root is fabricated.");
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(i),
321
- identityKey: a,
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, i, a, !0);
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: (n) => I(async () => {
334
- let r = R(n), i = await W(r);
335
- return e(async (e) => (await t(e, r), h(e, r, i)));
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: (n, r) => I(async () => {
338
- let i = H(n), s = a({
339
- account: i.account,
340
- providerCustomerId: i.providerCustomerId,
341
- ...i.cursor === null ? {} : { cursor: i.cursor }
342
- }), c = o(s, r);
343
- if (i.exhausted || i.revision >= j || i.pageOrdinal >= j) throw F();
344
- let l = await W(i), u = i.cursor?.afterInvoiceId ?? null, f = await L([
345
- "voltro.invoice-history-page.v1",
346
- i.id,
347
- i.round,
348
- u
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
- restart: (n) => I(async () => {
419
- let r = H(n), i = await W(r);
420
- if (r.round >= j || r.revision >= j) throw F();
421
- return e(async (e) => {
422
- await t(e, r);
423
- let n = await h(e, r, i, !0);
424
- return n === null || !K(n, r) ? null : x(e, n, H({
425
- ...n,
426
- revision: n.revision + 1,
427
- round: n.round + 1,
428
- cursor: null,
429
- exhausted: !1
430
- }), i);
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: (t, n) => I(async () => {
434
- let r = R(t), i = M(n);
435
- return e(async (e) => (await D(e, r, [i])).get(i) ?? null);
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: (n, r) => I(async () => {
438
- let i = R(n), a = r?.limit ?? 100, o = r?.afterReferenceId === void 0 ? null : M(r.afterReferenceId);
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(i);
441
- return e(async (e) => {
442
- await t(e, i);
443
- let n = await h(e, i, s);
444
- if (n === null) throw F("No owned invoice history walk exists.");
445
- let r = [d("walkId", n.id), _("pageOrdinal", n.pageOrdinal)];
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 t = await e.query(G(E, d("id", o), 2));
448
- if (t.length !== 1 || t[0]?.id !== o) throw F("The invoice reference cursor is not retained by this walk.");
449
- await C(e, n, t);
450
- let i = S(t[0], n);
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, n, c);
464
- let u = c.map((e) => S(e, n)), p = u.slice(0, a);
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: n.pageOrdinal,
469
- walkRevision: n.revision
493
+ watermarkPageOrdinal: t.pageOrdinal,
494
+ walkRevision: t.revision
470
495
  };
471
496
  });
472
497
  })