@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,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
@@ -16,7 +16,8 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
16
16
  }), b = m(_, {
17
17
  id: f({ prefix: "bihscan" }),
18
18
  ...y(),
19
- afterCustodyId: h().maxLength(191).nullable()
19
+ afterCustodyId: h().maxLength(191).nullable(),
20
+ cycleEndCustodyId: h().maxLength(191).nullable()
20
21
  }).unique("byBillingHistoryScanAccount", ["identityKey"]), x = m(v, {
21
22
  id: f({ prefix: "bihdispatch" }),
22
23
  ...y(),
@@ -26,13 +27,14 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
26
27
  walkId: h().maxLength(191).nullable(),
27
28
  afterUnpreparedId: h().maxLength(191).nullable(),
28
29
  afterReadyId: h().maxLength(191).nullable(),
29
- afterWorkingId: h().maxLength(191).nullable()
30
- }).unique("byBillingHistoryDispatchCustody", ["identityKey"]).index("byBillingHistoryDispatchTenant", ["tenantId"]), S = 2147483647, C = () => new e({
30
+ afterWorkingId: h().maxLength(191).nullable(),
31
+ afterObservedId: h().maxLength(191).nullable()
32
+ }).unique("byBillingHistoryDispatchCustody", ["identityKey"]).index("byBillingHistoryDispatchTenant", ["tenantId"]), S = 2147483647, C = () => new s({
31
33
  source: "invoiceHistoryProgress",
32
34
  message: "Invoice-history traversal requires fresh owned native state.",
33
35
  transient: !0
34
36
  }), w = (e, t = 191) => {
35
- if (!n(e) || e.length > t || !e.isWellFormed()) throw C();
37
+ if (!a(e) || e.length > t || !e.isWellFormed()) throw C();
36
38
  return e;
37
39
  }, T = (e, t) => w(d({
38
40
  kind: "typeid",
@@ -45,10 +47,10 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
45
47
  if (typeof e != "number" || !Number.isInteger(e) || e < 1 || e > S) throw C();
46
48
  return e;
47
49
  }, k = (e) => {
48
- let n = t(e.account);
49
- return w(n.providerAccountId), {
50
+ let t = i(e.account);
51
+ return w(t.providerAccountId), {
50
52
  provider: w(e.provider, 64),
51
- account: n
53
+ account: t
52
54
  };
53
55
  }, A = (e) => ({
54
56
  provider: e.provider,
@@ -68,81 +70,95 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
68
70
  skip: void 0,
69
71
  projection: void 0,
70
72
  ...n ? { lock: "update" } : {}
71
- }), N = (t) => c.tryPromise({
72
- try: t,
73
- catch: (t) => t instanceof e ? t : C()
73
+ }), N = (e) => c.tryPromise({
74
+ try: e,
75
+ catch: (e) => e instanceof s ? e : C()
74
76
  }), P = [
75
77
  "unprepared",
76
78
  "ready",
77
- "working"
78
- ], F = (e) => {
79
- let t = async (e, t) => {
80
- 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));
81
- if (l === null || l.id !== i || l.tenantId !== o || l.provider !== n.provider || !r(l.account, n.account)) throw C();
79
+ "working",
80
+ "observed"
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();
82
85
  return l;
83
- }, n = async (e, t, n) => {
84
- let r = await e.query({
85
- ...M(i, n),
86
- 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))
87
90
  });
88
- if (r.length !== 1 || r[0]?.id !== n) throw C();
89
- let o = await c.runPromise(a(e).getByProviderCustomerId(w(r[0].providerCustomerId), t));
90
- if (o === null || o.id !== n || o.tenantId !== w(r[0].tenantId)) throw C();
91
- }, d = async (e, t, n, r, i = !0) => {
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
+ order: [{
99
+ column: "id",
100
+ direction: "desc"
101
+ }],
102
+ take: 1
103
+ }), i = r[0] === void 0 ? null : w(r[0].id);
104
+ return i !== null && await s(t, n, i), i;
105
+ }, f = async (e, t, n, r, i = !0) => {
92
106
  let a = await e.query(M(t, r, i));
93
107
  if (a.length > 1) throw C();
94
108
  let o = a[0];
95
109
  if (!o) return null;
96
110
  if (o.identityKey !== r || o.provider !== n.provider || o.providerAccountId !== n.account.providerAccountId || o.livemode !== +!!n.account.livemode) throw C();
97
111
  return w(o.id), O(o.revision), D(o.updatedAt), o;
98
- }, f = async (e, t, r) => {
99
- let i = E(r.afterCustodyId);
100
- return i !== null && await n(e, t, i), {
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), {
101
115
  ...t,
102
- id: w(r.id),
103
- revision: O(r.revision),
104
- afterCustodyId: i,
105
- 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)
106
121
  };
