@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 { dataStoreInvoiceStore as t } from "./invoiceStore.js";
3
- import { captureProviderAccount as n, exactProviderIdentifier as r, sameProviderAccount as i } from "./providerIdentity.js";
4
- import { BILLING_INVOICE_HISTORY_REFERENCES_TABLE as a, invoiceHistoryJournalInTransaction as o } from "./invoiceHistoryJournal.js";
1
+ import { BILLING_INVOICE_HISTORY_REFERENCES_TABLE as e, invoiceHistoryJournalInTransaction as t } from "./invoiceHistoryJournal.js";
2
+ import { dataStoreInvoiceStore as n } from "./invoiceStore.js";
3
+ import { captureProviderAccount as r, exactProviderIdentifier as i, sameProviderAccount as a } from "./providerIdentity.js";
4
+ import { BillingError as o } from "./types.js";
5
5
  import { Effect as s, Schema as c } from "effect";
6
6
  import { and as l, eq as u, gt as d, id as f, inSet as p, integer as m, lte as h, notInSubquery as g, table as _, text as v, timestamp as y } from "@voltro/database";
7
7
  import { dataStoreIdempotencyStore as b } from "@voltro/runtime";
@@ -32,12 +32,12 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
32
32
  "walkId",
33
33
  "state",
34
34
  "nextAttemptAt"
