@voltro/plugin-billing 0.90.0 → 0.91.1

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.
@@ -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
@@ -51,7 +51,7 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
51
51
  "pageOrdinal",
52
52
  "position"
53
53
  ]).index("byBillingInvoiceHistoryReferenceInvoice", ["walkId", "providerInvoiceId"]).index("byBillingInvoiceHistoryReferenceTenant", ["tenantId"]), j = 2147483647, M = (e, t = 191) => {
54
- if (!n(e) || e.length > t || !e.isWellFormed()) throw F();
54
+ if (!a(e) || e.length > t || !e.isWellFormed()) throw F();
55
55
  return e;
56
56
  }, N = (e, t = 0) => {
57
57
  if (typeof e != "number" || !Number.isSafeInteger(e) || e < t || e > j) throw F();
@@ -60,18 +60,18 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
60
60
  let t = e instanceof Date ? new Date(e) : new Date(typeof e == "string" ? e : NaN);
61
61
  if (!Number.isFinite(t.getTime())) throw F();
62
62
  return t;
63
- }, F = (e = "The durable invoice history journal requires reconciliation.", t = !1) => s(e, t), I = (t) => c.tryPromise({
64
- try: t,
65
- 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)
66
66
  }), L = async (e) => {
67
67
  let t = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(e)));
68
68
  return [...new Uint8Array(t)].map((e) => e.toString(16).padStart(2, "0")).join("");
69
69
  }, R = (e) => {
70
- let n = t(e.account);
71
- return M(n.providerAccountId), Object.freeze({
70
+ let t = i(e.account);
71
+ return M(t.providerAccountId), Object.freeze({
72
72
  tenantId: M(e.tenantId),
73
73
  provider: M(e.provider, 64),
74
- account: Object.freeze(n),
74
+ account: Object.freeze(t),
75
75
  providerCustomerId: M(e.providerCustomerId),
76
76
  custodyId: M(e.custodyId ?? e.id)
77
77
  });
@@ -95,25 +95,25 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
95
95
  custodyId: M(e.custodyId)
96
96
  });
97
97
  }, V = (e, t) => {
98
- 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();
99
99
  }, H = (e) => {
100
- let t = R(e);
100
+ let n = R(e);
101
101
  if (typeof e.exhausted != "boolean" || e.exhausted && e.cursor !== null) throw F();
102
- let n = e.headCheckedAt === null ? null : P(e.headCheckedAt), r = e.cursor === null ? null : a({
103
- account: t.account,
104
- 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,
105
105
  cursor: e.cursor
106
106
  }).cursor;
107
- if (r === 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();
108
108
  return Object.freeze({
109
- ...t,
109
+ ...n,
110
110
  id: M(e.id),
111
111
  revision: N(e.revision, 1),
112
112
  round: N(e.round, 1),
113
113
  pageOrdinal: N(e.pageOrdinal),
114
- cursor: r,
114
+ cursor: i,
115
115
  exhausted: e.exhausted,
116
- headCheckedAt: n
116
+ headCheckedAt: r
117
117
  });
118
118
  }, U = (e) => {
119
119
  let t = B(e), n = e.cursorAfter, r = e.headCheckedAt;
@@ -146,15 +146,15 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
146
146
  skip: void 0,
147
147
  projection: void 0,
148
148
  ...r ? { lock: "update" } : {}
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 = (e) => {
150
- let t = async (e, t) => {
151
- let n = await c.runPromise(i(e).getByProviderCustomerId(t.providerCustomerId, t));
152
- if (n === null) throw F("Positive retained customer custody is required before invoice history admission.");
153
- V(R(n), t);
154
- }, 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) => {
155
- for (let r of [T, E]) {
156
- 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)));
157
- 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);
158
158
  }
