stripe-experiment-sync 1.0.6 → 1.0.7
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/README.md +25 -4
- package/dist/chunk-3P5TZKWU.js +595 -0
- package/dist/chunk-7JWRDXNB.js +3264 -0
- package/dist/chunk-SX3HLE4H.js +406 -0
- package/dist/{chunk-3UERGK2O.js → chunk-YXQZXR7S.js} +18 -6
- package/dist/cli/index.cjs +4376 -0
- package/dist/cli/index.d.cts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +55 -0
- package/dist/cli/lib.cjs +4359 -0
- package/dist/cli/lib.d.cts +70 -0
- package/dist/cli/lib.d.ts +70 -0
- package/dist/cli/lib.js +21 -0
- package/dist/index.cjs +48 -36
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +7 -3255
- package/dist/migrations/0057_rename_sync_tables.sql +57 -0
- package/dist/supabase/index.cjs +29 -7
- package/dist/supabase/index.js +12 -382
- package/package.json +18 -6
package/dist/index.js
CHANGED
|
@@ -1,3259 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// src/database/postgres.ts
|
|
10
|
-
import pg from "pg";
|
|
11
|
-
import { pg as sql } from "yesql";
|
|
12
|
-
var ORDERED_STRIPE_TABLES = [
|
|
13
|
-
"subscription_items",
|
|
14
|
-
"subscriptions",
|
|
15
|
-
"subscription_schedules",
|
|
16
|
-
"checkout_session_line_items",
|
|
17
|
-
"checkout_sessions",
|
|
18
|
-
"tax_ids",
|
|
19
|
-
"charges",
|
|
20
|
-
"refunds",
|
|
21
|
-
"credit_notes",
|
|
22
|
-
"disputes",
|
|
23
|
-
"early_fraud_warnings",
|
|
24
|
-
"invoices",
|
|
25
|
-
"payment_intents",
|
|
26
|
-
"payment_methods",
|
|
27
|
-
"setup_intents",
|
|
28
|
-
"prices",
|
|
29
|
-
"plans",
|
|
30
|
-
"products",
|
|
31
|
-
"features",
|
|
32
|
-
"active_entitlements",
|
|
33
|
-
"reviews",
|
|
34
|
-
"_managed_webhooks",
|
|
35
|
-
"customers",
|
|
36
|
-
"_sync_obj_run",
|
|
37
|
-
// Must be deleted before _sync_run (foreign key)
|
|
38
|
-
"_sync_run"
|
|
39
|
-
];
|
|
40
|
-
var TABLES_WITH_ACCOUNT_ID = /* @__PURE__ */ new Set(["_managed_webhooks"]);
|
|
41
|
-
var PostgresClient = class {
|
|
42
|
-
constructor(config) {
|
|
43
|
-
this.config = config;
|
|
44
|
-
this.pool = new pg.Pool(config.poolConfig);
|
|
45
|
-
}
|
|
46
|
-
pool;
|
|
47
|
-
async delete(table, id) {
|
|
48
|
-
const prepared = sql(`
|
|
49
|
-
delete from "${this.config.schema}"."${table}"
|
|
50
|
-
where id = :id
|
|
51
|
-
returning id;
|
|
52
|
-
`)({ id });
|
|
53
|
-
const { rows } = await this.query(prepared.text, prepared.values);
|
|
54
|
-
return rows.length > 0;
|
|
55
|
-
}
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
|
-
async query(text, params) {
|
|
58
|
-
return this.pool.query(text, params);
|
|
59
|
-
}
|
|
60
|
-
async upsertMany(entries, table) {
|
|
61
|
-
if (!entries.length) return [];
|
|
62
|
-
const chunkSize = 5;
|
|
63
|
-
const results = [];
|
|
64
|
-
for (let i = 0; i < entries.length; i += chunkSize) {
|
|
65
|
-
const chunk = entries.slice(i, i + chunkSize);
|
|
66
|
-
const queries = [];
|
|
67
|
-
chunk.forEach((entry) => {
|
|
68
|
-
const rawData = JSON.stringify(entry);
|
|
69
|
-
const upsertSql = `
|
|
70
|
-
INSERT INTO "${this.config.schema}"."${table}" ("_raw_data")
|
|
71
|
-
VALUES ($1::jsonb)
|
|
72
|
-
ON CONFLICT (id)
|
|
73
|
-
DO UPDATE SET
|
|
74
|
-
"_raw_data" = EXCLUDED."_raw_data"
|
|
75
|
-
RETURNING *
|
|
76
|
-
`;
|
|
77
|
-
queries.push(this.pool.query(upsertSql, [rawData]));
|
|
78
|
-
});
|
|
79
|
-
results.push(...await Promise.all(queries));
|
|
80
|
-
}
|
|
81
|
-
return results.flatMap((it) => it.rows);
|
|
82
|
-
}
|
|
83
|
-
async upsertManyWithTimestampProtection(entries, table, accountId, syncTimestamp) {
|
|
84
|
-
const timestamp = syncTimestamp || (/* @__PURE__ */ new Date()).toISOString();
|
|
85
|
-
if (!entries.length) return [];
|
|
86
|
-
const chunkSize = 5;
|
|
87
|
-
const results = [];
|
|
88
|
-
for (let i = 0; i < entries.length; i += chunkSize) {
|
|
89
|
-
const chunk = entries.slice(i, i + chunkSize);
|
|
90
|
-
const queries = [];
|
|
91
|
-
chunk.forEach((entry) => {
|
|
92
|
-
if (table.startsWith("_")) {
|
|
93
|
-
const columns = Object.keys(entry).filter(
|
|
94
|
-
(k) => k !== "last_synced_at" && k !== "account_id"
|
|
95
|
-
);
|
|
96
|
-
const upsertSql = `
|
|
97
|
-
INSERT INTO "${this.config.schema}"."${table}" (
|
|
98
|
-
${columns.map((c) => `"${c}"`).join(", ")}, "last_synced_at", "account_id"
|
|
99
|
-
)
|
|
100
|
-
VALUES (
|
|
101
|
-
${columns.map((c) => `:${c}`).join(", ")}, :last_synced_at, :account_id
|
|
102
|
-
)
|
|
103
|
-
ON CONFLICT ("id")
|
|
104
|
-
DO UPDATE SET
|
|
105
|
-
${columns.map((c) => `"${c}" = EXCLUDED."${c}"`).join(", ")},
|
|
106
|
-
"last_synced_at" = :last_synced_at,
|
|
107
|
-
"account_id" = EXCLUDED."account_id"
|
|
108
|
-
WHERE "${table}"."last_synced_at" IS NULL
|
|
109
|
-
OR "${table}"."last_synced_at" < :last_synced_at
|
|
110
|
-
RETURNING *
|
|
111
|
-
`;
|
|
112
|
-
const cleansed = this.cleanseArrayField(entry);
|
|
113
|
-
cleansed.last_synced_at = timestamp;
|
|
114
|
-
cleansed.account_id = accountId;
|
|
115
|
-
const prepared = sql(upsertSql, { useNullForMissing: true })(cleansed);
|
|
116
|
-
queries.push(this.pool.query(prepared.text, prepared.values));
|
|
117
|
-
} else {
|
|
118
|
-
const rawData = JSON.stringify(entry);
|
|
119
|
-
const upsertSql = `
|
|
120
|
-
INSERT INTO "${this.config.schema}"."${table}" ("_raw_data", "_last_synced_at", "_account_id")
|
|
121
|
-
VALUES ($1::jsonb, $2, $3)
|
|
122
|
-
ON CONFLICT (id)
|
|
123
|
-
DO UPDATE SET
|
|
124
|
-
"_raw_data" = EXCLUDED."_raw_data",
|
|
125
|
-
"_last_synced_at" = $2,
|
|
126
|
-
"_account_id" = EXCLUDED."_account_id"
|
|
127
|
-
WHERE "${table}"."_last_synced_at" IS NULL
|
|
128
|
-
OR "${table}"."_last_synced_at" < $2
|
|
129
|
-
RETURNING *
|
|
130
|
-
`;
|
|
131
|
-
queries.push(this.pool.query(upsertSql, [rawData, timestamp, accountId]));
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
results.push(...await Promise.all(queries));
|
|
135
|
-
}
|
|
136
|
-
return results.flatMap((it) => it.rows);
|
|
137
|
-
}
|
|
138
|
-
cleanseArrayField(obj) {
|
|
139
|
-
const cleansed = { ...obj };
|
|
140
|
-
Object.keys(cleansed).map((k) => {
|
|
141
|
-
const data = cleansed[k];
|
|
142
|
-
if (Array.isArray(data)) {
|
|
143
|
-
cleansed[k] = JSON.stringify(data);
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
return cleansed;
|
|
147
|
-
}
|
|
148
|
-
async findMissingEntries(table, ids) {
|
|
149
|
-
if (!ids.length) return [];
|
|
150
|
-
const prepared = sql(`
|
|
151
|
-
select id from "${this.config.schema}"."${table}"
|
|
152
|
-
where id=any(:ids::text[]);
|
|
153
|
-
`)({ ids });
|
|
154
|
-
const { rows } = await this.query(prepared.text, prepared.values);
|
|
155
|
-
const existingIds = rows.map((it) => it.id);
|
|
156
|
-
const missingIds = ids.filter((it) => !existingIds.includes(it));
|
|
157
|
-
return missingIds;
|
|
158
|
-
}
|
|
159
|
-
// Account management methods
|
|
160
|
-
async upsertAccount(accountData, apiKeyHash) {
|
|
161
|
-
const rawData = JSON.stringify(accountData.raw_data);
|
|
162
|
-
await this.query(
|
|
163
|
-
`INSERT INTO "${this.config.schema}"."accounts" ("_raw_data", "api_key_hashes", "first_synced_at", "_last_synced_at")
|
|
164
|
-
VALUES ($1::jsonb, ARRAY[$2], now(), now())
|
|
165
|
-
ON CONFLICT (id)
|
|
166
|
-
DO UPDATE SET
|
|
167
|
-
"_raw_data" = EXCLUDED."_raw_data",
|
|
168
|
-
"api_key_hashes" = (
|
|
169
|
-
SELECT ARRAY(
|
|
170
|
-
SELECT DISTINCT unnest(
|
|
171
|
-
COALESCE("${this.config.schema}"."accounts"."api_key_hashes", '{}') || ARRAY[$2]
|
|
172
|
-
)
|
|
173
|
-
)
|
|
174
|
-
),
|
|
175
|
-
"_last_synced_at" = now(),
|
|
176
|
-
"_updated_at" = now()`,
|
|
177
|
-
[rawData, apiKeyHash]
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
181
|
-
async getAllAccounts() {
|
|
182
|
-
const result = await this.query(
|
|
183
|
-
`SELECT _raw_data FROM "${this.config.schema}"."accounts"
|
|
184
|
-
ORDER BY _last_synced_at DESC`
|
|
185
|
-
);
|
|
186
|
-
return result.rows.map((row) => row._raw_data);
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Looks up an account ID by API key hash
|
|
190
|
-
* Uses the GIN index on api_key_hashes for fast lookups
|
|
191
|
-
* @param apiKeyHash - SHA-256 hash of the Stripe API key
|
|
192
|
-
* @returns Account ID if found, null otherwise
|
|
193
|
-
*/
|
|
194
|
-
async getAccountIdByApiKeyHash(apiKeyHash) {
|
|
195
|
-
const result = await this.query(
|
|
196
|
-
`SELECT id FROM "${this.config.schema}"."accounts"
|
|
197
|
-
WHERE $1 = ANY(api_key_hashes)
|
|
198
|
-
LIMIT 1`,
|
|
199
|
-
[apiKeyHash]
|
|
200
|
-
);
|
|
201
|
-
return result.rows.length > 0 ? result.rows[0].id : null;
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Looks up full account data by API key hash
|
|
205
|
-
* @param apiKeyHash - SHA-256 hash of the Stripe API key
|
|
206
|
-
* @returns Account raw data if found, null otherwise
|
|
207
|
-
*/
|
|
208
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
209
|
-
async getAccountByApiKeyHash(apiKeyHash) {
|
|
210
|
-
const result = await this.query(
|
|
211
|
-
`SELECT _raw_data FROM "${this.config.schema}"."accounts"
|
|
212
|
-
WHERE $1 = ANY(api_key_hashes)
|
|
213
|
-
LIMIT 1`,
|
|
214
|
-
[apiKeyHash]
|
|
215
|
-
);
|
|
216
|
-
return result.rows.length > 0 ? result.rows[0]._raw_data : null;
|
|
217
|
-
}
|
|
218
|
-
getAccountIdColumn(table) {
|
|
219
|
-
return TABLES_WITH_ACCOUNT_ID.has(table) ? "account_id" : "_account_id";
|
|
220
|
-
}
|
|
221
|
-
async getAccountRecordCounts(accountId) {
|
|
222
|
-
const counts = {};
|
|
223
|
-
for (const table of ORDERED_STRIPE_TABLES) {
|
|
224
|
-
const accountIdColumn = this.getAccountIdColumn(table);
|
|
225
|
-
const result = await this.query(
|
|
226
|
-
`SELECT COUNT(*) as count FROM "${this.config.schema}"."${table}"
|
|
227
|
-
WHERE "${accountIdColumn}" = $1`,
|
|
228
|
-
[accountId]
|
|
229
|
-
);
|
|
230
|
-
counts[table] = parseInt(result.rows[0].count);
|
|
231
|
-
}
|
|
232
|
-
return counts;
|
|
233
|
-
}
|
|
234
|
-
async deleteAccountWithCascade(accountId, useTransaction) {
|
|
235
|
-
const deletionCounts = {};
|
|
236
|
-
try {
|
|
237
|
-
if (useTransaction) {
|
|
238
|
-
await this.query("BEGIN");
|
|
239
|
-
}
|
|
240
|
-
for (const table of ORDERED_STRIPE_TABLES) {
|
|
241
|
-
const accountIdColumn = this.getAccountIdColumn(table);
|
|
242
|
-
const result = await this.query(
|
|
243
|
-
`DELETE FROM "${this.config.schema}"."${table}"
|
|
244
|
-
WHERE "${accountIdColumn}" = $1`,
|
|
245
|
-
[accountId]
|
|
246
|
-
);
|
|
247
|
-
deletionCounts[table] = result.rowCount || 0;
|
|
248
|
-
}
|
|
249
|
-
const accountResult = await this.query(
|
|
250
|
-
`DELETE FROM "${this.config.schema}"."accounts"
|
|
251
|
-
WHERE "id" = $1`,
|
|
252
|
-
[accountId]
|
|
253
|
-
);
|
|
254
|
-
deletionCounts["accounts"] = accountResult.rowCount || 0;
|
|
255
|
-
if (useTransaction) {
|
|
256
|
-
await this.query("COMMIT");
|
|
257
|
-
}
|
|
258
|
-
} catch (error) {
|
|
259
|
-
if (useTransaction) {
|
|
260
|
-
await this.query("ROLLBACK");
|
|
261
|
-
}
|
|
262
|
-
throw error;
|
|
263
|
-
}
|
|
264
|
-
return deletionCounts;
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Hash a string to a 32-bit integer for use with PostgreSQL advisory locks.
|
|
268
|
-
* Uses a simple hash algorithm that produces consistent results.
|
|
269
|
-
*/
|
|
270
|
-
hashToInt32(key) {
|
|
271
|
-
let hash = 0;
|
|
272
|
-
for (let i = 0; i < key.length; i++) {
|
|
273
|
-
const char = key.charCodeAt(i);
|
|
274
|
-
hash = (hash << 5) - hash + char;
|
|
275
|
-
hash = hash & hash;
|
|
276
|
-
}
|
|
277
|
-
return hash;
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Acquire a PostgreSQL advisory lock for the given key.
|
|
281
|
-
* This lock is automatically released when the connection is closed or explicitly released.
|
|
282
|
-
* Advisory locks are session-level and will block until the lock is available.
|
|
283
|
-
*
|
|
284
|
-
* @param key - A string key to lock on (will be hashed to an integer)
|
|
285
|
-
*/
|
|
286
|
-
async acquireAdvisoryLock(key) {
|
|
287
|
-
const lockId = this.hashToInt32(key);
|
|
288
|
-
await this.query("SELECT pg_advisory_lock($1)", [lockId]);
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Release a PostgreSQL advisory lock for the given key.
|
|
292
|
-
*
|
|
293
|
-
* @param key - The same string key used to acquire the lock
|
|
294
|
-
*/
|
|
295
|
-
async releaseAdvisoryLock(key) {
|
|
296
|
-
const lockId = this.hashToInt32(key);
|
|
297
|
-
await this.query("SELECT pg_advisory_unlock($1)", [lockId]);
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Execute a function while holding an advisory lock.
|
|
301
|
-
* The lock is automatically released after the function completes (success or error).
|
|
302
|
-
*
|
|
303
|
-
* IMPORTANT: This acquires a dedicated connection from the pool and holds it for the
|
|
304
|
-
* duration of the function execution. PostgreSQL advisory locks are session-level,
|
|
305
|
-
* so we must use the same connection for lock acquisition, operations, and release.
|
|
306
|
-
*
|
|
307
|
-
* @param key - A string key to lock on (will be hashed to an integer)
|
|
308
|
-
* @param fn - The function to execute while holding the lock
|
|
309
|
-
* @returns The result of the function
|
|
310
|
-
*/
|
|
311
|
-
async withAdvisoryLock(key, fn) {
|
|
312
|
-
const lockId = this.hashToInt32(key);
|
|
313
|
-
const client = await this.pool.connect();
|
|
314
|
-
try {
|
|
315
|
-
await client.query("SELECT pg_advisory_lock($1)", [lockId]);
|
|
316
|
-
return await fn();
|
|
317
|
-
} finally {
|
|
318
|
-
try {
|
|
319
|
-
await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
|
|
320
|
-
} finally {
|
|
321
|
-
client.release();
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
// =============================================================================
|
|
326
|
-
// Observable Sync System Methods
|
|
327
|
-
// =============================================================================
|
|
328
|
-
// These methods support long-running syncs with full observability.
|
|
329
|
-
// Uses two tables: _sync_run (parent) and _sync_obj_run (children)
|
|
330
|
-
// RunKey = (accountId, runStartedAt) - natural composite key
|
|
331
|
-
/**
|
|
332
|
-
* Cancel stale runs (running but no object updated in 5 minutes).
|
|
333
|
-
* Called before creating a new run to clean up crashed syncs.
|
|
334
|
-
* Only cancels runs that have objects AND none have recent activity.
|
|
335
|
-
* Runs without objects yet (just created) are not considered stale.
|
|
336
|
-
*/
|
|
337
|
-
async cancelStaleRuns(accountId) {
|
|
338
|
-
await this.query(
|
|
339
|
-
`UPDATE "${this.config.schema}"."_sync_obj_run" o
|
|
340
|
-
SET status = 'error',
|
|
341
|
-
error_message = 'Auto-cancelled: stale (no update in 5 min)',
|
|
342
|
-
completed_at = now()
|
|
343
|
-
WHERE o."_account_id" = $1
|
|
344
|
-
AND o.status = 'running'
|
|
345
|
-
AND o.updated_at < now() - interval '5 minutes'`,
|
|
346
|
-
[accountId]
|
|
347
|
-
);
|
|
348
|
-
await this.query(
|
|
349
|
-
`UPDATE "${this.config.schema}"."_sync_run" r
|
|
350
|
-
SET closed_at = now()
|
|
351
|
-
WHERE r."_account_id" = $1
|
|
352
|
-
AND r.closed_at IS NULL
|
|
353
|
-
AND EXISTS (
|
|
354
|
-
SELECT 1 FROM "${this.config.schema}"."_sync_obj_run" o
|
|
355
|
-
WHERE o."_account_id" = r."_account_id"
|
|
356
|
-
AND o.run_started_at = r.started_at
|
|
357
|
-
)
|
|
358
|
-
AND NOT EXISTS (
|
|
359
|
-
SELECT 1 FROM "${this.config.schema}"."_sync_obj_run" o
|
|
360
|
-
WHERE o."_account_id" = r."_account_id"
|
|
361
|
-
AND o.run_started_at = r.started_at
|
|
362
|
-
AND o.status IN ('pending', 'running')
|
|
363
|
-
)`,
|
|
364
|
-
[accountId]
|
|
365
|
-
);
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* Get or create a sync run for this account.
|
|
369
|
-
* Returns existing run if one is active, otherwise creates new one.
|
|
370
|
-
* Auto-cancels stale runs before checking.
|
|
371
|
-
*
|
|
372
|
-
* @returns RunKey with isNew flag, or null if constraint violation (race condition)
|
|
373
|
-
*/
|
|
374
|
-
async getOrCreateSyncRun(accountId, triggeredBy) {
|
|
375
|
-
await this.cancelStaleRuns(accountId);
|
|
376
|
-
const existing = await this.query(
|
|
377
|
-
`SELECT "_account_id", started_at FROM "${this.config.schema}"."_sync_run"
|
|
378
|
-
WHERE "_account_id" = $1 AND closed_at IS NULL`,
|
|
379
|
-
[accountId]
|
|
380
|
-
);
|
|
381
|
-
if (existing.rows.length > 0) {
|
|
382
|
-
const row = existing.rows[0];
|
|
383
|
-
return { accountId: row._account_id, runStartedAt: row.started_at, isNew: false };
|
|
384
|
-
}
|
|
385
|
-
try {
|
|
386
|
-
const result = await this.query(
|
|
387
|
-
`INSERT INTO "${this.config.schema}"."_sync_run" ("_account_id", triggered_by, started_at)
|
|
388
|
-
VALUES ($1, $2, date_trunc('milliseconds', now()))
|
|
389
|
-
RETURNING "_account_id", started_at`,
|
|
390
|
-
[accountId, triggeredBy ?? null]
|
|
391
|
-
);
|
|
392
|
-
const row = result.rows[0];
|
|
393
|
-
return { accountId: row._account_id, runStartedAt: row.started_at, isNew: true };
|
|
394
|
-
} catch (error) {
|
|
395
|
-
if (error instanceof Error && "code" in error && error.code === "23P01") {
|
|
396
|
-
return null;
|
|
397
|
-
}
|
|
398
|
-
throw error;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Get the active sync run for an account (if any).
|
|
403
|
-
*/
|
|
404
|
-
async getActiveSyncRun(accountId) {
|
|
405
|
-
const result = await this.query(
|
|
406
|
-
`SELECT "_account_id", started_at FROM "${this.config.schema}"."_sync_run"
|
|
407
|
-
WHERE "_account_id" = $1 AND closed_at IS NULL`,
|
|
408
|
-
[accountId]
|
|
409
|
-
);
|
|
410
|
-
if (result.rows.length === 0) return null;
|
|
411
|
-
const row = result.rows[0];
|
|
412
|
-
return { accountId: row._account_id, runStartedAt: row.started_at };
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Get sync run config (for concurrency control).
|
|
416
|
-
* Status is derived from sync_dashboard view.
|
|
417
|
-
*/
|
|
418
|
-
async getSyncRun(accountId, runStartedAt) {
|
|
419
|
-
const result = await this.query(
|
|
420
|
-
`SELECT "_account_id", started_at, max_concurrent, closed_at
|
|
421
|
-
FROM "${this.config.schema}"."_sync_run"
|
|
422
|
-
WHERE "_account_id" = $1 AND started_at = $2`,
|
|
423
|
-
[accountId, runStartedAt]
|
|
424
|
-
);
|
|
425
|
-
if (result.rows.length === 0) return null;
|
|
426
|
-
const row = result.rows[0];
|
|
427
|
-
return {
|
|
428
|
-
accountId: row._account_id,
|
|
429
|
-
runStartedAt: row.started_at,
|
|
430
|
-
maxConcurrent: row.max_concurrent,
|
|
431
|
-
closedAt: row.closed_at
|
|
432
|
-
};
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Close a sync run (mark as done).
|
|
436
|
-
* Status (complete/error) is derived from object run states.
|
|
437
|
-
*/
|
|
438
|
-
async closeSyncRun(accountId, runStartedAt) {
|
|
439
|
-
await this.query(
|
|
440
|
-
`UPDATE "${this.config.schema}"."_sync_run"
|
|
441
|
-
SET closed_at = now()
|
|
442
|
-
WHERE "_account_id" = $1 AND started_at = $2 AND closed_at IS NULL`,
|
|
443
|
-
[accountId, runStartedAt]
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Create object run entries for a sync run.
|
|
448
|
-
* All objects start as 'pending'.
|
|
449
|
-
*/
|
|
450
|
-
async createObjectRuns(accountId, runStartedAt, objects) {
|
|
451
|
-
if (objects.length === 0) return;
|
|
452
|
-
const values = objects.map((_, i) => `($1, $2, $${i + 3})`).join(", ");
|
|
453
|
-
await this.query(
|
|
454
|
-
`INSERT INTO "${this.config.schema}"."_sync_obj_run" ("_account_id", run_started_at, object)
|
|
455
|
-
VALUES ${values}
|
|
456
|
-
ON CONFLICT ("_account_id", run_started_at, object) DO NOTHING`,
|
|
457
|
-
[accountId, runStartedAt, ...objects]
|
|
458
|
-
);
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Try to start an object sync (respects max_concurrent).
|
|
462
|
-
* Returns true if claimed, false if already running or at concurrency limit.
|
|
463
|
-
*
|
|
464
|
-
* Note: There's a small race window where concurrent calls could result in
|
|
465
|
-
* max_concurrent + 1 objects running. This is acceptable behavior.
|
|
466
|
-
*/
|
|
467
|
-
async tryStartObjectSync(accountId, runStartedAt, object) {
|
|
468
|
-
const run = await this.getSyncRun(accountId, runStartedAt);
|
|
469
|
-
if (!run) return false;
|
|
470
|
-
const runningCount = await this.countRunningObjects(accountId, runStartedAt);
|
|
471
|
-
if (runningCount >= run.maxConcurrent) return false;
|
|
472
|
-
const result = await this.query(
|
|
473
|
-
`UPDATE "${this.config.schema}"."_sync_obj_run"
|
|
474
|
-
SET status = 'running', started_at = now(), updated_at = now()
|
|
475
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3 AND status = 'pending'
|
|
476
|
-
RETURNING *`,
|
|
477
|
-
[accountId, runStartedAt, object]
|
|
478
|
-
);
|
|
479
|
-
return (result.rowCount ?? 0) > 0;
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* Get object run details.
|
|
483
|
-
*/
|
|
484
|
-
async getObjectRun(accountId, runStartedAt, object) {
|
|
485
|
-
const result = await this.query(
|
|
486
|
-
`SELECT object, status, processed_count, cursor
|
|
487
|
-
FROM "${this.config.schema}"."_sync_obj_run"
|
|
488
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
489
|
-
[accountId, runStartedAt, object]
|
|
490
|
-
);
|
|
491
|
-
if (result.rows.length === 0) return null;
|
|
492
|
-
const row = result.rows[0];
|
|
493
|
-
return {
|
|
494
|
-
object: row.object,
|
|
495
|
-
status: row.status,
|
|
496
|
-
processedCount: row.processed_count,
|
|
497
|
-
cursor: row.cursor
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
/**
|
|
501
|
-
* Update progress for an object sync.
|
|
502
|
-
* Also touches updated_at for stale detection.
|
|
503
|
-
*/
|
|
504
|
-
async incrementObjectProgress(accountId, runStartedAt, object, count) {
|
|
505
|
-
await this.query(
|
|
506
|
-
`UPDATE "${this.config.schema}"."_sync_obj_run"
|
|
507
|
-
SET processed_count = processed_count + $4, updated_at = now()
|
|
508
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
509
|
-
[accountId, runStartedAt, object, count]
|
|
510
|
-
);
|
|
511
|
-
}
|
|
512
|
-
/**
|
|
513
|
-
* Update the cursor for an object sync.
|
|
514
|
-
* Only updates if the new cursor is higher than the existing one (cursors should never decrease).
|
|
515
|
-
* For numeric cursors (timestamps), uses GREATEST to ensure monotonic increase.
|
|
516
|
-
* For non-numeric cursors, just sets the value directly.
|
|
517
|
-
*/
|
|
518
|
-
async updateObjectCursor(accountId, runStartedAt, object, cursor) {
|
|
519
|
-
const isNumeric = cursor !== null && /^\d+$/.test(cursor);
|
|
520
|
-
if (isNumeric) {
|
|
521
|
-
await this.query(
|
|
522
|
-
`UPDATE "${this.config.schema}"."_sync_obj_run"
|
|
523
|
-
SET cursor = GREATEST(COALESCE(cursor::bigint, 0), $4::bigint)::text,
|
|
524
|
-
updated_at = now()
|
|
525
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
526
|
-
[accountId, runStartedAt, object, cursor]
|
|
527
|
-
);
|
|
528
|
-
} else {
|
|
529
|
-
await this.query(
|
|
530
|
-
`UPDATE "${this.config.schema}"."_sync_obj_run"
|
|
531
|
-
SET cursor = $4, updated_at = now()
|
|
532
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
533
|
-
[accountId, runStartedAt, object, cursor]
|
|
534
|
-
);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Get the highest cursor from previous syncs for an object type.
|
|
539
|
-
* This considers completed, error, AND running runs to ensure recovery syncs
|
|
540
|
-
* don't re-process data that was already synced before a crash.
|
|
541
|
-
* A 'running' status with a cursor means the process was killed mid-sync.
|
|
542
|
-
*/
|
|
543
|
-
async getLastCompletedCursor(accountId, object) {
|
|
544
|
-
const result = await this.query(
|
|
545
|
-
`SELECT MAX(o.cursor::bigint)::text as cursor
|
|
546
|
-
FROM "${this.config.schema}"."_sync_obj_run" o
|
|
547
|
-
WHERE o."_account_id" = $1
|
|
548
|
-
AND o.object = $2
|
|
549
|
-
AND o.cursor IS NOT NULL`,
|
|
550
|
-
[accountId, object]
|
|
551
|
-
);
|
|
552
|
-
return result.rows[0]?.cursor ?? null;
|
|
553
|
-
}
|
|
554
|
-
/**
|
|
555
|
-
* Delete all sync runs and object runs for an account.
|
|
556
|
-
* Useful for testing or resetting sync state.
|
|
557
|
-
*/
|
|
558
|
-
async deleteSyncRuns(accountId) {
|
|
559
|
-
await this.query(
|
|
560
|
-
`DELETE FROM "${this.config.schema}"."_sync_obj_run" WHERE "_account_id" = $1`,
|
|
561
|
-
[accountId]
|
|
562
|
-
);
|
|
563
|
-
await this.query(`DELETE FROM "${this.config.schema}"."_sync_run" WHERE "_account_id" = $1`, [
|
|
564
|
-
accountId
|
|
565
|
-
]);
|
|
566
|
-
}
|
|
567
|
-
/**
|
|
568
|
-
* Mark an object sync as complete.
|
|
569
|
-
* Auto-closes the run when all objects are done.
|
|
570
|
-
*/
|
|
571
|
-
async completeObjectSync(accountId, runStartedAt, object) {
|
|
572
|
-
await this.query(
|
|
573
|
-
`UPDATE "${this.config.schema}"."_sync_obj_run"
|
|
574
|
-
SET status = 'complete', completed_at = now()
|
|
575
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
576
|
-
[accountId, runStartedAt, object]
|
|
577
|
-
);
|
|
578
|
-
const allDone = await this.areAllObjectsComplete(accountId, runStartedAt);
|
|
579
|
-
if (allDone) {
|
|
580
|
-
await this.closeSyncRun(accountId, runStartedAt);
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
/**
|
|
584
|
-
* Mark an object sync as failed.
|
|
585
|
-
* Auto-closes the run when all objects are done.
|
|
586
|
-
*/
|
|
587
|
-
async failObjectSync(accountId, runStartedAt, object, errorMessage) {
|
|
588
|
-
await this.query(
|
|
589
|
-
`UPDATE "${this.config.schema}"."_sync_obj_run"
|
|
590
|
-
SET status = 'error', error_message = $4, completed_at = now()
|
|
591
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
592
|
-
[accountId, runStartedAt, object, errorMessage]
|
|
593
|
-
);
|
|
594
|
-
const allDone = await this.areAllObjectsComplete(accountId, runStartedAt);
|
|
595
|
-
if (allDone) {
|
|
596
|
-
await this.closeSyncRun(accountId, runStartedAt);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
/**
|
|
600
|
-
* Check if any object in a run has errored.
|
|
601
|
-
*/
|
|
602
|
-
async hasAnyObjectErrors(accountId, runStartedAt) {
|
|
603
|
-
const result = await this.query(
|
|
604
|
-
`SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_run"
|
|
605
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'error'`,
|
|
606
|
-
[accountId, runStartedAt]
|
|
607
|
-
);
|
|
608
|
-
return parseInt(result.rows[0].count) > 0;
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* Count running objects in a run.
|
|
612
|
-
*/
|
|
613
|
-
async countRunningObjects(accountId, runStartedAt) {
|
|
614
|
-
const result = await this.query(
|
|
615
|
-
`SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_run"
|
|
616
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'running'`,
|
|
617
|
-
[accountId, runStartedAt]
|
|
618
|
-
);
|
|
619
|
-
return parseInt(result.rows[0].count);
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Get the next pending object to process.
|
|
623
|
-
* Returns null if no pending objects or at concurrency limit.
|
|
624
|
-
*/
|
|
625
|
-
async getNextPendingObject(accountId, runStartedAt) {
|
|
626
|
-
const run = await this.getSyncRun(accountId, runStartedAt);
|
|
627
|
-
if (!run) return null;
|
|
628
|
-
const runningCount = await this.countRunningObjects(accountId, runStartedAt);
|
|
629
|
-
if (runningCount >= run.maxConcurrent) return null;
|
|
630
|
-
const result = await this.query(
|
|
631
|
-
`SELECT object FROM "${this.config.schema}"."_sync_obj_run"
|
|
632
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'pending'
|
|
633
|
-
ORDER BY object
|
|
634
|
-
LIMIT 1`,
|
|
635
|
-
[accountId, runStartedAt]
|
|
636
|
-
);
|
|
637
|
-
return result.rows.length > 0 ? result.rows[0].object : null;
|
|
638
|
-
}
|
|
639
|
-
/**
|
|
640
|
-
* Check if all objects in a run are complete (or error).
|
|
641
|
-
*/
|
|
642
|
-
async areAllObjectsComplete(accountId, runStartedAt) {
|
|
643
|
-
const result = await this.query(
|
|
644
|
-
`SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_run"
|
|
645
|
-
WHERE "_account_id" = $1 AND run_started_at = $2 AND status IN ('pending', 'running')`,
|
|
646
|
-
[accountId, runStartedAt]
|
|
647
|
-
);
|
|
648
|
-
return parseInt(result.rows[0].count) === 0;
|
|
649
|
-
}
|
|
650
|
-
/**
|
|
651
|
-
* Closes the database connection pool and cleans up resources.
|
|
652
|
-
* Call this when you're done using the PostgresClient instance.
|
|
653
|
-
*/
|
|
654
|
-
async close() {
|
|
655
|
-
await this.pool.end();
|
|
656
|
-
}
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
// src/schemas/managed_webhook.ts
|
|
660
|
-
var managedWebhookSchema = {
|
|
661
|
-
properties: [
|
|
662
|
-
"id",
|
|
663
|
-
"object",
|
|
664
|
-
"url",
|
|
665
|
-
"enabled_events",
|
|
666
|
-
"description",
|
|
667
|
-
"enabled",
|
|
668
|
-
"livemode",
|
|
669
|
-
"metadata",
|
|
670
|
-
"secret",
|
|
671
|
-
"status",
|
|
672
|
-
"api_version",
|
|
673
|
-
"created",
|
|
674
|
-
"account_id"
|
|
675
|
-
]
|
|
676
|
-
};
|
|
677
|
-
|
|
678
|
-
// src/utils/retry.ts
|
|
679
|
-
import Stripe from "stripe";
|
|
680
|
-
var DEFAULT_RETRY_CONFIG = {
|
|
681
|
-
maxRetries: 5,
|
|
682
|
-
initialDelayMs: 1e3,
|
|
683
|
-
// 1 second
|
|
684
|
-
maxDelayMs: 6e4,
|
|
685
|
-
// 60 seconds
|
|
686
|
-
jitterMs: 500
|
|
687
|
-
// randomization to prevent thundering herd
|
|
688
|
-
};
|
|
689
|
-
function isRetryableError(error) {
|
|
690
|
-
if (error instanceof Stripe.errors.StripeRateLimitError) {
|
|
691
|
-
return true;
|
|
692
|
-
}
|
|
693
|
-
if (error instanceof Stripe.errors.StripeAPIError) {
|
|
694
|
-
const statusCode = error.statusCode;
|
|
695
|
-
if (statusCode && [500, 502, 503, 504, 424].includes(statusCode)) {
|
|
696
|
-
return true;
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
if (error instanceof Stripe.errors.StripeConnectionError) {
|
|
700
|
-
return true;
|
|
701
|
-
}
|
|
702
|
-
return false;
|
|
703
|
-
}
|
|
704
|
-
function getRetryAfterMs(error) {
|
|
705
|
-
if (!(error instanceof Stripe.errors.StripeRateLimitError)) {
|
|
706
|
-
return null;
|
|
707
|
-
}
|
|
708
|
-
const retryAfterHeader = error.headers?.["retry-after"];
|
|
709
|
-
if (!retryAfterHeader) {
|
|
710
|
-
return null;
|
|
711
|
-
}
|
|
712
|
-
const retryAfterSeconds = Number(retryAfterHeader);
|
|
713
|
-
if (isNaN(retryAfterSeconds) || retryAfterSeconds <= 0) {
|
|
714
|
-
return null;
|
|
715
|
-
}
|
|
716
|
-
return retryAfterSeconds * 1e3;
|
|
717
|
-
}
|
|
718
|
-
function calculateDelay(attempt, config, retryAfterMs) {
|
|
719
|
-
if (retryAfterMs !== null && retryAfterMs !== void 0) {
|
|
720
|
-
const jitter2 = Math.random() * config.jitterMs;
|
|
721
|
-
return retryAfterMs + jitter2;
|
|
722
|
-
}
|
|
723
|
-
const exponentialDelay = Math.min(config.initialDelayMs * Math.pow(2, attempt), config.maxDelayMs);
|
|
724
|
-
const jitter = Math.random() * config.jitterMs;
|
|
725
|
-
return exponentialDelay + jitter;
|
|
726
|
-
}
|
|
727
|
-
function sleep(ms) {
|
|
728
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
729
|
-
}
|
|
730
|
-
function getErrorType(error) {
|
|
731
|
-
if (error instanceof Stripe.errors.StripeRateLimitError) {
|
|
732
|
-
return "rate_limit";
|
|
733
|
-
}
|
|
734
|
-
if (error instanceof Stripe.errors.StripeAPIError) {
|
|
735
|
-
return `api_error_${error.statusCode}`;
|
|
736
|
-
}
|
|
737
|
-
if (error instanceof Stripe.errors.StripeConnectionError) {
|
|
738
|
-
return "connection_error";
|
|
739
|
-
}
|
|
740
|
-
return "unknown";
|
|
741
|
-
}
|
|
742
|
-
async function withRetry(fn, config = {}, logger) {
|
|
743
|
-
const retryConfig = { ...DEFAULT_RETRY_CONFIG, ...config };
|
|
744
|
-
let lastError;
|
|
745
|
-
for (let attempt = 0; attempt <= retryConfig.maxRetries; attempt++) {
|
|
746
|
-
try {
|
|
747
|
-
return await fn();
|
|
748
|
-
} catch (error) {
|
|
749
|
-
lastError = error;
|
|
750
|
-
if (!isRetryableError(error)) {
|
|
751
|
-
throw error;
|
|
752
|
-
}
|
|
753
|
-
if (attempt >= retryConfig.maxRetries) {
|
|
754
|
-
logger?.error(
|
|
755
|
-
{
|
|
756
|
-
error: error instanceof Error ? error.message : String(error),
|
|
757
|
-
errorType: getErrorType(error),
|
|
758
|
-
attempt: attempt + 1,
|
|
759
|
-
maxRetries: retryConfig.maxRetries
|
|
760
|
-
},
|
|
761
|
-
"Max retries exhausted for Stripe error"
|
|
762
|
-
);
|
|
763
|
-
throw error;
|
|
764
|
-
}
|
|
765
|
-
const retryAfterMs = getRetryAfterMs(error);
|
|
766
|
-
const delay = calculateDelay(attempt, retryConfig, retryAfterMs);
|
|
767
|
-
logger?.warn(
|
|
768
|
-
{
|
|
769
|
-
error: error instanceof Error ? error.message : String(error),
|
|
770
|
-
errorType: getErrorType(error),
|
|
771
|
-
attempt: attempt + 1,
|
|
772
|
-
maxRetries: retryConfig.maxRetries,
|
|
773
|
-
delayMs: Math.round(delay),
|
|
774
|
-
retryAfterMs: retryAfterMs ?? void 0,
|
|
775
|
-
nextAttempt: attempt + 2
|
|
776
|
-
},
|
|
777
|
-
"Transient Stripe error, retrying after delay"
|
|
778
|
-
);
|
|
779
|
-
await sleep(delay);
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
throw lastError;
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
// src/utils/stripeClientWrapper.ts
|
|
786
|
-
function createRetryableStripeClient(stripe, retryConfig = {}, logger) {
|
|
787
|
-
return new Proxy(stripe, {
|
|
788
|
-
get(target, prop, receiver) {
|
|
789
|
-
const original = Reflect.get(target, prop, receiver);
|
|
790
|
-
if (original && typeof original === "object" && !isPromise(original)) {
|
|
791
|
-
return wrapResource(original, retryConfig, logger);
|
|
792
|
-
}
|
|
793
|
-
return original;
|
|
794
|
-
}
|
|
795
|
-
});
|
|
796
|
-
}
|
|
797
|
-
function wrapResource(resource, retryConfig, logger) {
|
|
798
|
-
return new Proxy(resource, {
|
|
799
|
-
get(target, prop, receiver) {
|
|
800
|
-
const original = Reflect.get(target, prop, receiver);
|
|
801
|
-
if (typeof original === "function") {
|
|
802
|
-
return function(...args) {
|
|
803
|
-
const result = original.apply(target, args);
|
|
804
|
-
if (result && typeof result === "object" && Symbol.asyncIterator in result) {
|
|
805
|
-
return result;
|
|
806
|
-
}
|
|
807
|
-
if (isPromise(result)) {
|
|
808
|
-
return withRetry(() => Promise.resolve(result), retryConfig, logger);
|
|
809
|
-
}
|
|
810
|
-
return result;
|
|
811
|
-
};
|
|
812
|
-
}
|
|
813
|
-
if (original && typeof original === "object" && !isPromise(original)) {
|
|
814
|
-
return wrapResource(original, retryConfig, logger);
|
|
815
|
-
}
|
|
816
|
-
return original;
|
|
817
|
-
}
|
|
818
|
-
});
|
|
819
|
-
}
|
|
820
|
-
function isPromise(value) {
|
|
821
|
-
return value !== null && typeof value === "object" && typeof value.then === "function";
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
// src/utils/hashApiKey.ts
|
|
825
|
-
import { createHash } from "crypto";
|
|
826
|
-
function hashApiKey(apiKey) {
|
|
827
|
-
return createHash("sha256").update(apiKey).digest("hex");
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
// src/stripeSync.ts
|
|
831
|
-
function getUniqueIds(entries, key) {
|
|
832
|
-
const set = new Set(
|
|
833
|
-
entries.map((subscription) => subscription?.[key]?.toString()).filter((it) => Boolean(it))
|
|
834
|
-
);
|
|
835
|
-
return Array.from(set);
|
|
836
|
-
}
|
|
837
|
-
var StripeSync = class {
|
|
838
|
-
constructor(config) {
|
|
839
|
-
this.config = config;
|
|
840
|
-
const baseStripe = new Stripe2(config.stripeSecretKey, {
|
|
841
|
-
// https://github.com/stripe/stripe-node#configuration
|
|
842
|
-
// @ts-ignore
|
|
843
|
-
apiVersion: config.stripeApiVersion,
|
|
844
|
-
appInfo: {
|
|
845
|
-
name: "Stripe Postgres Sync"
|
|
846
|
-
}
|
|
847
|
-
});
|
|
848
|
-
this.stripe = createRetryableStripeClient(baseStripe, {}, config.logger);
|
|
849
|
-
this.config.logger = config.logger ?? console;
|
|
850
|
-
this.config.logger?.info(
|
|
851
|
-
{ autoExpandLists: config.autoExpandLists, stripeApiVersion: config.stripeApiVersion },
|
|
852
|
-
"StripeSync initialized"
|
|
853
|
-
);
|
|
854
|
-
const poolConfig = config.poolConfig ?? {};
|
|
855
|
-
if (config.databaseUrl) {
|
|
856
|
-
poolConfig.connectionString = config.databaseUrl;
|
|
857
|
-
}
|
|
858
|
-
if (config.maxPostgresConnections) {
|
|
859
|
-
poolConfig.max = config.maxPostgresConnections;
|
|
860
|
-
}
|
|
861
|
-
if (poolConfig.max === void 0) {
|
|
862
|
-
poolConfig.max = 10;
|
|
863
|
-
}
|
|
864
|
-
if (poolConfig.keepAlive === void 0) {
|
|
865
|
-
poolConfig.keepAlive = true;
|
|
866
|
-
}
|
|
867
|
-
this.postgresClient = new PostgresClient({
|
|
868
|
-
schema: "stripe",
|
|
869
|
-
poolConfig
|
|
870
|
-
});
|
|
871
|
-
}
|
|
872
|
-
stripe;
|
|
873
|
-
postgresClient;
|
|
874
|
-
/**
|
|
875
|
-
* Get the Stripe account ID. Delegates to getCurrentAccount() for the actual lookup.
|
|
876
|
-
*/
|
|
877
|
-
async getAccountId(objectAccountId) {
|
|
878
|
-
const account = await this.getCurrentAccount(objectAccountId);
|
|
879
|
-
if (!account) {
|
|
880
|
-
throw new Error("Failed to retrieve Stripe account. Please ensure API key is valid.");
|
|
881
|
-
}
|
|
882
|
-
return account.id;
|
|
883
|
-
}
|
|
884
|
-
/**
|
|
885
|
-
* Upsert Stripe account information to the database
|
|
886
|
-
* @param account - Stripe account object
|
|
887
|
-
* @param apiKeyHash - SHA-256 hash of API key to store for fast lookups
|
|
888
|
-
*/
|
|
889
|
-
async upsertAccount(account, apiKeyHash) {
|
|
890
|
-
try {
|
|
891
|
-
await this.postgresClient.upsertAccount(
|
|
892
|
-
{
|
|
893
|
-
id: account.id,
|
|
894
|
-
raw_data: account
|
|
895
|
-
},
|
|
896
|
-
apiKeyHash
|
|
897
|
-
);
|
|
898
|
-
} catch (error) {
|
|
899
|
-
this.config.logger?.error(error, "Failed to upsert account to database");
|
|
900
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
901
|
-
throw new Error(`Failed to upsert account to database: ${errorMessage}`);
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
/**
|
|
905
|
-
* Get the current account being synced. Uses database lookup by API key hash,
|
|
906
|
-
* with fallback to Stripe API if not found (first-time setup or new API key).
|
|
907
|
-
* @param objectAccountId - Optional account ID from event data (Connect scenarios)
|
|
908
|
-
*/
|
|
909
|
-
async getCurrentAccount(objectAccountId) {
|
|
910
|
-
const apiKeyHash = hashApiKey(this.config.stripeSecretKey);
|
|
911
|
-
try {
|
|
912
|
-
const account = await this.postgresClient.getAccountByApiKeyHash(apiKeyHash);
|
|
913
|
-
if (account) {
|
|
914
|
-
return account;
|
|
915
|
-
}
|
|
916
|
-
} catch (error) {
|
|
917
|
-
this.config.logger?.warn(
|
|
918
|
-
error,
|
|
919
|
-
"Failed to lookup account by API key hash, falling back to API"
|
|
920
|
-
);
|
|
921
|
-
}
|
|
922
|
-
try {
|
|
923
|
-
const accountIdParam = objectAccountId || this.config.stripeAccountId;
|
|
924
|
-
const account = accountIdParam ? await this.stripe.accounts.retrieve(accountIdParam) : await this.stripe.accounts.retrieve();
|
|
925
|
-
await this.upsertAccount(account, apiKeyHash);
|
|
926
|
-
return account;
|
|
927
|
-
} catch (error) {
|
|
928
|
-
this.config.logger?.error(error, "Failed to retrieve account from Stripe API");
|
|
929
|
-
return null;
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
/**
|
|
933
|
-
* Get all accounts that have been synced to the database
|
|
934
|
-
*/
|
|
935
|
-
async getAllSyncedAccounts() {
|
|
936
|
-
try {
|
|
937
|
-
const accountsData = await this.postgresClient.getAllAccounts();
|
|
938
|
-
return accountsData;
|
|
939
|
-
} catch (error) {
|
|
940
|
-
this.config.logger?.error(error, "Failed to retrieve accounts from database");
|
|
941
|
-
throw new Error("Failed to retrieve synced accounts from database");
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
/**
|
|
945
|
-
* DANGEROUS: Delete an account and all associated data from the database
|
|
946
|
-
* This operation cannot be undone!
|
|
947
|
-
*
|
|
948
|
-
* @param accountId - The Stripe account ID to delete
|
|
949
|
-
* @param options - Options for deletion behavior
|
|
950
|
-
* @param options.dryRun - If true, only count records without deleting (default: false)
|
|
951
|
-
* @param options.useTransaction - If true, use transaction for atomic deletion (default: true)
|
|
952
|
-
* @returns Deletion summary with counts and warnings
|
|
953
|
-
*/
|
|
954
|
-
async dangerouslyDeleteSyncedAccountData(accountId, options) {
|
|
955
|
-
const dryRun = options?.dryRun ?? false;
|
|
956
|
-
const useTransaction = options?.useTransaction ?? true;
|
|
957
|
-
this.config.logger?.info(
|
|
958
|
-
`${dryRun ? "Preview" : "Deleting"} account ${accountId} (transaction: ${useTransaction})`
|
|
959
|
-
);
|
|
960
|
-
try {
|
|
961
|
-
const counts = await this.postgresClient.getAccountRecordCounts(accountId);
|
|
962
|
-
const warnings = [];
|
|
963
|
-
let totalRecords = 0;
|
|
964
|
-
for (const [table, count] of Object.entries(counts)) {
|
|
965
|
-
if (count > 0) {
|
|
966
|
-
totalRecords += count;
|
|
967
|
-
warnings.push(`Will delete ${count} ${table} record${count !== 1 ? "s" : ""}`);
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
if (totalRecords > 1e5) {
|
|
971
|
-
warnings.push(
|
|
972
|
-
`Large dataset detected (${totalRecords} total records). Consider using useTransaction: false for better performance.`
|
|
973
|
-
);
|
|
974
|
-
}
|
|
975
|
-
if (dryRun) {
|
|
976
|
-
this.config.logger?.info(`Dry-run complete: ${totalRecords} total records would be deleted`);
|
|
977
|
-
return {
|
|
978
|
-
deletedAccountId: accountId,
|
|
979
|
-
deletedRecordCounts: counts,
|
|
980
|
-
warnings
|
|
981
|
-
};
|
|
982
|
-
}
|
|
983
|
-
const deletionCounts = await this.postgresClient.deleteAccountWithCascade(
|
|
984
|
-
accountId,
|
|
985
|
-
useTransaction
|
|
986
|
-
);
|
|
987
|
-
this.config.logger?.info(
|
|
988
|
-
`Successfully deleted account ${accountId} with ${totalRecords} total records`
|
|
989
|
-
);
|
|
990
|
-
return {
|
|
991
|
-
deletedAccountId: accountId,
|
|
992
|
-
deletedRecordCounts: deletionCounts,
|
|
993
|
-
warnings
|
|
994
|
-
};
|
|
995
|
-
} catch (error) {
|
|
996
|
-
this.config.logger?.error(error, `Failed to delete account ${accountId}`);
|
|
997
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
998
|
-
throw new Error(`Failed to delete account ${accountId}: ${errorMessage}`);
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
async processWebhook(payload, signature) {
|
|
1002
|
-
let webhookSecret = this.config.stripeWebhookSecret;
|
|
1003
|
-
if (!webhookSecret) {
|
|
1004
|
-
const accountId = await this.getAccountId();
|
|
1005
|
-
const result = await this.postgresClient.query(
|
|
1006
|
-
`SELECT secret FROM "stripe"."_managed_webhooks" WHERE account_id = $1 LIMIT 1`,
|
|
1007
|
-
[accountId]
|
|
1008
|
-
);
|
|
1009
|
-
if (result.rows.length > 0) {
|
|
1010
|
-
webhookSecret = result.rows[0].secret;
|
|
1011
|
-
}
|
|
1012
|
-
}
|
|
1013
|
-
if (!webhookSecret) {
|
|
1014
|
-
throw new Error(
|
|
1015
|
-
"No webhook secret provided. Either create a managed webhook or configure stripeWebhookSecret."
|
|
1016
|
-
);
|
|
1017
|
-
}
|
|
1018
|
-
const event = await this.stripe.webhooks.constructEventAsync(payload, signature, webhookSecret);
|
|
1019
|
-
return this.processEvent(event);
|
|
1020
|
-
}
|
|
1021
|
-
// Event handler registry - maps event types to handler functions
|
|
1022
|
-
// Note: Uses 'any' for event parameter to allow handlers with specific Stripe event types
|
|
1023
|
-
// (e.g., CustomerDeletedEvent, ProductDeletedEvent) which TypeScript won't accept
|
|
1024
|
-
// as contravariant parameters when using the base Stripe.Event type
|
|
1025
|
-
eventHandlers = {
|
|
1026
|
-
"charge.captured": this.handleChargeEvent.bind(this),
|
|
1027
|
-
"charge.expired": this.handleChargeEvent.bind(this),
|
|
1028
|
-
"charge.failed": this.handleChargeEvent.bind(this),
|
|
1029
|
-
"charge.pending": this.handleChargeEvent.bind(this),
|
|
1030
|
-
"charge.refunded": this.handleChargeEvent.bind(this),
|
|
1031
|
-
"charge.succeeded": this.handleChargeEvent.bind(this),
|
|
1032
|
-
"charge.updated": this.handleChargeEvent.bind(this),
|
|
1033
|
-
"customer.deleted": this.handleCustomerDeletedEvent.bind(this),
|
|
1034
|
-
"customer.created": this.handleCustomerEvent.bind(this),
|
|
1035
|
-
"customer.updated": this.handleCustomerEvent.bind(this),
|
|
1036
|
-
"checkout.session.async_payment_failed": this.handleCheckoutSessionEvent.bind(this),
|
|
1037
|
-
"checkout.session.async_payment_succeeded": this.handleCheckoutSessionEvent.bind(this),
|
|
1038
|
-
"checkout.session.completed": this.handleCheckoutSessionEvent.bind(this),
|
|
1039
|
-
"checkout.session.expired": this.handleCheckoutSessionEvent.bind(this),
|
|
1040
|
-
"customer.subscription.created": this.handleSubscriptionEvent.bind(this),
|
|
1041
|
-
"customer.subscription.deleted": this.handleSubscriptionEvent.bind(this),
|
|
1042
|
-
"customer.subscription.paused": this.handleSubscriptionEvent.bind(this),
|
|
1043
|
-
"customer.subscription.pending_update_applied": this.handleSubscriptionEvent.bind(this),
|
|
1044
|
-
"customer.subscription.pending_update_expired": this.handleSubscriptionEvent.bind(this),
|
|
1045
|
-
"customer.subscription.trial_will_end": this.handleSubscriptionEvent.bind(this),
|
|
1046
|
-
"customer.subscription.resumed": this.handleSubscriptionEvent.bind(this),
|
|
1047
|
-
"customer.subscription.updated": this.handleSubscriptionEvent.bind(this),
|
|
1048
|
-
"customer.tax_id.updated": this.handleTaxIdEvent.bind(this),
|
|
1049
|
-
"customer.tax_id.created": this.handleTaxIdEvent.bind(this),
|
|
1050
|
-
"customer.tax_id.deleted": this.handleTaxIdDeletedEvent.bind(this),
|
|
1051
|
-
"invoice.created": this.handleInvoiceEvent.bind(this),
|
|
1052
|
-
"invoice.deleted": this.handleInvoiceEvent.bind(this),
|
|
1053
|
-
"invoice.finalized": this.handleInvoiceEvent.bind(this),
|
|
1054
|
-
"invoice.finalization_failed": this.handleInvoiceEvent.bind(this),
|
|
1055
|
-
"invoice.paid": this.handleInvoiceEvent.bind(this),
|
|
1056
|
-
"invoice.payment_action_required": this.handleInvoiceEvent.bind(this),
|
|
1057
|
-
"invoice.payment_failed": this.handleInvoiceEvent.bind(this),
|
|
1058
|
-
"invoice.payment_succeeded": this.handleInvoiceEvent.bind(this),
|
|
1059
|
-
"invoice.upcoming": this.handleInvoiceEvent.bind(this),
|
|
1060
|
-
"invoice.sent": this.handleInvoiceEvent.bind(this),
|
|
1061
|
-
"invoice.voided": this.handleInvoiceEvent.bind(this),
|
|
1062
|
-
"invoice.marked_uncollectible": this.handleInvoiceEvent.bind(this),
|
|
1063
|
-
"invoice.updated": this.handleInvoiceEvent.bind(this),
|
|
1064
|
-
"product.created": this.handleProductEvent.bind(this),
|
|
1065
|
-
"product.updated": this.handleProductEvent.bind(this),
|
|
1066
|
-
"product.deleted": this.handleProductDeletedEvent.bind(this),
|
|
1067
|
-
"price.created": this.handlePriceEvent.bind(this),
|
|
1068
|
-
"price.updated": this.handlePriceEvent.bind(this),
|
|
1069
|
-
"price.deleted": this.handlePriceDeletedEvent.bind(this),
|
|
1070
|
-
"plan.created": this.handlePlanEvent.bind(this),
|
|
1071
|
-
"plan.updated": this.handlePlanEvent.bind(this),
|
|
1072
|
-
"plan.deleted": this.handlePlanDeletedEvent.bind(this),
|
|
1073
|
-
"setup_intent.canceled": this.handleSetupIntentEvent.bind(this),
|
|
1074
|
-
"setup_intent.created": this.handleSetupIntentEvent.bind(this),
|
|
1075
|
-
"setup_intent.requires_action": this.handleSetupIntentEvent.bind(this),
|
|
1076
|
-
"setup_intent.setup_failed": this.handleSetupIntentEvent.bind(this),
|
|
1077
|
-
"setup_intent.succeeded": this.handleSetupIntentEvent.bind(this),
|
|
1078
|
-
"subscription_schedule.aborted": this.handleSubscriptionScheduleEvent.bind(this),
|
|
1079
|
-
"subscription_schedule.canceled": this.handleSubscriptionScheduleEvent.bind(this),
|
|
1080
|
-
"subscription_schedule.completed": this.handleSubscriptionScheduleEvent.bind(this),
|
|
1081
|
-
"subscription_schedule.created": this.handleSubscriptionScheduleEvent.bind(this),
|
|
1082
|
-
"subscription_schedule.expiring": this.handleSubscriptionScheduleEvent.bind(this),
|
|
1083
|
-
"subscription_schedule.released": this.handleSubscriptionScheduleEvent.bind(this),
|
|
1084
|
-
"subscription_schedule.updated": this.handleSubscriptionScheduleEvent.bind(this),
|
|
1085
|
-
"payment_method.attached": this.handlePaymentMethodEvent.bind(this),
|
|
1086
|
-
"payment_method.automatically_updated": this.handlePaymentMethodEvent.bind(this),
|
|
1087
|
-
"payment_method.detached": this.handlePaymentMethodEvent.bind(this),
|
|
1088
|
-
"payment_method.updated": this.handlePaymentMethodEvent.bind(this),
|
|
1089
|
-
"charge.dispute.created": this.handleDisputeEvent.bind(this),
|
|
1090
|
-
"charge.dispute.funds_reinstated": this.handleDisputeEvent.bind(this),
|
|
1091
|
-
"charge.dispute.funds_withdrawn": this.handleDisputeEvent.bind(this),
|
|
1092
|
-
"charge.dispute.updated": this.handleDisputeEvent.bind(this),
|
|
1093
|
-
"charge.dispute.closed": this.handleDisputeEvent.bind(this),
|
|
1094
|
-
"payment_intent.amount_capturable_updated": this.handlePaymentIntentEvent.bind(this),
|
|
1095
|
-
"payment_intent.canceled": this.handlePaymentIntentEvent.bind(this),
|
|
1096
|
-
"payment_intent.created": this.handlePaymentIntentEvent.bind(this),
|
|
1097
|
-
"payment_intent.partially_funded": this.handlePaymentIntentEvent.bind(this),
|
|
1098
|
-
"payment_intent.payment_failed": this.handlePaymentIntentEvent.bind(this),
|
|
1099
|
-
"payment_intent.processing": this.handlePaymentIntentEvent.bind(this),
|
|
1100
|
-
"payment_intent.requires_action": this.handlePaymentIntentEvent.bind(this),
|
|
1101
|
-
"payment_intent.succeeded": this.handlePaymentIntentEvent.bind(this),
|
|
1102
|
-
"credit_note.created": this.handleCreditNoteEvent.bind(this),
|
|
1103
|
-
"credit_note.updated": this.handleCreditNoteEvent.bind(this),
|
|
1104
|
-
"credit_note.voided": this.handleCreditNoteEvent.bind(this),
|
|
1105
|
-
"radar.early_fraud_warning.created": this.handleEarlyFraudWarningEvent.bind(this),
|
|
1106
|
-
"radar.early_fraud_warning.updated": this.handleEarlyFraudWarningEvent.bind(this),
|
|
1107
|
-
"refund.created": this.handleRefundEvent.bind(this),
|
|
1108
|
-
"refund.failed": this.handleRefundEvent.bind(this),
|
|
1109
|
-
"refund.updated": this.handleRefundEvent.bind(this),
|
|
1110
|
-
"charge.refund.updated": this.handleRefundEvent.bind(this),
|
|
1111
|
-
"review.closed": this.handleReviewEvent.bind(this),
|
|
1112
|
-
"review.opened": this.handleReviewEvent.bind(this),
|
|
1113
|
-
"entitlements.active_entitlement_summary.updated": this.handleEntitlementSummaryEvent.bind(this)
|
|
1114
|
-
};
|
|
1115
|
-
// Resource registry - maps SyncObject → list/upsert operations for processNext()
|
|
1116
|
-
// Complements eventHandlers which maps event types → handlers for webhooks
|
|
1117
|
-
// Both registries share the same underlying upsert methods
|
|
1118
|
-
// Order field determines backfill sequence - parents before children for FK dependencies
|
|
1119
|
-
resourceRegistry = {
|
|
1120
|
-
product: {
|
|
1121
|
-
order: 1,
|
|
1122
|
-
// No dependencies
|
|
1123
|
-
listFn: (p) => this.stripe.products.list(p),
|
|
1124
|
-
upsertFn: (items, id) => this.upsertProducts(items, id),
|
|
1125
|
-
supportsCreatedFilter: true
|
|
1126
|
-
},
|
|
1127
|
-
price: {
|
|
1128
|
-
order: 2,
|
|
1129
|
-
// Depends on product
|
|
1130
|
-
listFn: (p) => this.stripe.prices.list(p),
|
|
1131
|
-
upsertFn: (items, id, bf) => this.upsertPrices(items, id, bf),
|
|
1132
|
-
supportsCreatedFilter: true
|
|
1133
|
-
},
|
|
1134
|
-
plan: {
|
|
1135
|
-
order: 3,
|
|
1136
|
-
// Depends on product
|
|
1137
|
-
listFn: (p) => this.stripe.plans.list(p),
|
|
1138
|
-
upsertFn: (items, id, bf) => this.upsertPlans(items, id, bf),
|
|
1139
|
-
supportsCreatedFilter: true
|
|
1140
|
-
},
|
|
1141
|
-
customer: {
|
|
1142
|
-
order: 4,
|
|
1143
|
-
// No dependencies
|
|
1144
|
-
listFn: (p) => this.stripe.customers.list(p),
|
|
1145
|
-
upsertFn: (items, id) => this.upsertCustomers(items, id),
|
|
1146
|
-
supportsCreatedFilter: true
|
|
1147
|
-
},
|
|
1148
|
-
subscription: {
|
|
1149
|
-
order: 5,
|
|
1150
|
-
// Depends on customer, price
|
|
1151
|
-
listFn: (p) => this.stripe.subscriptions.list(p),
|
|
1152
|
-
upsertFn: (items, id, bf) => this.upsertSubscriptions(items, id, bf),
|
|
1153
|
-
supportsCreatedFilter: true
|
|
1154
|
-
},
|
|
1155
|
-
subscription_schedules: {
|
|
1156
|
-
order: 6,
|
|
1157
|
-
// Depends on customer
|
|
1158
|
-
listFn: (p) => this.stripe.subscriptionSchedules.list(p),
|
|
1159
|
-
upsertFn: (items, id, bf) => this.upsertSubscriptionSchedules(items, id, bf),
|
|
1160
|
-
supportsCreatedFilter: true
|
|
1161
|
-
},
|
|
1162
|
-
invoice: {
|
|
1163
|
-
order: 7,
|
|
1164
|
-
// Depends on customer, subscription
|
|
1165
|
-
listFn: (p) => this.stripe.invoices.list(p),
|
|
1166
|
-
upsertFn: (items, id, bf) => this.upsertInvoices(items, id, bf),
|
|
1167
|
-
supportsCreatedFilter: true
|
|
1168
|
-
},
|
|
1169
|
-
charge: {
|
|
1170
|
-
order: 8,
|
|
1171
|
-
// Depends on customer, invoice
|
|
1172
|
-
listFn: (p) => this.stripe.charges.list(p),
|
|
1173
|
-
upsertFn: (items, id, bf) => this.upsertCharges(items, id, bf),
|
|
1174
|
-
supportsCreatedFilter: true
|
|
1175
|
-
},
|
|
1176
|
-
setup_intent: {
|
|
1177
|
-
order: 9,
|
|
1178
|
-
// Depends on customer
|
|
1179
|
-
listFn: (p) => this.stripe.setupIntents.list(p),
|
|
1180
|
-
upsertFn: (items, id, bf) => this.upsertSetupIntents(items, id, bf),
|
|
1181
|
-
supportsCreatedFilter: true
|
|
1182
|
-
},
|
|
1183
|
-
payment_method: {
|
|
1184
|
-
order: 10,
|
|
1185
|
-
// Depends on customer (special: iterates customers)
|
|
1186
|
-
listFn: (p) => this.stripe.paymentMethods.list(p),
|
|
1187
|
-
upsertFn: (items, id, bf) => this.upsertPaymentMethods(items, id, bf),
|
|
1188
|
-
supportsCreatedFilter: false
|
|
1189
|
-
// Requires customer param, can't filter by created
|
|
1190
|
-
},
|
|
1191
|
-
payment_intent: {
|
|
1192
|
-
order: 11,
|
|
1193
|
-
// Depends on customer
|
|
1194
|
-
listFn: (p) => this.stripe.paymentIntents.list(p),
|
|
1195
|
-
upsertFn: (items, id, bf) => this.upsertPaymentIntents(items, id, bf),
|
|
1196
|
-
supportsCreatedFilter: true
|
|
1197
|
-
},
|
|
1198
|
-
tax_id: {
|
|
1199
|
-
order: 12,
|
|
1200
|
-
// Depends on customer
|
|
1201
|
-
listFn: (p) => this.stripe.taxIds.list(p),
|
|
1202
|
-
upsertFn: (items, id, bf) => this.upsertTaxIds(items, id, bf),
|
|
1203
|
-
supportsCreatedFilter: false
|
|
1204
|
-
// taxIds don't support created filter
|
|
1205
|
-
},
|
|
1206
|
-
credit_note: {
|
|
1207
|
-
order: 13,
|
|
1208
|
-
// Depends on invoice
|
|
1209
|
-
listFn: (p) => this.stripe.creditNotes.list(p),
|
|
1210
|
-
upsertFn: (items, id, bf) => this.upsertCreditNotes(items, id, bf),
|
|
1211
|
-
supportsCreatedFilter: true
|
|
1212
|
-
// credit_notes support created filter
|
|
1213
|
-
},
|
|
1214
|
-
dispute: {
|
|
1215
|
-
order: 14,
|
|
1216
|
-
// Depends on charge
|
|
1217
|
-
listFn: (p) => this.stripe.disputes.list(p),
|
|
1218
|
-
upsertFn: (items, id, bf) => this.upsertDisputes(items, id, bf),
|
|
1219
|
-
supportsCreatedFilter: true
|
|
1220
|
-
},
|
|
1221
|
-
early_fraud_warning: {
|
|
1222
|
-
order: 15,
|
|
1223
|
-
// Depends on charge
|
|
1224
|
-
listFn: (p) => this.stripe.radar.earlyFraudWarnings.list(p),
|
|
1225
|
-
upsertFn: (items, id) => this.upsertEarlyFraudWarning(items, id),
|
|
1226
|
-
supportsCreatedFilter: true
|
|
1227
|
-
},
|
|
1228
|
-
refund: {
|
|
1229
|
-
order: 16,
|
|
1230
|
-
// Depends on charge
|
|
1231
|
-
listFn: (p) => this.stripe.refunds.list(p),
|
|
1232
|
-
upsertFn: (items, id, bf) => this.upsertRefunds(items, id, bf),
|
|
1233
|
-
supportsCreatedFilter: true
|
|
1234
|
-
},
|
|
1235
|
-
checkout_sessions: {
|
|
1236
|
-
order: 17,
|
|
1237
|
-
// Depends on customer (optional)
|
|
1238
|
-
listFn: (p) => this.stripe.checkout.sessions.list(p),
|
|
1239
|
-
upsertFn: (items, id) => this.upsertCheckoutSessions(items, id),
|
|
1240
|
-
supportsCreatedFilter: true
|
|
1241
|
-
}
|
|
1242
|
-
};
|
|
1243
|
-
async processEvent(event) {
|
|
1244
|
-
const objectAccountId = event.data?.object && typeof event.data.object === "object" && "account" in event.data.object ? event.data.object.account : void 0;
|
|
1245
|
-
const accountId = await this.getAccountId(objectAccountId);
|
|
1246
|
-
await this.getCurrentAccount();
|
|
1247
|
-
const handler = this.eventHandlers[event.type];
|
|
1248
|
-
if (handler) {
|
|
1249
|
-
const entityId = event.data?.object && typeof event.data.object === "object" && "id" in event.data.object ? event.data.object.id : "unknown";
|
|
1250
|
-
this.config.logger?.info(`Received webhook ${event.id}: ${event.type} for ${entityId}`);
|
|
1251
|
-
await handler(event, accountId);
|
|
1252
|
-
} else {
|
|
1253
|
-
this.config.logger?.warn(
|
|
1254
|
-
`Received unhandled webhook event: ${event.type} (${event.id}). Ignoring.`
|
|
1255
|
-
);
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1258
|
-
/**
|
|
1259
|
-
* Returns an array of all webhook event types that this sync engine can handle.
|
|
1260
|
-
* Useful for configuring webhook endpoints with specific event subscriptions.
|
|
1261
|
-
*/
|
|
1262
|
-
getSupportedEventTypes() {
|
|
1263
|
-
return Object.keys(
|
|
1264
|
-
this.eventHandlers
|
|
1265
|
-
).sort();
|
|
1266
|
-
}
|
|
1267
|
-
/**
|
|
1268
|
-
* Returns an array of all object types that can be synced via processNext/processUntilDone.
|
|
1269
|
-
* Ordered for backfill: parents before children (products before prices, customers before subscriptions).
|
|
1270
|
-
* Order is determined by the `order` field in resourceRegistry.
|
|
1271
|
-
*/
|
|
1272
|
-
getSupportedSyncObjects() {
|
|
1273
|
-
return Object.entries(this.resourceRegistry).sort(([, a], [, b]) => a.order - b.order).map(([key]) => key);
|
|
1274
|
-
}
|
|
1275
|
-
// Event handler methods
|
|
1276
|
-
async handleChargeEvent(event, accountId) {
|
|
1277
|
-
const { entity: charge, refetched } = await this.fetchOrUseWebhookData(
|
|
1278
|
-
event.data.object,
|
|
1279
|
-
(id) => this.stripe.charges.retrieve(id),
|
|
1280
|
-
(charge2) => charge2.status === "failed" || charge2.status === "succeeded"
|
|
1281
|
-
);
|
|
1282
|
-
await this.upsertCharges([charge], accountId, false, this.getSyncTimestamp(event, refetched));
|
|
1283
|
-
}
|
|
1284
|
-
async handleCustomerDeletedEvent(event, accountId) {
|
|
1285
|
-
const customer = {
|
|
1286
|
-
id: event.data.object.id,
|
|
1287
|
-
object: "customer",
|
|
1288
|
-
deleted: true
|
|
1289
|
-
};
|
|
1290
|
-
await this.upsertCustomers([customer], accountId, this.getSyncTimestamp(event, false));
|
|
1291
|
-
}
|
|
1292
|
-
async handleCustomerEvent(event, accountId) {
|
|
1293
|
-
const { entity: customer, refetched } = await this.fetchOrUseWebhookData(
|
|
1294
|
-
event.data.object,
|
|
1295
|
-
(id) => this.stripe.customers.retrieve(id),
|
|
1296
|
-
(customer2) => customer2.deleted === true
|
|
1297
|
-
);
|
|
1298
|
-
await this.upsertCustomers([customer], accountId, this.getSyncTimestamp(event, refetched));
|
|
1299
|
-
}
|
|
1300
|
-
async handleCheckoutSessionEvent(event, accountId) {
|
|
1301
|
-
const { entity: checkoutSession, refetched } = await this.fetchOrUseWebhookData(
|
|
1302
|
-
event.data.object,
|
|
1303
|
-
(id) => this.stripe.checkout.sessions.retrieve(id)
|
|
1304
|
-
);
|
|
1305
|
-
await this.upsertCheckoutSessions(
|
|
1306
|
-
[checkoutSession],
|
|
1307
|
-
accountId,
|
|
1308
|
-
false,
|
|
1309
|
-
this.getSyncTimestamp(event, refetched)
|
|
1310
|
-
);
|
|
1311
|
-
}
|
|
1312
|
-
async handleSubscriptionEvent(event, accountId) {
|
|
1313
|
-
const { entity: subscription, refetched } = await this.fetchOrUseWebhookData(
|
|
1314
|
-
event.data.object,
|
|
1315
|
-
(id) => this.stripe.subscriptions.retrieve(id),
|
|
1316
|
-
(subscription2) => subscription2.status === "canceled" || subscription2.status === "incomplete_expired"
|
|
1317
|
-
);
|
|
1318
|
-
await this.upsertSubscriptions(
|
|
1319
|
-
[subscription],
|
|
1320
|
-
accountId,
|
|
1321
|
-
false,
|
|
1322
|
-
this.getSyncTimestamp(event, refetched)
|
|
1323
|
-
);
|
|
1324
|
-
}
|
|
1325
|
-
async handleTaxIdEvent(event, accountId) {
|
|
1326
|
-
const { entity: taxId, refetched } = await this.fetchOrUseWebhookData(
|
|
1327
|
-
event.data.object,
|
|
1328
|
-
(id) => this.stripe.taxIds.retrieve(id)
|
|
1329
|
-
);
|
|
1330
|
-
await this.upsertTaxIds([taxId], accountId, false, this.getSyncTimestamp(event, refetched));
|
|
1331
|
-
}
|
|
1332
|
-
async handleTaxIdDeletedEvent(event, _accountId) {
|
|
1333
|
-
const taxId = event.data.object;
|
|
1334
|
-
await this.deleteTaxId(taxId.id);
|
|
1335
|
-
}
|
|
1336
|
-
async handleInvoiceEvent(event, accountId) {
|
|
1337
|
-
const { entity: invoice, refetched } = await this.fetchOrUseWebhookData(
|
|
1338
|
-
event.data.object,
|
|
1339
|
-
(id) => this.stripe.invoices.retrieve(id),
|
|
1340
|
-
(invoice2) => invoice2.status === "void"
|
|
1341
|
-
);
|
|
1342
|
-
await this.upsertInvoices([invoice], accountId, false, this.getSyncTimestamp(event, refetched));
|
|
1343
|
-
}
|
|
1344
|
-
async handleProductEvent(event, accountId) {
|
|
1345
|
-
try {
|
|
1346
|
-
const { entity: product, refetched } = await this.fetchOrUseWebhookData(
|
|
1347
|
-
event.data.object,
|
|
1348
|
-
(id) => this.stripe.products.retrieve(id)
|
|
1349
|
-
);
|
|
1350
|
-
await this.upsertProducts([product], accountId, this.getSyncTimestamp(event, refetched));
|
|
1351
|
-
} catch (err) {
|
|
1352
|
-
if (err instanceof Stripe2.errors.StripeAPIError && err.code === "resource_missing") {
|
|
1353
|
-
const product = event.data.object;
|
|
1354
|
-
await this.deleteProduct(product.id);
|
|
1355
|
-
} else {
|
|
1356
|
-
throw err;
|
|
1357
|
-
}
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
async handleProductDeletedEvent(event, _accountId) {
|
|
1361
|
-
const product = event.data.object;
|
|
1362
|
-
await this.deleteProduct(product.id);
|
|
1363
|
-
}
|
|
1364
|
-
async handlePriceEvent(event, accountId) {
|
|
1365
|
-
try {
|
|
1366
|
-
const { entity: price, refetched } = await this.fetchOrUseWebhookData(
|
|
1367
|
-
event.data.object,
|
|
1368
|
-
(id) => this.stripe.prices.retrieve(id)
|
|
1369
|
-
);
|
|
1370
|
-
await this.upsertPrices([price], accountId, false, this.getSyncTimestamp(event, refetched));
|
|
1371
|
-
} catch (err) {
|
|
1372
|
-
if (err instanceof Stripe2.errors.StripeAPIError && err.code === "resource_missing") {
|
|
1373
|
-
const price = event.data.object;
|
|
1374
|
-
await this.deletePrice(price.id);
|
|
1375
|
-
} else {
|
|
1376
|
-
throw err;
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
}
|
|
1380
|
-
async handlePriceDeletedEvent(event, _accountId) {
|
|
1381
|
-
const price = event.data.object;
|
|
1382
|
-
await this.deletePrice(price.id);
|
|
1383
|
-
}
|
|
1384
|
-
async handlePlanEvent(event, accountId) {
|
|
1385
|
-
try {
|
|
1386
|
-
const { entity: plan, refetched } = await this.fetchOrUseWebhookData(
|
|
1387
|
-
event.data.object,
|
|
1388
|
-
(id) => this.stripe.plans.retrieve(id)
|
|
1389
|
-
);
|
|
1390
|
-
await this.upsertPlans([plan], accountId, false, this.getSyncTimestamp(event, refetched));
|
|
1391
|
-
} catch (err) {
|
|
1392
|
-
if (err instanceof Stripe2.errors.StripeAPIError && err.code === "resource_missing") {
|
|
1393
|
-
const plan = event.data.object;
|
|
1394
|
-
await this.deletePlan(plan.id);
|
|
1395
|
-
} else {
|
|
1396
|
-
throw err;
|
|
1397
|
-
}
|
|
1398
|
-
}
|
|
1399
|
-
}
|
|
1400
|
-
async handlePlanDeletedEvent(event, _accountId) {
|
|
1401
|
-
const plan = event.data.object;
|
|
1402
|
-
await this.deletePlan(plan.id);
|
|
1403
|
-
}
|
|
1404
|
-
async handleSetupIntentEvent(event, accountId) {
|
|
1405
|
-
const { entity: setupIntent, refetched } = await this.fetchOrUseWebhookData(
|
|
1406
|
-
event.data.object,
|
|
1407
|
-
(id) => this.stripe.setupIntents.retrieve(id),
|
|
1408
|
-
(setupIntent2) => setupIntent2.status === "canceled" || setupIntent2.status === "succeeded"
|
|
1409
|
-
);
|
|
1410
|
-
await this.upsertSetupIntents(
|
|
1411
|
-
[setupIntent],
|
|
1412
|
-
accountId,
|
|
1413
|
-
false,
|
|
1414
|
-
this.getSyncTimestamp(event, refetched)
|
|
1415
|
-
);
|
|
1416
|
-
}
|
|
1417
|
-
async handleSubscriptionScheduleEvent(event, accountId) {
|
|
1418
|
-
const { entity: subscriptionSchedule, refetched } = await this.fetchOrUseWebhookData(
|
|
1419
|
-
event.data.object,
|
|
1420
|
-
(id) => this.stripe.subscriptionSchedules.retrieve(id),
|
|
1421
|
-
(schedule) => schedule.status === "canceled" || schedule.status === "completed"
|
|
1422
|
-
);
|
|
1423
|
-
await this.upsertSubscriptionSchedules(
|
|
1424
|
-
[subscriptionSchedule],
|
|
1425
|
-
accountId,
|
|
1426
|
-
false,
|
|
1427
|
-
this.getSyncTimestamp(event, refetched)
|
|
1428
|
-
);
|
|
1429
|
-
}
|
|
1430
|
-
async handlePaymentMethodEvent(event, accountId) {
|
|
1431
|
-
const { entity: paymentMethod, refetched } = await this.fetchOrUseWebhookData(
|
|
1432
|
-
event.data.object,
|
|
1433
|
-
(id) => this.stripe.paymentMethods.retrieve(id)
|
|
1434
|
-
);
|
|
1435
|
-
await this.upsertPaymentMethods(
|
|
1436
|
-
[paymentMethod],
|
|
1437
|
-
accountId,
|
|
1438
|
-
false,
|
|
1439
|
-
this.getSyncTimestamp(event, refetched)
|
|
1440
|
-
);
|
|
1441
|
-
}
|
|
1442
|
-
async handleDisputeEvent(event, accountId) {
|
|
1443
|
-
const { entity: dispute, refetched } = await this.fetchOrUseWebhookData(
|
|
1444
|
-
event.data.object,
|
|
1445
|
-
(id) => this.stripe.disputes.retrieve(id),
|
|
1446
|
-
(dispute2) => dispute2.status === "won" || dispute2.status === "lost"
|
|
1447
|
-
);
|
|
1448
|
-
await this.upsertDisputes([dispute], accountId, false, this.getSyncTimestamp(event, refetched));
|
|
1449
|
-
}
|
|
1450
|
-
async handlePaymentIntentEvent(event, accountId) {
|
|
1451
|
-
const { entity: paymentIntent, refetched } = await this.fetchOrUseWebhookData(
|
|
1452
|
-
event.data.object,
|
|
1453
|
-
(id) => this.stripe.paymentIntents.retrieve(id),
|
|
1454
|
-
// Final states - do not re-fetch from API
|
|
1455
|
-
(entity) => entity.status === "canceled" || entity.status === "succeeded"
|
|
1456
|
-
);
|
|
1457
|
-
await this.upsertPaymentIntents(
|
|
1458
|
-
[paymentIntent],
|
|
1459
|
-
accountId,
|
|
1460
|
-
false,
|
|
1461
|
-
this.getSyncTimestamp(event, refetched)
|
|
1462
|
-
);
|
|
1463
|
-
}
|
|
1464
|
-
async handleCreditNoteEvent(event, accountId) {
|
|
1465
|
-
const { entity: creditNote, refetched } = await this.fetchOrUseWebhookData(
|
|
1466
|
-
event.data.object,
|
|
1467
|
-
(id) => this.stripe.creditNotes.retrieve(id),
|
|
1468
|
-
(creditNote2) => creditNote2.status === "void"
|
|
1469
|
-
);
|
|
1470
|
-
await this.upsertCreditNotes(
|
|
1471
|
-
[creditNote],
|
|
1472
|
-
accountId,
|
|
1473
|
-
false,
|
|
1474
|
-
this.getSyncTimestamp(event, refetched)
|
|
1475
|
-
);
|
|
1476
|
-
}
|
|
1477
|
-
async handleEarlyFraudWarningEvent(event, accountId) {
|
|
1478
|
-
const { entity: earlyFraudWarning, refetched } = await this.fetchOrUseWebhookData(
|
|
1479
|
-
event.data.object,
|
|
1480
|
-
(id) => this.stripe.radar.earlyFraudWarnings.retrieve(id)
|
|
1481
|
-
);
|
|
1482
|
-
await this.upsertEarlyFraudWarning(
|
|
1483
|
-
[earlyFraudWarning],
|
|
1484
|
-
accountId,
|
|
1485
|
-
false,
|
|
1486
|
-
this.getSyncTimestamp(event, refetched)
|
|
1487
|
-
);
|
|
1488
|
-
}
|
|
1489
|
-
async handleRefundEvent(event, accountId) {
|
|
1490
|
-
const { entity: refund, refetched } = await this.fetchOrUseWebhookData(
|
|
1491
|
-
event.data.object,
|
|
1492
|
-
(id) => this.stripe.refunds.retrieve(id)
|
|
1493
|
-
);
|
|
1494
|
-
await this.upsertRefunds([refund], accountId, false, this.getSyncTimestamp(event, refetched));
|
|
1495
|
-
}
|
|
1496
|
-
async handleReviewEvent(event, accountId) {
|
|
1497
|
-
const { entity: review, refetched } = await this.fetchOrUseWebhookData(
|
|
1498
|
-
event.data.object,
|
|
1499
|
-
(id) => this.stripe.reviews.retrieve(id)
|
|
1500
|
-
);
|
|
1501
|
-
await this.upsertReviews([review], accountId, false, this.getSyncTimestamp(event, refetched));
|
|
1502
|
-
}
|
|
1503
|
-
async handleEntitlementSummaryEvent(event, accountId) {
|
|
1504
|
-
const activeEntitlementSummary = event.data.object;
|
|
1505
|
-
let entitlements = activeEntitlementSummary.entitlements;
|
|
1506
|
-
let refetched = false;
|
|
1507
|
-
if (this.config.revalidateObjectsViaStripeApi?.includes("entitlements")) {
|
|
1508
|
-
const { lastResponse, ...rest } = await this.stripe.entitlements.activeEntitlements.list({
|
|
1509
|
-
customer: activeEntitlementSummary.customer
|
|
1510
|
-
});
|
|
1511
|
-
entitlements = rest;
|
|
1512
|
-
refetched = true;
|
|
1513
|
-
}
|
|
1514
|
-
await this.deleteRemovedActiveEntitlements(
|
|
1515
|
-
activeEntitlementSummary.customer,
|
|
1516
|
-
entitlements.data.map((entitlement) => entitlement.id)
|
|
1517
|
-
);
|
|
1518
|
-
await this.upsertActiveEntitlements(
|
|
1519
|
-
activeEntitlementSummary.customer,
|
|
1520
|
-
entitlements.data,
|
|
1521
|
-
accountId,
|
|
1522
|
-
false,
|
|
1523
|
-
this.getSyncTimestamp(event, refetched)
|
|
1524
|
-
);
|
|
1525
|
-
}
|
|
1526
|
-
getSyncTimestamp(event, refetched) {
|
|
1527
|
-
return refetched ? (/* @__PURE__ */ new Date()).toISOString() : new Date(event.created * 1e3).toISOString();
|
|
1528
|
-
}
|
|
1529
|
-
shouldRefetchEntity(entity) {
|
|
1530
|
-
return this.config.revalidateObjectsViaStripeApi?.includes(entity.object);
|
|
1531
|
-
}
|
|
1532
|
-
async fetchOrUseWebhookData(entity, fetchFn, entityInFinalState) {
|
|
1533
|
-
if (!entity.id) return { entity, refetched: false };
|
|
1534
|
-
if (entityInFinalState && entityInFinalState(entity)) return { entity, refetched: false };
|
|
1535
|
-
if (this.shouldRefetchEntity(entity)) {
|
|
1536
|
-
const fetchedEntity = await fetchFn(entity.id);
|
|
1537
|
-
return { entity: fetchedEntity, refetched: true };
|
|
1538
|
-
}
|
|
1539
|
-
return { entity, refetched: false };
|
|
1540
|
-
}
|
|
1541
|
-
async syncSingleEntity(stripeId) {
|
|
1542
|
-
const accountId = await this.getAccountId();
|
|
1543
|
-
if (stripeId.startsWith("cus_")) {
|
|
1544
|
-
return this.stripe.customers.retrieve(stripeId).then((it) => {
|
|
1545
|
-
if (!it || it.deleted) return;
|
|
1546
|
-
return this.upsertCustomers([it], accountId);
|
|
1547
|
-
});
|
|
1548
|
-
} else if (stripeId.startsWith("in_")) {
|
|
1549
|
-
return this.stripe.invoices.retrieve(stripeId).then((it) => this.upsertInvoices([it], accountId));
|
|
1550
|
-
} else if (stripeId.startsWith("price_")) {
|
|
1551
|
-
return this.stripe.prices.retrieve(stripeId).then((it) => this.upsertPrices([it], accountId));
|
|
1552
|
-
} else if (stripeId.startsWith("prod_")) {
|
|
1553
|
-
return this.stripe.products.retrieve(stripeId).then((it) => this.upsertProducts([it], accountId));
|
|
1554
|
-
} else if (stripeId.startsWith("sub_")) {
|
|
1555
|
-
return this.stripe.subscriptions.retrieve(stripeId).then((it) => this.upsertSubscriptions([it], accountId));
|
|
1556
|
-
} else if (stripeId.startsWith("seti_")) {
|
|
1557
|
-
return this.stripe.setupIntents.retrieve(stripeId).then((it) => this.upsertSetupIntents([it], accountId));
|
|
1558
|
-
} else if (stripeId.startsWith("pm_")) {
|
|
1559
|
-
return this.stripe.paymentMethods.retrieve(stripeId).then((it) => this.upsertPaymentMethods([it], accountId));
|
|
1560
|
-
} else if (stripeId.startsWith("dp_") || stripeId.startsWith("du_")) {
|
|
1561
|
-
return this.stripe.disputes.retrieve(stripeId).then((it) => this.upsertDisputes([it], accountId));
|
|
1562
|
-
} else if (stripeId.startsWith("ch_")) {
|
|
1563
|
-
return this.stripe.charges.retrieve(stripeId).then((it) => this.upsertCharges([it], accountId, true));
|
|
1564
|
-
} else if (stripeId.startsWith("pi_")) {
|
|
1565
|
-
return this.stripe.paymentIntents.retrieve(stripeId).then((it) => this.upsertPaymentIntents([it], accountId));
|
|
1566
|
-
} else if (stripeId.startsWith("txi_")) {
|
|
1567
|
-
return this.stripe.taxIds.retrieve(stripeId).then((it) => this.upsertTaxIds([it], accountId));
|
|
1568
|
-
} else if (stripeId.startsWith("cn_")) {
|
|
1569
|
-
return this.stripe.creditNotes.retrieve(stripeId).then((it) => this.upsertCreditNotes([it], accountId));
|
|
1570
|
-
} else if (stripeId.startsWith("issfr_")) {
|
|
1571
|
-
return this.stripe.radar.earlyFraudWarnings.retrieve(stripeId).then((it) => this.upsertEarlyFraudWarning([it], accountId));
|
|
1572
|
-
} else if (stripeId.startsWith("prv_")) {
|
|
1573
|
-
return this.stripe.reviews.retrieve(stripeId).then((it) => this.upsertReviews([it], accountId));
|
|
1574
|
-
} else if (stripeId.startsWith("re_")) {
|
|
1575
|
-
return this.stripe.refunds.retrieve(stripeId).then((it) => this.upsertRefunds([it], accountId));
|
|
1576
|
-
} else if (stripeId.startsWith("feat_")) {
|
|
1577
|
-
return this.stripe.entitlements.features.retrieve(stripeId).then((it) => this.upsertFeatures([it], accountId));
|
|
1578
|
-
} else if (stripeId.startsWith("cs_")) {
|
|
1579
|
-
return this.stripe.checkout.sessions.retrieve(stripeId).then((it) => this.upsertCheckoutSessions([it], accountId));
|
|
1580
|
-
}
|
|
1581
|
-
}
|
|
1582
|
-
/**
|
|
1583
|
-
* Process one page of items for the specified object type.
|
|
1584
|
-
* Returns the number of items processed and whether there are more pages.
|
|
1585
|
-
*
|
|
1586
|
-
* This method is designed for queue-based consumption where each page
|
|
1587
|
-
* is processed as a separate job. Uses the observable sync system for tracking.
|
|
1588
|
-
*
|
|
1589
|
-
* @param object - The Stripe object type to sync (e.g., 'customer', 'product')
|
|
1590
|
-
* @param params - Optional parameters for filtering and run context
|
|
1591
|
-
* @returns ProcessNextResult with processed count, hasMore flag, and runStartedAt
|
|
1592
|
-
*
|
|
1593
|
-
* @example
|
|
1594
|
-
* ```typescript
|
|
1595
|
-
* // Queue worker
|
|
1596
|
-
* const { hasMore, runStartedAt } = await stripeSync.processNext('customer')
|
|
1597
|
-
* if (hasMore) {
|
|
1598
|
-
* await queue.send({ object: 'customer', runStartedAt })
|
|
1599
|
-
* }
|
|
1600
|
-
* ```
|
|
1601
|
-
*/
|
|
1602
|
-
async processNext(object, params) {
|
|
1603
|
-
await this.getCurrentAccount();
|
|
1604
|
-
const accountId = await this.getAccountId();
|
|
1605
|
-
const resourceName = this.getResourceName(object);
|
|
1606
|
-
let runStartedAt;
|
|
1607
|
-
if (params?.runStartedAt) {
|
|
1608
|
-
runStartedAt = params.runStartedAt;
|
|
1609
|
-
} else {
|
|
1610
|
-
const runKey = await this.postgresClient.getOrCreateSyncRun(
|
|
1611
|
-
accountId,
|
|
1612
|
-
params?.triggeredBy ?? "processNext"
|
|
1613
|
-
);
|
|
1614
|
-
if (!runKey) {
|
|
1615
|
-
const activeRun = await this.postgresClient.getActiveSyncRun(accountId);
|
|
1616
|
-
if (!activeRun) {
|
|
1617
|
-
throw new Error("Failed to get or create sync run");
|
|
1618
|
-
}
|
|
1619
|
-
runStartedAt = activeRun.runStartedAt;
|
|
1620
|
-
} else {
|
|
1621
|
-
runStartedAt = runKey.runStartedAt;
|
|
1622
|
-
}
|
|
1623
|
-
}
|
|
1624
|
-
await this.postgresClient.createObjectRuns(accountId, runStartedAt, [resourceName]);
|
|
1625
|
-
const objRun = await this.postgresClient.getObjectRun(accountId, runStartedAt, resourceName);
|
|
1626
|
-
if (objRun?.status === "complete" || objRun?.status === "error") {
|
|
1627
|
-
return {
|
|
1628
|
-
processed: 0,
|
|
1629
|
-
hasMore: false,
|
|
1630
|
-
runStartedAt
|
|
1631
|
-
};
|
|
1632
|
-
}
|
|
1633
|
-
if (objRun?.status === "pending") {
|
|
1634
|
-
const started = await this.postgresClient.tryStartObjectSync(
|
|
1635
|
-
accountId,
|
|
1636
|
-
runStartedAt,
|
|
1637
|
-
resourceName
|
|
1638
|
-
);
|
|
1639
|
-
if (!started) {
|
|
1640
|
-
return {
|
|
1641
|
-
processed: 0,
|
|
1642
|
-
hasMore: true,
|
|
1643
|
-
runStartedAt
|
|
1644
|
-
};
|
|
1645
|
-
}
|
|
1646
|
-
}
|
|
1647
|
-
let cursor = null;
|
|
1648
|
-
if (!params?.created) {
|
|
1649
|
-
if (objRun?.cursor) {
|
|
1650
|
-
cursor = parseInt(objRun.cursor);
|
|
1651
|
-
} else {
|
|
1652
|
-
const lastCursor = await this.postgresClient.getLastCompletedCursor(accountId, resourceName);
|
|
1653
|
-
cursor = lastCursor ? parseInt(lastCursor) : null;
|
|
1654
|
-
}
|
|
1655
|
-
}
|
|
1656
|
-
const result = await this.fetchOnePage(
|
|
1657
|
-
object,
|
|
1658
|
-
accountId,
|
|
1659
|
-
resourceName,
|
|
1660
|
-
runStartedAt,
|
|
1661
|
-
cursor,
|
|
1662
|
-
params
|
|
1663
|
-
);
|
|
1664
|
-
return result;
|
|
1665
|
-
}
|
|
1666
|
-
/**
|
|
1667
|
-
* Get the database resource name for a SyncObject type
|
|
1668
|
-
*/
|
|
1669
|
-
getResourceName(object) {
|
|
1670
|
-
const mapping = {
|
|
1671
|
-
customer: "customers",
|
|
1672
|
-
invoice: "invoices",
|
|
1673
|
-
price: "prices",
|
|
1674
|
-
product: "products",
|
|
1675
|
-
subscription: "subscriptions",
|
|
1676
|
-
subscription_schedules: "subscription_schedules",
|
|
1677
|
-
setup_intent: "setup_intents",
|
|
1678
|
-
payment_method: "payment_methods",
|
|
1679
|
-
dispute: "disputes",
|
|
1680
|
-
charge: "charges",
|
|
1681
|
-
payment_intent: "payment_intents",
|
|
1682
|
-
plan: "plans",
|
|
1683
|
-
tax_id: "tax_ids",
|
|
1684
|
-
credit_note: "credit_notes",
|
|
1685
|
-
early_fraud_warning: "early_fraud_warnings",
|
|
1686
|
-
refund: "refunds",
|
|
1687
|
-
checkout_sessions: "checkout_sessions"
|
|
1688
|
-
};
|
|
1689
|
-
return mapping[object] || object;
|
|
1690
|
-
}
|
|
1691
|
-
/**
|
|
1692
|
-
* Fetch one page of items from Stripe and upsert to database.
|
|
1693
|
-
* Uses resourceRegistry for DRY list/upsert operations.
|
|
1694
|
-
* Uses the observable sync system for tracking progress.
|
|
1695
|
-
*/
|
|
1696
|
-
async fetchOnePage(object, accountId, resourceName, runStartedAt, cursor, params) {
|
|
1697
|
-
const limit = 100;
|
|
1698
|
-
if (object === "payment_method" || object === "tax_id") {
|
|
1699
|
-
this.config.logger?.warn(`processNext for ${object} requires customer context`);
|
|
1700
|
-
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
1701
|
-
return { processed: 0, hasMore: false, runStartedAt };
|
|
1702
|
-
}
|
|
1703
|
-
const config = this.resourceRegistry[object];
|
|
1704
|
-
if (!config) {
|
|
1705
|
-
throw new Error(`Unsupported object type for processNext: ${object}`);
|
|
1706
|
-
}
|
|
1707
|
-
try {
|
|
1708
|
-
const listParams = { limit };
|
|
1709
|
-
if (config.supportsCreatedFilter) {
|
|
1710
|
-
const created = params?.created ?? (cursor ? { gte: cursor } : void 0);
|
|
1711
|
-
if (created) {
|
|
1712
|
-
listParams.created = created;
|
|
1713
|
-
}
|
|
1714
|
-
}
|
|
1715
|
-
const response = await config.listFn(listParams);
|
|
1716
|
-
if (response.data.length > 0) {
|
|
1717
|
-
this.config.logger?.info(`processNext: upserting ${response.data.length} ${resourceName}`);
|
|
1718
|
-
await config.upsertFn(response.data, accountId, params?.backfillRelatedEntities);
|
|
1719
|
-
await this.postgresClient.incrementObjectProgress(
|
|
1720
|
-
accountId,
|
|
1721
|
-
runStartedAt,
|
|
1722
|
-
resourceName,
|
|
1723
|
-
response.data.length
|
|
1724
|
-
);
|
|
1725
|
-
const maxCreated = Math.max(
|
|
1726
|
-
...response.data.map((i) => i.created || 0)
|
|
1727
|
-
);
|
|
1728
|
-
if (maxCreated > 0) {
|
|
1729
|
-
await this.postgresClient.updateObjectCursor(
|
|
1730
|
-
accountId,
|
|
1731
|
-
runStartedAt,
|
|
1732
|
-
resourceName,
|
|
1733
|
-
String(maxCreated)
|
|
1734
|
-
);
|
|
1735
|
-
}
|
|
1736
|
-
}
|
|
1737
|
-
if (!response.has_more) {
|
|
1738
|
-
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
1739
|
-
}
|
|
1740
|
-
return {
|
|
1741
|
-
processed: response.data.length,
|
|
1742
|
-
hasMore: response.has_more,
|
|
1743
|
-
runStartedAt
|
|
1744
|
-
};
|
|
1745
|
-
} catch (error) {
|
|
1746
|
-
await this.postgresClient.failObjectSync(
|
|
1747
|
-
accountId,
|
|
1748
|
-
runStartedAt,
|
|
1749
|
-
resourceName,
|
|
1750
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
1751
|
-
);
|
|
1752
|
-
throw error;
|
|
1753
|
-
}
|
|
1754
|
-
}
|
|
1755
|
-
/**
|
|
1756
|
-
* Process all pages for all (or specified) object types until complete.
|
|
1757
|
-
*
|
|
1758
|
-
* @param params - Optional parameters for filtering and specifying object types
|
|
1759
|
-
* @returns SyncBackfill with counts for each synced resource type
|
|
1760
|
-
*/
|
|
1761
|
-
/**
|
|
1762
|
-
* Process all pages for a single object type until complete.
|
|
1763
|
-
* Loops processNext() internally until hasMore is false.
|
|
1764
|
-
*
|
|
1765
|
-
* @param object - The object type to sync
|
|
1766
|
-
* @param runStartedAt - The sync run to use (for sharing across objects)
|
|
1767
|
-
* @param params - Optional sync parameters
|
|
1768
|
-
* @returns Sync result with count of synced items
|
|
1769
|
-
*/
|
|
1770
|
-
async processObjectUntilDone(object, runStartedAt, params) {
|
|
1771
|
-
let totalSynced = 0;
|
|
1772
|
-
while (true) {
|
|
1773
|
-
const result = await this.processNext(object, {
|
|
1774
|
-
...params,
|
|
1775
|
-
runStartedAt,
|
|
1776
|
-
triggeredBy: "processUntilDone"
|
|
1777
|
-
});
|
|
1778
|
-
totalSynced += result.processed;
|
|
1779
|
-
if (!result.hasMore) {
|
|
1780
|
-
break;
|
|
1781
|
-
}
|
|
1782
|
-
}
|
|
1783
|
-
return { synced: totalSynced };
|
|
1784
|
-
}
|
|
1785
|
-
async processUntilDone(params) {
|
|
1786
|
-
const { object } = params ?? { object: "all" };
|
|
1787
|
-
await this.getCurrentAccount();
|
|
1788
|
-
const accountId = await this.getAccountId();
|
|
1789
|
-
const runKey = await this.postgresClient.getOrCreateSyncRun(accountId, "processUntilDone");
|
|
1790
|
-
if (!runKey) {
|
|
1791
|
-
const activeRun = await this.postgresClient.getActiveSyncRun(accountId);
|
|
1792
|
-
if (!activeRun) {
|
|
1793
|
-
throw new Error("Failed to get or create sync run");
|
|
1794
|
-
}
|
|
1795
|
-
return this.processUntilDoneWithRun(activeRun.runStartedAt, object, params);
|
|
1796
|
-
}
|
|
1797
|
-
return this.processUntilDoneWithRun(runKey.runStartedAt, object, params);
|
|
1798
|
-
}
|
|
1799
|
-
/**
|
|
1800
|
-
* Internal implementation of processUntilDone with an existing run.
|
|
1801
|
-
*/
|
|
1802
|
-
async processUntilDoneWithRun(runStartedAt, object, params) {
|
|
1803
|
-
const accountId = await this.getAccountId();
|
|
1804
|
-
const results = {};
|
|
1805
|
-
try {
|
|
1806
|
-
const objectsToSync = object === "all" || object === void 0 ? this.getSupportedSyncObjects() : [object];
|
|
1807
|
-
for (const obj of objectsToSync) {
|
|
1808
|
-
this.config.logger?.info(`Syncing ${obj}`);
|
|
1809
|
-
if (obj === "payment_method") {
|
|
1810
|
-
results.paymentMethods = await this.syncPaymentMethodsWithRun(runStartedAt, params);
|
|
1811
|
-
} else {
|
|
1812
|
-
const result = await this.processObjectUntilDone(obj, runStartedAt, params);
|
|
1813
|
-
switch (obj) {
|
|
1814
|
-
case "product":
|
|
1815
|
-
results.products = result;
|
|
1816
|
-
break;
|
|
1817
|
-
case "price":
|
|
1818
|
-
results.prices = result;
|
|
1819
|
-
break;
|
|
1820
|
-
case "plan":
|
|
1821
|
-
results.plans = result;
|
|
1822
|
-
break;
|
|
1823
|
-
case "customer":
|
|
1824
|
-
results.customers = result;
|
|
1825
|
-
break;
|
|
1826
|
-
case "subscription":
|
|
1827
|
-
results.subscriptions = result;
|
|
1828
|
-
break;
|
|
1829
|
-
case "subscription_schedules":
|
|
1830
|
-
results.subscriptionSchedules = result;
|
|
1831
|
-
break;
|
|
1832
|
-
case "invoice":
|
|
1833
|
-
results.invoices = result;
|
|
1834
|
-
break;
|
|
1835
|
-
case "charge":
|
|
1836
|
-
results.charges = result;
|
|
1837
|
-
break;
|
|
1838
|
-
case "setup_intent":
|
|
1839
|
-
results.setupIntents = result;
|
|
1840
|
-
break;
|
|
1841
|
-
case "payment_intent":
|
|
1842
|
-
results.paymentIntents = result;
|
|
1843
|
-
break;
|
|
1844
|
-
case "tax_id":
|
|
1845
|
-
results.taxIds = result;
|
|
1846
|
-
break;
|
|
1847
|
-
case "credit_note":
|
|
1848
|
-
results.creditNotes = result;
|
|
1849
|
-
break;
|
|
1850
|
-
case "dispute":
|
|
1851
|
-
results.disputes = result;
|
|
1852
|
-
break;
|
|
1853
|
-
case "early_fraud_warning":
|
|
1854
|
-
results.earlyFraudWarnings = result;
|
|
1855
|
-
break;
|
|
1856
|
-
case "refund":
|
|
1857
|
-
results.refunds = result;
|
|
1858
|
-
break;
|
|
1859
|
-
case "checkout_sessions":
|
|
1860
|
-
results.checkoutSessions = result;
|
|
1861
|
-
break;
|
|
1862
|
-
}
|
|
1863
|
-
}
|
|
1864
|
-
}
|
|
1865
|
-
await this.postgresClient.closeSyncRun(accountId, runStartedAt);
|
|
1866
|
-
return results;
|
|
1867
|
-
} catch (error) {
|
|
1868
|
-
await this.postgresClient.closeSyncRun(accountId, runStartedAt);
|
|
1869
|
-
throw error;
|
|
1870
|
-
}
|
|
1871
|
-
}
|
|
1872
|
-
/**
|
|
1873
|
-
* Sync payment methods with an existing run (special case - iterates customers)
|
|
1874
|
-
*/
|
|
1875
|
-
async syncPaymentMethodsWithRun(runStartedAt, syncParams) {
|
|
1876
|
-
const accountId = await this.getAccountId();
|
|
1877
|
-
const resourceName = "payment_methods";
|
|
1878
|
-
await this.postgresClient.createObjectRuns(accountId, runStartedAt, [resourceName]);
|
|
1879
|
-
await this.postgresClient.tryStartObjectSync(accountId, runStartedAt, resourceName);
|
|
1880
|
-
try {
|
|
1881
|
-
const prepared = sql2(
|
|
1882
|
-
`select id from "stripe"."customers" WHERE COALESCE(deleted, false) <> true;`
|
|
1883
|
-
)([]);
|
|
1884
|
-
const customerIds = await this.postgresClient.query(prepared.text, prepared.values).then(({ rows }) => rows.map((it) => it.id));
|
|
1885
|
-
this.config.logger?.info(`Getting payment methods for ${customerIds.length} customers`);
|
|
1886
|
-
let synced = 0;
|
|
1887
|
-
for (const customerIdChunk of chunkArray(customerIds, 10)) {
|
|
1888
|
-
await Promise.all(
|
|
1889
|
-
customerIdChunk.map(async (customerId) => {
|
|
1890
|
-
const CHECKPOINT_SIZE = 100;
|
|
1891
|
-
let currentBatch = [];
|
|
1892
|
-
for await (const item of this.stripe.paymentMethods.list({
|
|
1893
|
-
limit: 100,
|
|
1894
|
-
customer: customerId
|
|
1895
|
-
})) {
|
|
1896
|
-
currentBatch.push(item);
|
|
1897
|
-
if (currentBatch.length >= CHECKPOINT_SIZE) {
|
|
1898
|
-
await this.upsertPaymentMethods(
|
|
1899
|
-
currentBatch,
|
|
1900
|
-
accountId,
|
|
1901
|
-
syncParams?.backfillRelatedEntities
|
|
1902
|
-
);
|
|
1903
|
-
synced += currentBatch.length;
|
|
1904
|
-
await this.postgresClient.incrementObjectProgress(
|
|
1905
|
-
accountId,
|
|
1906
|
-
runStartedAt,
|
|
1907
|
-
resourceName,
|
|
1908
|
-
currentBatch.length
|
|
1909
|
-
);
|
|
1910
|
-
currentBatch = [];
|
|
1911
|
-
}
|
|
1912
|
-
}
|
|
1913
|
-
if (currentBatch.length > 0) {
|
|
1914
|
-
await this.upsertPaymentMethods(
|
|
1915
|
-
currentBatch,
|
|
1916
|
-
accountId,
|
|
1917
|
-
syncParams?.backfillRelatedEntities
|
|
1918
|
-
);
|
|
1919
|
-
synced += currentBatch.length;
|
|
1920
|
-
await this.postgresClient.incrementObjectProgress(
|
|
1921
|
-
accountId,
|
|
1922
|
-
runStartedAt,
|
|
1923
|
-
resourceName,
|
|
1924
|
-
currentBatch.length
|
|
1925
|
-
);
|
|
1926
|
-
}
|
|
1927
|
-
})
|
|
1928
|
-
);
|
|
1929
|
-
}
|
|
1930
|
-
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
1931
|
-
return { synced };
|
|
1932
|
-
} catch (error) {
|
|
1933
|
-
await this.postgresClient.failObjectSync(
|
|
1934
|
-
accountId,
|
|
1935
|
-
runStartedAt,
|
|
1936
|
-
resourceName,
|
|
1937
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
1938
|
-
);
|
|
1939
|
-
throw error;
|
|
1940
|
-
}
|
|
1941
|
-
}
|
|
1942
|
-
async syncProducts(syncParams) {
|
|
1943
|
-
this.config.logger?.info("Syncing products");
|
|
1944
|
-
return this.withSyncRun("products", "syncProducts", async (cursor, runStartedAt) => {
|
|
1945
|
-
const accountId = await this.getAccountId();
|
|
1946
|
-
const params = { limit: 100 };
|
|
1947
|
-
if (syncParams?.created) {
|
|
1948
|
-
params.created = syncParams.created;
|
|
1949
|
-
} else if (cursor) {
|
|
1950
|
-
params.created = { gte: cursor };
|
|
1951
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
1952
|
-
}
|
|
1953
|
-
return this.fetchAndUpsert(
|
|
1954
|
-
() => this.stripe.products.list(params),
|
|
1955
|
-
(products) => this.upsertProducts(products, accountId),
|
|
1956
|
-
accountId,
|
|
1957
|
-
"products",
|
|
1958
|
-
runStartedAt
|
|
1959
|
-
);
|
|
1960
|
-
});
|
|
1961
|
-
}
|
|
1962
|
-
async syncPrices(syncParams) {
|
|
1963
|
-
this.config.logger?.info("Syncing prices");
|
|
1964
|
-
return this.withSyncRun("prices", "syncPrices", async (cursor, runStartedAt) => {
|
|
1965
|
-
const accountId = await this.getAccountId();
|
|
1966
|
-
const params = { limit: 100 };
|
|
1967
|
-
if (syncParams?.created) {
|
|
1968
|
-
params.created = syncParams.created;
|
|
1969
|
-
} else if (cursor) {
|
|
1970
|
-
params.created = { gte: cursor };
|
|
1971
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
1972
|
-
}
|
|
1973
|
-
return this.fetchAndUpsert(
|
|
1974
|
-
() => this.stripe.prices.list(params),
|
|
1975
|
-
(prices) => this.upsertPrices(prices, accountId, syncParams?.backfillRelatedEntities),
|
|
1976
|
-
accountId,
|
|
1977
|
-
"prices",
|
|
1978
|
-
runStartedAt
|
|
1979
|
-
);
|
|
1980
|
-
});
|
|
1981
|
-
}
|
|
1982
|
-
async syncPlans(syncParams) {
|
|
1983
|
-
this.config.logger?.info("Syncing plans");
|
|
1984
|
-
return this.withSyncRun("plans", "syncPlans", async (cursor, runStartedAt) => {
|
|
1985
|
-
const accountId = await this.getAccountId();
|
|
1986
|
-
const params = { limit: 100 };
|
|
1987
|
-
if (syncParams?.created) {
|
|
1988
|
-
params.created = syncParams.created;
|
|
1989
|
-
} else if (cursor) {
|
|
1990
|
-
params.created = { gte: cursor };
|
|
1991
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
1992
|
-
}
|
|
1993
|
-
return this.fetchAndUpsert(
|
|
1994
|
-
() => this.stripe.plans.list(params),
|
|
1995
|
-
(plans) => this.upsertPlans(plans, accountId, syncParams?.backfillRelatedEntities),
|
|
1996
|
-
accountId,
|
|
1997
|
-
"plans",
|
|
1998
|
-
runStartedAt
|
|
1999
|
-
);
|
|
2000
|
-
});
|
|
2001
|
-
}
|
|
2002
|
-
async syncCustomers(syncParams) {
|
|
2003
|
-
this.config.logger?.info("Syncing customers");
|
|
2004
|
-
return this.withSyncRun("customers", "syncCustomers", async (cursor, runStartedAt) => {
|
|
2005
|
-
const accountId = await this.getAccountId();
|
|
2006
|
-
const params = { limit: 100 };
|
|
2007
|
-
if (syncParams?.created) {
|
|
2008
|
-
params.created = syncParams.created;
|
|
2009
|
-
} else if (cursor) {
|
|
2010
|
-
params.created = { gte: cursor };
|
|
2011
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2012
|
-
}
|
|
2013
|
-
return this.fetchAndUpsert(
|
|
2014
|
-
() => this.stripe.customers.list(params),
|
|
2015
|
-
// @ts-expect-error
|
|
2016
|
-
(items) => this.upsertCustomers(items, accountId),
|
|
2017
|
-
accountId,
|
|
2018
|
-
"customers",
|
|
2019
|
-
runStartedAt
|
|
2020
|
-
);
|
|
2021
|
-
});
|
|
2022
|
-
}
|
|
2023
|
-
async syncSubscriptions(syncParams) {
|
|
2024
|
-
this.config.logger?.info("Syncing subscriptions");
|
|
2025
|
-
return this.withSyncRun("subscriptions", "syncSubscriptions", async (cursor, runStartedAt) => {
|
|
2026
|
-
const accountId = await this.getAccountId();
|
|
2027
|
-
const params = { status: "all", limit: 100 };
|
|
2028
|
-
if (syncParams?.created) {
|
|
2029
|
-
params.created = syncParams.created;
|
|
2030
|
-
} else if (cursor) {
|
|
2031
|
-
params.created = { gte: cursor };
|
|
2032
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2033
|
-
}
|
|
2034
|
-
return this.fetchAndUpsert(
|
|
2035
|
-
() => this.stripe.subscriptions.list(params),
|
|
2036
|
-
(items) => this.upsertSubscriptions(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2037
|
-
accountId,
|
|
2038
|
-
"subscriptions",
|
|
2039
|
-
runStartedAt
|
|
2040
|
-
);
|
|
2041
|
-
});
|
|
2042
|
-
}
|
|
2043
|
-
async syncSubscriptionSchedules(syncParams) {
|
|
2044
|
-
this.config.logger?.info("Syncing subscription schedules");
|
|
2045
|
-
return this.withSyncRun(
|
|
2046
|
-
"subscription_schedules",
|
|
2047
|
-
"syncSubscriptionSchedules",
|
|
2048
|
-
async (cursor, runStartedAt) => {
|
|
2049
|
-
const accountId = await this.getAccountId();
|
|
2050
|
-
const params = { limit: 100 };
|
|
2051
|
-
if (syncParams?.created) {
|
|
2052
|
-
params.created = syncParams.created;
|
|
2053
|
-
} else if (cursor) {
|
|
2054
|
-
params.created = { gte: cursor };
|
|
2055
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2056
|
-
}
|
|
2057
|
-
return this.fetchAndUpsert(
|
|
2058
|
-
() => this.stripe.subscriptionSchedules.list(params),
|
|
2059
|
-
(items) => this.upsertSubscriptionSchedules(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2060
|
-
accountId,
|
|
2061
|
-
"subscription_schedules",
|
|
2062
|
-
runStartedAt
|
|
2063
|
-
);
|
|
2064
|
-
}
|
|
2065
|
-
);
|
|
2066
|
-
}
|
|
2067
|
-
async syncInvoices(syncParams) {
|
|
2068
|
-
this.config.logger?.info("Syncing invoices");
|
|
2069
|
-
return this.withSyncRun("invoices", "syncInvoices", async (cursor, runStartedAt) => {
|
|
2070
|
-
const accountId = await this.getAccountId();
|
|
2071
|
-
const params = { limit: 100 };
|
|
2072
|
-
if (syncParams?.created) {
|
|
2073
|
-
params.created = syncParams.created;
|
|
2074
|
-
} else if (cursor) {
|
|
2075
|
-
params.created = { gte: cursor };
|
|
2076
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2077
|
-
}
|
|
2078
|
-
return this.fetchAndUpsert(
|
|
2079
|
-
() => this.stripe.invoices.list(params),
|
|
2080
|
-
(items) => this.upsertInvoices(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2081
|
-
accountId,
|
|
2082
|
-
"invoices",
|
|
2083
|
-
runStartedAt
|
|
2084
|
-
);
|
|
2085
|
-
});
|
|
2086
|
-
}
|
|
2087
|
-
async syncCharges(syncParams) {
|
|
2088
|
-
this.config.logger?.info("Syncing charges");
|
|
2089
|
-
return this.withSyncRun("charges", "syncCharges", async (cursor, runStartedAt) => {
|
|
2090
|
-
const accountId = await this.getAccountId();
|
|
2091
|
-
const params = { limit: 100 };
|
|
2092
|
-
if (syncParams?.created) {
|
|
2093
|
-
params.created = syncParams.created;
|
|
2094
|
-
} else if (cursor) {
|
|
2095
|
-
params.created = { gte: cursor };
|
|
2096
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2097
|
-
}
|
|
2098
|
-
return this.fetchAndUpsert(
|
|
2099
|
-
() => this.stripe.charges.list(params),
|
|
2100
|
-
(items) => this.upsertCharges(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2101
|
-
accountId,
|
|
2102
|
-
"charges",
|
|
2103
|
-
runStartedAt
|
|
2104
|
-
);
|
|
2105
|
-
});
|
|
2106
|
-
}
|
|
2107
|
-
async syncSetupIntents(syncParams) {
|
|
2108
|
-
this.config.logger?.info("Syncing setup_intents");
|
|
2109
|
-
return this.withSyncRun("setup_intents", "syncSetupIntents", async (cursor, runStartedAt) => {
|
|
2110
|
-
const accountId = await this.getAccountId();
|
|
2111
|
-
const params = { limit: 100 };
|
|
2112
|
-
if (syncParams?.created) {
|
|
2113
|
-
params.created = syncParams.created;
|
|
2114
|
-
} else if (cursor) {
|
|
2115
|
-
params.created = { gte: cursor };
|
|
2116
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2117
|
-
}
|
|
2118
|
-
return this.fetchAndUpsert(
|
|
2119
|
-
() => this.stripe.setupIntents.list(params),
|
|
2120
|
-
(items) => this.upsertSetupIntents(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2121
|
-
accountId,
|
|
2122
|
-
"setup_intents",
|
|
2123
|
-
runStartedAt
|
|
2124
|
-
);
|
|
2125
|
-
});
|
|
2126
|
-
}
|
|
2127
|
-
async syncPaymentIntents(syncParams) {
|
|
2128
|
-
this.config.logger?.info("Syncing payment_intents");
|
|
2129
|
-
return this.withSyncRun(
|
|
2130
|
-
"payment_intents",
|
|
2131
|
-
"syncPaymentIntents",
|
|
2132
|
-
async (cursor, runStartedAt) => {
|
|
2133
|
-
const accountId = await this.getAccountId();
|
|
2134
|
-
const params = { limit: 100 };
|
|
2135
|
-
if (syncParams?.created) {
|
|
2136
|
-
params.created = syncParams.created;
|
|
2137
|
-
} else if (cursor) {
|
|
2138
|
-
params.created = { gte: cursor };
|
|
2139
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2140
|
-
}
|
|
2141
|
-
return this.fetchAndUpsert(
|
|
2142
|
-
() => this.stripe.paymentIntents.list(params),
|
|
2143
|
-
(items) => this.upsertPaymentIntents(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2144
|
-
accountId,
|
|
2145
|
-
"payment_intents",
|
|
2146
|
-
runStartedAt
|
|
2147
|
-
);
|
|
2148
|
-
}
|
|
2149
|
-
);
|
|
2150
|
-
}
|
|
2151
|
-
async syncTaxIds(syncParams) {
|
|
2152
|
-
this.config.logger?.info("Syncing tax_ids");
|
|
2153
|
-
return this.withSyncRun("tax_ids", "syncTaxIds", async (_cursor, runStartedAt) => {
|
|
2154
|
-
const accountId = await this.getAccountId();
|
|
2155
|
-
const params = { limit: 100 };
|
|
2156
|
-
return this.fetchAndUpsert(
|
|
2157
|
-
() => this.stripe.taxIds.list(params),
|
|
2158
|
-
(items) => this.upsertTaxIds(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2159
|
-
accountId,
|
|
2160
|
-
"tax_ids",
|
|
2161
|
-
runStartedAt
|
|
2162
|
-
);
|
|
2163
|
-
});
|
|
2164
|
-
}
|
|
2165
|
-
async syncPaymentMethods(syncParams) {
|
|
2166
|
-
this.config.logger?.info("Syncing payment method");
|
|
2167
|
-
return this.withSyncRun(
|
|
2168
|
-
"payment_methods",
|
|
2169
|
-
"syncPaymentMethods",
|
|
2170
|
-
async (_cursor, runStartedAt) => {
|
|
2171
|
-
const accountId = await this.getAccountId();
|
|
2172
|
-
const prepared = sql2(
|
|
2173
|
-
`select id from "stripe"."customers" WHERE COALESCE(deleted, false) <> true;`
|
|
2174
|
-
)([]);
|
|
2175
|
-
const customerIds = await this.postgresClient.query(prepared.text, prepared.values).then(({ rows }) => rows.map((it) => it.id));
|
|
2176
|
-
this.config.logger?.info(`Getting payment methods for ${customerIds.length} customers`);
|
|
2177
|
-
let synced = 0;
|
|
2178
|
-
for (const customerIdChunk of chunkArray(customerIds, 10)) {
|
|
2179
|
-
await Promise.all(
|
|
2180
|
-
customerIdChunk.map(async (customerId) => {
|
|
2181
|
-
const CHECKPOINT_SIZE = 100;
|
|
2182
|
-
let currentBatch = [];
|
|
2183
|
-
for await (const item of this.stripe.paymentMethods.list({
|
|
2184
|
-
limit: 100,
|
|
2185
|
-
customer: customerId
|
|
2186
|
-
})) {
|
|
2187
|
-
currentBatch.push(item);
|
|
2188
|
-
if (currentBatch.length >= CHECKPOINT_SIZE) {
|
|
2189
|
-
await this.upsertPaymentMethods(
|
|
2190
|
-
currentBatch,
|
|
2191
|
-
accountId,
|
|
2192
|
-
syncParams?.backfillRelatedEntities
|
|
2193
|
-
);
|
|
2194
|
-
synced += currentBatch.length;
|
|
2195
|
-
await this.postgresClient.incrementObjectProgress(
|
|
2196
|
-
accountId,
|
|
2197
|
-
runStartedAt,
|
|
2198
|
-
"payment_methods",
|
|
2199
|
-
currentBatch.length
|
|
2200
|
-
);
|
|
2201
|
-
currentBatch = [];
|
|
2202
|
-
}
|
|
2203
|
-
}
|
|
2204
|
-
if (currentBatch.length > 0) {
|
|
2205
|
-
await this.upsertPaymentMethods(
|
|
2206
|
-
currentBatch,
|
|
2207
|
-
accountId,
|
|
2208
|
-
syncParams?.backfillRelatedEntities
|
|
2209
|
-
);
|
|
2210
|
-
synced += currentBatch.length;
|
|
2211
|
-
await this.postgresClient.incrementObjectProgress(
|
|
2212
|
-
accountId,
|
|
2213
|
-
runStartedAt,
|
|
2214
|
-
"payment_methods",
|
|
2215
|
-
currentBatch.length
|
|
2216
|
-
);
|
|
2217
|
-
}
|
|
2218
|
-
})
|
|
2219
|
-
);
|
|
2220
|
-
}
|
|
2221
|
-
await this.postgresClient.completeObjectSync(accountId, runStartedAt, "payment_methods");
|
|
2222
|
-
return { synced };
|
|
2223
|
-
}
|
|
2224
|
-
);
|
|
2225
|
-
}
|
|
2226
|
-
async syncDisputes(syncParams) {
|
|
2227
|
-
this.config.logger?.info("Syncing disputes");
|
|
2228
|
-
return this.withSyncRun("disputes", "syncDisputes", async (cursor, runStartedAt) => {
|
|
2229
|
-
const accountId = await this.getAccountId();
|
|
2230
|
-
const params = { limit: 100 };
|
|
2231
|
-
if (syncParams?.created) {
|
|
2232
|
-
params.created = syncParams.created;
|
|
2233
|
-
} else if (cursor) {
|
|
2234
|
-
params.created = { gte: cursor };
|
|
2235
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2236
|
-
}
|
|
2237
|
-
return this.fetchAndUpsert(
|
|
2238
|
-
() => this.stripe.disputes.list(params),
|
|
2239
|
-
(items) => this.upsertDisputes(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2240
|
-
accountId,
|
|
2241
|
-
"disputes",
|
|
2242
|
-
runStartedAt
|
|
2243
|
-
);
|
|
2244
|
-
});
|
|
2245
|
-
}
|
|
2246
|
-
async syncEarlyFraudWarnings(syncParams) {
|
|
2247
|
-
this.config.logger?.info("Syncing early fraud warnings");
|
|
2248
|
-
return this.withSyncRun(
|
|
2249
|
-
"early_fraud_warnings",
|
|
2250
|
-
"syncEarlyFraudWarnings",
|
|
2251
|
-
async (cursor, runStartedAt) => {
|
|
2252
|
-
const accountId = await this.getAccountId();
|
|
2253
|
-
const params = { limit: 100 };
|
|
2254
|
-
if (syncParams?.created) {
|
|
2255
|
-
params.created = syncParams.created;
|
|
2256
|
-
} else if (cursor) {
|
|
2257
|
-
params.created = { gte: cursor };
|
|
2258
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2259
|
-
}
|
|
2260
|
-
return this.fetchAndUpsert(
|
|
2261
|
-
() => this.stripe.radar.earlyFraudWarnings.list(params),
|
|
2262
|
-
(items) => this.upsertEarlyFraudWarning(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2263
|
-
accountId,
|
|
2264
|
-
"early_fraud_warnings",
|
|
2265
|
-
runStartedAt
|
|
2266
|
-
);
|
|
2267
|
-
}
|
|
2268
|
-
);
|
|
2269
|
-
}
|
|
2270
|
-
async syncRefunds(syncParams) {
|
|
2271
|
-
this.config.logger?.info("Syncing refunds");
|
|
2272
|
-
return this.withSyncRun("refunds", "syncRefunds", async (cursor, runStartedAt) => {
|
|
2273
|
-
const accountId = await this.getAccountId();
|
|
2274
|
-
const params = { limit: 100 };
|
|
2275
|
-
if (syncParams?.created) {
|
|
2276
|
-
params.created = syncParams.created;
|
|
2277
|
-
} else if (cursor) {
|
|
2278
|
-
params.created = { gte: cursor };
|
|
2279
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2280
|
-
}
|
|
2281
|
-
return this.fetchAndUpsert(
|
|
2282
|
-
() => this.stripe.refunds.list(params),
|
|
2283
|
-
(items) => this.upsertRefunds(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2284
|
-
accountId,
|
|
2285
|
-
"refunds",
|
|
2286
|
-
runStartedAt
|
|
2287
|
-
);
|
|
2288
|
-
});
|
|
2289
|
-
}
|
|
2290
|
-
async syncCreditNotes(syncParams) {
|
|
2291
|
-
this.config.logger?.info("Syncing credit notes");
|
|
2292
|
-
return this.withSyncRun("credit_notes", "syncCreditNotes", async (cursor, runStartedAt) => {
|
|
2293
|
-
const accountId = await this.getAccountId();
|
|
2294
|
-
const params = { limit: 100 };
|
|
2295
|
-
if (syncParams?.created) {
|
|
2296
|
-
params.created = syncParams.created;
|
|
2297
|
-
} else if (cursor) {
|
|
2298
|
-
params.created = { gte: cursor };
|
|
2299
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2300
|
-
}
|
|
2301
|
-
return this.fetchAndUpsert(
|
|
2302
|
-
() => this.stripe.creditNotes.list(params),
|
|
2303
|
-
(creditNotes) => this.upsertCreditNotes(creditNotes, accountId),
|
|
2304
|
-
accountId,
|
|
2305
|
-
"credit_notes",
|
|
2306
|
-
runStartedAt
|
|
2307
|
-
);
|
|
2308
|
-
});
|
|
2309
|
-
}
|
|
2310
|
-
async syncFeatures(syncParams) {
|
|
2311
|
-
this.config.logger?.info("Syncing features");
|
|
2312
|
-
return this.withSyncRun("features", "syncFeatures", async (cursor, runStartedAt) => {
|
|
2313
|
-
const accountId = await this.getAccountId();
|
|
2314
|
-
const params = {
|
|
2315
|
-
limit: 100,
|
|
2316
|
-
...syncParams?.pagination
|
|
2317
|
-
};
|
|
2318
|
-
return this.fetchAndUpsert(
|
|
2319
|
-
() => this.stripe.entitlements.features.list(params),
|
|
2320
|
-
(features) => this.upsertFeatures(features, accountId),
|
|
2321
|
-
accountId,
|
|
2322
|
-
"features",
|
|
2323
|
-
runStartedAt
|
|
2324
|
-
);
|
|
2325
|
-
});
|
|
2326
|
-
}
|
|
2327
|
-
async syncEntitlements(customerId, syncParams) {
|
|
2328
|
-
this.config.logger?.info("Syncing entitlements");
|
|
2329
|
-
return this.withSyncRun(
|
|
2330
|
-
"active_entitlements",
|
|
2331
|
-
"syncEntitlements",
|
|
2332
|
-
async (cursor, runStartedAt) => {
|
|
2333
|
-
const accountId = await this.getAccountId();
|
|
2334
|
-
const params = {
|
|
2335
|
-
customer: customerId,
|
|
2336
|
-
limit: 100,
|
|
2337
|
-
...syncParams?.pagination
|
|
2338
|
-
};
|
|
2339
|
-
return this.fetchAndUpsert(
|
|
2340
|
-
() => this.stripe.entitlements.activeEntitlements.list(params),
|
|
2341
|
-
(entitlements) => this.upsertActiveEntitlements(customerId, entitlements, accountId),
|
|
2342
|
-
accountId,
|
|
2343
|
-
"active_entitlements",
|
|
2344
|
-
runStartedAt
|
|
2345
|
-
);
|
|
2346
|
-
}
|
|
2347
|
-
);
|
|
2348
|
-
}
|
|
2349
|
-
async syncCheckoutSessions(syncParams) {
|
|
2350
|
-
this.config.logger?.info("Syncing checkout sessions");
|
|
2351
|
-
return this.withSyncRun(
|
|
2352
|
-
"checkout_sessions",
|
|
2353
|
-
"syncCheckoutSessions",
|
|
2354
|
-
async (cursor, runStartedAt) => {
|
|
2355
|
-
const accountId = await this.getAccountId();
|
|
2356
|
-
const params = { limit: 100 };
|
|
2357
|
-
if (syncParams?.created) {
|
|
2358
|
-
params.created = syncParams.created;
|
|
2359
|
-
} else if (cursor) {
|
|
2360
|
-
params.created = { gte: cursor };
|
|
2361
|
-
this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
|
|
2362
|
-
}
|
|
2363
|
-
return this.fetchAndUpsert(
|
|
2364
|
-
() => this.stripe.checkout.sessions.list(params),
|
|
2365
|
-
(items) => this.upsertCheckoutSessions(items, accountId, syncParams?.backfillRelatedEntities),
|
|
2366
|
-
accountId,
|
|
2367
|
-
"checkout_sessions",
|
|
2368
|
-
runStartedAt
|
|
2369
|
-
);
|
|
2370
|
-
}
|
|
2371
|
-
);
|
|
2372
|
-
}
|
|
2373
|
-
/**
|
|
2374
|
-
* Helper to wrap a sync operation in the observable sync system.
|
|
2375
|
-
* Creates/gets a sync run, sets up the object run, gets cursor, and handles completion.
|
|
2376
|
-
*
|
|
2377
|
-
* @param resourceName - The resource being synced (e.g., 'products', 'customers')
|
|
2378
|
-
* @param triggeredBy - What triggered this sync (for observability)
|
|
2379
|
-
* @param fn - The sync function to execute, receives cursor and runStartedAt
|
|
2380
|
-
* @returns The result of the sync function
|
|
2381
|
-
*/
|
|
2382
|
-
async withSyncRun(resourceName, triggeredBy, fn) {
|
|
2383
|
-
const accountId = await this.getAccountId();
|
|
2384
|
-
const lastCursor = await this.postgresClient.getLastCompletedCursor(accountId, resourceName);
|
|
2385
|
-
const cursor = lastCursor ? parseInt(lastCursor) : null;
|
|
2386
|
-
const runKey = await this.postgresClient.getOrCreateSyncRun(accountId, triggeredBy);
|
|
2387
|
-
if (!runKey) {
|
|
2388
|
-
const activeRun = await this.postgresClient.getActiveSyncRun(accountId);
|
|
2389
|
-
if (!activeRun) {
|
|
2390
|
-
throw new Error("Failed to get or create sync run");
|
|
2391
|
-
}
|
|
2392
|
-
throw new Error("Another sync is already running for this account");
|
|
2393
|
-
}
|
|
2394
|
-
const { runStartedAt } = runKey;
|
|
2395
|
-
await this.postgresClient.createObjectRuns(accountId, runStartedAt, [resourceName]);
|
|
2396
|
-
await this.postgresClient.tryStartObjectSync(accountId, runStartedAt, resourceName);
|
|
2397
|
-
try {
|
|
2398
|
-
const result = await fn(cursor, runStartedAt);
|
|
2399
|
-
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
2400
|
-
return result;
|
|
2401
|
-
} catch (error) {
|
|
2402
|
-
await this.postgresClient.failObjectSync(
|
|
2403
|
-
accountId,
|
|
2404
|
-
runStartedAt,
|
|
2405
|
-
resourceName,
|
|
2406
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
2407
|
-
);
|
|
2408
|
-
throw error;
|
|
2409
|
-
}
|
|
2410
|
-
}
|
|
2411
|
-
async fetchAndUpsert(fetch, upsert, accountId, resourceName, runStartedAt) {
|
|
2412
|
-
const CHECKPOINT_SIZE = 100;
|
|
2413
|
-
let totalSynced = 0;
|
|
2414
|
-
let currentBatch = [];
|
|
2415
|
-
try {
|
|
2416
|
-
this.config.logger?.info("Fetching items to sync from Stripe");
|
|
2417
|
-
try {
|
|
2418
|
-
for await (const item of fetch()) {
|
|
2419
|
-
currentBatch.push(item);
|
|
2420
|
-
if (currentBatch.length >= CHECKPOINT_SIZE) {
|
|
2421
|
-
this.config.logger?.info(`Upserting batch of ${currentBatch.length} items`);
|
|
2422
|
-
await upsert(currentBatch, accountId);
|
|
2423
|
-
totalSynced += currentBatch.length;
|
|
2424
|
-
await this.postgresClient.incrementObjectProgress(
|
|
2425
|
-
accountId,
|
|
2426
|
-
runStartedAt,
|
|
2427
|
-
resourceName,
|
|
2428
|
-
currentBatch.length
|
|
2429
|
-
);
|
|
2430
|
-
const maxCreated = Math.max(
|
|
2431
|
-
...currentBatch.map((i) => i.created || 0)
|
|
2432
|
-
);
|
|
2433
|
-
if (maxCreated > 0) {
|
|
2434
|
-
await this.postgresClient.updateObjectCursor(
|
|
2435
|
-
accountId,
|
|
2436
|
-
runStartedAt,
|
|
2437
|
-
resourceName,
|
|
2438
|
-
String(maxCreated)
|
|
2439
|
-
);
|
|
2440
|
-
this.config.logger?.info(`Checkpoint: cursor updated to ${maxCreated}`);
|
|
2441
|
-
}
|
|
2442
|
-
currentBatch = [];
|
|
2443
|
-
}
|
|
2444
|
-
}
|
|
2445
|
-
if (currentBatch.length > 0) {
|
|
2446
|
-
this.config.logger?.info(`Upserting final batch of ${currentBatch.length} items`);
|
|
2447
|
-
await upsert(currentBatch, accountId);
|
|
2448
|
-
totalSynced += currentBatch.length;
|
|
2449
|
-
await this.postgresClient.incrementObjectProgress(
|
|
2450
|
-
accountId,
|
|
2451
|
-
runStartedAt,
|
|
2452
|
-
resourceName,
|
|
2453
|
-
currentBatch.length
|
|
2454
|
-
);
|
|
2455
|
-
const maxCreated = Math.max(
|
|
2456
|
-
...currentBatch.map((i) => i.created || 0)
|
|
2457
|
-
);
|
|
2458
|
-
if (maxCreated > 0) {
|
|
2459
|
-
await this.postgresClient.updateObjectCursor(
|
|
2460
|
-
accountId,
|
|
2461
|
-
runStartedAt,
|
|
2462
|
-
resourceName,
|
|
2463
|
-
String(maxCreated)
|
|
2464
|
-
);
|
|
2465
|
-
}
|
|
2466
|
-
}
|
|
2467
|
-
} catch (error) {
|
|
2468
|
-
if (currentBatch.length > 0) {
|
|
2469
|
-
this.config.logger?.info(
|
|
2470
|
-
`Error occurred, saving partial progress: ${currentBatch.length} items`
|
|
2471
|
-
);
|
|
2472
|
-
await upsert(currentBatch, accountId);
|
|
2473
|
-
totalSynced += currentBatch.length;
|
|
2474
|
-
await this.postgresClient.incrementObjectProgress(
|
|
2475
|
-
accountId,
|
|
2476
|
-
runStartedAt,
|
|
2477
|
-
resourceName,
|
|
2478
|
-
currentBatch.length
|
|
2479
|
-
);
|
|
2480
|
-
const maxCreated = Math.max(
|
|
2481
|
-
...currentBatch.map((i) => i.created || 0)
|
|
2482
|
-
);
|
|
2483
|
-
if (maxCreated > 0) {
|
|
2484
|
-
await this.postgresClient.updateObjectCursor(
|
|
2485
|
-
accountId,
|
|
2486
|
-
runStartedAt,
|
|
2487
|
-
resourceName,
|
|
2488
|
-
String(maxCreated)
|
|
2489
|
-
);
|
|
2490
|
-
}
|
|
2491
|
-
}
|
|
2492
|
-
throw error;
|
|
2493
|
-
}
|
|
2494
|
-
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
2495
|
-
this.config.logger?.info(`Sync complete: ${totalSynced} items synced`);
|
|
2496
|
-
return { synced: totalSynced };
|
|
2497
|
-
} catch (error) {
|
|
2498
|
-
await this.postgresClient.failObjectSync(
|
|
2499
|
-
accountId,
|
|
2500
|
-
runStartedAt,
|
|
2501
|
-
resourceName,
|
|
2502
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
2503
|
-
);
|
|
2504
|
-
throw error;
|
|
2505
|
-
}
|
|
2506
|
-
}
|
|
2507
|
-
async upsertCharges(charges, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2508
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2509
|
-
await Promise.all([
|
|
2510
|
-
this.backfillCustomers(getUniqueIds(charges, "customer"), accountId),
|
|
2511
|
-
this.backfillInvoices(getUniqueIds(charges, "invoice"), accountId)
|
|
2512
|
-
]);
|
|
2513
|
-
}
|
|
2514
|
-
await this.expandEntity(
|
|
2515
|
-
charges,
|
|
2516
|
-
"refunds",
|
|
2517
|
-
(id) => this.stripe.refunds.list({ charge: id, limit: 100 })
|
|
2518
|
-
);
|
|
2519
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2520
|
-
charges,
|
|
2521
|
-
"charges",
|
|
2522
|
-
accountId,
|
|
2523
|
-
syncTimestamp
|
|
2524
|
-
);
|
|
2525
|
-
}
|
|
2526
|
-
async backfillCharges(chargeIds, accountId) {
|
|
2527
|
-
const missingChargeIds = await this.postgresClient.findMissingEntries("charges", chargeIds);
|
|
2528
|
-
await this.fetchMissingEntities(
|
|
2529
|
-
missingChargeIds,
|
|
2530
|
-
(id) => this.stripe.charges.retrieve(id)
|
|
2531
|
-
).then((charges) => this.upsertCharges(charges, accountId));
|
|
2532
|
-
}
|
|
2533
|
-
async backfillPaymentIntents(paymentIntentIds, accountId) {
|
|
2534
|
-
const missingIds = await this.postgresClient.findMissingEntries(
|
|
2535
|
-
"payment_intents",
|
|
2536
|
-
paymentIntentIds
|
|
2537
|
-
);
|
|
2538
|
-
await this.fetchMissingEntities(
|
|
2539
|
-
missingIds,
|
|
2540
|
-
(id) => this.stripe.paymentIntents.retrieve(id)
|
|
2541
|
-
).then((paymentIntents) => this.upsertPaymentIntents(paymentIntents, accountId));
|
|
2542
|
-
}
|
|
2543
|
-
async upsertCreditNotes(creditNotes, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2544
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2545
|
-
await Promise.all([
|
|
2546
|
-
this.backfillCustomers(getUniqueIds(creditNotes, "customer"), accountId),
|
|
2547
|
-
this.backfillInvoices(getUniqueIds(creditNotes, "invoice"), accountId)
|
|
2548
|
-
]);
|
|
2549
|
-
}
|
|
2550
|
-
await this.expandEntity(
|
|
2551
|
-
creditNotes,
|
|
2552
|
-
"lines",
|
|
2553
|
-
(id) => this.stripe.creditNotes.listLineItems(id, { limit: 100 })
|
|
2554
|
-
);
|
|
2555
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2556
|
-
creditNotes,
|
|
2557
|
-
"credit_notes",
|
|
2558
|
-
accountId,
|
|
2559
|
-
syncTimestamp
|
|
2560
|
-
);
|
|
2561
|
-
}
|
|
2562
|
-
async upsertCheckoutSessions(checkoutSessions, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2563
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2564
|
-
await Promise.all([
|
|
2565
|
-
this.backfillCustomers(getUniqueIds(checkoutSessions, "customer"), accountId),
|
|
2566
|
-
this.backfillSubscriptions(getUniqueIds(checkoutSessions, "subscription"), accountId),
|
|
2567
|
-
this.backfillPaymentIntents(getUniqueIds(checkoutSessions, "payment_intent"), accountId),
|
|
2568
|
-
this.backfillInvoices(getUniqueIds(checkoutSessions, "invoice"), accountId)
|
|
2569
|
-
]);
|
|
2570
|
-
}
|
|
2571
|
-
const rows = await this.postgresClient.upsertManyWithTimestampProtection(
|
|
2572
|
-
checkoutSessions,
|
|
2573
|
-
"checkout_sessions",
|
|
2574
|
-
accountId,
|
|
2575
|
-
syncTimestamp
|
|
2576
|
-
);
|
|
2577
|
-
await this.fillCheckoutSessionsLineItems(
|
|
2578
|
-
checkoutSessions.map((cs) => cs.id),
|
|
2579
|
-
accountId,
|
|
2580
|
-
syncTimestamp
|
|
2581
|
-
);
|
|
2582
|
-
return rows;
|
|
2583
|
-
}
|
|
2584
|
-
async upsertEarlyFraudWarning(earlyFraudWarnings, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2585
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2586
|
-
await Promise.all([
|
|
2587
|
-
this.backfillPaymentIntents(getUniqueIds(earlyFraudWarnings, "payment_intent"), accountId),
|
|
2588
|
-
this.backfillCharges(getUniqueIds(earlyFraudWarnings, "charge"), accountId)
|
|
2589
|
-
]);
|
|
2590
|
-
}
|
|
2591
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2592
|
-
earlyFraudWarnings,
|
|
2593
|
-
"early_fraud_warnings",
|
|
2594
|
-
accountId,
|
|
2595
|
-
syncTimestamp
|
|
2596
|
-
);
|
|
2597
|
-
}
|
|
2598
|
-
async upsertRefunds(refunds, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2599
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2600
|
-
await Promise.all([
|
|
2601
|
-
this.backfillPaymentIntents(getUniqueIds(refunds, "payment_intent"), accountId),
|
|
2602
|
-
this.backfillCharges(getUniqueIds(refunds, "charge"), accountId)
|
|
2603
|
-
]);
|
|
2604
|
-
}
|
|
2605
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2606
|
-
refunds,
|
|
2607
|
-
"refunds",
|
|
2608
|
-
accountId,
|
|
2609
|
-
syncTimestamp
|
|
2610
|
-
);
|
|
2611
|
-
}
|
|
2612
|
-
async upsertReviews(reviews, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2613
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2614
|
-
await Promise.all([
|
|
2615
|
-
this.backfillPaymentIntents(getUniqueIds(reviews, "payment_intent"), accountId),
|
|
2616
|
-
this.backfillCharges(getUniqueIds(reviews, "charge"), accountId)
|
|
2617
|
-
]);
|
|
2618
|
-
}
|
|
2619
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2620
|
-
reviews,
|
|
2621
|
-
"reviews",
|
|
2622
|
-
accountId,
|
|
2623
|
-
syncTimestamp
|
|
2624
|
-
);
|
|
2625
|
-
}
|
|
2626
|
-
async upsertCustomers(customers, accountId, syncTimestamp) {
|
|
2627
|
-
const deletedCustomers = customers.filter((customer) => customer.deleted);
|
|
2628
|
-
const nonDeletedCustomers = customers.filter((customer) => !customer.deleted);
|
|
2629
|
-
await this.postgresClient.upsertManyWithTimestampProtection(
|
|
2630
|
-
nonDeletedCustomers,
|
|
2631
|
-
"customers",
|
|
2632
|
-
accountId,
|
|
2633
|
-
syncTimestamp
|
|
2634
|
-
);
|
|
2635
|
-
await this.postgresClient.upsertManyWithTimestampProtection(
|
|
2636
|
-
deletedCustomers,
|
|
2637
|
-
"customers",
|
|
2638
|
-
accountId,
|
|
2639
|
-
syncTimestamp
|
|
2640
|
-
);
|
|
2641
|
-
return customers;
|
|
2642
|
-
}
|
|
2643
|
-
async backfillCustomers(customerIds, accountId) {
|
|
2644
|
-
const missingIds = await this.postgresClient.findMissingEntries("customers", customerIds);
|
|
2645
|
-
await this.fetchMissingEntities(missingIds, (id) => this.stripe.customers.retrieve(id)).then((entries) => this.upsertCustomers(entries, accountId)).catch((err) => {
|
|
2646
|
-
this.config.logger?.error(err, "Failed to backfill");
|
|
2647
|
-
throw err;
|
|
2648
|
-
});
|
|
2649
|
-
}
|
|
2650
|
-
async upsertDisputes(disputes, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2651
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2652
|
-
await this.backfillCharges(getUniqueIds(disputes, "charge"), accountId);
|
|
2653
|
-
}
|
|
2654
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2655
|
-
disputes,
|
|
2656
|
-
"disputes",
|
|
2657
|
-
accountId,
|
|
2658
|
-
syncTimestamp
|
|
2659
|
-
);
|
|
2660
|
-
}
|
|
2661
|
-
async upsertInvoices(invoices, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2662
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2663
|
-
await Promise.all([
|
|
2664
|
-
this.backfillCustomers(getUniqueIds(invoices, "customer"), accountId),
|
|
2665
|
-
this.backfillSubscriptions(getUniqueIds(invoices, "subscription"), accountId)
|
|
2666
|
-
]);
|
|
2667
|
-
}
|
|
2668
|
-
await this.expandEntity(
|
|
2669
|
-
invoices,
|
|
2670
|
-
"lines",
|
|
2671
|
-
(id) => this.stripe.invoices.listLineItems(id, { limit: 100 })
|
|
2672
|
-
);
|
|
2673
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2674
|
-
invoices,
|
|
2675
|
-
"invoices",
|
|
2676
|
-
accountId,
|
|
2677
|
-
syncTimestamp
|
|
2678
|
-
);
|
|
2679
|
-
}
|
|
2680
|
-
backfillInvoices = async (invoiceIds, accountId) => {
|
|
2681
|
-
const missingIds = await this.postgresClient.findMissingEntries("invoices", invoiceIds);
|
|
2682
|
-
await this.fetchMissingEntities(missingIds, (id) => this.stripe.invoices.retrieve(id)).then(
|
|
2683
|
-
(entries) => this.upsertInvoices(entries, accountId)
|
|
2684
|
-
);
|
|
2685
|
-
};
|
|
2686
|
-
backfillPrices = async (priceIds, accountId) => {
|
|
2687
|
-
const missingIds = await this.postgresClient.findMissingEntries("prices", priceIds);
|
|
2688
|
-
await this.fetchMissingEntities(missingIds, (id) => this.stripe.prices.retrieve(id)).then(
|
|
2689
|
-
(entries) => this.upsertPrices(entries, accountId)
|
|
2690
|
-
);
|
|
2691
|
-
};
|
|
2692
|
-
async upsertPlans(plans, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2693
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2694
|
-
await this.backfillProducts(getUniqueIds(plans, "product"), accountId);
|
|
2695
|
-
}
|
|
2696
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2697
|
-
plans,
|
|
2698
|
-
"plans",
|
|
2699
|
-
accountId,
|
|
2700
|
-
syncTimestamp
|
|
2701
|
-
);
|
|
2702
|
-
}
|
|
2703
|
-
async deletePlan(id) {
|
|
2704
|
-
return this.postgresClient.delete("plans", id);
|
|
2705
|
-
}
|
|
2706
|
-
async upsertPrices(prices, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2707
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2708
|
-
await this.backfillProducts(getUniqueIds(prices, "product"), accountId);
|
|
2709
|
-
}
|
|
2710
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2711
|
-
prices,
|
|
2712
|
-
"prices",
|
|
2713
|
-
accountId,
|
|
2714
|
-
syncTimestamp
|
|
2715
|
-
);
|
|
2716
|
-
}
|
|
2717
|
-
async deletePrice(id) {
|
|
2718
|
-
return this.postgresClient.delete("prices", id);
|
|
2719
|
-
}
|
|
2720
|
-
async upsertProducts(products, accountId, syncTimestamp) {
|
|
2721
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2722
|
-
products,
|
|
2723
|
-
"products",
|
|
2724
|
-
accountId,
|
|
2725
|
-
syncTimestamp
|
|
2726
|
-
);
|
|
2727
|
-
}
|
|
2728
|
-
async deleteProduct(id) {
|
|
2729
|
-
return this.postgresClient.delete("products", id);
|
|
2730
|
-
}
|
|
2731
|
-
async backfillProducts(productIds, accountId) {
|
|
2732
|
-
const missingProductIds = await this.postgresClient.findMissingEntries("products", productIds);
|
|
2733
|
-
await this.fetchMissingEntities(
|
|
2734
|
-
missingProductIds,
|
|
2735
|
-
(id) => this.stripe.products.retrieve(id)
|
|
2736
|
-
).then((products) => this.upsertProducts(products, accountId));
|
|
2737
|
-
}
|
|
2738
|
-
async upsertPaymentIntents(paymentIntents, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2739
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2740
|
-
await Promise.all([
|
|
2741
|
-
this.backfillCustomers(getUniqueIds(paymentIntents, "customer"), accountId),
|
|
2742
|
-
this.backfillInvoices(getUniqueIds(paymentIntents, "invoice"), accountId)
|
|
2743
|
-
]);
|
|
2744
|
-
}
|
|
2745
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2746
|
-
paymentIntents,
|
|
2747
|
-
"payment_intents",
|
|
2748
|
-
accountId,
|
|
2749
|
-
syncTimestamp
|
|
2750
|
-
);
|
|
2751
|
-
}
|
|
2752
|
-
async upsertPaymentMethods(paymentMethods, accountId, backfillRelatedEntities = false, syncTimestamp) {
|
|
2753
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2754
|
-
await this.backfillCustomers(getUniqueIds(paymentMethods, "customer"), accountId);
|
|
2755
|
-
}
|
|
2756
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2757
|
-
paymentMethods,
|
|
2758
|
-
"payment_methods",
|
|
2759
|
-
accountId,
|
|
2760
|
-
syncTimestamp
|
|
2761
|
-
);
|
|
2762
|
-
}
|
|
2763
|
-
async upsertSetupIntents(setupIntents, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2764
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2765
|
-
await this.backfillCustomers(getUniqueIds(setupIntents, "customer"), accountId);
|
|
2766
|
-
}
|
|
2767
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2768
|
-
setupIntents,
|
|
2769
|
-
"setup_intents",
|
|
2770
|
-
accountId,
|
|
2771
|
-
syncTimestamp
|
|
2772
|
-
);
|
|
2773
|
-
}
|
|
2774
|
-
async upsertTaxIds(taxIds, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2775
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2776
|
-
await this.backfillCustomers(getUniqueIds(taxIds, "customer"), accountId);
|
|
2777
|
-
}
|
|
2778
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2779
|
-
taxIds,
|
|
2780
|
-
"tax_ids",
|
|
2781
|
-
accountId,
|
|
2782
|
-
syncTimestamp
|
|
2783
|
-
);
|
|
2784
|
-
}
|
|
2785
|
-
async deleteTaxId(id) {
|
|
2786
|
-
return this.postgresClient.delete("tax_ids", id);
|
|
2787
|
-
}
|
|
2788
|
-
async upsertSubscriptionItems(subscriptionItems, accountId, syncTimestamp) {
|
|
2789
|
-
const modifiedSubscriptionItems = subscriptionItems.map((subscriptionItem) => {
|
|
2790
|
-
const priceId = subscriptionItem.price.id.toString();
|
|
2791
|
-
const deleted = subscriptionItem.deleted;
|
|
2792
|
-
const quantity = subscriptionItem.quantity;
|
|
2793
|
-
return {
|
|
2794
|
-
...subscriptionItem,
|
|
2795
|
-
price: priceId,
|
|
2796
|
-
deleted: deleted ?? false,
|
|
2797
|
-
quantity: quantity ?? null
|
|
2798
|
-
};
|
|
2799
|
-
});
|
|
2800
|
-
await this.postgresClient.upsertManyWithTimestampProtection(
|
|
2801
|
-
modifiedSubscriptionItems,
|
|
2802
|
-
"subscription_items",
|
|
2803
|
-
accountId,
|
|
2804
|
-
syncTimestamp
|
|
2805
|
-
);
|
|
2806
|
-
}
|
|
2807
|
-
async fillCheckoutSessionsLineItems(checkoutSessionIds, accountId, syncTimestamp) {
|
|
2808
|
-
for (const checkoutSessionId of checkoutSessionIds) {
|
|
2809
|
-
const lineItemResponses = [];
|
|
2810
|
-
for await (const lineItem of this.stripe.checkout.sessions.listLineItems(checkoutSessionId, {
|
|
2811
|
-
limit: 100
|
|
2812
|
-
})) {
|
|
2813
|
-
lineItemResponses.push(lineItem);
|
|
2814
|
-
}
|
|
2815
|
-
await this.upsertCheckoutSessionLineItems(
|
|
2816
|
-
lineItemResponses,
|
|
2817
|
-
checkoutSessionId,
|
|
2818
|
-
accountId,
|
|
2819
|
-
syncTimestamp
|
|
2820
|
-
);
|
|
2821
|
-
}
|
|
2822
|
-
}
|
|
2823
|
-
async upsertCheckoutSessionLineItems(lineItems, checkoutSessionId, accountId, syncTimestamp) {
|
|
2824
|
-
await this.backfillPrices(
|
|
2825
|
-
lineItems.map((lineItem) => lineItem.price?.id?.toString() ?? void 0).filter((id) => id !== void 0),
|
|
2826
|
-
accountId
|
|
2827
|
-
);
|
|
2828
|
-
const modifiedLineItems = lineItems.map((lineItem) => {
|
|
2829
|
-
const priceId = typeof lineItem.price === "object" && lineItem.price?.id ? lineItem.price.id.toString() : lineItem.price?.toString() || null;
|
|
2830
|
-
return {
|
|
2831
|
-
...lineItem,
|
|
2832
|
-
price: priceId,
|
|
2833
|
-
checkout_session: checkoutSessionId
|
|
2834
|
-
};
|
|
2835
|
-
});
|
|
2836
|
-
await this.postgresClient.upsertManyWithTimestampProtection(
|
|
2837
|
-
modifiedLineItems,
|
|
2838
|
-
"checkout_session_line_items",
|
|
2839
|
-
accountId,
|
|
2840
|
-
syncTimestamp
|
|
2841
|
-
);
|
|
2842
|
-
}
|
|
2843
|
-
async markDeletedSubscriptionItems(subscriptionId, currentSubItemIds) {
|
|
2844
|
-
let prepared = sql2(`
|
|
2845
|
-
select id from "stripe"."subscription_items"
|
|
2846
|
-
where subscription = :subscriptionId and COALESCE(deleted, false) = false;
|
|
2847
|
-
`)({ subscriptionId });
|
|
2848
|
-
const { rows } = await this.postgresClient.query(prepared.text, prepared.values);
|
|
2849
|
-
const deletedIds = rows.filter(
|
|
2850
|
-
({ id }) => currentSubItemIds.includes(id) === false
|
|
2851
|
-
);
|
|
2852
|
-
if (deletedIds.length > 0) {
|
|
2853
|
-
const ids = deletedIds.map(({ id }) => id);
|
|
2854
|
-
prepared = sql2(`
|
|
2855
|
-
update "stripe"."subscription_items"
|
|
2856
|
-
set _raw_data = jsonb_set(_raw_data, '{deleted}', 'true'::jsonb)
|
|
2857
|
-
where id=any(:ids::text[]);
|
|
2858
|
-
`)({ ids });
|
|
2859
|
-
const { rowCount } = await this.postgresClient.query(prepared.text, prepared.values);
|
|
2860
|
-
return { rowCount: rowCount || 0 };
|
|
2861
|
-
} else {
|
|
2862
|
-
return { rowCount: 0 };
|
|
2863
|
-
}
|
|
2864
|
-
}
|
|
2865
|
-
async upsertSubscriptionSchedules(subscriptionSchedules, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2866
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2867
|
-
const customerIds = getUniqueIds(subscriptionSchedules, "customer");
|
|
2868
|
-
await this.backfillCustomers(customerIds, accountId);
|
|
2869
|
-
}
|
|
2870
|
-
const rows = await this.postgresClient.upsertManyWithTimestampProtection(
|
|
2871
|
-
subscriptionSchedules,
|
|
2872
|
-
"subscription_schedules",
|
|
2873
|
-
accountId,
|
|
2874
|
-
syncTimestamp
|
|
2875
|
-
);
|
|
2876
|
-
return rows;
|
|
2877
|
-
}
|
|
2878
|
-
async upsertSubscriptions(subscriptions, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2879
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2880
|
-
const customerIds = getUniqueIds(subscriptions, "customer");
|
|
2881
|
-
await this.backfillCustomers(customerIds, accountId);
|
|
2882
|
-
}
|
|
2883
|
-
await this.expandEntity(
|
|
2884
|
-
subscriptions,
|
|
2885
|
-
"items",
|
|
2886
|
-
(id) => this.stripe.subscriptionItems.list({ subscription: id, limit: 100 })
|
|
2887
|
-
);
|
|
2888
|
-
const rows = await this.postgresClient.upsertManyWithTimestampProtection(
|
|
2889
|
-
subscriptions,
|
|
2890
|
-
"subscriptions",
|
|
2891
|
-
accountId,
|
|
2892
|
-
syncTimestamp
|
|
2893
|
-
);
|
|
2894
|
-
const allSubscriptionItems = subscriptions.flatMap((subscription) => subscription.items.data);
|
|
2895
|
-
await this.upsertSubscriptionItems(allSubscriptionItems, accountId, syncTimestamp);
|
|
2896
|
-
const markSubscriptionItemsDeleted = [];
|
|
2897
|
-
for (const subscription of subscriptions) {
|
|
2898
|
-
const subscriptionItems = subscription.items.data;
|
|
2899
|
-
const subItemIds = subscriptionItems.map((x) => x.id);
|
|
2900
|
-
markSubscriptionItemsDeleted.push(
|
|
2901
|
-
this.markDeletedSubscriptionItems(subscription.id, subItemIds)
|
|
2902
|
-
);
|
|
2903
|
-
}
|
|
2904
|
-
await Promise.all(markSubscriptionItemsDeleted);
|
|
2905
|
-
return rows;
|
|
2906
|
-
}
|
|
2907
|
-
async deleteRemovedActiveEntitlements(customerId, currentActiveEntitlementIds) {
|
|
2908
|
-
const prepared = sql2(`
|
|
2909
|
-
delete from "stripe"."active_entitlements"
|
|
2910
|
-
where customer = :customerId and id <> ALL(:currentActiveEntitlementIds::text[]);
|
|
2911
|
-
`)({ customerId, currentActiveEntitlementIds });
|
|
2912
|
-
const { rowCount } = await this.postgresClient.query(prepared.text, prepared.values);
|
|
2913
|
-
return { rowCount: rowCount || 0 };
|
|
2914
|
-
}
|
|
2915
|
-
async upsertFeatures(features, accountId, syncTimestamp) {
|
|
2916
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2917
|
-
features,
|
|
2918
|
-
"features",
|
|
2919
|
-
accountId,
|
|
2920
|
-
syncTimestamp
|
|
2921
|
-
);
|
|
2922
|
-
}
|
|
2923
|
-
async backfillFeatures(featureIds, accountId) {
|
|
2924
|
-
const missingFeatureIds = await this.postgresClient.findMissingEntries("features", featureIds);
|
|
2925
|
-
await this.fetchMissingEntities(
|
|
2926
|
-
missingFeatureIds,
|
|
2927
|
-
(id) => this.stripe.entitlements.features.retrieve(id)
|
|
2928
|
-
).then((features) => this.upsertFeatures(features, accountId)).catch((err) => {
|
|
2929
|
-
this.config.logger?.error(err, "Failed to backfill features");
|
|
2930
|
-
throw err;
|
|
2931
|
-
});
|
|
2932
|
-
}
|
|
2933
|
-
async upsertActiveEntitlements(customerId, activeEntitlements, accountId, backfillRelatedEntities, syncTimestamp) {
|
|
2934
|
-
if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
|
|
2935
|
-
await Promise.all([
|
|
2936
|
-
this.backfillCustomers(getUniqueIds(activeEntitlements, "customer"), accountId),
|
|
2937
|
-
this.backfillFeatures(getUniqueIds(activeEntitlements, "feature"), accountId)
|
|
2938
|
-
]);
|
|
2939
|
-
}
|
|
2940
|
-
const entitlements = activeEntitlements.map((entitlement) => ({
|
|
2941
|
-
id: entitlement.id,
|
|
2942
|
-
object: entitlement.object,
|
|
2943
|
-
feature: typeof entitlement.feature === "string" ? entitlement.feature : entitlement.feature.id,
|
|
2944
|
-
customer: customerId,
|
|
2945
|
-
livemode: entitlement.livemode,
|
|
2946
|
-
lookup_key: entitlement.lookup_key
|
|
2947
|
-
}));
|
|
2948
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
2949
|
-
entitlements,
|
|
2950
|
-
"active_entitlements",
|
|
2951
|
-
accountId,
|
|
2952
|
-
syncTimestamp
|
|
2953
|
-
);
|
|
2954
|
-
}
|
|
2955
|
-
async findOrCreateManagedWebhook(url, params) {
|
|
2956
|
-
const webhookParams = {
|
|
2957
|
-
enabled_events: this.getSupportedEventTypes(),
|
|
2958
|
-
...params
|
|
2959
|
-
};
|
|
2960
|
-
const accountId = await this.getAccountId();
|
|
2961
|
-
const lockKey = `webhook:${accountId}:${url}`;
|
|
2962
|
-
return this.postgresClient.withAdvisoryLock(lockKey, async () => {
|
|
2963
|
-
const existingWebhook = await this.getManagedWebhookByUrl(url);
|
|
2964
|
-
if (existingWebhook) {
|
|
2965
|
-
try {
|
|
2966
|
-
const stripeWebhook = await this.stripe.webhookEndpoints.retrieve(existingWebhook.id);
|
|
2967
|
-
if (stripeWebhook.status === "enabled") {
|
|
2968
|
-
return stripeWebhook;
|
|
2969
|
-
}
|
|
2970
|
-
this.config.logger?.info(
|
|
2971
|
-
{ webhookId: existingWebhook.id },
|
|
2972
|
-
"Webhook is disabled, deleting and will recreate"
|
|
2973
|
-
);
|
|
2974
|
-
await this.stripe.webhookEndpoints.del(existingWebhook.id);
|
|
2975
|
-
await this.postgresClient.delete("_managed_webhooks", existingWebhook.id);
|
|
2976
|
-
} catch (error) {
|
|
2977
|
-
const stripeError = error;
|
|
2978
|
-
if (stripeError?.statusCode === 404 || stripeError?.code === "resource_missing") {
|
|
2979
|
-
this.config.logger?.warn(
|
|
2980
|
-
{ error, webhookId: existingWebhook.id },
|
|
2981
|
-
"Webhook not found in Stripe (404), removing from database"
|
|
2982
|
-
);
|
|
2983
|
-
await this.postgresClient.delete("_managed_webhooks", existingWebhook.id);
|
|
2984
|
-
} else {
|
|
2985
|
-
this.config.logger?.error(
|
|
2986
|
-
{ error, webhookId: existingWebhook.id },
|
|
2987
|
-
"Error retrieving webhook from Stripe, keeping in database"
|
|
2988
|
-
);
|
|
2989
|
-
throw error;
|
|
2990
|
-
}
|
|
2991
|
-
}
|
|
2992
|
-
}
|
|
2993
|
-
const allDbWebhooks = await this.listManagedWebhooks();
|
|
2994
|
-
for (const dbWebhook of allDbWebhooks) {
|
|
2995
|
-
if (dbWebhook.url !== url) {
|
|
2996
|
-
this.config.logger?.info(
|
|
2997
|
-
{ webhookId: dbWebhook.id, oldUrl: dbWebhook.url, newUrl: url },
|
|
2998
|
-
"Webhook URL mismatch, deleting"
|
|
2999
|
-
);
|
|
3000
|
-
try {
|
|
3001
|
-
await this.stripe.webhookEndpoints.del(dbWebhook.id);
|
|
3002
|
-
} catch (error) {
|
|
3003
|
-
this.config.logger?.warn(
|
|
3004
|
-
{ error, webhookId: dbWebhook.id },
|
|
3005
|
-
"Failed to delete old webhook from Stripe"
|
|
3006
|
-
);
|
|
3007
|
-
}
|
|
3008
|
-
await this.postgresClient.delete("_managed_webhooks", dbWebhook.id);
|
|
3009
|
-
}
|
|
3010
|
-
}
|
|
3011
|
-
try {
|
|
3012
|
-
const stripeWebhooks = await this.stripe.webhookEndpoints.list({ limit: 100 });
|
|
3013
|
-
for (const stripeWebhook of stripeWebhooks.data) {
|
|
3014
|
-
const isManagedByMetadata = stripeWebhook.metadata?.managed_by?.toLowerCase().replace(/[\s\-]+/g, "") === "stripesync";
|
|
3015
|
-
const normalizedDescription = stripeWebhook.description?.toLowerCase().replace(/[\s\-]+/g, "") || "";
|
|
3016
|
-
const isManagedByDescription = normalizedDescription.includes("stripesync");
|
|
3017
|
-
if (isManagedByMetadata || isManagedByDescription) {
|
|
3018
|
-
const existsInDb = allDbWebhooks.some((dbWebhook) => dbWebhook.id === stripeWebhook.id);
|
|
3019
|
-
if (!existsInDb) {
|
|
3020
|
-
this.config.logger?.warn(
|
|
3021
|
-
{ webhookId: stripeWebhook.id, url: stripeWebhook.url },
|
|
3022
|
-
"Found orphaned managed webhook in Stripe, deleting"
|
|
3023
|
-
);
|
|
3024
|
-
await this.stripe.webhookEndpoints.del(stripeWebhook.id);
|
|
3025
|
-
}
|
|
3026
|
-
}
|
|
3027
|
-
}
|
|
3028
|
-
} catch (error) {
|
|
3029
|
-
this.config.logger?.warn({ error }, "Failed to check for orphaned webhooks");
|
|
3030
|
-
}
|
|
3031
|
-
const webhook = await this.stripe.webhookEndpoints.create({
|
|
3032
|
-
...webhookParams,
|
|
3033
|
-
url,
|
|
3034
|
-
// Always set metadata to identify managed webhooks
|
|
3035
|
-
metadata: {
|
|
3036
|
-
...webhookParams.metadata,
|
|
3037
|
-
managed_by: "stripe-sync",
|
|
3038
|
-
version: package_default.version
|
|
3039
|
-
}
|
|
3040
|
-
});
|
|
3041
|
-
const accountId2 = await this.getAccountId();
|
|
3042
|
-
await this.upsertManagedWebhooks([webhook], accountId2);
|
|
3043
|
-
return webhook;
|
|
3044
|
-
});
|
|
3045
|
-
}
|
|
3046
|
-
async getManagedWebhook(id) {
|
|
3047
|
-
const accountId = await this.getAccountId();
|
|
3048
|
-
const result = await this.postgresClient.query(
|
|
3049
|
-
`SELECT * FROM "stripe"."_managed_webhooks" WHERE id = $1 AND "account_id" = $2`,
|
|
3050
|
-
[id, accountId]
|
|
3051
|
-
);
|
|
3052
|
-
return result.rows.length > 0 ? result.rows[0] : null;
|
|
3053
|
-
}
|
|
3054
|
-
/**
|
|
3055
|
-
* Get a managed webhook by URL and account ID.
|
|
3056
|
-
* Used for race condition recovery: when createManagedWebhook hits a unique constraint
|
|
3057
|
-
* violation (another instance created the webhook), we need to fetch the existing webhook
|
|
3058
|
-
* by URL since we only know the URL, not the ID of the webhook that won the race.
|
|
3059
|
-
*/
|
|
3060
|
-
async getManagedWebhookByUrl(url) {
|
|
3061
|
-
const accountId = await this.getAccountId();
|
|
3062
|
-
const result = await this.postgresClient.query(
|
|
3063
|
-
`SELECT * FROM "stripe"."_managed_webhooks" WHERE url = $1 AND "account_id" = $2`,
|
|
3064
|
-
[url, accountId]
|
|
3065
|
-
);
|
|
3066
|
-
return result.rows.length > 0 ? result.rows[0] : null;
|
|
3067
|
-
}
|
|
3068
|
-
async listManagedWebhooks() {
|
|
3069
|
-
const accountId = await this.getAccountId();
|
|
3070
|
-
const result = await this.postgresClient.query(
|
|
3071
|
-
`SELECT * FROM "stripe"."_managed_webhooks" WHERE "account_id" = $1 ORDER BY created DESC`,
|
|
3072
|
-
[accountId]
|
|
3073
|
-
);
|
|
3074
|
-
return result.rows;
|
|
3075
|
-
}
|
|
3076
|
-
async updateManagedWebhook(id, params) {
|
|
3077
|
-
const webhook = await this.stripe.webhookEndpoints.update(id, params);
|
|
3078
|
-
const accountId = await this.getAccountId();
|
|
3079
|
-
await this.upsertManagedWebhooks([webhook], accountId);
|
|
3080
|
-
return webhook;
|
|
3081
|
-
}
|
|
3082
|
-
async deleteManagedWebhook(id) {
|
|
3083
|
-
await this.stripe.webhookEndpoints.del(id);
|
|
3084
|
-
return this.postgresClient.delete("_managed_webhooks", id);
|
|
3085
|
-
}
|
|
3086
|
-
async upsertManagedWebhooks(webhooks, accountId, syncTimestamp) {
|
|
3087
|
-
const filteredWebhooks = webhooks.map((webhook) => {
|
|
3088
|
-
const filtered = {};
|
|
3089
|
-
for (const prop of managedWebhookSchema.properties) {
|
|
3090
|
-
if (prop in webhook) {
|
|
3091
|
-
filtered[prop] = webhook[prop];
|
|
3092
|
-
}
|
|
3093
|
-
}
|
|
3094
|
-
return filtered;
|
|
3095
|
-
});
|
|
3096
|
-
return this.postgresClient.upsertManyWithTimestampProtection(
|
|
3097
|
-
filteredWebhooks,
|
|
3098
|
-
"_managed_webhooks",
|
|
3099
|
-
accountId,
|
|
3100
|
-
syncTimestamp
|
|
3101
|
-
);
|
|
3102
|
-
}
|
|
3103
|
-
async backfillSubscriptions(subscriptionIds, accountId) {
|
|
3104
|
-
const missingSubscriptionIds = await this.postgresClient.findMissingEntries(
|
|
3105
|
-
"subscriptions",
|
|
3106
|
-
subscriptionIds
|
|
3107
|
-
);
|
|
3108
|
-
await this.fetchMissingEntities(
|
|
3109
|
-
missingSubscriptionIds,
|
|
3110
|
-
(id) => this.stripe.subscriptions.retrieve(id)
|
|
3111
|
-
).then((subscriptions) => this.upsertSubscriptions(subscriptions, accountId));
|
|
3112
|
-
}
|
|
3113
|
-
backfillSubscriptionSchedules = async (subscriptionIds, accountId) => {
|
|
3114
|
-
const missingSubscriptionIds = await this.postgresClient.findMissingEntries(
|
|
3115
|
-
"subscription_schedules",
|
|
3116
|
-
subscriptionIds
|
|
3117
|
-
);
|
|
3118
|
-
await this.fetchMissingEntities(
|
|
3119
|
-
missingSubscriptionIds,
|
|
3120
|
-
(id) => this.stripe.subscriptionSchedules.retrieve(id)
|
|
3121
|
-
).then(
|
|
3122
|
-
(subscriptionSchedules) => this.upsertSubscriptionSchedules(subscriptionSchedules, accountId)
|
|
3123
|
-
);
|
|
3124
|
-
};
|
|
3125
|
-
/**
|
|
3126
|
-
* Stripe only sends the first 10 entries by default, the option will actively fetch all entries.
|
|
3127
|
-
*/
|
|
3128
|
-
async expandEntity(entities, property, listFn) {
|
|
3129
|
-
if (!this.config.autoExpandLists) return;
|
|
3130
|
-
for (const entity of entities) {
|
|
3131
|
-
if (entity[property]?.has_more) {
|
|
3132
|
-
const allData = [];
|
|
3133
|
-
for await (const fetchedEntity of listFn(entity.id)) {
|
|
3134
|
-
allData.push(fetchedEntity);
|
|
3135
|
-
}
|
|
3136
|
-
entity[property] = {
|
|
3137
|
-
...entity[property],
|
|
3138
|
-
data: allData,
|
|
3139
|
-
has_more: false
|
|
3140
|
-
};
|
|
3141
|
-
}
|
|
3142
|
-
}
|
|
3143
|
-
}
|
|
3144
|
-
async fetchMissingEntities(ids, fetch) {
|
|
3145
|
-
if (!ids.length) return [];
|
|
3146
|
-
const entities = [];
|
|
3147
|
-
for (const id of ids) {
|
|
3148
|
-
const entity = await fetch(id);
|
|
3149
|
-
entities.push(entity);
|
|
3150
|
-
}
|
|
3151
|
-
return entities;
|
|
3152
|
-
}
|
|
3153
|
-
/**
|
|
3154
|
-
* Closes the database connection pool and cleans up resources.
|
|
3155
|
-
* Call this when you're done using the StripeSync instance.
|
|
3156
|
-
*/
|
|
3157
|
-
async close() {
|
|
3158
|
-
await this.postgresClient.pool.end();
|
|
3159
|
-
}
|
|
3160
|
-
};
|
|
3161
|
-
function chunkArray(array, chunkSize) {
|
|
3162
|
-
const result = [];
|
|
3163
|
-
for (let i = 0; i < array.length; i += chunkSize) {
|
|
3164
|
-
result.push(array.slice(i, i + chunkSize));
|
|
3165
|
-
}
|
|
3166
|
-
return result;
|
|
3167
|
-
}
|
|
3168
|
-
|
|
3169
|
-
// src/database/migrate.ts
|
|
3170
|
-
import { Client } from "pg";
|
|
3171
|
-
import { migrate } from "pg-node-migrations";
|
|
3172
|
-
import fs from "fs";
|
|
3173
|
-
import path from "path";
|
|
3174
|
-
import { fileURLToPath } from "url";
|
|
3175
|
-
var __filename2 = fileURLToPath(import.meta.url);
|
|
3176
|
-
var __dirname2 = path.dirname(__filename2);
|
|
3177
|
-
async function doesTableExist(client, schema, tableName) {
|
|
3178
|
-
const result = await client.query(
|
|
3179
|
-
`SELECT EXISTS (
|
|
3180
|
-
SELECT 1
|
|
3181
|
-
FROM information_schema.tables
|
|
3182
|
-
WHERE table_schema = $1
|
|
3183
|
-
AND table_name = $2
|
|
3184
|
-
)`,
|
|
3185
|
-
[schema, tableName]
|
|
3186
|
-
);
|
|
3187
|
-
return result.rows[0]?.exists || false;
|
|
3188
|
-
}
|
|
3189
|
-
async function renameMigrationsTableIfNeeded(client, schema = "stripe", logger) {
|
|
3190
|
-
const oldTableExists = await doesTableExist(client, schema, "migrations");
|
|
3191
|
-
const newTableExists = await doesTableExist(client, schema, "_migrations");
|
|
3192
|
-
if (oldTableExists && !newTableExists) {
|
|
3193
|
-
logger?.info("Renaming migrations table to _migrations");
|
|
3194
|
-
await client.query(`ALTER TABLE "${schema}"."migrations" RENAME TO "_migrations"`);
|
|
3195
|
-
logger?.info("Successfully renamed migrations table");
|
|
3196
|
-
}
|
|
3197
|
-
}
|
|
3198
|
-
async function cleanupSchema(client, schema, logger) {
|
|
3199
|
-
logger?.warn(`Migrations table is empty - dropping and recreating schema "${schema}"`);
|
|
3200
|
-
await client.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
|
|
3201
|
-
await client.query(`CREATE SCHEMA "${schema}"`);
|
|
3202
|
-
logger?.info(`Schema "${schema}" has been reset`);
|
|
3203
|
-
}
|
|
3204
|
-
async function connectAndMigrate(client, migrationsDirectory, config, logOnError = false) {
|
|
3205
|
-
if (!fs.existsSync(migrationsDirectory)) {
|
|
3206
|
-
config.logger?.info(`Migrations directory ${migrationsDirectory} not found, skipping`);
|
|
3207
|
-
return;
|
|
3208
|
-
}
|
|
3209
|
-
const optionalConfig = {
|
|
3210
|
-
schemaName: "stripe",
|
|
3211
|
-
tableName: "_migrations"
|
|
3212
|
-
};
|
|
3213
|
-
try {
|
|
3214
|
-
await migrate({ client }, migrationsDirectory, optionalConfig);
|
|
3215
|
-
} catch (error) {
|
|
3216
|
-
if (logOnError && error instanceof Error) {
|
|
3217
|
-
config.logger?.error(error, "Migration error:");
|
|
3218
|
-
} else {
|
|
3219
|
-
throw error;
|
|
3220
|
-
}
|
|
3221
|
-
}
|
|
3222
|
-
}
|
|
3223
|
-
async function runMigrations(config) {
|
|
3224
|
-
const client = new Client({
|
|
3225
|
-
connectionString: config.databaseUrl,
|
|
3226
|
-
ssl: config.ssl,
|
|
3227
|
-
connectionTimeoutMillis: 1e4
|
|
3228
|
-
});
|
|
3229
|
-
const schema = "stripe";
|
|
3230
|
-
try {
|
|
3231
|
-
await client.connect();
|
|
3232
|
-
await client.query(`CREATE SCHEMA IF NOT EXISTS ${schema};`);
|
|
3233
|
-
await renameMigrationsTableIfNeeded(client, schema, config.logger);
|
|
3234
|
-
const tableExists = await doesTableExist(client, schema, "_migrations");
|
|
3235
|
-
if (tableExists) {
|
|
3236
|
-
const migrationCount = await client.query(
|
|
3237
|
-
`SELECT COUNT(*) as count FROM "${schema}"."_migrations"`
|
|
3238
|
-
);
|
|
3239
|
-
const isEmpty = migrationCount.rows[0]?.count === "0";
|
|
3240
|
-
if (isEmpty) {
|
|
3241
|
-
await cleanupSchema(client, schema, config.logger);
|
|
3242
|
-
}
|
|
3243
|
-
}
|
|
3244
|
-
config.logger?.info("Running migrations");
|
|
3245
|
-
await connectAndMigrate(client, path.resolve(__dirname2, "./migrations"), config);
|
|
3246
|
-
} catch (err) {
|
|
3247
|
-
config.logger?.error(err, "Error running migrations");
|
|
3248
|
-
throw err;
|
|
3249
|
-
} finally {
|
|
3250
|
-
await client.end();
|
|
3251
|
-
config.logger?.info("Finished migrations");
|
|
3252
|
-
}
|
|
3253
|
-
}
|
|
3254
|
-
|
|
3255
|
-
// src/index.ts
|
|
3256
|
-
var VERSION = package_default.version;
|
|
2
|
+
PostgresClient,
|
|
3
|
+
StripeSync,
|
|
4
|
+
VERSION,
|
|
5
|
+
hashApiKey,
|
|
6
|
+
runMigrations
|
|
7
|
+
} from "./chunk-7JWRDXNB.js";
|
|
8
|
+
import "./chunk-YXQZXR7S.js";
|
|
3257
9
|
export {
|
|
3258
10
|
PostgresClient,
|
|
3259
11
|
StripeSync,
|