35
- ]).index("byBillingInvoiceHistoryWorkTenant", ["tenantId"]), k = 2147483647, A = 6e4, j = 864e5, M = E("billing.invoice-history-work", []), N = (t = "Invoice reference work remains unconfirmed and requires reconciliation.", n = !0) => new e({
35
+ ]).index("byBillingInvoiceHistoryWorkTenant", ["tenantId"]), k = 2147483647, A = 6e4, j = 864e5, M = E("billing.invoice-history-work", []), N = (e = "Invoice reference work remains unconfirmed and requires reconciliation.", t = !0) => new o({
36
36
  source: "invoiceHistoryWork",
37
- message: t,
38
- transient: n
37
+ message: e,
38
+ transient: t
39
39
  }), P = (e, t = 191) => {
40
- if (!r(e) || e.length > t || !e.isWellFormed()) throw N();
40
+ if (!i(e) || e.length > t || !e.isWellFormed()) throw N();
41
41
  return e;
42
42
  }, F = (e, t = 0) => {
43
43
  if (typeof e != "number" || !Number.isInteger(e) || e < t || e > k) throw N();
@@ -46,9 +46,9 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
46
46
  let t = e instanceof Date ? new Date(e) : new Date(typeof e == "string" ? e : NaN);
47
47
  if (!Number.isFinite(t.getTime())) throw N();
48
48
  return t;
49
- }, L = (e, t) => I(new Date(e.getTime() + t)), R = (t) => s.tryPromise({
50
- try: t,
51
- catch: (t) => t instanceof e ? t : N()
49
+ }, L = (e, t) => I(new Date(e.getTime() + t)), R = (e) => s.tryPromise({
50
+ try: e,
51
+ catch: (e) => e instanceof o ? e : N()
52
52
  }), z = (e, t = !1) => ({
53
53
  table: D,
54
54
  predicate: u("referenceId", e),
@@ -75,20 +75,20 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
75
75
  providerInvoiceId: e.providerInvoiceId
76
76
  }), H = (e, t) => {
77
77
  if (e.livemode !== 0 && e.livemode !== 1) throw N();
78
- let r = n({
78
+ let n = r({
79
79
  providerAccountId: P(e.providerAccountId),
80
80
  livemode: e.livemode === 1
81
81
  });
82
- if (e.tenantId !== t.tenantId || e.provider !== t.provider || e.providerCustomerId !== t.providerCustomerId || e.custodyId !== t.custodyId || e.walkId !== t.walkId || e.referenceId !== t.id || e.providerInvoiceId !== t.providerInvoiceId || !i(r, t.account)) throw N();
83
- let a = e.state, o = e.lastOutcome, s = e.activeClaimId;
84
- if (a !== "queued" && a !== "working" && a !== "retry" && a !== "deferred" && a !== "observed" || o !== "unconfirmed" && o !== "open" && o !== "observed") throw N();
82
+ if (e.tenantId !== t.tenantId || e.provider !== t.provider || e.providerCustomerId !== t.providerCustomerId || e.custodyId !== t.custodyId || e.walkId !== t.walkId || e.referenceId !== t.id || e.providerInvoiceId !== t.providerInvoiceId || !a(n, t.account)) throw N();
83
+ let i = e.state, o = e.lastOutcome, s = e.activeClaimId;
84
+ if (i !== "queued" && i !== "working" && i !== "retry" && i !== "deferred" && i !== "observed" || o !== "unconfirmed" && o !== "open" && o !== "observed") throw N();
85
85
  let c = e.observationId === null ? null : P(e.observationId), l = e.observationRevision === null ? null : F(e.observationRevision, 1);
86
- if (c === null != (l === null) || a === "working" != (s !== null) || (a === "observed" || a === "deferred") && c === null || a === "observed" && o !== "observed" || a === "deferred" && o !== "open") throw N();
86
+ if (c === null != (l === null) || i === "working" != (s !== null) || (i === "observed" || i === "deferred") && c === null || i === "observed" && o !== "observed" || i === "deferred" && o !== "open") throw N();
87
87
  let u = {
88
88
  id: P(e.id),
89
89
  tenantId: t.tenantId,
90
90
  provider: t.provider,
91
- account: r,
91
+ account: n,
92
92
  providerCustomerId: t.providerCustomerId,
93
93
  custodyId: t.custodyId,
94
94
  walkId: t.walkId,
@@ -97,7 +97,7 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
97
97
  revision: F(e.revision, 1),
98
98
  attempt: F(e.attempt),
99
99
  executions: F(e.executions),
100
- state: a,
100
+ state: i,
101
101
  activeClaimId: s === null ? null : P(s),
102
102
  nextAttemptAt: I(e.nextAttemptAt),
103
103
  lastOutcome: o,
@@ -105,7 +105,7 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
105
105
  observationRevision: l,
106
106
  updatedAt: I(e.updatedAt)
107
107
  };
108
- if (a === "working" && (u.attempt === 0 || u.executions === 0)) throw N();
108
+ if (i === "working" && (u.attempt === 0 || u.executions === 0)) throw N();
109
109
  return u;
110
110
  }, U = (e) => "billing.invoice-history-work.v1:" + e.walkId, W = (e, t) => e.id + ":" + t, G = async (e, t) => (await T(c.Unknown, [
111
111
  e.id,
@@ -131,72 +131,72 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
131
131
  work: Object.freeze(t),
132
132
  claim: n
133
133
  });
134
- }, q = async (e, n) => {
135
- let r = t(e), i = V(n), a = await s.runPromise(r.currentPaymentEpoch(i, { forUpdate: !0 }));
134
+ }, q = async (e, t) => {
135
+ let r = n(e), i = V(t), a = await s.runPromise(r.currentPaymentEpoch(i, { forUpdate: !0 }));
136
136
  if (a === null) return null;
137
137
  let o = await s.runPromise(r.get(i, { forUpdate: !0 }));
138
138
  return o?.paymentEpoch?.headId === a.headId && o.paymentEpoch.epoch === a.epoch ? o : null;
139
- }, J = (e) => {
140
- let t = b(e), n = async (e, t, n) => {
141
- let r = await s.runPromise(o(e).reference(t, n));
142
- if (r === null) throw N("The original owned invoice history reference is missing.", !1);
143
- return r;
144
- }, r = async (e, t, n = !1) => {
139
+ }, J = (n) => {
140
+ let r = b(n), i = async (e, n, r) => {
141
+ let i = await s.runPromise(t(e).reference(n, r));
142
+ if (i === null) throw N("The original owned invoice history reference is missing.", !1);
143
+ return i;
144
+ }, a = async (e, t, n = !1) => {
145
145
  let r = await e.query(z(t.id, n));
146
146
  if (r.length > 1) throw N();
147
147
  return r[0] ? H(r[0], t) : null;
148
- }, i = async (e, t, n, i, a) => {
148
+ }, o = async (e, t, n, r, i) => {
149
149
  if (n.revision >= k) throw N("The invoice reference work revision is exhausted.", !1);
150
150
  if (await e.updateMany("_voltro_billing_invoice_history_work", {
151
- ...i,
151
+ ...r,
152
152
  revision: n.revision + 1,
153
- updatedAt: a
153
+ updatedAt: i
154
154
  }, { where: l(u("id", n.id), u("referenceId", t.id), u("revision", n.revision), u("custodyId", t.custodyId), u("walkId", t.walkId)) }) !== 1) throw N();
155
- let o = await r(e, t, !0);
156
- if (o === null || o.id !== n.id || o.revision !== n.revision + 1 || o.updatedAt.getTime() !== a.getTime()) throw N();
157
- for (let [e, t] of Object.entries(i)) {
155
+ let o = await a(e, t, !0);
156
+ if (o === null || o.id !== n.id || o.revision !== n.revision + 1 || o.updatedAt.getTime() !== i.getTime()) throw N();
157
+ for (let [e, t] of Object.entries(r)) {
158
158
  let n = Object.getOwnPropertyDescriptor(o, e)?.value;
159
159
  if (t instanceof Date ? !(n instanceof Date) || n.getTime() !== t.getTime() : n !== t) throw N("The invoice work acknowledgement was not read back exactly.");
160
160
  }
161
161
  return o;
162
162
  }, c = async (e, t, n) => {
163
- let i = await r(e, t, !0), a = n.work;
164
- return i === null || i.id !== a.id || i.referenceId !== a.referenceId || i.state !== "working" || i.attempt !== a.attempt || i.activeClaimId !== n.claim.id || n.claim.inputHash !== await G(i, i.attempt) ? null : i;
163
+ let r = await a(e, t, !0), i = n.work;
164
+ return r === null || r.id !== i.id || r.referenceId !== i.referenceId || r.state !== "working" || r.attempt !== i.attempt || r.activeClaimId !== n.claim.id || n.claim.inputHash !== await G(r, r.attempt) ? null : r;
165
165
  };
166
166
  return {
167
- pending: (t, r, i, c) => R(async () => {
168
- let f = structuredClone(t), m = I(i), _ = c?.limit ?? 25;
167
+ pending: (r, a, o, c) => R(async () => {
168
+ let f = structuredClone(r), m = I(o), _ = c?.limit ?? 25;
169
169
  if (![
170
170
  "unprepared",
171
171
  "ready",
172
172
  "working",
173
173
  "observed"
174
- ].includes(r) || !Number.isInteger(_) || _ < 1 || _ > 100) throw N();
174
+ ].includes(a) || !Number.isInteger(_) || _ < 1 || _ > 100) throw N();
175
175
  let v = c?.cursor === void 0 ? null : {
176
176
  lane: c.cursor.lane,
177
177
  walkId: P(c.cursor.walkId),
178
178
  afterId: P(c.cursor.afterId)
179
179
  };
180
- if (v !== null && v.lane !== r) throw N("Invoice work cursors belong to one selection lane.", !1);
181
- return e.transactional(async (e) => {
182
- let t = o(e), i = await s.runPromise(t.read(f));
183
- if (i === null) throw N("No retained owned invoice history walk exists.");
184
- let c = await s.runPromise(t.begin(f));
185
- if (c.id !== i.id || v !== null && v.walkId !== c.id) throw N("Invoice work cursor differs from the retained walk.", !1);
186
- let y = l(u("tenantId", c.tenantId), u("provider", c.provider), u("providerAccountId", c.account.providerAccountId), u("livemode", +!!c.account.livemode), u("providerCustomerId", c.providerCustomerId), u("custodyId", c.custodyId), u("walkId", c.id)), b = r === "unprepared", x = r === "working" ? ["working"] : r === "observed" ? ["observed"] : [
180
+ if (v !== null && v.lane !== a) throw N("Invoice work cursors belong to one selection lane.", !1);
181
+ return n.transactional(async (n) => {
182
+ let r = t(n), o = await s.runPromise(r.read(f));
183
+ if (o === null) throw N("No retained owned invoice history walk exists.");
184
+ let c = await s.runPromise(r.begin(f));
185
+ if (c.id !== o.id || v !== null && v.walkId !== c.id) throw N("Invoice work cursor differs from the retained walk.", !1);
186
+ let y = l(u("tenantId", c.tenantId), u("provider", c.provider), u("providerAccountId", c.account.providerAccountId), u("livemode", +!!c.account.livemode), u("providerCustomerId", c.providerCustomerId), u("custodyId", c.custodyId), u("walkId", c.id)), b = a === "unprepared", x = a === "working" ? ["working"] : a === "observed" ? ["observed"] : [
187
187
  "queued",
188
188
  "retry",
189
189
  "deferred"
190
- ], S = b ? a : D;
190
+ ], S = b ? e : D;
191
191
  if (v !== null) {
192
- let t = await e.query({
192
+ let e = await n.query({
193
193
  ...z(v.afterId),
194
194
  table: S,
195
195
  predicate: l(y, u("id", v.afterId))
196
196
  });
197
- if (t.length !== 1 || t[0]?.id !== v.afterId) throw N("Invoice work cursor is no longer retained.", !0);
198
- let r = await n(e, f, b ? v.afterId : P(t[0]?.referenceId));
199
- b || H(t[0], r);
197
+ if (e.length !== 1 || e[0]?.id !== v.afterId) throw N("Invoice work cursor is no longer retained.", !0);
198
+ let t = await i(n, f, b ? v.afterId : P(e[0]?.referenceId));
199
+ b || H(e[0], t);
200
200
  }
201
201
  let C = [y];
202
202
  v !== null && C.push(d("id", v.afterId)), b ? C.push(h("pageOrdinal", c.pageOrdinal), g("id", {
@@ -205,7 +205,7 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
205
205
  projection: ["referenceId"],
206
206
  take: void 0
207
207
  })) : C.push(h("nextAttemptAt", m), p("state", x));
208
- let w = await e.query({
208
+ let w = await n.query({
209
209
  ...z(c.id),
210
210
  table: S,
211
211
  predicate: l(...C),
@@ -216,8 +216,8 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
216
216
  take: _ + 1
217
217
  });
218
218
  if (w.length > _ + 1) throw N();
219
- let T = await s.runPromise(t.referencesByIds(f, w.map((e) => P(b ? e.id : e.referenceId))));
220
- if (b && w.length > 0 && (await e.query({
219
+ let T = await s.runPromise(r.referencesByIds(f, w.map((e) => P(b ? e.id : e.referenceId))));
220
+ if (b && w.length > 0 && (await n.query({
221
221
  ...z(c.id),
222
222
  predicate: p("referenceId", T.map((e) => e.id)),
223
223
  take: 1
@@ -239,7 +239,7 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
239
239
  return {
240
240
  entries: O.slice(0, _),
241
241
  nextCursor: w.length > _ ? {
242
- lane: r,
242
+ lane: a,
243
243
  walkId: c.id,
244
244
  afterId: P(w[_ - 1]?.id)
245
245
  } : null,
@@ -247,10 +247,10 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
247
247
  };
248
248
  });
249
249
  }),
250
- claim: (t, a, o) => R(async () => {
251
- let s = structuredClone(t), c = P(a), l = I(o);
252
- return e.transactional(async (e) => {
253
- let t = await n(e, s, c);
250
+ claim: (e, t, r) => R(async () => {
251
+ let s = structuredClone(e), c = P(t), l = I(r);
252
+ return n.transactional(async (e) => {
253
+ let t = await i(e, s, c);
254
254
  await e.insertIgnore(D, {
255
255
  id: "bihwork_" + globalThis.crypto.randomUUID(),
256
256
  ...B(t),
@@ -265,89 +265,89 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
265
265
  observationRevision: null,
266
266
  updatedAt: l
267
267
  }, { conflictColumns: ["referenceId"] });
268
- let a = await r(e, t, !0);
269
- if (a === null) throw N();
270
- if (a.state === "observed") {
271
- let n = await q(e, t);
272
- if (n !== null && n.id === a.observationId && n.revision >= a.observationRevision && n.snapshot.status !== "open") return {
268
+ let n = await a(e, t, !0);
269
+ if (n === null) throw N();
270
+ if (n.state === "observed") {
271
+ let r = await q(e, t);
272
+ if (r !== null && r.id === n.observationId && r.revision >= n.observationRevision && r.snapshot.status !== "open") return {
273
273
  kind: "observed",
274
- work: a
274
+ work: n
275
275
  };
276
- a = await i(e, t, a, {
276
+ n = await o(e, t, n, {
277
277
  state: "queued",
278
278
  activeClaimId: null,
279
279
  lastOutcome: "unconfirmed",
280
280
  nextAttemptAt: l
281
281
  }, l);
282
282
  }
283
- if (a.state !== "working" && a.nextAttemptAt.getTime() > l.getTime()) return {
283
+ if (n.state !== "working" && n.nextAttemptAt.getTime() > l.getTime()) return {
284
284
  kind: "busy",
285
- work: a
285
+ work: n
286
286
  };
287
- let o = a.state === "working" ? a.attempt : a.attempt + 1;
288
- if (o > k || a.executions >= k) throw N("The invoice reference attempt counter is exhausted.", !1);
289
- let u = await G(a, o), d = await S(b(e), U(a), W(a, o), u, j, l.getTime());
290
- if (d.kind === "replay") return a = await i(e, t, a, {
291
- attempt: o,
287
+ let r = n.state === "working" ? n.attempt : n.attempt + 1;
288
+ if (r > k || n.executions >= k) throw N("The invoice reference attempt counter is exhausted.", !1);
289
+ let u = await G(n, r), d = await S(b(e), U(n), W(n, r), u, j, l.getTime());
290
+ if (d.kind === "replay") return n = await o(e, t, n, {
291
+ attempt: r,
292
292
  state: "queued",
293
293
  activeClaimId: null,
294
294
  nextAttemptAt: l,
295
295
  lastOutcome: "unconfirmed"
296
296
  }, l), {
297
297
  kind: "busy",
298
- work: a
298
+ work: n
299
299
  };
300
300
  if (d.kind === "conflict") return {
301
301
  kind: "busy",
302
- work: a
302
+ work: n
303
303
  };
304
304
  if (d.kind !== "fresh") throw N("The retained invoice attempt has a different input binding.", !1);
305
- return a = await i(e, t, a, {
306
- attempt: o,
307
- executions: a.executions + 1,
305
+ return n = await o(e, t, n, {
306
+ attempt: r,
307
+ executions: n.executions + 1,
308
308
  state: "working",
309
309
  activeClaimId: d.claim.id,
310
310
  nextAttemptAt: L(l, x),
311
311
  lastOutcome: "unconfirmed"
312
312
  }, l), {
313
313
  kind: "claimed",
314
- work: a,
314
+ work: n,
315
315
  attempt: Object.freeze({
316
- work: a,
316
+ work: n,
317
317
  claim: d.claim
318
318
  })
319
319
  };
320
320
  });
321
321
  }),
322
- observed: (t, r, a, o) => R(async () => {
323
- let s = structuredClone(t), l = K(r), u = I(o), d = P(a.id), f = F(a.revision, 1);
324
- return e.transactional(async (e) => {
325
- let t = await n(e, s, l.work.referenceId), r = await c(e, t, l);
326
- if (r === null) return null;
327
- let a = await q(e, t);
328
- if (a === null || a.id !== d || a.revision < f) throw N("The qualified native invoice observation is missing or older than the offered acknowledgement.");
329
- let o = a.snapshot.status === "open";
322
+ observed: (e, t, r, a) => R(async () => {
323
+ let s = structuredClone(e), l = K(t), u = I(a), d = P(r.id), f = F(r.revision, 1);
324
+ return n.transactional(async (e) => {
325
+ let t = await i(e, s, l.work.referenceId), n = await c(e, t, l);
326
+ if (n === null) return null;
327
+ let r = await q(e, t);
328
+ if (r === null || r.id !== d || r.revision < f) throw N("The qualified native invoice observation is missing or older than the offered acknowledgement.");
329
+ let a = r.snapshot.status === "open";
330
330
  return await C(b(e), l.claim, {
331
- status: o ? 202 : 204,
331
+ status: a ? 202 : 204,
332
332
  body: null
333
- }, u.getTime()) ? i(e, t, r, {
334
- state: o ? "deferred" : "observed",
333
+ }, u.getTime()) ? o(e, t, n, {
334
+ state: a ? "deferred" : "observed",
335
335
  activeClaimId: null,
336
- observationId: a.id,
337
- observationRevision: a.revision,
338
- lastOutcome: o ? "open" : "observed",
339
- nextAttemptAt: o ? L(u, A) : u
336
+ observationId: r.id,
337
+ observationRevision: r.revision,
338
+ lastOutcome: a ? "open" : "observed",
339
+ nextAttemptAt: a ? L(u, A) : u
340
340
  }, u) : null;
341
341
  });
342
342
  }),
343
- failed: (t, r, a) => R(async () => {
344
- let o = structuredClone(t), s = K(r), l = I(a);
345
- return e.transactional(async (e) => {
346
- let t = await n(e, o, s.work.referenceId), r = await c(e, t, s);
347
- return r === null || !await C(b(e), s.claim, {
343
+ failed: (e, t, r) => R(async () => {
344
+ let a = structuredClone(e), s = K(t), l = I(r);
345
+ return n.transactional(async (e) => {
346
+ let t = await i(e, a, s.work.referenceId), n = await c(e, t, s);
347
+ return n === null || !await C(b(e), s.claim, {
348
348
  status: 503,
349
349
  body: null
350
- }, l.getTime()) ? null : i(e, t, r, {
350
+ }, l.getTime()) ? null : o(e, t, n, {
351
351
  state: "retry",
352
352
  activeClaimId: null,
353
353
  lastOutcome: "unconfirmed",
@@ -355,9 +355,9 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
355
355
  }, l);
356
356
  });
357
357
  }),
358
- hold: (e, n) => {
359
- let r = K(e);
360
- return w(t, r.claim, x, n);
358
+ hold: (e, t) => {
359
+ let n = K(e);
360
+ return w(r, n.claim, x, t);
361
361
  }
362
362
  };
363
363
  };
@@ -1,13 +1,13 @@
1
- import { BillingError as e } from "./types.js";
2
- import { InvoicePaymentObservationSchema as t, assertInvoicePaymentObservation as n } from "./invoicePaymentObservation.js";
1
+ import { InvoicePaymentObservationSchema as e, assertInvoicePaymentObservation as t } from "./invoicePaymentObservation.js";
2
+ import { BillingError as n } from "./types.js";
3
3
  import { Schema as r } from "effect";
4
4
  // src/invoiceSnapshot.ts
5
- var i = (t, n = !1) => new e({
5
+ var i = (e, t = !1) => new n({
6
6
  source: "invoiceSnapshot",
7
- message: t,
8
- transient: n
7
+ message: e,
8
+ transient: t
9
9
  }), a = (e) => typeof e == "string" && e.length > 0 && e.trim() === e, o = (e) => e === null || a(e), s = (e) => e instanceof Date && Number.isSafeInteger(e.getTime()), c = (e) => Number.isSafeInteger(e) && e >= 0, l = r.Struct({
10
- paymentObservation: r.optionalKey(r.NullOr(t)),
10
+ paymentObservation: r.optionalKey(r.NullOr(e)),
11
11
  providerInvoiceId: r.String,
12
12
  providerCustomerId: r.String,
13
13
  providerSubscriptionId: r.NullOr(r.String),
@@ -62,14 +62,14 @@ var i = (t, n = !1) => new e({
62
62
  "void"
63
63
  ].includes(e.status) || typeof e.livemode != "boolean" || !/^[a-z]{3}$/.test(e.currency) || !c(e.amountDueMinor) || !c(e.amountPaidMinor) || !c(e.amountRemainingMinor) || !s(e.finalizedAt) || !s(e.observedAt) || e.paidAt !== null && (!s(e.paidAt) || e.paidAt < e.finalizedAt) || e.status === "paid" && e.paidAt === null || !Array.isArray(e.lines) || e.lines.length > 1e4) throw i("A complete finalized invoice observation is required.");
64
64
  if (e.paymentObservation !== null) try {
65
- if (n(e.paymentObservation), e.paymentObservation.providerInvoiceId !== e.providerInvoiceId || e.paymentObservation.providerCustomerId !== e.providerCustomerId || e.paymentObservation.currency !== e.currency || e.paymentObservation.livemode !== e.livemode) throw TypeError("Invoice payment identity mismatch.");
65
+ if (t(e.paymentObservation), e.paymentObservation.providerInvoiceId !== e.providerInvoiceId || e.paymentObservation.providerCustomerId !== e.providerCustomerId || e.paymentObservation.currency !== e.currency || e.paymentObservation.livemode !== e.livemode) throw TypeError("Invoice payment identity mismatch.");
66
66
  } catch {
67
67
  throw i("Invoice payment observations must match the complete invoice ownership.");
68
68
  }
69
- let t = /* @__PURE__ */ new Set();
70
- for (let n of e.lines) {
71
- if (!a(n.providerLineId) || t.has(n.providerLineId) || !o(n.providerSubscriptionId) || !o(n.providerSubscriptionItemId) || !o(n.providerPriceId) || !Number.isSafeInteger(n.amountMinor) || n.currency !== e.currency || n.quantityDecimal !== null && (typeof n.quantityDecimal != "string" || !/^\d+(?:\.\d{1,12})?$/.test(n.quantityDecimal)) || !s(n.periodStart) || !s(n.periodEnd) || n.periodEnd < n.periodStart || n.proration !== null && typeof n.proration != "boolean") throw i("Invoice lines must retain exact identities, money and periods.");
72
- t.add(n.providerLineId);
69
+ let n = /* @__PURE__ */ new Set();
70
+ for (let t of e.lines) {
71
+ if (!a(t.providerLineId) || n.has(t.providerLineId) || !o(t.providerSubscriptionId) || !o(t.providerSubscriptionItemId) || !o(t.providerPriceId) || !Number.isSafeInteger(t.amountMinor) || t.currency !== e.currency || t.quantityDecimal !== null && (typeof t.quantityDecimal != "string" || !/^\d+(?:\.\d{1,12})?$/.test(t.quantityDecimal)) || !s(t.periodStart) || !s(t.periodEnd) || t.periodEnd < t.periodStart || t.proration !== null && typeof t.proration != "boolean") throw i("Invoice lines must retain exact identities, money and periods.");
72
+ n.add(t.providerLineId);
73
73
  }
74
74
  };
75
75
  export { p as assertInvoiceSnapshot, f as decodeInvoiceSnapshot, d as encodeInvoiceSnapshot, i as invoiceSnapshotError };
@@ -1,6 +1,6 @@
1
- import { BillingError as e } from "./types.js";
2
- import { captureInvoicePaymentEpoch as t, currentInvoicePaymentEpoch as n, invalidateInvoicePayments as r, withInvoicePaymentEpoch as i } from "./invoicePaymentEpoch.js";
3
- import { decodeInvoiceSnapshot as a, encodeInvoiceSnapshot as o, invoiceSnapshotError as s } from "./invoiceSnapshot.js";
1
+ import { captureInvoicePaymentEpoch as e, currentInvoicePaymentEpoch as t, invalidateInvoicePayments as n, withInvoicePaymentEpoch as r } from "./invoicePaymentEpoch.js";
2
+ import { decodeInvoiceSnapshot as i, encodeInvoiceSnapshot as a, invoiceSnapshotError as o } from "./invoiceSnapshot.js";
3
+ import { BillingError as s } from "./types.js";
4
4
  import { Effect as c } from "effect";
5
5
  import { and as l, eq as u, id as d, integer as f, json as p, table as m, text as h, timestamp as g } from "@voltro/database";
6
6
  import "@voltro/runtime";
@@ -42,9 +42,9 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
42
42
  take: t,
43
43
  skip: void 0,
44
44
  projection: void 0
45
- }), C = (t) => c.tryPromise({
46
- try: t,
47
- catch: (t) => t instanceof e ? t : s("The native invoice observation could not be persisted or read.", !0)
45
+ }), C = (e) => c.tryPromise({
46
+ try: e,
47
+ catch: (e) => e instanceof s ? e : o("The native invoice observation could not be persisted or read.", !0)
48
48
  }), w = (e) => {
49
49
  if (![
50
50
  e.tenantId,
@@ -52,7 +52,7 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
52
52
  e.providerAccountId,
53
53
  e.providerCustomerId,
54
54
  e.providerInvoiceId
55
- ].every(x) || typeof e.livemode != "boolean") throw s("A valid customer-bound invoice identity is required.");
55
+ ].every(x) || typeof e.livemode != "boolean") throw o("A valid customer-bound invoice identity is required.");
56
56
  return Object.freeze({
57
57
  tenantId: e.tenantId,
58
58
  provider: e.provider,
@@ -71,23 +71,23 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
71
71
  ])));
72
72
  return [...new Uint8Array(t)].map((e) => e.toString(16).padStart(2, "0")).join("");
73
73
  }, E = (e, t) => e.tenantId === t.tenantId && e.provider === t.provider && e.providerAccountId === t.providerAccountId && e.livemode === t.livemode && e.providerCustomerId === t.providerCustomerId && e.providerInvoiceId === t.providerInvoiceId, D = (e, t) => {
74
- if (!E(e, t)) throw s("The invoice is bound to a different tenant or billing customer.");
75
- }, O = (e) => {
76
- let n = e.tenantId, r = e.provider, i = e.providerInvoiceId, o = e.providerAccountId, c = e.livemode, l = e.providerCustomerId, u = e.identityKey, d = e.revision, f = e.id;
77
- if (!x(n) || !x(r) || !x(i) || !x(l) || !x(f) || !x(o) || c !== 0 && c !== 1 || typeof u != "string" || !/^[0-9a-f]{64}$/.test(u) || typeof d != "number" || !Number.isInteger(d) || d < 1 || d > b) throw s("The native invoice observation identity is invalid.");
78
- let p = a(e.providerSnapshot);
79
- if (p.providerCustomerId !== l || p.providerInvoiceId !== i || p.livemode !== (c === 1)) throw s("The stored observation has a different customer or invoice.");
80
- let m = e.paymentHeadId == null && e.paymentEpoch == null ? null : t({
81
- headId: e.paymentHeadId,
82
- epoch: e.paymentEpoch
74
+ if (!E(e, t)) throw o("The invoice is bound to a different tenant or billing customer.");
75
+ }, O = (t) => {
76
+ let n = t.tenantId, r = t.provider, a = t.providerInvoiceId, s = t.providerAccountId, c = t.livemode, l = t.providerCustomerId, u = t.identityKey, d = t.revision, f = t.id;
77
+ if (!x(n) || !x(r) || !x(a) || !x(l) || !x(f) || !x(s) || c !== 0 && c !== 1 || typeof u != "string" || !/^[0-9a-f]{64}$/.test(u) || typeof d != "number" || !Number.isInteger(d) || d < 1 || d > b) throw o("The native invoice observation identity is invalid.");
78
+ let p = i(t.providerSnapshot);
79
+ if (p.providerCustomerId !== l || p.providerInvoiceId !== a || p.livemode !== (c === 1)) throw o("The stored observation has a different customer or invoice.");
80
+ let m = t.paymentHeadId == null && t.paymentEpoch == null ? null : e({
81
+ headId: t.paymentHeadId,
82
+ epoch: t.paymentEpoch
83
83
  });
84
84
  return {
85
85
  id: f,
86
86
  tenantId: n,
87
87
  provider: r,
88
- providerAccountId: o,
88
+ providerAccountId: s,
89
89
  livemode: c === 1,
90
- providerInvoiceId: i,
90
+ providerInvoiceId: a,
91
91
  providerCustomerId: l,
92
92
  identityKey: u,
93
93
  revision: d,
@@ -99,35 +99,35 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
99
99
  ...t,
100
100
  ...w(t)
101
101
  });
102
- if (i !== null && (D(i, r), !x(i.id) || !/^[0-9a-f]{64}$/.test(i.identityKey) || !Number.isInteger(i.revision) || i.revision < 1 || i.revision >= b)) throw s("The expected native invoice revision is invalid or exhausted.");
103
- if (n.providerInvoiceId !== r.providerInvoiceId || n.providerCustomerId !== r.providerCustomerId || n.livemode !== r.livemode) throw s("The complete invoice must match the confirmed customer binding.");
102
+ if (i !== null && (D(i, r), !x(i.id) || !/^[0-9a-f]{64}$/.test(i.identityKey) || !Number.isInteger(i.revision) || i.revision < 1 || i.revision >= b)) throw o("The expected native invoice revision is invalid or exhausted.");
103
+ if (n.providerInvoiceId !== r.providerInvoiceId || n.providerCustomerId !== r.providerCustomerId || n.livemode !== r.livemode) throw o("The complete invoice must match the confirmed customer binding.");
104
104
  return {
105
105
  owner: r,
106
106
  previous: i,
107
- providerSnapshot: structuredClone(o(n))
107
+ providerSnapshot: structuredClone(a(n))
108
108
  };
109
- }, A = (e) => {
110
- let a = async (t, n = e, r = !1) => {
111
- let i = S(u("identityKey", t), 2), a = await n.query(r ? {
112
- ...i,
109
+ }, A = (i) => {
110
+ let a = async (e, t = i, n = !1) => {
111
+ let r = S(u("identityKey", e), 2), a = await t.query(n ? {
112
+ ...r,
113
113
  lock: "update"
114
- } : i);
115
- if (a.length > 1) throw s("The native invoice identity is not unique.");
116
- let o = a[0] ? O(a[0]) : null;
117
- if (o !== null && (o.identityKey !== t || await T(o) !== t)) throw s("The native invoice identity digest could not be confirmed.");
118
- return o;
114
+ } : r);
115
+ if (a.length > 1) throw o("The native invoice identity is not unique.");
116
+ let s = a[0] ? O(a[0]) : null;
117
+ if (s !== null && (s.identityKey !== e || await T(s) !== e)) throw o("The native invoice identity digest could not be confirmed.");
118
+ return s;
119
119
  };
120
120
  return {
121
- capturePaymentEpoch: (t) => C(() => i(e, t, !0, async (e, t) => t)),
122
- currentPaymentEpoch: (t, r) => C(() => n(e, t, r?.forUpdate === !0)),
123
- invalidatePayments: (t, n) => C(() => r(e, t, n)),
124
- get: (t, n) => C(async () => {
125
- let r = w(t), i = await a(await T(r), e, n?.forUpdate === !0);
126
- return i !== null && D(i, r), i;
121
+ capturePaymentEpoch: (e) => C(() => r(i, e, !0, async (e, t) => t)),
122
+ currentPaymentEpoch: (e, n) => C(() => t(i, e, n?.forUpdate === !0)),
123
+ invalidatePayments: (e, t) => C(() => n(i, e, t)),
124
+ get: (e, t) => C(async () => {
125
+ let n = w(e), r = await a(await T(n), i, t?.forUpdate === !0);
126
+ return r !== null && D(r, n), r;
127
127
  }),
128
- complete: (n, r, o, c) => C(async () => {
129
- let { owner: d, previous: f, providerSnapshot: p } = k(n, r, o), m = t(c), h = await T(d);
130
- return i(e, d, !1, async (e, t) => {
128
+ complete: (t, n, s, c) => C(async () => {
129
+ let { owner: d, previous: f, providerSnapshot: p } = k(t, n, s), m = e(c), h = await T(d);
130
+ return r(i, d, !1, async (e, t) => {
131
131
  if (t.headId !== m.headId || t.epoch !== m.epoch) return !1;
132
132
  if (f === null) {
133
133
  let t = "binvobs_" + globalThis.crypto.randomUUID();
@@ -143,10 +143,10 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
143
143
  createdAt: /* @__PURE__ */ new Date()
144
144
  }, { conflictColumns: ["identityKey"] });
145
145
  let n = await a(h, e, !0);
146
- if (n === null) throw s("The persisted invoice observation could not be confirmed.", !0);
146
+ if (n === null) throw o("The persisted invoice observation could not be confirmed.", !0);
147
147
  return D(n, d), n.id === t && (j(n, m, p), !0);
148
148
  }
149
- if (h !== f.identityKey) throw s("The expected invoice identity digest is invalid.");
149
+ if (h !== f.identityKey) throw o("The expected invoice identity digest is invalid.");
150
150
  if (await e.updateMany("_voltro_billing_account_invoices", {
151
151
  providerSnapshot: p,
152
152
  revision: f.revision + 1,
@@ -154,13 +154,13 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
154
154
  paymentEpoch: m.epoch
155
155
  }, { where: l(u("id", f.id), u("identityKey", h), u("revision", f.revision), u("tenantId", d.tenantId), u("provider", d.provider), u("providerAccountId", d.providerAccountId), u("livemode", +!!d.livemode), u("providerCustomerId", d.providerCustomerId), u("providerInvoiceId", d.providerInvoiceId)) }) !== 1) return !1;
156
156
  let n = await a(h, e, !0);
157
- if (!n || n.id !== f.id || n.revision !== f.revision + 1) throw s("The updated invoice revision could not be read back.", !0);
157
+ if (!n || n.id !== f.id || n.revision !== f.revision + 1) throw o("The updated invoice revision could not be read back.", !0);
158
158
  return D(n, d), j(n, m, p), !0;
159
159
  });
160
160
  })
161
161
  };
162
162
  }, j = (e, t, n) => {
163
163
  let r = e.paymentEpoch;
164
- if (r === null || r.headId !== t.headId || r.epoch !== t.epoch || JSON.stringify(o(e.snapshot)) !== JSON.stringify(n)) throw s("Invoice payment generation or exact observation could not be read back.", !0);
164
+ if (r === null || r.headId !== t.headId || r.epoch !== t.epoch || JSON.stringify(a(e.snapshot)) !== JSON.stringify(n)) throw o("Invoice payment generation or exact observation could not be read back.", !0);
165
165
  };
166
166
  export { _ as BILLING_INVOICE_OBSERVATIONS_TABLE, y as billingInvoiceObservationsTable, v as billingRetainedInvoiceObservationsTable, A as dataStoreInvoiceStore };