159
159
  }, s = (e) => {
160
160
  if (typeof e == "bigint") {
@@ -184,13 +184,13 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
184
184
  let r = N(n[0].round, 1), i = n[0].nextCursorAfter;
185
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.");
186
186
  }
187
- }, h = async (e, t, n, i = !1) => {
188
- let a = await e.query(G(w, d("identityKey", n), 2, i));
189
- if (a.length > 1) throw F();
190
- if (!a[0]) return null;
191
- let o = U(a[0]);
192
- if (V(o, t), a[0].identityKey !== n || await W(o) !== n) throw F();
193
- 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;
194
194
  }, x = async (e, t, n, r) => {
195
195
  if (t.revision >= j) throw F("The invoice history revision is exhausted.");
196
196
  if (await e.updateMany("_voltro_billing_invoice_history_walks", {
@@ -283,19 +283,19 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
283
283
  let t = s.get(M(e.pageId));
284
284
  if (t === void 0 || !t.some((t) => t.id === e.id && t.referenceKey === e.referenceKey)) throw F();
285
285
  }
286
- }, D = async (e, n, r) => {
287
- if (r.length > 101 || new Set(r).size !== r.length) throw F();
288
- await t(e, n);
289
- let i = await h(e, n, await W(n), !0);
290
- if (i === null) throw F("No retained owned invoice history walk exists.");
291
- if (r.length === 0) return /* @__PURE__ */ new Map();
292
- let a = await e.query(G(E, m("id", r), r.length + 1));
293
- if (a.length > r.length) throw F();
294
- 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);
295
295
  let o = /* @__PURE__ */ new Map();
296
296
  for (let e of a) {
297
- let t = S(e, i);
298
- 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();
299
299
  o.set(t.id, t);
300
300
  }
301
301
  return o;
@@ -385,10 +385,10 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
385
385
  }), x(e, t, v, i);
386
386
  };
387
387
  return {
388
- referencesByIds: (t, n) => I(async () => {
389
- let r = R(t), i = n.map((e) => M(e));
390
- return e(async (e) => {
391
- 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);
392
392
  return i.map((e) => {
393
393
  let n = t.get(e);
394
394
  if (n === void 0) throw F("A selected invoice history reference is missing.");
@@ -396,17 +396,17 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
396
396
  });
397
397
  });
398
398
  }),
399
- begin: (r) => I(async () => {
400
- let i = R(r), a = await W(i);
401
- return e(async (e) => {
402
- await t(e, i);
403
- 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);
404
404
  if (r !== null) return r;
405
- 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.");
406
406
  await e.insertIgnore(w, {
407
407
  id: "bihwalk_" + globalThis.crypto.randomUUID(),
408
- ...z(i),
409
- identityKey: a,
408
+ ...z(t),
409
+ identityKey: n,
410
410
  revision: 1,
411
411
  round: 1,
412
412
  pageOrdinal: 0,
@@ -414,65 +414,65 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
414
414
  exhausted: 0,
415
415
  headCheckedAt: null
416
416
  }, { conflictColumns: ["identityKey"] });
417
- let o = await h(e, i, a, !0);
417
+ let o = await h(e, t, n, !0);
418
418
  if (o === null) throw F("The invoice history owner reservation was not read back.", !0);
419
419
  return o;
420
420
  });
421
421
  }),
422
- read: (n) => I(async () => {
423
- let r = R(n), i = await W(r);
424
- 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)));
425
425
  }),
426
- commitPage: (n, r) => I(async () => {
427
- let i = H(n), s = a({
428
- account: i.account,
429
- providerCustomerId: i.providerCustomerId,
430
- ...i.cursor === null ? {} : { cursor: i.cursor }
431
- }), c = o(s, r);
432
- if (i.exhausted || i.revision >= j || i.pageOrdinal >= j) throw F();
433
- let l = await W(i);
434
- return e(async (e) => {
435
- await t(e, i);
436
- let n = await h(e, i, l, !0);
437
- return n === null || !K(n, i) ? null : k(e, n, n.round, c, 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);
438
438
  });
439
439
  }),
