@voltro/plugin-billing 0.89.2 → 0.90.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1822 -0
- package/README.md +0 -435
- package/dist/catalog.js +175 -76
- package/dist/custodyInventory.js +101 -65
- package/dist/dunning.js +14 -6
- package/dist/eventDelivery.js +14 -5
- package/dist/historicalUsage.js +94 -0
- package/dist/index.d.ts +773 -67
- package/dist/index.js +340 -219
- package/dist/invoiceHistoryJournal.js +115 -90
- package/dist/invoiceHistoryProgress.js +49 -25
- package/dist/invoiceHistoryWork.js +162 -160
- package/dist/invoicePaymentEpoch.js +145 -0
- package/dist/invoicePaymentObservation.js +64 -0
- package/dist/invoiceSnapshot.js +52 -42
- package/dist/invoiceStore.js +122 -88
- package/dist/paidInvoiceCoverage.js +84 -0
- package/dist/plans.js +15 -2
- package/dist/providers.js +124 -67
- package/dist/provision.js +84 -73
- package/dist/rpc.d.ts +53 -13
- package/dist/rpc.js +141 -78
- package/dist/service.js +1159 -673
- package/dist/stores.js +231 -139
- package/dist/stripeInvoicePayments.js +129 -0
- package/dist/stripeInvoiceSnapshot.js +59 -40
- package/dist/stripeProvider.js +542 -173
- package/dist/subscriptionObservation.js +93 -0
- package/dist/subscriptionWriteStore.js +325 -0
- package/dist/table.js +75 -66
- package/dist/types.d.ts +415 -27
- package/dist/types.js +21 -4
- package/dist/web/index.d.ts +93 -3
- package/dist/web/index.js +76 -33
- package/dist/web/writeRequest.js +65 -0
- package/package.json +8 -8
package/dist/custodyInventory.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BillingError as e } from "./types.js";
|
|
2
2
|
import { exactProviderIdentifier as t } from "./providerIdentity.js";
|
|
3
3
|
import { Effect as n } from "effect";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { and as r, eq as i, gt as a } from "@voltro/database";
|
|
5
|
+
import { createHash as o } from "node:crypto";
|
|
6
|
+
import { canonicalJson as s } from "@voltro/protocol";
|
|
7
7
|
// src/custodyInventory.ts
|
|
8
8
|
var c = Object.freeze([
|
|
9
9
|
"_voltro_billing_customer_accounts",
|
|
@@ -22,40 +22,74 @@ var c = Object.freeze([
|
|
|
22
22
|
"_voltro_billing_usage_events",
|
|
23
23
|
"_voltro_billing_checkout_operations",
|
|
24
24
|
"_voltro_billing_close_operations",
|
|
25
|
-
"_voltro_billing_dunning_notices"
|
|
26
|
-
|
|
25
|
+
"_voltro_billing_dunning_notices",
|
|
26
|
+
"_voltro_billing_subscription_write_heads",
|
|
27
|
+
"_voltro_billing_subscription_writes",
|
|
28
|
+
"_voltro_billing_invoice_payment_heads"
|
|
29
|
+
]), l = Object.freeze(["_voltro_billing_invoice_history_scan_accounts"]), u = (r, i) => n.tryPromise({
|
|
27
30
|
try: async () => {
|
|
28
31
|
let e = i.tenantId, n = i.pageSize ?? 200, a = i.maxRowsPerTable ?? 5e3;
|
|
29
|
-
if (!t(e) || e.length > 191 || !e.isWellFormed() || !Number.isSafeInteger(n) || n < 1 || n > 500 || !Number.isSafeInteger(a) || a < 1 || a > 5e4) throw
|
|
30
|
-
if (r.integrityQuery === void 0) throw
|
|
32
|
+
if (!t(e) || e.length > 191 || !e.isWellFormed() || !Number.isSafeInteger(n) || n < 1 || n > 500 || !Number.isSafeInteger(a) || a < 1 || a > 5e4) throw x("Inventory tenant and bounds must be valid.", !1);
|
|
33
|
+
if (r.integrityQuery === void 0) throw x("Complete native integrity reads are required.", !1);
|
|
31
34
|
let o = {
|
|
32
35
|
format: "voltro.billing-custody-inventory.v1",
|
|
33
36
|
tenantId: e,
|
|
34
37
|
tables: await r.transactional(async (t) => {
|
|
35
|
-
if (t.integrityQuery === void 0) throw
|
|
36
|
-
let r = await
|
|
37
|
-
if (
|
|
38
|
+
if (t.integrityQuery === void 0) throw x("Transactional native integrity reads are required.", !1);
|
|
39
|
+
let r = await p(t, c, f(e), n, a), i = await p(t, c, f(e), n, a);
|
|
40
|
+
if (y(r) !== y(i)) throw x("Billing state changed during inventory. Quiesce writers and retry.", !0);
|
|
38
41
|
return i;
|
|
39
42
|
})
|
|
40
43
|
};
|
|
41
44
|
return Object.freeze({
|
|
42
45
|
...o,
|
|
43
|
-
digest:
|
|
46
|
+
digest: y(o),
|
|
44
47
|
observedAt: /* @__PURE__ */ new Date(),
|
|
45
48
|
purpose: "review-only"
|
|
46
49
|
});
|
|
47
50
|
},
|
|
48
|
-
catch: (t) => t instanceof e ? t :
|
|
49
|
-
}),
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
let
|
|
51
|
+
catch: (t) => t instanceof e ? t : x("The native billing inventory could not be completed.", !0)
|
|
52
|
+
}), d = (a, o) => n.tryPromise({
|
|
53
|
+
try: async () => {
|
|
54
|
+
let e = o.provider, n = o.providerAccountId, s = o.livemode, c = o.pageSize ?? 200, u = o.maxRowsPerTable ?? 5e3;
|
|
55
|
+
if (!t(e) || e.length > 64 || !e.isWellFormed() || !t(n) || n.length > 191 || !n.isWellFormed() || typeof s != "boolean" || !Number.isSafeInteger(c) || c < 1 || c > 500 || !Number.isSafeInteger(u) || u < 1 || u > 5e4) throw x("Inventory account and bounds must be valid.", !1);
|
|
56
|
+
if (a.integrityQuery === void 0) throw x("Complete native integrity reads are required.", !1);
|
|
57
|
+
let d = {
|
|
58
|
+
predicate: r(i("provider", e), i("providerAccountId", n), i("livemode", +!!s)),
|
|
59
|
+
owns: (t) => t.provider === e && t.providerAccountId === n && t.livemode === +!!s
|
|
60
|
+
}, f = {
|
|
61
|
+
format: "voltro.billing-account-custody-inventory.v1",
|
|
62
|
+
provider: e,
|
|
63
|
+
providerAccountId: n,
|
|
64
|
+
livemode: s,
|
|
65
|
+
tables: await a.transactional(async (e) => {
|
|
66
|
+
if (e.integrityQuery === void 0) throw x("Transactional native integrity reads are required.", !1);
|
|
67
|
+
let t = await p(e, l, d, c, u), n = await p(e, l, d, c, u);
|
|
68
|
+
if (y(t) !== y(n)) throw x("Billing state changed during inventory. Quiesce writers and retry.", !0);
|
|
69
|
+
return n;
|
|
70
|
+
})
|
|
71
|
+
};
|
|
72
|
+
return Object.freeze({
|
|
73
|
+
...f,
|
|
74
|
+
digest: y(f),
|
|
75
|
+
observedAt: /* @__PURE__ */ new Date(),
|
|
76
|
+
purpose: "review-only"
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
catch: (t) => t instanceof e ? t : x("The native billing account inventory could not be completed.", !0)
|
|
80
|
+
}), f = (e) => ({
|
|
81
|
+
predicate: i("tenantId", e),
|
|
82
|
+
owns: (t) => t.tenantId === e
|
|
83
|
+
}), p = async (e, n, i, o, c) => {
|
|
84
|
+
let l = e.integrityQuery;
|
|
85
|
+
if (l === void 0) throw x("Complete native integrity reads are required.", !1);
|
|
86
|
+
let u = [], d = 0;
|
|
87
|
+
for (let f of n) {
|
|
88
|
+
let n = [], p = /* @__PURE__ */ new Set(), S;
|
|
55
89
|
for (;;) {
|
|
56
|
-
let
|
|
57
|
-
table:
|
|
58
|
-
predicate:
|
|
90
|
+
let u = Math.min(o, c - n.length + 1), y = {
|
|
91
|
+
table: f,
|
|
92
|
+
predicate: S === void 0 ? i.predicate : r(i.predicate, a("id", S)),
|
|
59
93
|
crossTenant: !0,
|
|
60
94
|
includeDeleted: !0,
|
|
61
95
|
includeExpired: !0,
|
|
@@ -63,92 +97,94 @@ var c = Object.freeze([
|
|
|
63
97
|
column: "id",
|
|
64
98
|
direction: "asc"
|
|
65
99
|
}],
|
|
66
|
-
take:
|
|
100
|
+
take: u,
|
|
67
101
|
skip: void 0,
|
|
68
102
|
projection: void 0
|
|
69
|
-
},
|
|
70
|
-
if (!Array.isArray(
|
|
71
|
-
for (let e of
|
|
72
|
-
let
|
|
73
|
-
if (!
|
|
74
|
-
let a =
|
|
75
|
-
if (!t(a) || i.
|
|
76
|
-
if (
|
|
77
|
-
|
|
103
|
+
}, C = await l.call(e, y);
|
|
104
|
+
if (!Array.isArray(C) || C.length > u) throw x(`Invalid inventory page for ${f}.`, !1);
|
|
105
|
+
for (let e of C) {
|
|
106
|
+
let r = v(e);
|
|
107
|
+
if (!_(r)) throw x(`Invalid inventory row for ${f}.`, !1);
|
|
108
|
+
let a = r.id;
|
|
109
|
+
if (!t(a) || !i.owns(r) || p.has(a)) throw x(`Invalid inventory identity for ${f}.`, !1);
|
|
110
|
+
if (n.length === c) throw x(`Inventory bound exceeded for ${f}; no partial inventory is returned.`, !1);
|
|
111
|
+
p.add(a);
|
|
78
112
|
let o = {};
|
|
79
|
-
for (let e of
|
|
80
|
-
let n =
|
|
113
|
+
for (let e of m) {
|
|
114
|
+
let n = r[e];
|
|
81
115
|
if (n != null) {
|
|
82
|
-
if (!t(n)) throw
|
|
116
|
+
if (!t(n)) throw x(`Invalid inventory reference for ${f}.`, !1);
|
|
83
117
|
o[e] = n;
|
|
84
118
|
}
|
|
85
119
|
}
|
|
86
|
-
let
|
|
87
|
-
if (
|
|
88
|
-
|
|
120
|
+
let l = s(r), u = Buffer.byteLength(l, "utf8");
|
|
121
|
+
if (d += u, u > h || d > g) throw x("Native billing inventory byte bound exceeded; no partial inventory is returned.", !1);
|
|
122
|
+
n.push(Object.freeze({
|
|
89
123
|
id: a,
|
|
90
|
-
digest:
|
|
124
|
+
digest: b(l),
|
|
91
125
|
references: Object.freeze(o)
|
|
92
|
-
})),
|
|
126
|
+
})), S = a;
|
|
93
127
|
}
|
|
94
|
-
if (
|
|
128
|
+
if (C.length < u) break;
|
|
95
129
|
}
|
|
96
|
-
|
|
97
|
-
tableName:
|
|
98
|
-
rowCount:
|
|
99
|
-
digest:
|
|
100
|
-
rows: Object.freeze(
|
|
130
|
+
n.sort((e, t) => e.id < t.id ? -1 : +(e.id > t.id)), u.push(Object.freeze({
|
|
131
|
+
tableName: f,
|
|
132
|
+
rowCount: n.length,
|
|
133
|
+
digest: y(n),
|
|
134
|
+
rows: Object.freeze(n)
|
|
101
135
|
}));
|
|
102
136
|
}
|
|
103
|
-
return Object.freeze(
|
|
104
|
-
},
|
|
137
|
+
return Object.freeze(u);
|
|
138
|
+
}, m = [
|
|
105
139
|
"provider",
|
|
106
140
|
"providerAccountId",
|
|
107
141
|
"providerCustomerId",
|
|
108
142
|
"providerSubscriptionId",
|
|
109
|
-
"providerInvoiceId"
|
|
110
|
-
|
|
111
|
-
|
|
143
|
+
"providerInvoiceId",
|
|
144
|
+
"afterCustodyId",
|
|
145
|
+
"cycleEndCustodyId"
|
|
146
|
+
], h = 4194304, g = 67108864, _ = (e) => typeof e == "object" && !!e && !Array.isArray(e), v = (e, t = /* @__PURE__ */ new Set(), n = 0) => {
|
|
147
|
+
if (n > 64) throw x("Native inventory value exceeds supported depth.", !1);
|
|
112
148
|
if (e === null || typeof e == "boolean") return e;
|
|
113
149
|
if (typeof e == "string") {
|
|
114
|
-
if (!e.isWellFormed()) throw
|
|
150
|
+
if (!e.isWellFormed()) throw x("Native inventory contains malformed text.", !1);
|
|
115
151
|
return e;
|
|
116
152
|
}
|
|
117
153
|
if (typeof e == "number") {
|
|
118
|
-
if (!Number.isFinite(e) || !Number.isSafeInteger(e) && Math.abs(e) > 2 ** 53 - 1 || Object.is(e, -0)) throw
|
|
154
|
+
if (!Number.isFinite(e) || !Number.isSafeInteger(e) && Math.abs(e) > 2 ** 53 - 1 || Object.is(e, -0)) throw x("Native inventory numeric evidence cannot be encoded exactly.", !1);
|
|
119
155
|
return e;
|
|
120
156
|
}
|
|
121
157
|
if (e instanceof Date) {
|
|
122
|
-
if (!Number.isFinite(e.getTime())) throw
|
|
158
|
+
if (!Number.isFinite(e.getTime())) throw x("Native inventory contains an invalid date.", !1);
|
|
123
159
|
return e.toISOString();
|
|
124
160
|
}
|
|
125
|
-
if (typeof e != "object" || t.has(e)) throw
|
|
161
|
+
if (typeof e != "object" || t.has(e)) throw x("Native inventory contains a non-JSON value.", !1);
|
|
126
162
|
let r = Object.getPrototypeOf(e);
|
|
127
|
-
if (!Array.isArray(e) && r !== Object.prototype && r !== null) throw
|
|
128
|
-
if (Object.getOwnPropertySymbols(e).length !== 0) throw
|
|
163
|
+
if (!Array.isArray(e) && r !== Object.prototype && r !== null) throw x("Native inventory value has an unsupported prototype.", !1);
|
|
164
|
+
if (Object.getOwnPropertySymbols(e).length !== 0) throw x("Native inventory contains unsupported symbol fields.", !1);
|
|
129
165
|
t.add(e);
|
|
130
166
|
try {
|
|
131
167
|
if (Array.isArray(e)) {
|
|
132
|
-
if (Object.keys(e).length !== e.length || Object.getOwnPropertyNames(e).length !== e.length + 1) throw
|
|
168
|
+
if (Object.keys(e).length !== e.length || Object.getOwnPropertyNames(e).length !== e.length + 1) throw x("Native inventory contains a sparse or extended array.", !1);
|
|
133
169
|
return Array.from({ length: e.length }, (r, i) => {
|
|
134
170
|
let a = Object.getOwnPropertyDescriptor(e, String(i));
|
|
135
|
-
if (a === void 0 || !("value" in a)) throw
|
|
136
|
-
return
|
|
171
|
+
if (a === void 0 || !("value" in a)) throw x("Native inventory contains a sparse or accessor array.", !1);
|
|
172
|
+
return v(a.value, t, n + 1);
|
|
137
173
|
});
|
|
138
174
|
}
|
|
139
|
-
if (Object.getOwnPropertyNames(e).length !== Object.keys(e).length) throw
|
|
175
|
+
if (Object.getOwnPropertyNames(e).length !== Object.keys(e).length) throw x("Native inventory contains non-enumerable fields.", !1);
|
|
140
176
|
return Object.fromEntries(Object.keys(e).map((r) => {
|
|
141
|
-
if (!r.isWellFormed()) throw
|
|
177
|
+
if (!r.isWellFormed()) throw x("Native inventory contains a malformed field.", !1);
|
|
142
178
|
let i = Object.getOwnPropertyDescriptor(e, r);
|
|
143
|
-
if (i === void 0 || !("value" in i)) throw
|
|
144
|
-
return [r,
|
|
179
|
+
if (i === void 0 || !("value" in i)) throw x("Native inventory contains an accessor field.", !1);
|
|
180
|
+
return [r, v(i.value, t, n + 1)];
|
|
145
181
|
}));
|
|
146
182
|
} finally {
|
|
147
183
|
t.delete(e);
|
|
148
184
|
}
|
|
149
|
-
},
|
|
185
|
+
}, y = (e) => b(s(e)), b = (e) => o("sha256").update(e, "utf8").digest("hex"), x = (t, n) => new e({
|
|
150
186
|
source: "billingCustodyInventory",
|
|
151
187
|
message: t,
|
|
152
188
|
transient: n
|
|
153
189
|
});
|
|
154
|
-
export { c as BILLING_CUSTODY_INVENTORY_TABLES,
|
|
190
|
+
export { l as BILLING_ACCOUNT_CUSTODY_INVENTORY_TABLES, c as BILLING_CUSTODY_INVENTORY_TABLES, d as inspectBillingAccountCustody, u as inspectBillingCustody };
|
package/dist/dunning.js
CHANGED
|
@@ -143,12 +143,20 @@ var l = "_voltro_billing_dunning_notices", u = o(l, {
|
|
|
143
143
|
html: `<p>${a} Your subscription stays active until ${i}.</p>${n}`,
|
|
144
144
|
text: `${a} Your subscription stays active until ${i}.\n\n${r}`
|
|
145
145
|
};
|
|
146
|
-
}, w = (e) => (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
146
|
+
}, w = (e, t = {}) => (r) => {
|
|
147
|
+
let i = r.to;
|
|
148
|
+
return i === null ? n.void : (t.compose === void 0 ? n.succeed({
|
|
149
|
+
subject: r.subject,
|
|
150
|
+
html: r.html,
|
|
151
|
+
text: r.text
|
|
152
|
+
}) : t.compose(r)).pipe(n.flatMap((n) => e.send({
|
|
153
|
+
to: i,
|
|
154
|
+
subject: n.subject,
|
|
155
|
+
html: n.html,
|
|
156
|
+
text: n.text,
|
|
157
|
+
...t.tags === void 0 ? {} : { tags: t.tags }
|
|
158
|
+
})), n.asVoid);
|
|
159
|
+
}, T = (e, t, n) => `${e}\u0000${t}\u0000${n}`, E = () => {
|
|
152
160
|
let e = /* @__PURE__ */ new Map();
|
|
153
161
|
return {
|
|
154
162
|
claim: (t, r, i, a) => n.sync(() => {
|
package/dist/eventDelivery.js
CHANGED
|
@@ -14,6 +14,7 @@ var f = 864e5, p = (t) => new e({
|
|
|
14
14
|
let e = structuredClone(l);
|
|
15
15
|
if (e.rejected) throw p("The provider event could not be normalized and requires reconciliation.");
|
|
16
16
|
if (typeof e.id != "string" || !e.id || e.id.trim() !== e.id || e.createdAt !== null && (!(e.createdAt instanceof Date) || !Number.isFinite(e.createdAt.getTime()))) throw p("The verified provider delivery requires exact identity and time evidence.");
|
|
17
|
+
if (e.event === null && e.providerCustomerId === null) return "processed";
|
|
17
18
|
let d = m.receipts?.store() ?? h;
|
|
18
19
|
if (g === void 0) {
|
|
19
20
|
let e = m.provider.fetchAccountIdentity?.bind(m.provider);
|
|
@@ -39,12 +40,20 @@ var f = 864e5, p = (t) => new e({
|
|
|
39
40
|
if (!await d.renew(x.claim, Date.now())) throw p("The provider delivery claim changed before processing.");
|
|
40
41
|
let t = e.event, n = e.providerCustomerId ?? (t !== null && "providerCustomerId" in t ? t.providerCustomerId : null);
|
|
41
42
|
if (t !== null && "providerCustomerId" in t && e.providerCustomerId !== null && t.providerCustomerId !== e.providerCustomerId) throw p("The provider delivery has inconsistent customer identities.");
|
|
42
|
-
let i =
|
|
43
|
-
if (t
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
let i = [];
|
|
44
|
+
if (t?._tag === "invoicePaymentsCorrected") i.push(...await r.runPromise(m.service.reconcilePaymentCorrection({
|
|
45
|
+
...v,
|
|
46
|
+
providerCustomerId: t.providerCustomerId,
|
|
47
|
+
providerEventId: e.id
|
|
48
|
+
})));
|
|
49
|
+
else {
|
|
50
|
+
let e = t?._tag === "invoicePaid" || t?._tag === "invoicePaymentFailed", a = n !== null && t?._tag !== "customerUnlinked" && !e;
|
|
51
|
+
if (t !== null && (!a || t._tag !== "subscriptionUpserted" && t._tag !== "subscriptionCanceled" && t._tag !== "customerLinked") && i.push(await r.runPromise(m.service.applyEvent(t))), a) {
|
|
52
|
+
let e = await r.runPromise(m.service.syncCustomer(n, t !== null && "tenantId" in t && typeof t.tenantId == "string" ? t.tenantId : void 0));
|
|
53
|
+
i.push(...e.events);
|
|
54
|
+
}
|
|
46
55
|
}
|
|
47
|
-
for (let t of
|
|
56
|
+
for (let t of i) {
|
|
48
57
|
if (!await d.renew(x.claim, Date.now())) throw p("The provider delivery claim changed before callback dispatch.");
|
|
49
58
|
let n = m.onEvent?.[t._tag];
|
|
50
59
|
n !== void 0 && await r.runPromise(n(t, Object.freeze({
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { BillingError as e } from "./types.js";
|
|
2
|
+
import { Effect as t } from "effect";
|
|
3
|
+
// src/historicalUsage.ts
|
|
4
|
+
var n = /^(?:0|[1-9][0-9]*)(?:\.[0-9]{1,12})?$/, r = (e, t) => e !== null && n.test(e) ? e : t !== null && Number.isSafeInteger(t) && t >= 0 ? String(t) : null, i = (e) => Number(e) === 0, a = (e, t, n) => {
|
|
5
|
+
if (!Number.isSafeInteger(t) || !Number.isSafeInteger(n) || t < 0 || n <= t) return null;
|
|
6
|
+
if (e.tiers === null) {
|
|
7
|
+
let i = r(null, e.unitAmountMinor);
|
|
8
|
+
return i === null ? null : [{
|
|
9
|
+
quantity: n - t,
|
|
10
|
+
unitAmountDecimal: i
|
|
11
|
+
}];
|
|
12
|
+
}
|
|
13
|
+
if (e.tiersMode !== "graduated") return null;
|
|
14
|
+
let i = [], a = 0;
|
|
15
|
+
for (let o of e.tiers) {
|
|
16
|
+
if (o.flatAmountMinor !== null && o.flatAmountMinor !== 0) return null;
|
|
17
|
+
let e = o.upTo ?? Infinity;
|
|
18
|
+
if (e <= a) return null;
|
|
19
|
+
let s = Math.min(n, e) - Math.max(t, a);
|
|
20
|
+
if (s > 0) {
|
|
21
|
+
let e = r(o.unitAmountDecimal, o.unitAmountMinor);
|
|
22
|
+
if (e === null) return null;
|
|
23
|
+
i.push({
|
|
24
|
+
quantity: s,
|
|
25
|
+
unitAmountDecimal: e
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (o.upTo === null) return i;
|
|
29
|
+
a = e;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}, o = (e, t, n) => {
|
|
33
|
+
let r = e.find((e) => e.id === t);
|
|
34
|
+
if (r === void 0) return null;
|
|
35
|
+
let i = null;
|
|
36
|
+
for (let t of e) t.productId === r.productId && t.currency === r.currency && t.usageType === r.usageType && t.interval === r.interval && (t.createdAt.getTime() >= n.getTime() || (i === null || t.createdAt.getTime() > i.createdAt.getTime() || t.createdAt.getTime() === i.createdAt.getTime() && t.id > i.id) && (i = t));
|
|
37
|
+
return i;
|
|
38
|
+
}, s = (t, n = !1) => new e({
|
|
39
|
+
source: "historicalUsage",
|
|
40
|
+
message: t,
|
|
41
|
+
transient: n
|
|
42
|
+
}), c = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,179}$/, l = (e) => (n) => t.gen(function* () {
|
|
43
|
+
let r = n.period.start.getTime(), l = n.period.end.getTime(), u = e.now().getTime();
|
|
44
|
+
if (n.tenantId.length === 0 || n.priceId.length === 0 || !c.test(n.idempotencyKey) || n.description.trim().length === 0 || n.description.length > 500 || !Number.isFinite(r) || !Number.isFinite(l) || r >= l || !Number.isSafeInteger(n.fromUnits) || !Number.isSafeInteger(n.toUnits) || n.fromUnits < 0 || n.toUnits <= n.fromUnits) return yield* t.fail(s("Historical usage requires a tenant, a price, a valid closed period, a positive unit interval and a stable key."));
|
|
45
|
+
if (l > u) return yield* t.fail(s("Historical usage belongs to a closed period; this period has not ended yet."));
|
|
46
|
+
let d = e.provider.fetchPriceVersions, f = e.provider.ensureInvoiceItem;
|
|
47
|
+
if (d === void 0 || f === void 0) return yield* t.fail(s(`Billing provider "${e.provider.name}" cannot invoice historical usage.`));
|
|
48
|
+
if (yield* e.sealed(n.tenantId)) return yield* t.fail(s("Usage arrived after final billing dispatch was sealed. Financial reconciliation is required."));
|
|
49
|
+
let p = o(yield* d(n.priceId), n.priceId, n.period.end);
|
|
50
|
+
if (p === null) return yield* t.fail(s("The provider holds no version of this price that was in force during the period."));
|
|
51
|
+
let m = a(p, n.fromUnits, n.toUnits);
|
|
52
|
+
if (m === null) return yield* t.fail(s("The period's price is not a flat or graduated per-unit price; its amount cannot be applied exactly."));
|
|
53
|
+
let h = m.map((e, t) => ({
|
|
54
|
+
segment: e,
|
|
55
|
+
index: t
|
|
56
|
+
})).filter(({ segment: e }) => !i(e.unitAmountDecimal));
|
|
57
|
+
if (h.length === 0) return {
|
|
58
|
+
priceId: p.id,
|
|
59
|
+
currency: p.currency,
|
|
60
|
+
lines: []
|
|
61
|
+
};
|
|
62
|
+
let g = yield* e.customerOf(n.tenantId);
|
|
63
|
+
if (g === null) return yield* t.fail(s("Historical usage requires a linked provider customer.", !0));
|
|
64
|
+
let _ = yield* e.liveSubscriptionOf(n.tenantId), v = [];
|
|
65
|
+
for (let { segment: e, index: t } of h) {
|
|
66
|
+
let r = yield* f({
|
|
67
|
+
providerCustomerId: g,
|
|
68
|
+
..._ === null ? {} : { providerSubscriptionId: _ },
|
|
69
|
+
productId: p.productId,
|
|
70
|
+
currency: p.currency,
|
|
71
|
+
quantity: e.quantity,
|
|
72
|
+
unitAmountDecimal: e.unitAmountDecimal,
|
|
73
|
+
taxBehavior: p.taxBehavior,
|
|
74
|
+
period: {
|
|
75
|
+
start: n.period.start,
|
|
76
|
+
end: n.period.end
|
|
77
|
+
},
|
|
78
|
+
description: n.description,
|
|
79
|
+
idempotencyKey: `${n.idempotencyKey}:${String(t)}`,
|
|
80
|
+
notBefore: n.period.end
|
|
81
|
+
});
|
|
82
|
+
v.push({
|
|
83
|
+
providerInvoiceItemId: r.providerInvoiceItemId,
|
|
84
|
+
quantity: e.quantity,
|
|
85
|
+
unitAmountDecimal: e.unitAmountDecimal
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
priceId: p.id,
|
|
90
|
+
currency: p.currency,
|
|
91
|
+
lines: v
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
export { a as historicalUsageSegments, l as makeHistoricalUsageInvoicer, o as priceVersionInForce };
|