@codelitdev/billing 0.1.0-alpha.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.
- package/LICENSE +21 -0
- package/README.md +339 -0
- package/dist/adapters/drizzle/index.d.ts +28 -0
- package/dist/adapters/drizzle/index.d.ts.map +1 -0
- package/dist/adapters/drizzle/index.js +75 -0
- package/dist/adapters/drizzle/index.js.map +1 -0
- package/dist/adapters/drizzle/store.d.ts +37 -0
- package/dist/adapters/drizzle/store.d.ts.map +1 -0
- package/dist/adapters/drizzle/store.js +889 -0
- package/dist/adapters/drizzle/store.js.map +1 -0
- package/dist/catalog/index.d.ts +5 -0
- package/dist/catalog/index.d.ts.map +1 -0
- package/dist/catalog/index.js +4 -0
- package/dist/catalog/index.js.map +1 -0
- package/dist/catalog/public-view.d.ts +7 -0
- package/dist/catalog/public-view.d.ts.map +1 -0
- package/dist/catalog/public-view.js +22 -0
- package/dist/catalog/public-view.js.map +1 -0
- package/dist/catalog/revisions.d.ts +8 -0
- package/dist/catalog/revisions.d.ts.map +1 -0
- package/dist/catalog/revisions.js +12 -0
- package/dist/catalog/revisions.js.map +1 -0
- package/dist/catalog/types.d.ts +28 -0
- package/dist/catalog/types.d.ts.map +1 -0
- package/dist/catalog/types.js +2 -0
- package/dist/catalog/types.js.map +1 -0
- package/dist/catalog/validate.d.ts +16 -0
- package/dist/catalog/validate.d.ts.map +1 -0
- package/dist/catalog/validate.js +66 -0
- package/dist/catalog/validate.js.map +1 -0
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +35 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/generate.d.ts +15 -0
- package/dist/cli/generate.d.ts.map +1 -0
- package/dist/cli/generate.js +68 -0
- package/dist/cli/generate.js.map +1 -0
- package/dist/config/define.d.ts +3 -0
- package/dist/config/define.d.ts.map +1 -0
- package/dist/config/define.js +5 -0
- package/dist/config/define.js.map +1 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +3 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/validate.d.ts +32 -0
- package/dist/config/validate.d.ts.map +1 -0
- package/dist/config/validate.js +227 -0
- package/dist/config/validate.js.map +1 -0
- package/dist/core/checkout-attempt.d.ts +29 -0
- package/dist/core/checkout-attempt.d.ts.map +1 -0
- package/dist/core/checkout-attempt.js +10 -0
- package/dist/core/checkout-attempt.js.map +1 -0
- package/dist/core/clock.d.ts +10 -0
- package/dist/core/clock.d.ts.map +1 -0
- package/dist/core/clock.js +26 -0
- package/dist/core/clock.js.map +1 -0
- package/dist/core/customer.d.ts +13 -0
- package/dist/core/customer.d.ts.map +1 -0
- package/dist/core/customer.js +2 -0
- package/dist/core/customer.js.map +1 -0
- package/dist/core/errors.d.ts +28 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +43 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/ids.d.ts +15 -0
- package/dist/core/ids.d.ts.map +1 -0
- package/dist/core/ids.js +29 -0
- package/dist/core/ids.js.map +1 -0
- package/dist/core/index.d.ts +14 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +14 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/money.d.ts +8 -0
- package/dist/core/money.d.ts.map +1 -0
- package/dist/core/money.js +23 -0
- package/dist/core/money.js.map +1 -0
- package/dist/core/plan-change-attempt.d.ts +30 -0
- package/dist/core/plan-change-attempt.d.ts.map +1 -0
- package/dist/core/plan-change-attempt.js +9 -0
- package/dist/core/plan-change-attempt.js.map +1 -0
- package/dist/core/projection-diff.d.ts +32 -0
- package/dist/core/projection-diff.d.ts.map +1 -0
- package/dist/core/projection-diff.js +32 -0
- package/dist/core/projection-diff.js.map +1 -0
- package/dist/core/reconciliation-job.d.ts +21 -0
- package/dist/core/reconciliation-job.d.ts.map +1 -0
- package/dist/core/reconciliation-job.js +26 -0
- package/dist/core/reconciliation-job.js.map +1 -0
- package/dist/core/subscription.d.ts +62 -0
- package/dist/core/subscription.d.ts.map +1 -0
- package/dist/core/subscription.js +14 -0
- package/dist/core/subscription.js.map +1 -0
- package/dist/core/transitions.d.ts +25 -0
- package/dist/core/transitions.d.ts.map +1 -0
- package/dist/core/transitions.js +161 -0
- package/dist/core/transitions.js.map +1 -0
- package/dist/core/webhook-inbox.d.ts +23 -0
- package/dist/core/webhook-inbox.d.ts.map +1 -0
- package/dist/core/webhook-inbox.js +9 -0
- package/dist/core/webhook-inbox.js.map +1 -0
- package/dist/maintenance/batches.d.ts +3 -0
- package/dist/maintenance/batches.d.ts.map +1 -0
- package/dist/maintenance/batches.js +3 -0
- package/dist/maintenance/batches.js.map +1 -0
- package/dist/maintenance/claims.d.ts +2 -0
- package/dist/maintenance/claims.d.ts.map +1 -0
- package/dist/maintenance/claims.js +2 -0
- package/dist/maintenance/claims.js.map +1 -0
- package/dist/maintenance/health.d.ts +13 -0
- package/dist/maintenance/health.d.ts.map +1 -0
- package/dist/maintenance/health.js +19 -0
- package/dist/maintenance/health.js.map +1 -0
- package/dist/maintenance/retry.d.ts +9 -0
- package/dist/maintenance/retry.d.ts.map +1 -0
- package/dist/maintenance/retry.js +19 -0
- package/dist/maintenance/retry.js.map +1 -0
- package/dist/operations/index.d.ts +28 -0
- package/dist/operations/index.d.ts.map +1 -0
- package/dist/operations/index.js +106 -0
- package/dist/operations/index.js.map +1 -0
- package/dist/persistence/memory.d.ts +104 -0
- package/dist/persistence/memory.d.ts.map +1 -0
- package/dist/persistence/memory.js +657 -0
- package/dist/persistence/memory.js.map +1 -0
- package/dist/persistence/store.d.ts +126 -0
- package/dist/persistence/store.d.ts.map +1 -0
- package/dist/persistence/store.js +2 -0
- package/dist/persistence/store.js.map +1 -0
- package/dist/ports/audit.d.ts +20 -0
- package/dist/ports/audit.d.ts.map +1 -0
- package/dist/ports/audit.js +9 -0
- package/dist/ports/audit.js.map +1 -0
- package/dist/ports/authorization.d.ts +28 -0
- package/dist/ports/authorization.d.ts.map +1 -0
- package/dist/ports/authorization.js +33 -0
- package/dist/ports/authorization.js.map +1 -0
- package/dist/ports/lifecycle.d.ts +23 -0
- package/dist/ports/lifecycle.d.ts.map +1 -0
- package/dist/ports/lifecycle.js +2 -0
- package/dist/ports/lifecycle.js.map +1 -0
- package/dist/ports/sensitive-values.d.ts +23 -0
- package/dist/ports/sensitive-values.d.ts.map +1 -0
- package/dist/ports/sensitive-values.js +19 -0
- package/dist/ports/sensitive-values.js.map +1 -0
- package/dist/ports/telemetry.d.ts +16 -0
- package/dist/ports/telemetry.d.ts.map +1 -0
- package/dist/ports/telemetry.js +48 -0
- package/dist/ports/telemetry.js.map +1 -0
- package/dist/providers/contract.d.ts +98 -0
- package/dist/providers/contract.d.ts.map +1 -0
- package/dist/providers/contract.js +2 -0
- package/dist/providers/contract.js.map +1 -0
- package/dist/providers/dodo/index.d.ts +25 -0
- package/dist/providers/dodo/index.d.ts.map +1 -0
- package/dist/providers/dodo/index.js +296 -0
- package/dist/providers/dodo/index.js.map +1 -0
- package/dist/providers/dodo/normalize.d.ts +7 -0
- package/dist/providers/dodo/normalize.d.ts.map +1 -0
- package/dist/providers/dodo/normalize.js +77 -0
- package/dist/providers/dodo/normalize.js.map +1 -0
- package/dist/providers/fake/index.d.ts +59 -0
- package/dist/providers/fake/index.d.ts.map +1 -0
- package/dist/providers/fake/index.js +312 -0
- package/dist/providers/fake/index.js.map +1 -0
- package/dist/providers/index.d.ts +4 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +3 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/registry.d.ts +10 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +32 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/schema/canonical.d.ts +7 -0
- package/dist/schema/canonical.d.ts.map +1 -0
- package/dist/schema/canonical.js +35 -0
- package/dist/schema/canonical.js.map +1 -0
- package/dist/schema/generator.d.ts +3 -0
- package/dist/schema/generator.d.ts.map +1 -0
- package/dist/schema/generator.js +496 -0
- package/dist/schema/generator.js.map +1 -0
- package/dist/schema/pglite-sql.d.ts +3 -0
- package/dist/schema/pglite-sql.d.ts.map +1 -0
- package/dist/schema/pglite-sql.js +226 -0
- package/dist/schema/pglite-sql.js.map +1 -0
- package/dist/testing/clocks.d.ts +2 -0
- package/dist/testing/clocks.d.ts.map +1 -0
- package/dist/testing/clocks.js +2 -0
- package/dist/testing/clocks.js.map +1 -0
- package/dist/testing/consumer-conformance.d.ts +12 -0
- package/dist/testing/consumer-conformance.d.ts.map +1 -0
- package/dist/testing/consumer-conformance.js +17 -0
- package/dist/testing/consumer-conformance.js.map +1 -0
- package/dist/testing/fixtures.d.ts +9 -0
- package/dist/testing/fixtures.d.ts.map +1 -0
- package/dist/testing/fixtures.js +69 -0
- package/dist/testing/fixtures.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +7 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/provider-contract.d.ts +14 -0
- package/dist/testing/provider-contract.d.ts.map +1 -0
- package/dist/testing/provider-contract.js +238 -0
- package/dist/testing/provider-contract.js.map +1 -0
- package/dist/testing/state-machine.d.ts +2 -0
- package/dist/testing/state-machine.d.ts.map +1 -0
- package/dist/testing/state-machine.js +2 -0
- package/dist/testing/state-machine.js.map +1 -0
- package/dist/testing/workflow-harness.d.ts +224 -0
- package/dist/testing/workflow-harness.d.ts.map +1 -0
- package/dist/testing/workflow-harness.js +65 -0
- package/dist/testing/workflow-harness.js.map +1 -0
- package/dist/workflows/engine.d.ts +157 -0
- package/dist/workflows/engine.d.ts.map +1 -0
- package/dist/workflows/engine.js +2015 -0
- package/dist/workflows/engine.js.map +1 -0
- package/dist/workflows/index.d.ts +10 -0
- package/dist/workflows/index.d.ts.map +1 -0
- package/dist/workflows/index.js +6 -0
- package/dist/workflows/index.js.map +1 -0
- package/dist/workflows/lifecycle.d.ts +5 -0
- package/dist/workflows/lifecycle.d.ts.map +1 -0
- package/dist/workflows/lifecycle.js +7 -0
- package/dist/workflows/lifecycle.js.map +1 -0
- package/docs/architecture.md +1188 -0
- package/docs/decisions/0001-extraction-discrepancies.md +29 -0
- package/docs/runbooks.md +140 -0
- package/package.json +110 -0
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { nanoid } from "nanoid";
|
|
3
|
+
export class MemoryBillingStore {
|
|
4
|
+
transactionDepth = 0;
|
|
5
|
+
isInTransaction() {
|
|
6
|
+
return this.transactionDepth > 0;
|
|
7
|
+
}
|
|
8
|
+
providerCallsWhileOpen = 0;
|
|
9
|
+
priceEntries = [];
|
|
10
|
+
revisions = [];
|
|
11
|
+
revisionItems = [];
|
|
12
|
+
customers = [];
|
|
13
|
+
checkouts = [];
|
|
14
|
+
planChanges = [];
|
|
15
|
+
subscriptions = [];
|
|
16
|
+
planStates = new Map();
|
|
17
|
+
webhooks = [];
|
|
18
|
+
jobs = [];
|
|
19
|
+
auditEffects = [];
|
|
20
|
+
transactionCount = 0;
|
|
21
|
+
failOnTransaction = 0;
|
|
22
|
+
transactionBackup = null;
|
|
23
|
+
transactionTail = Promise.resolve();
|
|
24
|
+
async withTransaction(fn) {
|
|
25
|
+
let release;
|
|
26
|
+
const previous = this.transactionTail;
|
|
27
|
+
this.transactionTail = new Promise((resolve) => {
|
|
28
|
+
release = resolve;
|
|
29
|
+
});
|
|
30
|
+
await previous;
|
|
31
|
+
const outermost = this.transactionDepth === 0;
|
|
32
|
+
if (outermost) {
|
|
33
|
+
this.transactionBackup = structuredClone({
|
|
34
|
+
priceEntries: this.priceEntries,
|
|
35
|
+
revisions: this.revisions,
|
|
36
|
+
revisionItems: this.revisionItems,
|
|
37
|
+
customers: this.customers,
|
|
38
|
+
checkouts: this.checkouts,
|
|
39
|
+
planChanges: this.planChanges,
|
|
40
|
+
subscriptions: this.subscriptions,
|
|
41
|
+
planStates: [...this.planStates.entries()],
|
|
42
|
+
webhooks: this.webhooks,
|
|
43
|
+
jobs: this.jobs,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
this.transactionDepth += 1;
|
|
47
|
+
this.transactionCount += 1;
|
|
48
|
+
try {
|
|
49
|
+
if (this.failOnTransaction === this.transactionCount) {
|
|
50
|
+
throw new Error("local_finalize_failed");
|
|
51
|
+
}
|
|
52
|
+
return await fn();
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (outermost && this.transactionBackup) {
|
|
56
|
+
this.priceEntries = this.transactionBackup.priceEntries;
|
|
57
|
+
this.revisions = this.transactionBackup.revisions;
|
|
58
|
+
this.revisionItems = this.transactionBackup.revisionItems;
|
|
59
|
+
this.customers = this.transactionBackup.customers;
|
|
60
|
+
this.checkouts = this.transactionBackup.checkouts;
|
|
61
|
+
this.planChanges = this.transactionBackup.planChanges;
|
|
62
|
+
this.subscriptions = this.transactionBackup.subscriptions;
|
|
63
|
+
this.planStates = new Map(this.transactionBackup.planStates);
|
|
64
|
+
this.webhooks = this.transactionBackup.webhooks;
|
|
65
|
+
this.jobs = this.transactionBackup.jobs;
|
|
66
|
+
}
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
this.transactionDepth -= 1;
|
|
71
|
+
if (outermost)
|
|
72
|
+
this.transactionBackup = null;
|
|
73
|
+
release();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
noteProviderCall() {
|
|
77
|
+
if (this.transactionDepth > 0)
|
|
78
|
+
this.providerCallsWhileOpen += 1;
|
|
79
|
+
}
|
|
80
|
+
ensurePlanState(billableEntityId) {
|
|
81
|
+
const existing = this.planStates.get(billableEntityId);
|
|
82
|
+
if (existing)
|
|
83
|
+
return existing;
|
|
84
|
+
const created = {
|
|
85
|
+
billableEntityId,
|
|
86
|
+
activeSubscriptionId: null,
|
|
87
|
+
projectionVersion: 0,
|
|
88
|
+
};
|
|
89
|
+
this.planStates.set(billableEntityId, created);
|
|
90
|
+
return created;
|
|
91
|
+
}
|
|
92
|
+
findPlanState(billableEntityId) {
|
|
93
|
+
return this.planStates.get(billableEntityId);
|
|
94
|
+
}
|
|
95
|
+
seedCatalog(input) {
|
|
96
|
+
const revision = {
|
|
97
|
+
id: randomUUID(),
|
|
98
|
+
revision: input.revision,
|
|
99
|
+
checkoutProvider: input.provider,
|
|
100
|
+
status: input.status ?? "active",
|
|
101
|
+
verifiedAt: input.status === "pending_verification" ? null : new Date(0),
|
|
102
|
+
activatedAt: (input.status ?? "active") === "active" ? new Date(0) : null,
|
|
103
|
+
retiredAt: input.status === "retired" ? new Date(0) : null,
|
|
104
|
+
};
|
|
105
|
+
this.revisions.push(revision);
|
|
106
|
+
const items = input.offers.map((offer) => {
|
|
107
|
+
const price = {
|
|
108
|
+
id: randomUUID(),
|
|
109
|
+
offerKey: offer.key,
|
|
110
|
+
plan: offer.plan,
|
|
111
|
+
interval: offer.interval,
|
|
112
|
+
currency: offer.currency,
|
|
113
|
+
amountMinor: offer.amountMinor,
|
|
114
|
+
providerTrialDays: offer.providerTrialDays,
|
|
115
|
+
provider: offer.provider,
|
|
116
|
+
providerProductId: offer.providerProductId,
|
|
117
|
+
verifiedAt: new Date(0),
|
|
118
|
+
};
|
|
119
|
+
this.priceEntries.push(price);
|
|
120
|
+
this.revisionItems.push({
|
|
121
|
+
id: randomUUID(),
|
|
122
|
+
revisionId: revision.id,
|
|
123
|
+
offerKey: offer.key,
|
|
124
|
+
priceEntryId: price.id,
|
|
125
|
+
});
|
|
126
|
+
return { offerKey: offer.key, price };
|
|
127
|
+
});
|
|
128
|
+
return { revision, items };
|
|
129
|
+
}
|
|
130
|
+
getActiveCatalog(provider) {
|
|
131
|
+
const revision = this.revisions.find((row) => row.checkoutProvider === provider && row.status === "active");
|
|
132
|
+
if (!revision)
|
|
133
|
+
return null;
|
|
134
|
+
const items = this.revisionItems
|
|
135
|
+
.filter((item) => item.revisionId === revision.id)
|
|
136
|
+
.map((item) => ({
|
|
137
|
+
offerKey: item.offerKey,
|
|
138
|
+
price: this.priceEntries.find((price) => price.id === item.priceEntryId),
|
|
139
|
+
}));
|
|
140
|
+
return { revision, items };
|
|
141
|
+
}
|
|
142
|
+
getCatalogRevision(provider, revisionNumber) {
|
|
143
|
+
const revision = this.revisions.find((row) => row.checkoutProvider === provider &&
|
|
144
|
+
row.revision === revisionNumber);
|
|
145
|
+
if (!revision)
|
|
146
|
+
return null;
|
|
147
|
+
const items = this.revisionItems
|
|
148
|
+
.filter((item) => item.revisionId === revision.id)
|
|
149
|
+
.map((item) => {
|
|
150
|
+
const price = this.priceEntries.find((candidate) => candidate.id === item.priceEntryId);
|
|
151
|
+
if (!price)
|
|
152
|
+
throw new Error("catalog_price_missing");
|
|
153
|
+
return { offerKey: item.offerKey, price };
|
|
154
|
+
});
|
|
155
|
+
return { revision, items };
|
|
156
|
+
}
|
|
157
|
+
insertCatalogRevision(revision) {
|
|
158
|
+
if (this.revisions.some((row) => row.revision === revision.revision ||
|
|
159
|
+
(row.checkoutProvider === revision.checkoutProvider &&
|
|
160
|
+
row.status === "active" &&
|
|
161
|
+
revision.status === "active"))) {
|
|
162
|
+
throw new Error("catalog_revision_duplicate");
|
|
163
|
+
}
|
|
164
|
+
this.revisions.push(revision);
|
|
165
|
+
}
|
|
166
|
+
saveCatalogRevision(revision) {
|
|
167
|
+
const existing = this.revisions.find((row) => row.id === revision.id);
|
|
168
|
+
if (!existing)
|
|
169
|
+
throw new Error("catalog_revision_missing");
|
|
170
|
+
Object.assign(existing, revision);
|
|
171
|
+
}
|
|
172
|
+
insertPriceEntry(price) {
|
|
173
|
+
if (this.priceEntries.some((row) => row.id === price.id ||
|
|
174
|
+
(row.provider === price.provider &&
|
|
175
|
+
row.providerProductId === price.providerProductId))) {
|
|
176
|
+
throw new Error("catalog_price_duplicate");
|
|
177
|
+
}
|
|
178
|
+
this.priceEntries.push(price);
|
|
179
|
+
}
|
|
180
|
+
insertCatalogRevisionItem(item) {
|
|
181
|
+
if (this.revisionItems.some((row) => row.id === item.id ||
|
|
182
|
+
(row.revisionId === item.revisionId &&
|
|
183
|
+
(row.offerKey === item.offerKey ||
|
|
184
|
+
row.priceEntryId === item.priceEntryId)))) {
|
|
185
|
+
throw new Error("catalog_revision_item_duplicate");
|
|
186
|
+
}
|
|
187
|
+
this.revisionItems.push(item);
|
|
188
|
+
}
|
|
189
|
+
findPriceByProviderProduct(provider, productId) {
|
|
190
|
+
return this.priceEntries.find((row) => row.provider === provider &&
|
|
191
|
+
row.providerProductId === productId);
|
|
192
|
+
}
|
|
193
|
+
findPriceById(id) {
|
|
194
|
+
return this.priceEntries.find((row) => row.id === id);
|
|
195
|
+
}
|
|
196
|
+
listRevisions() {
|
|
197
|
+
return this.revisions;
|
|
198
|
+
}
|
|
199
|
+
newAttemptId() {
|
|
200
|
+
return `bca_${nanoid(12)}`;
|
|
201
|
+
}
|
|
202
|
+
newChangeId() {
|
|
203
|
+
return `bpc_${nanoid(12)}`;
|
|
204
|
+
}
|
|
205
|
+
savePlanState(state) {
|
|
206
|
+
this.planStates.set(state.billableEntityId, state);
|
|
207
|
+
}
|
|
208
|
+
findCustomerByPayer(provider, payerId) {
|
|
209
|
+
return this.customers.find((row) => row.provider === provider && row.payerId === payerId);
|
|
210
|
+
}
|
|
211
|
+
findCustomerById(id) {
|
|
212
|
+
return this.customers.find((row) => row.id === id);
|
|
213
|
+
}
|
|
214
|
+
findCustomerByProviderCustomerId(provider, providerCustomerId) {
|
|
215
|
+
return this.customers.find((row) => row.provider === provider &&
|
|
216
|
+
row.providerCustomerId === providerCustomerId);
|
|
217
|
+
}
|
|
218
|
+
listCustomersByPayer(payerId) {
|
|
219
|
+
return this.customers.filter((row) => row.payerId === payerId);
|
|
220
|
+
}
|
|
221
|
+
insertCustomer(customer) {
|
|
222
|
+
if (this.customers.some((row) => row.provider === customer.provider &&
|
|
223
|
+
row.payerId === customer.payerId) ||
|
|
224
|
+
this.customers.some((row) => row.idempotencyKey === customer.idempotencyKey)) {
|
|
225
|
+
throw new Error("customer_duplicate");
|
|
226
|
+
}
|
|
227
|
+
this.customers.push(customer);
|
|
228
|
+
}
|
|
229
|
+
saveCustomer(customer) {
|
|
230
|
+
const existing = this.customers.find((row) => row.id === customer.id);
|
|
231
|
+
if (existing)
|
|
232
|
+
Object.assign(existing, customer);
|
|
233
|
+
else
|
|
234
|
+
this.customers.push(customer);
|
|
235
|
+
}
|
|
236
|
+
findLiveCheckout(billableEntityId) {
|
|
237
|
+
return this.checkouts.find((row) => row.billableEntityId === billableEntityId &&
|
|
238
|
+
(row.status === "creating" || row.status === "open"));
|
|
239
|
+
}
|
|
240
|
+
findCheckoutById(id) {
|
|
241
|
+
return this.checkouts.find((row) => row.id === id);
|
|
242
|
+
}
|
|
243
|
+
findCheckoutByAttemptId(attemptId) {
|
|
244
|
+
return this.checkouts.find((row) => row.attemptId === attemptId);
|
|
245
|
+
}
|
|
246
|
+
insertCheckout(attempt) {
|
|
247
|
+
if (this.checkouts.some((row) => row.idempotencyKey === attempt.idempotencyKey) ||
|
|
248
|
+
this.checkouts.some((row) => row.billableEntityId === attempt.billableEntityId &&
|
|
249
|
+
(row.status === "creating" || row.status === "open"))) {
|
|
250
|
+
throw new Error("checkout_duplicate");
|
|
251
|
+
}
|
|
252
|
+
this.checkouts.push(attempt);
|
|
253
|
+
}
|
|
254
|
+
saveCheckout(attempt) {
|
|
255
|
+
const existing = this.checkouts.find((row) => row.id === attempt.id);
|
|
256
|
+
if (existing)
|
|
257
|
+
Object.assign(existing, attempt);
|
|
258
|
+
else
|
|
259
|
+
this.checkouts.push(attempt);
|
|
260
|
+
}
|
|
261
|
+
countCreatingCheckouts() {
|
|
262
|
+
return this.checkouts.filter((row) => row.status === "creating").length;
|
|
263
|
+
}
|
|
264
|
+
listExpiredCheckouts(now, limit) {
|
|
265
|
+
return this.checkouts
|
|
266
|
+
.filter((row) => (row.status === "creating" || row.status === "open") &&
|
|
267
|
+
row.expiresAt.getTime() <= now.getTime())
|
|
268
|
+
.slice(0, limit);
|
|
269
|
+
}
|
|
270
|
+
listPurgeableCheckouts(before, limit) {
|
|
271
|
+
return this.checkouts
|
|
272
|
+
.filter((row) => row.checkoutUrlEncrypted !== null &&
|
|
273
|
+
row.completedAt != null &&
|
|
274
|
+
row.completedAt.getTime() <= before.getTime() &&
|
|
275
|
+
row.status !== "creating" &&
|
|
276
|
+
row.status !== "open")
|
|
277
|
+
.slice(0, limit);
|
|
278
|
+
}
|
|
279
|
+
findEntitlementSubscription(billableEntityId) {
|
|
280
|
+
return this.subscriptions.find((row) => row.billableEntityId === billableEntityId &&
|
|
281
|
+
row.isEntitlementSource);
|
|
282
|
+
}
|
|
283
|
+
listSubscriptionsByEntity(billableEntityId) {
|
|
284
|
+
return this.subscriptions.filter((row) => row.billableEntityId === billableEntityId);
|
|
285
|
+
}
|
|
286
|
+
findSubscriptionById(id) {
|
|
287
|
+
return this.subscriptions.find((row) => row.id === id);
|
|
288
|
+
}
|
|
289
|
+
findSubscriptionByProviderIds(provider, providerSubscriptionId) {
|
|
290
|
+
return this.subscriptions.find((row) => row.provider === provider &&
|
|
291
|
+
row.providerSubscriptionId === providerSubscriptionId);
|
|
292
|
+
}
|
|
293
|
+
listSubscriptionsByPayer(payerId) {
|
|
294
|
+
return this.subscriptions.filter((row) => row.payerId === payerId);
|
|
295
|
+
}
|
|
296
|
+
upsertSubscription(subscription) {
|
|
297
|
+
if (subscription.isEntitlementSource &&
|
|
298
|
+
this.subscriptions.some((row) => row.id !== subscription.id &&
|
|
299
|
+
row.billableEntityId === subscription.billableEntityId &&
|
|
300
|
+
row.isEntitlementSource)) {
|
|
301
|
+
throw new Error("subscription_source_conflict");
|
|
302
|
+
}
|
|
303
|
+
const existing = this.subscriptions.find((row) => row.id === subscription.id);
|
|
304
|
+
if (existing)
|
|
305
|
+
Object.assign(existing, subscription);
|
|
306
|
+
else
|
|
307
|
+
this.subscriptions.push(subscription);
|
|
308
|
+
}
|
|
309
|
+
listDueSubscriptionDeadlines(now, limit) {
|
|
310
|
+
return this.subscriptions
|
|
311
|
+
.filter((row) => row.isEntitlementSource &&
|
|
312
|
+
row.status === "cancelled" &&
|
|
313
|
+
row.cancelAtPeriodEnd &&
|
|
314
|
+
row.paidThroughAt !== null &&
|
|
315
|
+
row.paidThroughAt.getTime() <= now.getTime())
|
|
316
|
+
.slice(0, limit);
|
|
317
|
+
}
|
|
318
|
+
findLivePlanChange(billableEntityId) {
|
|
319
|
+
return this.planChanges.find((row) => row.billableEntityId === billableEntityId &&
|
|
320
|
+
(row.status === "creating" || row.status === "pending"));
|
|
321
|
+
}
|
|
322
|
+
findPlanChangeBySubscriptionOffer(subscriptionId, offerKey) {
|
|
323
|
+
return this.planChanges.find((row) => row.subscriptionId === subscriptionId &&
|
|
324
|
+
(row.status === "pending" || row.status === "creating") &&
|
|
325
|
+
row.targetOfferKey === offerKey);
|
|
326
|
+
}
|
|
327
|
+
findPlanChangeById(id) {
|
|
328
|
+
return this.planChanges.find((row) => row.id === id);
|
|
329
|
+
}
|
|
330
|
+
insertPlanChange(attempt) {
|
|
331
|
+
if (this.planChanges.some((row) => row.idempotencyKey === attempt.idempotencyKey) ||
|
|
332
|
+
this.planChanges.some((row) => row.billableEntityId === attempt.billableEntityId &&
|
|
333
|
+
(row.status === "creating" || row.status === "pending"))) {
|
|
334
|
+
throw new Error("plan_change_duplicate");
|
|
335
|
+
}
|
|
336
|
+
this.planChanges.push(attempt);
|
|
337
|
+
}
|
|
338
|
+
savePlanChange(attempt) {
|
|
339
|
+
const existing = this.planChanges.find((row) => row.id === attempt.id);
|
|
340
|
+
if (existing)
|
|
341
|
+
Object.assign(existing, attempt);
|
|
342
|
+
else
|
|
343
|
+
this.planChanges.push(attempt);
|
|
344
|
+
}
|
|
345
|
+
listPurgeablePlanChanges(before, limit) {
|
|
346
|
+
return this.planChanges
|
|
347
|
+
.filter((row) => row.paymentUrlEncrypted !== null &&
|
|
348
|
+
row.completedAt != null &&
|
|
349
|
+
row.completedAt.getTime() <= before.getTime() &&
|
|
350
|
+
row.status !== "creating" &&
|
|
351
|
+
row.status !== "pending")
|
|
352
|
+
.slice(0, limit);
|
|
353
|
+
}
|
|
354
|
+
findWebhook(provider, providerEventId) {
|
|
355
|
+
return this.webhooks.find((row) => row.provider === provider &&
|
|
356
|
+
row.providerEventId === providerEventId);
|
|
357
|
+
}
|
|
358
|
+
findWebhookByEventId(providerEventId) {
|
|
359
|
+
return this.webhooks.find((row) => row.providerEventId === providerEventId);
|
|
360
|
+
}
|
|
361
|
+
insertWebhook(record) {
|
|
362
|
+
if (this.findWebhook(record.provider, record.providerEventId)) {
|
|
363
|
+
throw new Error("webhook_duplicate");
|
|
364
|
+
}
|
|
365
|
+
this.webhooks.push(record);
|
|
366
|
+
}
|
|
367
|
+
saveWebhook(record) {
|
|
368
|
+
const existing = this.webhooks.find((row) => row.id === record.id);
|
|
369
|
+
if (existing)
|
|
370
|
+
Object.assign(existing, record);
|
|
371
|
+
else
|
|
372
|
+
this.webhooks.push(record);
|
|
373
|
+
}
|
|
374
|
+
saveWebhookIfOwned(record, workerId) {
|
|
375
|
+
const existing = this.webhooks.find((row) => row.id === record.id);
|
|
376
|
+
if (!existing ||
|
|
377
|
+
existing.status !== "processing" ||
|
|
378
|
+
existing.workerId !== workerId ||
|
|
379
|
+
!existing.lockedAt ||
|
|
380
|
+
!record.lockedAt ||
|
|
381
|
+
existing.lockedAt.getTime() !== record.lockedAt.getTime()) {
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
Object.assign(existing, record);
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
listDueWebhooks(now, limit) {
|
|
388
|
+
return this.webhooks
|
|
389
|
+
.filter((row) => {
|
|
390
|
+
if (row.status === "processed" ||
|
|
391
|
+
row.status === "ignored" ||
|
|
392
|
+
row.status === "quarantined") {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
if (row.status === "processing" &&
|
|
396
|
+
row.leaseExpiresAt &&
|
|
397
|
+
row.leaseExpiresAt > now) {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
return row.availableAt <= now;
|
|
401
|
+
})
|
|
402
|
+
.slice(0, limit);
|
|
403
|
+
}
|
|
404
|
+
claimDueWebhooks(now, limit, workerId) {
|
|
405
|
+
const claimed = [];
|
|
406
|
+
for (const row of this.listDueWebhooks(now, limit)) {
|
|
407
|
+
if (claimed.length >= limit)
|
|
408
|
+
break;
|
|
409
|
+
const leaseExpired = row.status === "processing" &&
|
|
410
|
+
(!row.leaseExpiresAt ||
|
|
411
|
+
row.leaseExpiresAt.getTime() <= now.getTime());
|
|
412
|
+
if (!(row.status === "pending" ||
|
|
413
|
+
row.status === "failed" ||
|
|
414
|
+
leaseExpired)) {
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
row.status = "processing";
|
|
418
|
+
row.workerId = workerId;
|
|
419
|
+
row.lockedAt = now;
|
|
420
|
+
row.leaseExpiresAt = new Date(now.getTime() + 5 * 60 * 1000);
|
|
421
|
+
row.processingAttempts += 1;
|
|
422
|
+
claimed.push(structuredClone(row));
|
|
423
|
+
}
|
|
424
|
+
return claimed;
|
|
425
|
+
}
|
|
426
|
+
listPurgeableWebhooks(before, limit) {
|
|
427
|
+
return this.webhooks
|
|
428
|
+
.filter((row) => row.payloadEncrypted != null &&
|
|
429
|
+
row.processedAt != null &&
|
|
430
|
+
row.processedAt.getTime() <= before.getTime() &&
|
|
431
|
+
(row.status === "processed" || row.status === "ignored"))
|
|
432
|
+
.slice(0, limit);
|
|
433
|
+
}
|
|
434
|
+
findLiveJob(subject) {
|
|
435
|
+
if (!subject.checkoutAttemptId &&
|
|
436
|
+
!subject.planChangeAttemptId &&
|
|
437
|
+
!subject.subscriptionId &&
|
|
438
|
+
!subject.providerCustomerId) {
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
441
|
+
return this.jobs.find((job) => {
|
|
442
|
+
if (job.status === "completed" || job.status === "quarantined")
|
|
443
|
+
return false;
|
|
444
|
+
if (job.provider !== subject.provider)
|
|
445
|
+
return false;
|
|
446
|
+
if (subject.checkoutAttemptId)
|
|
447
|
+
return job.checkoutAttemptId === subject.checkoutAttemptId;
|
|
448
|
+
if (subject.planChangeAttemptId)
|
|
449
|
+
return job.planChangeAttemptId === subject.planChangeAttemptId;
|
|
450
|
+
if (subject.subscriptionId)
|
|
451
|
+
return job.subscriptionId === subject.subscriptionId;
|
|
452
|
+
return job.providerCustomerId === subject.providerCustomerId;
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
insertJob(job) {
|
|
456
|
+
if (job.operation !== undefined &&
|
|
457
|
+
job.operation !== "reconcile" &&
|
|
458
|
+
job.operation !== "cancellation") {
|
|
459
|
+
throw new Error("job_operation_invalid");
|
|
460
|
+
}
|
|
461
|
+
if (job.operation === "cancellation" && !job.subscriptionId) {
|
|
462
|
+
throw new Error("job_operation_subject_invalid");
|
|
463
|
+
}
|
|
464
|
+
const subjectCount = [
|
|
465
|
+
job.checkoutAttemptId,
|
|
466
|
+
job.planChangeAttemptId,
|
|
467
|
+
job.subscriptionId,
|
|
468
|
+
job.providerCustomerId,
|
|
469
|
+
].filter(Boolean).length;
|
|
470
|
+
if (subjectCount !== 1)
|
|
471
|
+
throw new Error("job_subject_invalid");
|
|
472
|
+
if (this.findLiveJob(job))
|
|
473
|
+
throw new Error("job_duplicate");
|
|
474
|
+
this.jobs.push(job);
|
|
475
|
+
}
|
|
476
|
+
saveJob(job) {
|
|
477
|
+
if (job.operation !== undefined &&
|
|
478
|
+
job.operation !== "reconcile" &&
|
|
479
|
+
job.operation !== "cancellation") {
|
|
480
|
+
throw new Error("job_operation_invalid");
|
|
481
|
+
}
|
|
482
|
+
if (job.operation === "cancellation" && !job.subscriptionId) {
|
|
483
|
+
throw new Error("job_operation_subject_invalid");
|
|
484
|
+
}
|
|
485
|
+
const existing = this.jobs.find((row) => row.id === job.id);
|
|
486
|
+
if (existing)
|
|
487
|
+
Object.assign(existing, job);
|
|
488
|
+
else
|
|
489
|
+
this.jobs.push(job);
|
|
490
|
+
}
|
|
491
|
+
saveJobIfOwned(job, workerId) {
|
|
492
|
+
if (job.operation !== undefined &&
|
|
493
|
+
job.operation !== "reconcile" &&
|
|
494
|
+
job.operation !== "cancellation") {
|
|
495
|
+
throw new Error("job_operation_invalid");
|
|
496
|
+
}
|
|
497
|
+
if (job.operation === "cancellation" && !job.subscriptionId) {
|
|
498
|
+
throw new Error("job_operation_subject_invalid");
|
|
499
|
+
}
|
|
500
|
+
const existing = this.jobs.find((row) => row.id === job.id);
|
|
501
|
+
if (!existing ||
|
|
502
|
+
existing.status !== "processing" ||
|
|
503
|
+
existing.workerId !== workerId ||
|
|
504
|
+
!existing.lockedAt ||
|
|
505
|
+
!job.lockedAt ||
|
|
506
|
+
existing.lockedAt.getTime() !== job.lockedAt.getTime()) {
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
509
|
+
Object.assign(existing, job);
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
512
|
+
listClaimableJobs(now, limit) {
|
|
513
|
+
const claimed = [];
|
|
514
|
+
for (const job of this.jobs) {
|
|
515
|
+
if (claimed.length >= limit)
|
|
516
|
+
break;
|
|
517
|
+
const leaseExpired = job.status === "processing" &&
|
|
518
|
+
(!job.leaseExpiresAt ||
|
|
519
|
+
job.leaseExpiresAt.getTime() <= now.getTime());
|
|
520
|
+
const claimable = (job.status === "pending" ||
|
|
521
|
+
job.status === "failed" ||
|
|
522
|
+
leaseExpired) &&
|
|
523
|
+
job.availableAt.getTime() <= now.getTime();
|
|
524
|
+
if (claimable)
|
|
525
|
+
claimed.push(job);
|
|
526
|
+
}
|
|
527
|
+
return claimed;
|
|
528
|
+
}
|
|
529
|
+
claimClaimableJobs(now, limit, workerId) {
|
|
530
|
+
const claimed = [];
|
|
531
|
+
for (const job of this.listClaimableJobs(now, limit)) {
|
|
532
|
+
if (claimed.length >= limit)
|
|
533
|
+
break;
|
|
534
|
+
const leaseExpired = job.status === "processing" &&
|
|
535
|
+
(!job.leaseExpiresAt ||
|
|
536
|
+
job.leaseExpiresAt.getTime() <= now.getTime());
|
|
537
|
+
if (!(job.status === "pending" ||
|
|
538
|
+
job.status === "failed" ||
|
|
539
|
+
leaseExpired)) {
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
job.status = "processing";
|
|
543
|
+
job.workerId = workerId;
|
|
544
|
+
job.lockedAt = now;
|
|
545
|
+
job.leaseExpiresAt = new Date(now.getTime() + 5 * 60 * 1000);
|
|
546
|
+
job.attemptCount += 1;
|
|
547
|
+
claimed.push(structuredClone(job));
|
|
548
|
+
}
|
|
549
|
+
return claimed;
|
|
550
|
+
}
|
|
551
|
+
health(_now, provider) {
|
|
552
|
+
const pendingInbox = this.webhooks.filter((row) => row.status === "pending" || row.status === "failed");
|
|
553
|
+
const oldest = pendingInbox
|
|
554
|
+
.map((row) => row.occurredAt)
|
|
555
|
+
.sort((a, b) => a.getTime() - b.getTime())[0];
|
|
556
|
+
const active = this.revisions.find((row) => row.status === "active" &&
|
|
557
|
+
(provider === undefined || row.checkoutProvider === provider));
|
|
558
|
+
return {
|
|
559
|
+
oldestPendingInboxOccurredAt: oldest ?? null,
|
|
560
|
+
quarantinedWebhooks: this.webhooks.filter((row) => row.status === "quarantined").length,
|
|
561
|
+
quarantinedJobs: this.jobs.filter((row) => row.status === "quarantined").length,
|
|
562
|
+
stuckCreatingCheckouts: this.checkouts.filter((row) => row.status === "creating").length,
|
|
563
|
+
stuckCreatingCustomers: this.customers.filter((row) => row.status === "creating").length,
|
|
564
|
+
unreconciledSubscriptions: this.subscriptions.filter((row) => !row.lastReconciledAt).length,
|
|
565
|
+
activeRevision: active?.revision ?? null,
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
export function newId() {
|
|
570
|
+
return randomUUID();
|
|
571
|
+
}
|
|
572
|
+
export function applySubscriptionProjection(store, next, now, options = {}) {
|
|
573
|
+
return Promise.resolve(applySubscriptionProjectionSync(store, next, now, options));
|
|
574
|
+
}
|
|
575
|
+
async function applySubscriptionProjectionSync(store, next, now, options = {}) {
|
|
576
|
+
const state = await store.ensurePlanState(next.billableEntityId);
|
|
577
|
+
const existing = await store.findSubscriptionById(next.id);
|
|
578
|
+
const previousMaterial = existing
|
|
579
|
+
? {
|
|
580
|
+
status: existing.status,
|
|
581
|
+
providerProductId: existing.providerProductId,
|
|
582
|
+
priceEntryId: existing.priceEntryId,
|
|
583
|
+
catalogRevision: existing.catalogRevision,
|
|
584
|
+
offerKey: existing.offerKey,
|
|
585
|
+
plan: existing.plan,
|
|
586
|
+
interval: existing.interval,
|
|
587
|
+
currentPeriodStartsAt: existing.currentPeriodStartsAt,
|
|
588
|
+
currentPeriodEndsAt: existing.currentPeriodEndsAt,
|
|
589
|
+
paidThroughAt: existing.paidThroughAt,
|
|
590
|
+
trialEndsAt: existing.trialEndsAt,
|
|
591
|
+
cancelAtPeriodEnd: existing.cancelAtPeriodEnd,
|
|
592
|
+
activeSubscriptionId: state.activeSubscriptionId,
|
|
593
|
+
}
|
|
594
|
+
: null;
|
|
595
|
+
const nextMaterial = {
|
|
596
|
+
status: next.status,
|
|
597
|
+
providerProductId: next.providerProductId,
|
|
598
|
+
priceEntryId: next.priceEntryId,
|
|
599
|
+
catalogRevision: next.catalogRevision,
|
|
600
|
+
offerKey: next.offerKey,
|
|
601
|
+
plan: next.plan,
|
|
602
|
+
interval: next.interval,
|
|
603
|
+
currentPeriodStartsAt: next.currentPeriodStartsAt,
|
|
604
|
+
currentPeriodEndsAt: next.currentPeriodEndsAt,
|
|
605
|
+
paidThroughAt: next.paidThroughAt,
|
|
606
|
+
trialEndsAt: next.trialEndsAt,
|
|
607
|
+
cancelAtPeriodEnd: next.cancelAtPeriodEnd,
|
|
608
|
+
activeSubscriptionId: next.isEntitlementSource
|
|
609
|
+
? next.id
|
|
610
|
+
: state.activeSubscriptionId === next.id
|
|
611
|
+
? null
|
|
612
|
+
: state.activeSubscriptionId,
|
|
613
|
+
};
|
|
614
|
+
const material = !previousMaterial ||
|
|
615
|
+
JSON.stringify(serializeMaterial(previousMaterial)) !==
|
|
616
|
+
JSON.stringify(serializeMaterial(nextMaterial));
|
|
617
|
+
const freshness = {
|
|
618
|
+
lastObservedAt: now,
|
|
619
|
+
lastReconciledAt: options.reconcile
|
|
620
|
+
? now
|
|
621
|
+
: (existing?.lastReconciledAt ?? null),
|
|
622
|
+
};
|
|
623
|
+
const merged = {
|
|
624
|
+
...(existing ?? next),
|
|
625
|
+
...next,
|
|
626
|
+
lastObservedAt: freshness.lastObservedAt,
|
|
627
|
+
lastReconciledAt: freshness.lastReconciledAt,
|
|
628
|
+
};
|
|
629
|
+
await store.upsertSubscription(merged);
|
|
630
|
+
const stored = (await store.findSubscriptionById(merged.id)) ?? merged;
|
|
631
|
+
if (material) {
|
|
632
|
+
state.projectionVersion += 1;
|
|
633
|
+
state.activeSubscriptionId = nextMaterial.activeSubscriptionId;
|
|
634
|
+
await store.savePlanState(state);
|
|
635
|
+
const effectId = `projection:${stored.id}:${state.projectionVersion}`;
|
|
636
|
+
return {
|
|
637
|
+
subscription: stored,
|
|
638
|
+
planState: state,
|
|
639
|
+
material: true,
|
|
640
|
+
effectId,
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
return {
|
|
644
|
+
subscription: stored,
|
|
645
|
+
planState: state,
|
|
646
|
+
material: false,
|
|
647
|
+
effectId: null,
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
function serializeMaterial(value) {
|
|
651
|
+
const out = {};
|
|
652
|
+
for (const [key, item] of Object.entries(value)) {
|
|
653
|
+
out[key] = item instanceof Date ? item.toISOString() : item;
|
|
654
|
+
}
|
|
655
|
+
return out;
|
|
656
|
+
}
|
|
657
|
+
//# sourceMappingURL=memory.js.map
|