440
- scanHead: (n, r) => I(async () => {
441
- let i = H(n), s = o(a({
442
- account: i.account,
443
- providerCustomerId: i.providerCustomerId
444
- }), r);
445
- if (!i.exhausted || i.round >= j || i.revision >= 2147483646 || i.pageOrdinal >= j) throw F();
446
- let c = await W(i);
447
- return e(async (e) => {
448
- await t(e, i);
449
- let n = await h(e, i, c, !0);
450
- if (n === null || !K(n, i)) return null;
451
- let r = await O(e, n, n.round + 1, s);
452
- return s.invoices.some((e) => !r.has(e.providerInvoiceId)) ? k(e, n, n.round + 1, s, c, r) : n.headCheckedAt?.getTime() === s.observedAt.getTime() ? n : x(e, n, H({
453
- ...n,
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
454
  headCheckedAt: s.observedAt
455
455
  }), c);
456
456
  });
457
457
  }),
458
- reference: (t, n) => I(async () => {
459
- let r = R(t), i = M(n);
460
- 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);
461
461
  }),
462
- references: (n, r) => I(async () => {
463
- 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);
464
464
  if (!Number.isSafeInteger(a) || a < 1 || a > 100) throw F();
465
- let s = await W(i);
466
- return e(async (e) => {
467
- await t(e, i);
468
- let n = await h(e, i, s);
469
- if (n === null) throw F("No owned invoice history walk exists.");
470
- 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)];
471
471
  if (o !== null) {
472
- let t = await e.query(G(E, d("id", o), 2));
473
- if (t.length !== 1 || t[0]?.id !== o) throw F("The invoice reference cursor is not retained by this walk.");
474
- await C(e, n, t);
475
- 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);
476
476
  r.push(y(f("pageOrdinal", i.pageOrdinal), l(d("pageOrdinal", i.pageOrdinal), f("position", i.position))));
477
477
  }
478
478
  let c = await e.query({
@@ -485,13 +485,13 @@ var w = "_voltro_billing_invoice_history_walks", T = "_voltro_billing_invoice_hi
485
485
  direction: "asc"
486
486
  }]
487
487
  });
488
- await C(e, n, c);
489
- 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);
490
490
  return {
491
491
  references: p,
492
492
  nextReferenceId: u.length > a ? p.at(-1).id : null,
493
- watermarkPageOrdinal: n.pageOrdinal,
494
- walkRevision: n.revision
493
+ watermarkPageOrdinal: t.pageOrdinal,
494
+ walkRevision: t.revision
495
495
  };
496
496
  });
497
497
  })
@@ -1,8 +1,8 @@
1
- import { BillingError as e } from "./types.js";
2
- import { captureProviderAccount as t, exactProviderIdentifier as n, sameProviderAccount as r } from "./providerIdentity.js";
3
- import { BILLING_CUSTOMER_CUSTODY_TABLE as i, dataStoreCustomerStore as a } from "./customerStore.js";
4
- import { invoiceHistoryJournalInTransaction as o } from "./invoiceHistoryJournal.js";
5
- import { BILLING_INVOICE_HISTORY_WORK_TABLE as s } from "./invoiceHistoryWork.js";
1
+ import { BILLING_CUSTOMER_CUSTODY_TABLE as e, dataStoreCustomerStore as t } from "./customerStore.js";
2
+ import { invoiceHistoryJournalInTransaction as n } from "./invoiceHistoryJournal.js";
3
+ import { BILLING_INVOICE_HISTORY_WORK_TABLE as r } from "./invoiceHistoryWork.js";
4
+ import { captureProviderAccount as i, exactProviderIdentifier as a, sameProviderAccount as o } from "./providerIdentity.js";
5
+ import { BillingError as s } from "./types.js";
6
6
  import { Effect as c } from "effect";
7
7
  import { and as l, eq as u, generateId as d, id as f, integer as p, table as m, text as h, timestamp as g } from "@voltro/database";