107
- }, p = async (e, t, n) => {
108
- if (n.tenantId !== t.tenantId || n.providerCustomerId !== t.providerCustomerId || n.custodyId !== t.id) throw C();
109
- let r = E(n.walkId), i = {
110
- unprepared: E(n.afterUnpreparedId),
111
- ready: E(n.afterReadyId),
112
- working: E(n.afterWorkingId)
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)
113
129
  };
114
- if (r === null && P.some((e) => i[e] !== null)) throw C();
115
- if (r !== null) {
116
- let n = o(e), a = await c.runPromise(n.read(t));
117
- if (a === null || a.id !== r) throw C();
118
- for (let a of P) {
119
- let o = i[a];
120
- if (o === null) continue;
121
- let l = o;
122
- 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") {
123
139
  let n = await e.query({
124
- ...M(s, o),
125
- predicate: u("id", o)
140
+ ...M(r, s),
141
+ predicate: u("id", s)
126
142
  });
127
- if (n.length !== 1 || n[0]?.id !== o) throw C();
143
+ if (n.length !== 1 || n[0]?.id !== s) throw C();
128
144
  let i = n[0];
129
- 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();
130
146
  l = w(i.referenceId);
131
147
  }
132
- let d = await c.runPromise(n.reference(t, l));
133
- 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();
134
150
  }
135
151
  }
136
152
  return {
137
- id: w(n.id),
138
- revision: O(n.revision),
153
+ id: w(i.id),
154
+ revision: O(i.revision),
139
155
  owner: t,
140
- walkId: r,
141
- cursors: i,
142
- updatedAt: D(n.updatedAt)
156
+ walkId: a,
157
+ cursors: o,
158
+ updatedAt: D(i.updatedAt)
143
159
  };
144
- }, m = async (e, t, n, r, i, a, o) => {
145
- let s = await d(e, t, n, r);
160
+ }, h = async (e, t, n, r, i, a, o) => {
161
+ let s = await f(e, t, n, r);
146
162
  if (s === null || s.id !== w(i.id) || s.revision !== O(i.revision)) return null;
147
163
  if (Object.entries(a).every(([e, t]) => t instanceof Date ? D(s[e]).getTime() === t.getTime() : s[e] === t)) return s;
148
164
  if (i.revision >= S) throw C();
@@ -150,83 +166,91 @@ var _ = "_voltro_billing_invoice_history_scan_accounts", v = "_voltro_billing_in
150
166
  ...a,
151
167
  revision: i.revision + 1,
152
168
  updatedAt: o
153
- }, f = await e.updateMany(t, c, { where: l(u("id", i.id), u("identityKey", r), u("revision", i.revision)) });
154
- if (f === 0) return null;
155
- if (f !== 1) throw C();
156
- let p = await d(e, t, n, r);
169
+ }, d = await e.updateMany(t, c, { where: l(u("id", i.id), u("identityKey", r), u("revision", i.revision)) });
170
+ if (d === 0) return null;
171
+ if (d !== 1) throw C();
172
+ let p = await f(e, t, n, r);
157
173
  if (p === null || p.id !== i.id) throw C();