8
8
  // src/invoiceHistoryProgress.ts
@@ -29,12 +29,12 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
29
29
  afterReadyId: h().maxLength(191).nullable(),
30
30
  afterWorkingId: h().maxLength(191).nullable(),
31
31
  afterObservedId: h().maxLength(191).nullable()
32
- }).unique("byBillingHistoryDispatchCustody", ["identityKey"]).index("byBillingHistoryDispatchTenant", ["tenantId"]), S = 2147483647, C = () => new e({
32
+ }).unique("byBillingHistoryDispatchCustody", ["identityKey"]).index("byBillingHistoryDispatchTenant", ["tenantId"]), S = 2147483647, C = () => new s({
33
33
  source: "invoiceHistoryProgress",
34
34
  message: "Invoice-history traversal requires fresh owned native state.",
35
35
  transient: !0
36
36
  }), w = (e, t = 191) => {
37
- if (!n(e) || e.length > t || !e.isWellFormed()) throw C();
37
+ if (!a(e) || e.length > t || !e.isWellFormed()) throw C();
38
38
  return e;
39
39
  }, T = (e, t) => w(d({
40
40
  kind: "typeid",
@@ -47,10 +47,10 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
47
47
  if (typeof e != "number" || !Number.isInteger(e) || e < 1 || e > S) throw C();
48
48
  return e;
49
49
  }, k = (e) => {
50
- let n = t(e.account);
51
- return w(n.providerAccountId), {
50
+ let t = i(e.account);
51
+ return w(t.providerAccountId), {
52
52
  provider: w(e.provider, 64),
53
- account: n
53
+ account: t
54
54
  };
55
55
  }, A = (e) => ({
56
56
  provider: e.provider,
@@ -70,38 +70,38 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
70
70
  skip: void 0,
71
71
  projection: void 0,
72
72
  ...n ? { lock: "update" } : {}
73
- }), N = (t) => c.tryPromise({
74
- try: t,
75
- catch: (t) => t instanceof e ? t : C()
73
+ }), N = (e) => c.tryPromise({
74
+ try: e,
75
+ catch: (e) => e instanceof s ? e : C()
76
76
  }), P = [
77
77
  "unprepared",
78
78
  "ready",
79
79
  "working",
80
80
  "observed"
81
- ], F = (e) => {
82
- let t = async (e, t) => {
83
- let n = k(t), i = w(t.id), o = w(t.tenantId), s = w(t.providerCustomerId), l = await c.runPromise(a(e).getByProviderCustomerId(s, n));
84
- if (l === null || l.id !== i || l.tenantId !== o || l.provider !== n.provider || !r(l.account, n.account)) throw C();
81
+ ], F = (i) => {
82
+ let a = async (e, n) => {
83
+ let r = k(n), i = w(n.id), a = w(n.tenantId), s = w(n.providerCustomerId), l = await c.runPromise(t(e).getByProviderCustomerId(s, r));
84
+ if (l === null || l.id !== i || l.tenantId !== a || l.provider !== r.provider || !o(l.account, r.account)) throw C();
85
85
  return l;
86
- }, n = async (e, t, n) => {
87
- let r = await e.query({
88
- ...M(i, n),
89
- predicate: l(u("id", n), u("provider", t.provider), u("providerAccountId", t.account.providerAccountId), u("livemode", +!!t.account.livemode))
86
+ }, s = async (n, r, i) => {
87
+ let a = await n.query({
88
+ ...M(e, i),
89
+ predicate: l(u("id", i), u("provider", r.provider), u("providerAccountId", r.account.providerAccountId), u("livemode", +!!r.account.livemode))
90
90
  });
91
- if (r.length !== 1 || r[0]?.id !== n) throw C();
92
- let o = await c.runPromise(a(e).getByProviderCustomerId(w(r[0].providerCustomerId), t));
93
- if (o === null || o.id !== n || o.tenantId !== w(r[0].tenantId)) throw C();
94
- }, d = async (e, t) => {
95
- let r = await e.query({
96
- ...M(i, ""),
97
- predicate: l(u("provider", t.provider), u("providerAccountId", t.account.providerAccountId), u("livemode", +!!t.account.livemode)),
91
+ if (a.length !== 1 || a[0]?.id !== i) throw C();
92
+ let o = await c.runPromise(t(n).getByProviderCustomerId(w(a[0].providerCustomerId), r));
93
+ if (o === null || o.id !== i || o.tenantId !== w(a[0].tenantId)) throw C();
94
+ }, d = async (t, n) => {
95
+ let r = await t.query({
96
+ ...M(e, ""),
97
+ predicate: l(u("provider", n.provider), u("providerAccountId", n.account.providerAccountId), u("livemode", +!!n.account.livemode)),
98
98
  order: [{
99
99
  column: "id",
100
100
  direction: "desc"
101
101
  }],
102
102
  take: 1
103
- }), a = r[0] === void 0 ? null : w(r[0].id);
104
- return a !== null && await n(e, t, a), a;
103
+ }), i = r[0] === void 0 ? null : w(r[0].id);
104
+ return i !== null && await s(t, n, i), i;
105
105
  }, f = async (e, t, n, r, i = !0) => {
106
106
  let a = await e.query(M(t, r, i));
107
107
  if (a.length > 1) throw C();
@@ -109,53 +109,53 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
109
109
  if (!o) return null;
110
110
  if (o.identityKey !== r || o.provider !== n.provider || o.providerAccountId !== n.account.providerAccountId || o.livemode !== +!!n.account.livemode) throw C();
111
111
  return w(o.id), O(o.revision), D(o.updatedAt), o;
112
- }, p = async (e, t, r) => {
113
- let i = E(r.afterCustodyId), a = E(r.cycleEndCustodyId ?? null);
114
- return i !== null && await n(e, t, i), a !== null && await n(e, t, a), {
112
+ }, p = async (e, t, n) => {
113
+ let r = E(n.afterCustodyId), i = E(n.cycleEndCustodyId ?? null);
114
+ return r !== null && await s(e, t, r), i !== null && await s(e, t, i), {
115
115
  ...t,
116
- id: w(r.id),
117
- revision: O(r.revision),
118
- afterCustodyId: i,
119
- cycleEndCustodyId: a,
120
- updatedAt: D(r.updatedAt)
116
+ id: w(n.id),
117
+ revision: O(n.revision),
118
+ afterCustodyId: r,
119
+ cycleEndCustodyId: i,
120
+ updatedAt: D(n.updatedAt)
121
121
  };
122
- }, m = async (e, t, n) => {
123
- if (n.tenantId !== t.tenantId || n.providerCustomerId !== t.providerCustomerId || n.custodyId !== t.id) throw C();
124
- let r = E(n.walkId), i = {
125
- unprepared: E(n.afterUnpreparedId),
126
- ready: E(n.afterReadyId),
127
- working: E(n.afterWorkingId),
128
- observed: E(n.afterObservedId)
122
+ }, m = async (e, t, i) => {
123
+ if (i.tenantId !== t.tenantId || i.providerCustomerId !== t.providerCustomerId || i.custodyId !== t.id) throw C();
124
+ let a = E(i.walkId), o = {
125
+ unprepared: E(i.afterUnpreparedId),
126
+ ready: E(i.afterReadyId),
127
+ working: E(i.afterWorkingId),
128
+ observed: E(i.afterObservedId)
129
129
  };
130
- if (r === null && P.some((e) => i[e] !== null)) throw C();
131
- if (r !== null) {
132
- let n = o(e), a = await c.runPromise(n.read(t));
133
- if (a === null || a.id !== r) throw C();
134
- for (let a of P) {
135
- let o = i[a];
136
- if (o === null) continue;
137
- let l = o;
138
- if (a !== "unprepared") {
130
+ if (a === null && P.some((e) => o[e] !== null)) throw C();
131
+ if (a !== null) {
132
+ let i = n(e), s = await c.runPromise(i.read(t));
133
+ if (s === null || s.id !== a) throw C();
134
+ for (let n of P) {
135
+ let s = o[n];
136
+ if (s === null) continue;
137
+ let l = s;
138
+ if (n !== "unprepared") {
139
139
  let n = await e.query({
140
- ...M(s, o),
141
- predicate: u("id", o)
140
+ ...M(r, s),
141
+ predicate: u("id", s)
142
142
  });
143
- if (n.length !== 1 || n[0]?.id !== o) throw C();
143
+ if (n.length !== 1 || n[0]?.id !== s) throw C();
144
144
  let i = n[0];
145
- if (i.walkId !== r || i.custodyId !== t.id || i.tenantId !== t.tenantId || i.providerCustomerId !== t.providerCustomerId || i.provider !== t.provider || i.providerAccountId !== t.account.providerAccountId || i.livemode !== +!!t.account.livemode) throw C();
145
+ if (i.walkId !== a || i.custodyId !== t.id || i.tenantId !== t.tenantId || i.providerCustomerId !== t.providerCustomerId || i.provider !== t.provider || i.providerAccountId !== t.account.providerAccountId || i.livemode !== +!!t.account.livemode) throw C();
146
146
  l = w(i.referenceId);
147
147
  }
148
- let d = await c.runPromise(n.reference(t, l));
149
- if (d === null || d.walkId !== r) throw C();
148
+ let d = await c.runPromise(i.reference(t, l));
149
+ if (d === null || d.walkId !== a) throw C();
150
150
  }
151
151
  }
152
152
  return {
153
- id: w(n.id),
154
- revision: O(n.revision),
153
+ id: w(i.id),
154
+ revision: O(i.revision),
155
155
  owner: t,
156
- walkId: r,
157
- cursors: i,
158
- updatedAt: D(n.updatedAt)
156
+ walkId: a,
157
+ cursors: o,
158
+ updatedAt: D(i.updatedAt)
159
159
  };
160
160
  }, h = async (e, t, n, r, i, a, o) => {
161
161
  let s = await f(e, t, n, r);
@@ -175,82 +175,82 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
175
175
  return p;
176
176
  };
177
177
  return {
178
- account: (t, n) => N(async () => {
179
- let r = k(t), i = D(n), a = await j(r, null);
180
- return e.transactional(async (e) => {
178
+ account: (e, t) => N(async () => {
179
+ let n = k(e), r = D(t), a = await j(n, null);
180
+ return i.transactional(async (e) => {
181
181
  await e.insertIgnore(_, {
182
182
  id: T(_, "bihscan"),
183
- ...A(r),
183
+ ...A(n),
184
184
  identityKey: a,
185
185
  revision: 1,
186
186
  afterCustodyId: null,
187
- cycleEndCustodyId: await d(e, r),
188
- updatedAt: i
187
+ cycleEndCustodyId: await d(e, n),
188
+ updatedAt: r
189
189
  }, { conflictColumns: ["identityKey"] });
190
- let t = await f(e, _, r, a);
190
+ let t = await f(e, _, n, a);
191
191
  if (t === null) throw C();
192
- return p(e, r, t);
192
+ return p(e, n, t);
193
193
  });
194
194
  }),
195
- advanceAccount: (t, r, i) => N(async () => {
196
- let a = structuredClone(t), o = k(a), s = E(r), c = D(i), l = await j(o, null);
197
- return e.transactional(async (e) => {
198
- s !== null && await n(e, o, s);
199
- let t = s === null || (a.cycleEndCustodyId ?? null) === null ? await d(e, o) : E(a.cycleEndCustodyId);
200
- t !== null && await n(e, o, t);
201
- let r = await h(e, _, o, l, a, {
202
- afterCustodyId: s,
195
+ advanceAccount: (e, t, n) => N(async () => {
196
+ let r = structuredClone(e), a = k(r), o = E(t), c = D(n), l = await j(a, null);
197
+ return i.transactional(async (e) => {
198
+ o !== null && await s(e, a, o);
199
+ let t = o === null || (r.cycleEndCustodyId ?? null) === null ? await d(e, a) : E(r.cycleEndCustodyId);
200
+ t !== null && await s(e, a, t);
201
+ let n = await h(e, _, a, l, r, {
202
+ afterCustodyId: o,
203
203
  cycleEndCustodyId: t
204
204
  }, c);
205
- return r === null ? null : p(e, o, r);
205
+ return n === null ? null : p(e, a, n);
206
206
  });
207
207
  }),
208
- customer: (n, r) => N(async () => {
209
- let i = structuredClone(n), a = D(r);
210
- return e.transactional(async (e) => {
211
- let n = await t(e, i), r = await j(n, n.id);
208
+ customer: (e, t) => N(async () => {
209
+ let n = structuredClone(e), r = D(t);
210
+ return i.transactional(async (e) => {
211
+ let t = await a(e, n), i = await j(t, t.id);
212
212
  await e.insertIgnore(v, {
213
213
  id: T(v, "bihdispatch"),
214
- ...A(n),
215
- tenantId: n.tenantId,
216
- providerCustomerId: n.providerCustomerId,
217
- custodyId: n.id,
218
- identityKey: r,
214
+ ...A(t),
215
+ tenantId: t.tenantId,
216
+ providerCustomerId: t.providerCustomerId,
217
+ custodyId: t.id,
218
+ identityKey: i,
219
219
  revision: 1,
220
220
  walkId: null,
221
221
  afterUnpreparedId: null,
222
222
  afterReadyId: null,
223
223
  afterWorkingId: null,
224
224
  afterObservedId: null,
225
- updatedAt: a
225
+ updatedAt: r
226
226
  }, { conflictColumns: ["identityKey"] });
227
- let o = await f(e, v, n, r);
227
+ let o = await f(e, v, t, i);
228
228
  if (o === null) throw C();
229
- return m(e, n, o);
229
+ return m(e, t, o);
230
230
  });
231
231
  }),
232
- advanceCustomer: (n, r, i) => N(async () => {
233
- let a = structuredClone(n), o = structuredClone(r), s = D(i);
234
- return e.transactional(async (e) => {
235
- let n = await t(e, a.owner), r = await j(n, n.id), i = {
232
+ advanceCustomer: (e, t, n) => N(async () => {
233
+ let r = structuredClone(e), o = structuredClone(t), s = D(n);
234
+ return i.transactional(async (e) => {
235
+ let t = await a(e, r.owner), n = await j(t, t.id), i = {
236
236
  walkId: E(o.walkId),
237
237
  afterUnpreparedId: E(o.cursors.unprepared),
238
238
  afterReadyId: E(o.cursors.ready),
239
239
  afterWorkingId: E(o.cursors.working),
240
240
  afterObservedId: E(o.cursors.observed)
241
241
  };
242
- await m(e, n, {
243
- id: a.id,
244
- revision: a.revision,
245
- ...A(n),
246
- tenantId: n.tenantId,
247
- providerCustomerId: n.providerCustomerId,
248
- custodyId: n.id,
242
+ await m(e, t, {
243
+ id: r.id,
244
+ revision: r.revision,
245
+ ...A(t),
246
+ tenantId: t.tenantId,
247
+ providerCustomerId: t.providerCustomerId,
248
+ custodyId: t.id,
249
249
  ...i,
250
250
  updatedAt: s
251
251
  });
252
- let c = await h(e, v, n, r, a, i, s);
253
- return c === null ? null : m(e, n, c);
252
+ let c = await h(e, v, t, n, r, i, s);
253
+ return c === null ? null : m(e, t, c);
254
254
  });
255
255
  })
256
256
  };