158
174
  for (let [e, t] of Object.entries(c)) if (t instanceof Date ? D(p[e]).getTime() !== t.getTime() : p[e] !== t) throw C();
159
175
  return p;
160
176
  };
161
177
  return {
162
- account: (t, n) => N(async () => {
163
- let r = k(t), i = D(n), a = await j(r, null);
164
- 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) => {
165
181
  await e.insertIgnore(_, {
166
182
  id: T(_, "bihscan"),
167
- ...A(r),
183
+ ...A(n),
168
184
  identityKey: a,
169
185
  revision: 1,
170
186
  afterCustodyId: null,
171
- updatedAt: i
187
+ cycleEndCustodyId: await d(e, n),
188
+ updatedAt: r
172
189
  }, { conflictColumns: ["identityKey"] });
173
- let t = await d(e, _, r, a);
190
+ let t = await f(e, _, n, a);
174
191
  if (t === null) throw C();
175
- return f(e, r, t);
192
+ return p(e, n, t);
176
193
  });
177
194
  }),
178
- advanceAccount: (t, r, i) => N(async () => {
179
- let a = structuredClone(t), o = k(a), s = E(r), c = D(i), l = await j(o, null);
180
- return e.transactional(async (e) => {
181
- s !== null && await n(e, o, s);
182
- let t = await m(e, _, o, l, a, { afterCustodyId: s }, c);
183
- return t === null ? null : f(e, o, t);
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
+ cycleEndCustodyId: t
204
+ }, c);
205
+ return n === null ? null : p(e, a, n);
184
206
  });
185
207
  }),
186
- customer: (n, r) => N(async () => {
187
- let i = structuredClone(n), a = D(r);
188
- return e.transactional(async (e) => {
189
- 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);
190
212
  await e.insertIgnore(v, {
191
213
  id: T(v, "bihdispatch"),
192
- ...A(n),
193
- tenantId: n.tenantId,
194
- providerCustomerId: n.providerCustomerId,
195
- custodyId: n.id,
196
- identityKey: r,
214
+ ...A(t),
215
+ tenantId: t.tenantId,
216
+ providerCustomerId: t.providerCustomerId,
217
+ custodyId: t.id,
218
+ identityKey: i,
197
219
  revision: 1,
198
220
  walkId: null,
199
221
  afterUnpreparedId: null,
200
222
  afterReadyId: null,
201
223
  afterWorkingId: null,
202
- updatedAt: a
224
+ afterObservedId: null,
225
+ updatedAt: r
203
226
  }, { conflictColumns: ["identityKey"] });
204
- let o = await d(e, v, n, r);
227
+ let o = await f(e, v, t, i);
205
228
  if (o === null) throw C();
206
- return p(e, n, o);
229
+ return m(e, t, o);
207
230
  });
208
231
  }),
209
- advanceCustomer: (n, r, i) => N(async () => {
210
- let a = structuredClone(n), o = structuredClone(r), s = D(i);
211
- return e.transactional(async (e) => {
212
- 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 = {
213
236
  walkId: E(o.walkId),
214
237
  afterUnpreparedId: E(o.cursors.unprepared),
215
238
  afterReadyId: E(o.cursors.ready),
216
- afterWorkingId: E(o.cursors.working)
239
+ afterWorkingId: E(o.cursors.working),
240
+ afterObservedId: E(o.cursors.observed)
217
241
  };
218
- await p(e, n, {
219
- id: a.id,
220
- revision: a.revision,
221
- ...A(n),
222
- tenantId: n.tenantId,
223
- providerCustomerId: n.providerCustomerId,
224
- 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,
225
249
  ...i,
226
250
  updatedAt: s
227
251
  });
228
- let c = await m(e, v, n, r, a, i, s);
229
- return c === null ? null : p(e, n, c);
252
+ let c = await h(e, v, t, n, r, i, s);
253
+ return c === null ? null : m(e, t, c);
230
254
  });
231
255
  })
232
256
  };