stripe-experiment-sync 1.0.19 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4130 +0,0 @@
1
- import {
2
- package_default
3
- } from "./chunk-CMGFQCD7.js";
4
-
5
- // src/stripeSync.ts
6
- import Stripe3 from "stripe";
7
- import { pg as sql2 } from "yesql";
8
-
9
- // src/database/postgres.ts
10
- import pg from "pg";
11
- import { pg as sql } from "yesql";
12
-
13
- // src/database/QueryUtils.ts
14
- var QueryUtils = class _QueryUtils {
15
- constructor() {
16
- }
17
- static quoteIdent(name) {
18
- return `"${name}"`;
19
- }
20
- static quotedList(names) {
21
- return names.map(_QueryUtils.quoteIdent).join(", ");
22
- }
23
- static buildInsertParts(columns) {
24
- const columnsSql = columns.map((c) => _QueryUtils.quoteIdent(c.column)).join(", ");
25
- const valuesSql = columns.map((c, i) => {
26
- const placeholder = `$${i + 1}`;
27
- return `${placeholder}::${c.pgType}`;
28
- }).join(", ");
29
- const params = columns.map((c) => c.value);
30
- return { columnsSql, valuesSql, params };
31
- }
32
- static buildRawJsonUpsertQuery(schema, table, columns, conflictTarget) {
33
- const { columnsSql, valuesSql, params } = _QueryUtils.buildInsertParts(columns);
34
- const conflictSql = _QueryUtils.quotedList(conflictTarget);
35
- const tsParamIdx = columns.findIndex((c) => c.column === "_last_synced_at") + 1;
36
- if (tsParamIdx <= 0) {
37
- throw new Error("buildRawJsonUpsertQuery requires _last_synced_at column");
38
- }
39
- const sql3 = `
40
- INSERT INTO ${_QueryUtils.quoteIdent(schema)}.${_QueryUtils.quoteIdent(table)} (${columnsSql})
41
- VALUES (${valuesSql})
42
- ON CONFLICT (${conflictSql})
43
- DO UPDATE SET
44
- "_raw_data" = EXCLUDED."_raw_data",
45
- "_last_synced_at" = $${tsParamIdx},
46
- "_account_id" = EXCLUDED."_account_id"
47
- WHERE ${_QueryUtils.quoteIdent(table)}."_last_synced_at" IS NULL
48
- OR ${_QueryUtils.quoteIdent(table)}."_last_synced_at" < $${tsParamIdx}
49
- RETURNING *
50
- `;
51
- return { sql: sql3, params };
52
- }
53
- };
54
-
55
- // src/database/postgres.ts
56
- var ORDERED_STRIPE_TABLES = [
57
- "exchange_rates_from_usd",
58
- "subscription_items",
59
- "subscription_item_change_events_v2_beta",
60
- "subscriptions",
61
- "subscription_schedules",
62
- "checkout_session_line_items",
63
- "checkout_sessions",
64
- "tax_ids",
65
- "charges",
66
- "refunds",
67
- "credit_notes",
68
- "disputes",
69
- "early_fraud_warnings",
70
- "invoices",
71
- "payment_intents",
72
- "payment_methods",
73
- "setup_intents",
74
- "prices",
75
- "plans",
76
- "products",
77
- "features",
78
- "active_entitlements",
79
- "reviews",
80
- "_managed_webhooks",
81
- "customers",
82
- "_sync_obj_runs",
83
- // Must be deleted before _sync_runs (foreign key)
84
- "_sync_runs"
85
- ];
86
- var TABLES_WITH_ACCOUNT_ID = /* @__PURE__ */ new Set(["_managed_webhooks"]);
87
- var PostgresClient = class {
88
- constructor(config) {
89
- this.config = config;
90
- this.pool = new pg.Pool(config.poolConfig);
91
- }
92
- pool;
93
- async delete(table, id) {
94
- const prepared = sql(`
95
- delete from "${this.config.schema}"."${table}"
96
- where id = :id
97
- returning id;
98
- `)({ id });
99
- const { rows } = await this.query(prepared.text, prepared.values);
100
- return rows.length > 0;
101
- }
102
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
- async query(text, params) {
104
- return this.pool.query(text, params);
105
- }
106
- async upsertMany(entries, table) {
107
- if (!entries.length) return [];
108
- const chunkSize = 5;
109
- const results = [];
110
- for (let i = 0; i < entries.length; i += chunkSize) {
111
- const chunk = entries.slice(i, i + chunkSize);
112
- const queries = [];
113
- chunk.forEach((entry) => {
114
- const rawData = JSON.stringify(entry);
115
- const upsertSql = `
116
- INSERT INTO "${this.config.schema}"."${table}" ("_raw_data")
117
- VALUES ($1::jsonb)
118
- ON CONFLICT (id)
119
- DO UPDATE SET
120
- "_raw_data" = EXCLUDED."_raw_data"
121
- RETURNING *
122
- `;
123
- queries.push(this.pool.query(upsertSql, [rawData]));
124
- });
125
- results.push(...await Promise.all(queries));
126
- }
127
- return results.flatMap((it) => it.rows);
128
- }
129
- async upsertManyWithTimestampProtection(entries, table, accountId, syncTimestamp, upsertOptions) {
130
- const timestamp = syncTimestamp || (/* @__PURE__ */ new Date()).toISOString();
131
- if (!entries.length) return [];
132
- const chunkSize = 5;
133
- const results = [];
134
- for (let i = 0; i < entries.length; i += chunkSize) {
135
- const chunk = entries.slice(i, i + chunkSize);
136
- const queries = [];
137
- chunk.forEach((entry) => {
138
- if (table.startsWith("_")) {
139
- const columns = Object.keys(entry).filter(
140
- (k) => k !== "last_synced_at" && k !== "account_id"
141
- );
142
- const upsertSql = `
143
- INSERT INTO "${this.config.schema}"."${table}" (
144
- ${columns.map((c) => `"${c}"`).join(", ")}, "last_synced_at", "account_id"
145
- )
146
- VALUES (
147
- ${columns.map((c) => `:${c}`).join(", ")}, :last_synced_at, :account_id
148
- )
149
- ON CONFLICT ("id")
150
- DO UPDATE SET
151
- ${columns.map((c) => `"${c}" = EXCLUDED."${c}"`).join(", ")},
152
- "last_synced_at" = :last_synced_at,
153
- "account_id" = EXCLUDED."account_id"
154
- WHERE "${table}"."last_synced_at" IS NULL
155
- OR "${table}"."last_synced_at" < :last_synced_at
156
- RETURNING *
157
- `;
158
- const cleansed = this.cleanseArrayField(entry);
159
- cleansed.last_synced_at = timestamp;
160
- cleansed.account_id = accountId;
161
- const prepared = sql(upsertSql, { useNullForMissing: true })(cleansed);
162
- queries.push(this.pool.query(prepared.text, prepared.values));
163
- } else {
164
- const conflictTarget = upsertOptions?.conflictTarget ?? ["id"];
165
- const extraColumns = upsertOptions?.extraColumns ?? [];
166
- if (!conflictTarget.length) {
167
- throw new Error(`Invalid upsert config for ${table}: conflictTarget must be non-empty`);
168
- }
169
- const columns = [
170
- { column: "_raw_data", pgType: "jsonb", value: JSON.stringify(entry) },
171
- ...extraColumns.map((c) => ({
172
- column: c.column,
173
- pgType: c.pgType,
174
- value: entry[c.entryKey]
175
- })),
176
- { column: "_last_synced_at", pgType: "timestamptz", value: timestamp },
177
- { column: "_account_id", pgType: "text", value: accountId }
178
- ];
179
- for (const c of columns) {
180
- if (c.value === void 0) {
181
- throw new Error(`Missing required value for ${table}.${c.column}`);
182
- }
183
- }
184
- const { sql: upsertSql, params } = QueryUtils.buildRawJsonUpsertQuery(
185
- this.config.schema,
186
- table,
187
- columns,
188
- conflictTarget
189
- );
190
- queries.push(this.pool.query(upsertSql, params));
191
- }
192
- });
193
- results.push(...await Promise.all(queries));
194
- }
195
- return results.flatMap((it) => it.rows);
196
- }
197
- cleanseArrayField(obj) {
198
- const cleansed = { ...obj };
199
- Object.keys(cleansed).map((k) => {
200
- const data = cleansed[k];
201
- if (Array.isArray(data)) {
202
- cleansed[k] = JSON.stringify(data);
203
- }
204
- });
205
- return cleansed;
206
- }
207
- async findMissingEntries(table, ids) {
208
- if (!ids.length) return [];
209
- const prepared = sql(`
210
- select id from "${this.config.schema}"."${table}"
211
- where id=any(:ids::text[]);
212
- `)({ ids });
213
- const { rows } = await this.query(prepared.text, prepared.values);
214
- const existingIds = rows.map((it) => it.id);
215
- const missingIds = ids.filter((it) => !existingIds.includes(it));
216
- return missingIds;
217
- }
218
- // Account management methods
219
- async upsertAccount(accountData, apiKeyHash) {
220
- const rawData = JSON.stringify(accountData.raw_data);
221
- await this.query(
222
- `INSERT INTO "${this.config.schema}"."accounts" ("_raw_data", "api_key_hashes", "first_synced_at", "_last_synced_at")
223
- VALUES ($1::jsonb, ARRAY[$2], now(), now())
224
- ON CONFLICT (id)
225
- DO UPDATE SET
226
- "_raw_data" = EXCLUDED."_raw_data",
227
- "api_key_hashes" = (
228
- SELECT ARRAY(
229
- SELECT DISTINCT unnest(
230
- COALESCE("${this.config.schema}"."accounts"."api_key_hashes", '{}') || ARRAY[$2]
231
- )
232
- )
233
- ),
234
- "_last_synced_at" = now(),
235
- "_updated_at" = now()`,
236
- [rawData, apiKeyHash]
237
- );
238
- }
239
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
240
- async getAllAccounts() {
241
- const result = await this.query(
242
- `SELECT _raw_data FROM "${this.config.schema}"."accounts"
243
- ORDER BY _last_synced_at DESC`
244
- );
245
- return result.rows.map((row) => row._raw_data);
246
- }
247
- /**
248
- * Looks up an account ID by API key hash
249
- * Uses the GIN index on api_key_hashes for fast lookups
250
- * @param apiKeyHash - SHA-256 hash of the Stripe API key
251
- * @returns Account ID if found, null otherwise
252
- */
253
- async getAccountIdByApiKeyHash(apiKeyHash) {
254
- const result = await this.query(
255
- `SELECT id FROM "${this.config.schema}"."accounts"
256
- WHERE $1 = ANY(api_key_hashes)
257
- LIMIT 1`,
258
- [apiKeyHash]
259
- );
260
- return result.rows.length > 0 ? result.rows[0].id : null;
261
- }
262
- /**
263
- * Looks up full account data by API key hash
264
- * @param apiKeyHash - SHA-256 hash of the Stripe API key
265
- * @returns Account raw data if found, null otherwise
266
- */
267
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
268
- async getAccountByApiKeyHash(apiKeyHash) {
269
- const result = await this.query(
270
- `SELECT _raw_data FROM "${this.config.schema}"."accounts"
271
- WHERE $1 = ANY(api_key_hashes)
272
- LIMIT 1`,
273
- [apiKeyHash]
274
- );
275
- return result.rows.length > 0 ? result.rows[0]._raw_data : null;
276
- }
277
- getAccountIdColumn(table) {
278
- return TABLES_WITH_ACCOUNT_ID.has(table) ? "account_id" : "_account_id";
279
- }
280
- async getAccountRecordCounts(accountId) {
281
- const counts = {};
282
- for (const table of ORDERED_STRIPE_TABLES) {
283
- const accountIdColumn = this.getAccountIdColumn(table);
284
- const result = await this.query(
285
- `SELECT COUNT(*) as count FROM "${this.config.schema}"."${table}"
286
- WHERE "${accountIdColumn}" = $1`,
287
- [accountId]
288
- );
289
- counts[table] = parseInt(result.rows[0].count);
290
- }
291
- return counts;
292
- }
293
- async deleteAccountWithCascade(accountId, useTransaction) {
294
- const deletionCounts = {};
295
- try {
296
- if (useTransaction) {
297
- await this.query("BEGIN");
298
- }
299
- for (const table of ORDERED_STRIPE_TABLES) {
300
- const accountIdColumn = this.getAccountIdColumn(table);
301
- const result = await this.query(
302
- `DELETE FROM "${this.config.schema}"."${table}"
303
- WHERE "${accountIdColumn}" = $1`,
304
- [accountId]
305
- );
306
- deletionCounts[table] = result.rowCount || 0;
307
- }
308
- const accountResult = await this.query(
309
- `DELETE FROM "${this.config.schema}"."accounts"
310
- WHERE "id" = $1`,
311
- [accountId]
312
- );
313
- deletionCounts["accounts"] = accountResult.rowCount || 0;
314
- if (useTransaction) {
315
- await this.query("COMMIT");
316
- }
317
- } catch (error) {
318
- if (useTransaction) {
319
- await this.query("ROLLBACK");
320
- }
321
- throw error;
322
- }
323
- return deletionCounts;
324
- }
325
- /**
326
- * Hash a string to a 32-bit integer for use with PostgreSQL advisory locks.
327
- * Uses a simple hash algorithm that produces consistent results.
328
- */
329
- hashToInt32(key) {
330
- let hash = 0;
331
- for (let i = 0; i < key.length; i++) {
332
- const char = key.charCodeAt(i);
333
- hash = (hash << 5) - hash + char;
334
- hash = hash & hash;
335
- }
336
- return hash;
337
- }
338
- /**
339
- * Acquire a PostgreSQL advisory lock for the given key.
340
- * This lock is automatically released when the connection is closed or explicitly released.
341
- * Advisory locks are session-level and will block until the lock is available.
342
- *
343
- * @param key - A string key to lock on (will be hashed to an integer)
344
- */
345
- async acquireAdvisoryLock(key) {
346
- const lockId = this.hashToInt32(key);
347
- await this.query("SELECT pg_advisory_lock($1)", [lockId]);
348
- }
349
- /**
350
- * Release a PostgreSQL advisory lock for the given key.
351
- *
352
- * @param key - The same string key used to acquire the lock
353
- */
354
- async releaseAdvisoryLock(key) {
355
- const lockId = this.hashToInt32(key);
356
- await this.query("SELECT pg_advisory_unlock($1)", [lockId]);
357
- }
358
- /**
359
- * Execute a function while holding an advisory lock.
360
- * The lock is automatically released after the function completes (success or error).
361
- *
362
- * IMPORTANT: This acquires a dedicated connection from the pool and holds it for the
363
- * duration of the function execution. PostgreSQL advisory locks are session-level,
364
- * so we must use the same connection for lock acquisition, operations, and release.
365
- *
366
- * @param key - A string key to lock on (will be hashed to an integer)
367
- * @param fn - The function to execute while holding the lock
368
- * @returns The result of the function
369
- */
370
- async withAdvisoryLock(key, fn) {
371
- const lockId = this.hashToInt32(key);
372
- const client = await this.pool.connect();
373
- try {
374
- await client.query("SELECT pg_advisory_lock($1)", [lockId]);
375
- return await fn();
376
- } finally {
377
- try {
378
- await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
379
- } finally {
380
- client.release();
381
- }
382
- }
383
- }
384
- // =============================================================================
385
- // Observable Sync System Methods
386
- // =============================================================================
387
- // These methods support long-running syncs with full observability.
388
- // Uses two tables: _sync_runs (parent) and _sync_obj_runs (children)
389
- // RunKey = (accountId, runStartedAt) - natural composite key
390
- /**
391
- * Cancel stale runs (running but no object updated in 5 minutes).
392
- * Called before creating a new run to clean up crashed syncs.
393
- * Only cancels runs that have objects AND none have recent activity.
394
- * Runs without objects yet (just created) are not considered stale.
395
- */
396
- async cancelStaleRuns(accountId) {
397
- await this.query(
398
- `UPDATE "${this.config.schema}"."_sync_obj_runs" o
399
- SET status = 'error',
400
- error_message = 'Auto-cancelled: stale (no update in 5 min)',
401
- completed_at = now(),
402
- page_cursor = NULL
403
- WHERE o."_account_id" = $1
404
- AND o.status = 'running'
405
- AND o.updated_at < now() - interval '5 minutes'`,
406
- [accountId]
407
- );
408
- await this.query(
409
- `UPDATE "${this.config.schema}"."_sync_runs" r
410
- SET closed_at = now()
411
- WHERE r."_account_id" = $1
412
- AND r.closed_at IS NULL
413
- AND EXISTS (
414
- SELECT 1 FROM "${this.config.schema}"."_sync_obj_runs" o
415
- WHERE o."_account_id" = r."_account_id"
416
- AND o.run_started_at = r.started_at
417
- )
418
- AND NOT EXISTS (
419
- SELECT 1 FROM "${this.config.schema}"."_sync_obj_runs" o
420
- WHERE o."_account_id" = r."_account_id"
421
- AND o.run_started_at = r.started_at
422
- AND o.status IN ('pending', 'running')
423
- )`,
424
- [accountId]
425
- );
426
- }
427
- /**
428
- * Get or create a sync run for this account.
429
- * Returns existing run if one is active, otherwise creates new one.
430
- * Auto-cancels stale runs before checking.
431
- *
432
- * @returns RunKey with isNew flag, or null if constraint violation (race condition)
433
- */
434
- async getOrCreateSyncRun(accountId, triggeredBy) {
435
- await this.cancelStaleRuns(accountId);
436
- const existing = await this.query(
437
- `SELECT "_account_id", started_at FROM "${this.config.schema}"."_sync_runs"
438
- WHERE "_account_id" = $1 AND closed_at IS NULL`,
439
- [accountId]
440
- );
441
- if (existing.rows.length > 0) {
442
- const row = existing.rows[0];
443
- return { accountId: row._account_id, runStartedAt: row.started_at, isNew: false };
444
- }
445
- try {
446
- const result = await this.query(
447
- `INSERT INTO "${this.config.schema}"."_sync_runs" ("_account_id", triggered_by, started_at)
448
- VALUES ($1, $2, date_trunc('milliseconds', now()))
449
- RETURNING "_account_id", started_at`,
450
- [accountId, triggeredBy ?? null]
451
- );
452
- const row = result.rows[0];
453
- return { accountId: row._account_id, runStartedAt: row.started_at, isNew: true };
454
- } catch (error) {
455
- if (error instanceof Error && "code" in error && error.code === "23P01") {
456
- return null;
457
- }
458
- throw error;
459
- }
460
- }
461
- /**
462
- * Get the active sync run for an account (if any).
463
- */
464
- async getActiveSyncRun(accountId) {
465
- const result = await this.query(
466
- `SELECT "_account_id", started_at FROM "${this.config.schema}"."_sync_runs"
467
- WHERE "_account_id" = $1 AND closed_at IS NULL`,
468
- [accountId]
469
- );
470
- if (result.rows.length === 0) return null;
471
- const row = result.rows[0];
472
- return { accountId: row._account_id, runStartedAt: row.started_at };
473
- }
474
- /**
475
- * Get sync run config (for concurrency control).
476
- * Status is derived from sync_runs view.
477
- */
478
- async getSyncRun(accountId, runStartedAt) {
479
- const result = await this.query(
480
- `SELECT "_account_id", started_at, max_concurrent, closed_at
481
- FROM "${this.config.schema}"."_sync_runs"
482
- WHERE "_account_id" = $1 AND started_at = $2`,
483
- [accountId, runStartedAt]
484
- );
485
- if (result.rows.length === 0) return null;
486
- const row = result.rows[0];
487
- return {
488
- accountId: row._account_id,
489
- runStartedAt: row.started_at,
490
- maxConcurrent: row.max_concurrent,
491
- closedAt: row.closed_at
492
- };
493
- }
494
- /**
495
- * Close a sync run (mark as done).
496
- * Status (complete/error) is derived from object run states.
497
- */
498
- async closeSyncRun(accountId, runStartedAt) {
499
- await this.query(
500
- `UPDATE "${this.config.schema}"."_sync_runs"
501
- SET closed_at = now()
502
- WHERE "_account_id" = $1 AND started_at = $2 AND closed_at IS NULL`,
503
- [accountId, runStartedAt]
504
- );
505
- }
506
- /**
507
- * Create object run entries for a sync run.
508
- * All objects start as 'pending'.
509
- *
510
- * @param resourceNames - Database resource names (e.g. 'products', 'customers', NOT 'product', 'customer')
511
- */
512
- async createObjectRuns(accountId, runStartedAt, resourceNames) {
513
- if (resourceNames.length === 0) return;
514
- const values = resourceNames.map((_, i) => `($1, $2, $${i + 3})`).join(", ");
515
- await this.query(
516
- `INSERT INTO "${this.config.schema}"."_sync_obj_runs" ("_account_id", run_started_at, object)
517
- VALUES ${values}
518
- ON CONFLICT ("_account_id", run_started_at, object) DO NOTHING`,
519
- [accountId, runStartedAt, ...resourceNames]
520
- );
521
- }
522
- /**
523
- * Try to start an object sync (respects max_concurrent).
524
- * Returns true if claimed, false if already running or at concurrency limit.
525
- *
526
- * Note: There's a small race window where concurrent calls could result in
527
- * max_concurrent + 1 objects running. This is acceptable behavior.
528
- */
529
- async tryStartObjectSync(accountId, runStartedAt, object) {
530
- const run = await this.getSyncRun(accountId, runStartedAt);
531
- if (!run) return false;
532
- const runningCount = await this.countRunningObjects(accountId, runStartedAt);
533
- if (runningCount >= run.maxConcurrent) return false;
534
- const result = await this.query(
535
- `UPDATE "${this.config.schema}"."_sync_obj_runs"
536
- SET status = 'running', started_at = now(), updated_at = now()
537
- WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3 AND status = 'pending'
538
- RETURNING *`,
539
- [accountId, runStartedAt, object]
540
- );
541
- return (result.rowCount ?? 0) > 0;
542
- }
543
- /**
544
- * Get object run details.
545
- */
546
- async getObjectRun(accountId, runStartedAt, object) {
547
- const result = await this.query(
548
- `SELECT object, status, processed_count, cursor, page_cursor
549
- FROM "${this.config.schema}"."_sync_obj_runs"
550
- WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
551
- [accountId, runStartedAt, object]
552
- );
553
- if (result.rows.length === 0) return null;
554
- const row = result.rows[0];
555
- return {
556
- object: row.object,
557
- status: row.status,
558
- processedCount: row.processed_count,
559
- cursor: row.cursor,
560
- pageCursor: row.page_cursor
561
- };
562
- }
563
- /**
564
- * Update progress for an object sync.
565
- * Also touches updated_at for stale detection.
566
- */
567
- async incrementObjectProgress(accountId, runStartedAt, object, count) {
568
- await this.query(
569
- `UPDATE "${this.config.schema}"."_sync_obj_runs"
570
- SET processed_count = processed_count + $4, updated_at = now()
571
- WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
572
- [accountId, runStartedAt, object, count]
573
- );
574
- }
575
- /**
576
- * Update the pagination page_cursor used for backfills using Stripe list calls.
577
- */
578
- async updateObjectPageCursor(accountId, runStartedAt, object, pageCursor) {
579
- await this.query(
580
- `UPDATE "${this.config.schema}"."_sync_obj_runs"
581
- SET page_cursor = $4, updated_at = now()
582
- WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
583
- [accountId, runStartedAt, object, pageCursor]
584
- );
585
- }
586
- /**
587
- * Clear the pagination page_cursor for an object sync.
588
- */
589
- async clearObjectPageCursor(accountId, runStartedAt, object) {
590
- await this.updateObjectPageCursor(accountId, runStartedAt, object, null);
591
- }
592
- /**
593
- * Update the cursor for an object sync.
594
- * Only updates if the new cursor is higher than the existing one (cursors should never decrease).
595
- * For numeric cursors (timestamps), uses GREATEST to ensure monotonic increase.
596
- * For non-numeric cursors, just sets the value directly.
597
- */
598
- async updateObjectCursor(accountId, runStartedAt, object, cursor) {
599
- const isNumeric = cursor !== null && /^\d+$/.test(cursor);
600
- if (isNumeric) {
601
- await this.query(
602
- `UPDATE "${this.config.schema}"."_sync_obj_runs"
603
- SET cursor = GREATEST(COALESCE(cursor::bigint, 0), $4::bigint)::text,
604
- updated_at = now()
605
- WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
606
- [accountId, runStartedAt, object, cursor]
607
- );
608
- } else {
609
- await this.query(
610
- `UPDATE "${this.config.schema}"."_sync_obj_runs"
611
- SET cursor = CASE
612
- WHEN cursor IS NULL THEN $4
613
- WHEN (cursor COLLATE "C") < ($4::text COLLATE "C") THEN $4
614
- ELSE cursor
615
- END,
616
- updated_at = now()
617
- WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
618
- [accountId, runStartedAt, object, cursor]
619
- );
620
- }
621
- }
622
- /**
623
- * Get the highest cursor from previous syncs for an object type.
624
- * Uses only completed object runs.
625
- * - During the initial backfill we page through history, but we also update the cursor as we go.
626
- * If we crash mid-backfill and reuse that cursor, we can accidentally switch into incremental mode
627
- * too early and only ever fetch the newest page (breaking the historical backfill).
628
- *
629
- * Handles two cursor formats:
630
- * - Numeric: compared as bigint for correct ordering
631
- * - Composite cursors: compared as strings with COLLATE "C"
632
- */
633
- async getLastCompletedCursor(accountId, object) {
634
- const result = await this.query(
635
- `SELECT CASE
636
- WHEN BOOL_OR(o.cursor !~ '^\\d+$') THEN MAX(o.cursor COLLATE "C")
637
- ELSE MAX(CASE WHEN o.cursor ~ '^\\d+$' THEN o.cursor::bigint END)::text
638
- END as cursor
639
- FROM "${this.config.schema}"."_sync_obj_runs" o
640
- WHERE o."_account_id" = $1
641
- AND o.object = $2
642
- AND o.cursor IS NOT NULL
643
- AND o.status = 'complete'`,
644
- [accountId, object]
645
- );
646
- return result.rows[0]?.cursor ?? null;
647
- }
648
- /**
649
- * Get the highest cursor from previous syncs for an object type, excluding the current run.
650
- */
651
- async getLastCursorBeforeRun(accountId, object, runStartedAt) {
652
- const result = await this.query(
653
- `SELECT CASE
654
- WHEN BOOL_OR(o.cursor !~ '^\\d+$') THEN MAX(o.cursor COLLATE "C")
655
- ELSE MAX(CASE WHEN o.cursor ~ '^\\d+$' THEN o.cursor::bigint END)::text
656
- END as cursor
657
- FROM "${this.config.schema}"."_sync_obj_runs" o
658
- WHERE o."_account_id" = $1
659
- AND o.object = $2
660
- AND o.cursor IS NOT NULL
661
- AND o.status = 'complete'
662
- AND o.run_started_at < $3`,
663
- [accountId, object, runStartedAt]
664
- );
665
- return result.rows[0]?.cursor ?? null;
666
- }
667
- /**
668
- * Delete all sync runs and object runs for an account.
669
- * Useful for testing or resetting sync state.
670
- */
671
- async deleteSyncRuns(accountId) {
672
- await this.query(
673
- `DELETE FROM "${this.config.schema}"."_sync_obj_runs" WHERE "_account_id" = $1`,
674
- [accountId]
675
- );
676
- await this.query(`DELETE FROM "${this.config.schema}"."_sync_runs" WHERE "_account_id" = $1`, [
677
- accountId
678
- ]);
679
- }
680
- /**
681
- * Mark an object sync as complete.
682
- * Auto-closes the run when all objects are done.
683
- */
684
- async completeObjectSync(accountId, runStartedAt, object) {
685
- await this.query(
686
- `UPDATE "${this.config.schema}"."_sync_obj_runs"
687
- SET status = 'complete', completed_at = now(), page_cursor = NULL
688
- WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
689
- [accountId, runStartedAt, object]
690
- );
691
- const allDone = await this.areAllObjectsComplete(accountId, runStartedAt);
692
- if (allDone) {
693
- await this.closeSyncRun(accountId, runStartedAt);
694
- }
695
- }
696
- /**
697
- * Mark an object sync as failed.
698
- * Auto-closes the run when all objects are done.
699
- */
700
- async failObjectSync(accountId, runStartedAt, object, errorMessage) {
701
- await this.query(
702
- `UPDATE "${this.config.schema}"."_sync_obj_runs"
703
- SET status = 'error', error_message = $4, completed_at = now(), page_cursor = NULL
704
- WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
705
- [accountId, runStartedAt, object, errorMessage]
706
- );
707
- const allDone = await this.areAllObjectsComplete(accountId, runStartedAt);
708
- if (allDone) {
709
- await this.closeSyncRun(accountId, runStartedAt);
710
- }
711
- }
712
- /**
713
- * Check if any object in a run has errored.
714
- */
715
- async hasAnyObjectErrors(accountId, runStartedAt) {
716
- const result = await this.query(
717
- `SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_runs"
718
- WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'error'`,
719
- [accountId, runStartedAt]
720
- );
721
- return parseInt(result.rows[0].count) > 0;
722
- }
723
- /**
724
- * Count running objects in a run.
725
- */
726
- async countRunningObjects(accountId, runStartedAt) {
727
- const result = await this.query(
728
- `SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_runs"
729
- WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'running'`,
730
- [accountId, runStartedAt]
731
- );
732
- return parseInt(result.rows[0].count);
733
- }
734
- /**
735
- * Get the next pending object to process.
736
- * Returns null if no pending objects or at concurrency limit.
737
- */
738
- async getNextPendingObject(accountId, runStartedAt) {
739
- const run = await this.getSyncRun(accountId, runStartedAt);
740
- if (!run) return null;
741
- const runningCount = await this.countRunningObjects(accountId, runStartedAt);
742
- if (runningCount >= run.maxConcurrent) return null;
743
- const result = await this.query(
744
- `SELECT object FROM "${this.config.schema}"."_sync_obj_runs"
745
- WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'pending'
746
- ORDER BY object
747
- LIMIT 1`,
748
- [accountId, runStartedAt]
749
- );
750
- return result.rows.length > 0 ? result.rows[0].object : null;
751
- }
752
- /**
753
- * Check if all objects in a run are complete (or error).
754
- */
755
- async areAllObjectsComplete(accountId, runStartedAt) {
756
- const result = await this.query(
757
- `SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_runs"
758
- WHERE "_account_id" = $1 AND run_started_at = $2 AND status IN ('pending', 'running')`,
759
- [accountId, runStartedAt]
760
- );
761
- return parseInt(result.rows[0].count) === 0;
762
- }
763
- /**
764
- * Closes the database connection pool and cleans up resources.
765
- * Call this when you're done using the PostgresClient instance.
766
- */
767
- async close() {
768
- await this.pool.end();
769
- }
770
- };
771
-
772
- // src/schemas/managed_webhook.ts
773
- var managedWebhookSchema = {
774
- properties: [
775
- "id",
776
- "object",
777
- "url",
778
- "enabled_events",
779
- "description",
780
- "enabled",
781
- "livemode",
782
- "metadata",
783
- "secret",
784
- "status",
785
- "api_version",
786
- "created",
787
- "account_id"
788
- ]
789
- };
790
-
791
- // src/utils/retry.ts
792
- import Stripe from "stripe";
793
- var DEFAULT_RETRY_CONFIG = {
794
- maxRetries: 5,
795
- initialDelayMs: 1e3,
796
- // 1 second
797
- maxDelayMs: 6e4,
798
- // 60 seconds
799
- jitterMs: 500
800
- // randomization to prevent thundering herd
801
- };
802
- function isRetryableError(error) {
803
- if (error instanceof Stripe.errors.StripeRateLimitError) {
804
- return true;
805
- }
806
- if (error instanceof Stripe.errors.StripeAPIError) {
807
- const statusCode = error.statusCode;
808
- if (statusCode && [500, 502, 503, 504, 424].includes(statusCode)) {
809
- return true;
810
- }
811
- }
812
- if (error instanceof Stripe.errors.StripeConnectionError) {
813
- return true;
814
- }
815
- return false;
816
- }
817
- function getRetryAfterMs(error) {
818
- if (!(error instanceof Stripe.errors.StripeRateLimitError)) {
819
- return null;
820
- }
821
- const retryAfterHeader = error.headers?.["retry-after"];
822
- if (!retryAfterHeader) {
823
- return null;
824
- }
825
- const retryAfterSeconds = Number(retryAfterHeader);
826
- if (isNaN(retryAfterSeconds) || retryAfterSeconds <= 0) {
827
- return null;
828
- }
829
- return retryAfterSeconds * 1e3;
830
- }
831
- function calculateDelay(attempt, config, retryAfterMs) {
832
- if (retryAfterMs !== null && retryAfterMs !== void 0) {
833
- const jitter2 = Math.random() * config.jitterMs;
834
- return retryAfterMs + jitter2;
835
- }
836
- const exponentialDelay = Math.min(config.initialDelayMs * Math.pow(2, attempt), config.maxDelayMs);
837
- const jitter = Math.random() * config.jitterMs;
838
- return exponentialDelay + jitter;
839
- }
840
- function sleep(ms) {
841
- return new Promise((resolve) => setTimeout(resolve, ms));
842
- }
843
- function getErrorType(error) {
844
- if (error instanceof Stripe.errors.StripeRateLimitError) {
845
- return "rate_limit";
846
- }
847
- if (error instanceof Stripe.errors.StripeAPIError) {
848
- return `api_error_${error.statusCode}`;
849
- }
850
- if (error instanceof Stripe.errors.StripeConnectionError) {
851
- return "connection_error";
852
- }
853
- return "unknown";
854
- }
855
- async function withRetry(fn, config = {}, logger) {
856
- const retryConfig = { ...DEFAULT_RETRY_CONFIG, ...config };
857
- let lastError;
858
- for (let attempt = 0; attempt <= retryConfig.maxRetries; attempt++) {
859
- try {
860
- return await fn();
861
- } catch (error) {
862
- lastError = error;
863
- if (!isRetryableError(error)) {
864
- throw error;
865
- }
866
- if (attempt >= retryConfig.maxRetries) {
867
- logger?.error(
868
- {
869
- error: error instanceof Error ? error.message : String(error),
870
- errorType: getErrorType(error),
871
- attempt: attempt + 1,
872
- maxRetries: retryConfig.maxRetries
873
- },
874
- "Max retries exhausted for Stripe error"
875
- );
876
- throw error;
877
- }
878
- const retryAfterMs = getRetryAfterMs(error);
879
- const delay = calculateDelay(attempt, retryConfig, retryAfterMs);
880
- logger?.warn(
881
- {
882
- error: error instanceof Error ? error.message : String(error),
883
- errorType: getErrorType(error),
884
- attempt: attempt + 1,
885
- maxRetries: retryConfig.maxRetries,
886
- delayMs: Math.round(delay),
887
- retryAfterMs: retryAfterMs ?? void 0,
888
- nextAttempt: attempt + 2
889
- },
890
- "Transient Stripe error, retrying after delay"
891
- );
892
- await sleep(delay);
893
- }
894
- }
895
- throw lastError;
896
- }
897
-
898
- // src/utils/stripeClientWrapper.ts
899
- function createRetryableStripeClient(stripe, retryConfig = {}, logger) {
900
- return new Proxy(stripe, {
901
- get(target, prop, receiver) {
902
- const original = Reflect.get(target, prop, receiver);
903
- if (original && typeof original === "object" && !isPromise(original)) {
904
- return wrapResource(original, retryConfig, logger);
905
- }
906
- return original;
907
- }
908
- });
909
- }
910
- function wrapResource(resource, retryConfig, logger) {
911
- return new Proxy(resource, {
912
- get(target, prop, receiver) {
913
- const original = Reflect.get(target, prop, receiver);
914
- if (typeof original === "function") {
915
- return function(...args) {
916
- const result = original.apply(target, args);
917
- if (result && typeof result === "object" && Symbol.asyncIterator in result) {
918
- return result;
919
- }
920
- if (isPromise(result)) {
921
- return withRetry(() => Promise.resolve(result), retryConfig, logger);
922
- }
923
- return result;
924
- };
925
- }
926
- if (original && typeof original === "object" && !isPromise(original)) {
927
- return wrapResource(original, retryConfig, logger);
928
- }
929
- return original;
930
- }
931
- });
932
- }
933
- function isPromise(value) {
934
- return value !== null && typeof value === "object" && typeof value.then === "function";
935
- }
936
-
937
- // src/utils/hashApiKey.ts
938
- import { createHash } from "crypto";
939
- function hashApiKey(apiKey) {
940
- return createHash("sha256").update(apiKey).digest("hex");
941
- }
942
-
943
- // src/sigma/sigmaApi.ts
944
- import Papa from "papaparse";
945
- import Stripe2 from "stripe";
946
- var STRIPE_FILES_BASE = "https://files.stripe.com/v1";
947
- function sleep2(ms) {
948
- return new Promise((resolve) => setTimeout(resolve, ms));
949
- }
950
- function parseCsvObjects(csv) {
951
- const input = csv.replace(/^\uFEFF/, "");
952
- const parsed = Papa.parse(input, {
953
- header: true,
954
- skipEmptyLines: "greedy"
955
- });
956
- if (parsed.errors.length > 0) {
957
- throw new Error(`Failed to parse Sigma CSV: ${parsed.errors[0]?.message ?? "unknown error"}`);
958
- }
959
- return parsed.data.filter((row) => row && Object.keys(row).length > 0).map(
960
- (row) => Object.fromEntries(
961
- Object.entries(row).map(([k, v]) => [k, v == null || v === "" ? null : String(v)])
962
- )
963
- );
964
- }
965
- function normalizeSigmaTimestampToIso(value) {
966
- const v = value.trim();
967
- if (!v) return null;
968
- const hasExplicitTz = /z$|[+-]\d{2}:?\d{2}$/i.test(v);
969
- const isoish = v.includes("T") ? v : v.replace(" ", "T");
970
- const candidate = hasExplicitTz ? isoish : `${isoish}Z`;
971
- const d = new Date(candidate);
972
- if (Number.isNaN(d.getTime())) return null;
973
- return d.toISOString();
974
- }
975
- async function fetchStripeText(url, apiKey, options) {
976
- const res = await fetch(url, {
977
- ...options,
978
- headers: {
979
- ...options.headers ?? {},
980
- Authorization: `Bearer ${apiKey}`
981
- }
982
- });
983
- const text = await res.text();
984
- if (!res.ok) {
985
- throw new Error(`Sigma file download error (${res.status}) for ${url}: ${text}`);
986
- }
987
- return text;
988
- }
989
- async function runSigmaQueryAndDownloadCsv(params) {
990
- const pollTimeoutMs = params.pollTimeoutMs ?? 5 * 60 * 1e3;
991
- const pollIntervalMs = params.pollIntervalMs ?? 2e3;
992
- const stripe = new Stripe2(params.apiKey, {
993
- appInfo: {
994
- name: "Stripe Sync Engine",
995
- version: package_default.version,
996
- url: package_default.homepage
997
- }
998
- });
999
- const created = await stripe.rawRequest("POST", "/v1/sigma/query_runs", {
1000
- sql: params.sql
1001
- });
1002
- const queryRunId = created.id;
1003
- const start = Date.now();
1004
- let current = created;
1005
- while (current.status === "running") {
1006
- if (Date.now() - start > pollTimeoutMs) {
1007
- throw new Error(`Sigma query run timed out after ${pollTimeoutMs}ms: ${queryRunId}`);
1008
- }
1009
- await sleep2(pollIntervalMs);
1010
- current = await stripe.rawRequest(
1011
- "GET",
1012
- `/v1/sigma/query_runs/${queryRunId}`,
1013
- {}
1014
- );
1015
- }
1016
- if (current.status !== "succeeded") {
1017
- throw new Error(
1018
- `Sigma query run did not succeed (status=${current.status}) id=${queryRunId} error=${JSON.stringify(
1019
- current.error
1020
- )}`
1021
- );
1022
- }
1023
- const fileId = current.result?.file;
1024
- if (!fileId) {
1025
- throw new Error(`Sigma query run succeeded but result.file is missing (id=${queryRunId})`);
1026
- }
1027
- const csv = await fetchStripeText(
1028
- `${STRIPE_FILES_BASE}/files/${fileId}/contents`,
1029
- params.apiKey,
1030
- { method: "GET" }
1031
- );
1032
- return { queryRunId, fileId, csv };
1033
- }
1034
-
1035
- // src/sigma/sigmaIngestionConfigs.ts
1036
- var SIGMA_INGESTION_CONFIGS = {
1037
- subscription_item_change_events_v2_beta: {
1038
- sigmaTable: "subscription_item_change_events_v2_beta",
1039
- destinationTable: "subscription_item_change_events_v2_beta",
1040
- pageSize: 1e4,
1041
- cursor: {
1042
- version: 1,
1043
- columns: [
1044
- { column: "event_timestamp", type: "timestamp" },
1045
- { column: "event_type", type: "string" },
1046
- { column: "subscription_item_id", type: "string" }
1047
- ]
1048
- },
1049
- upsert: {
1050
- conflictTarget: ["_account_id", "event_timestamp", "event_type", "subscription_item_id"],
1051
- extraColumns: [
1052
- { column: "event_timestamp", pgType: "timestamptz", entryKey: "event_timestamp" },
1053
- { column: "event_type", pgType: "text", entryKey: "event_type" },
1054
- { column: "subscription_item_id", pgType: "text", entryKey: "subscription_item_id" }
1055
- ]
1056
- }
1057
- },
1058
- exchange_rates_from_usd: {
1059
- sigmaTable: "exchange_rates_from_usd",
1060
- destinationTable: "exchange_rates_from_usd",
1061
- pageSize: 1e4,
1062
- cursor: {
1063
- version: 1,
1064
- columns: [
1065
- { column: "date", type: "string" },
1066
- { column: "sell_currency", type: "string" }
1067
- ]
1068
- },
1069
- upsert: {
1070
- conflictTarget: ["_account_id", "date", "sell_currency"],
1071
- extraColumns: [
1072
- { column: "date", pgType: "date", entryKey: "date" },
1073
- { column: "sell_currency", pgType: "text", entryKey: "sell_currency" }
1074
- ]
1075
- }
1076
- }
1077
- };
1078
-
1079
- // src/sigma/sigmaIngestion.ts
1080
- var SIGMA_CURSOR_DELIM = "";
1081
- function escapeSigmaSqlStringLiteral(value) {
1082
- return value.replace(/'/g, "''");
1083
- }
1084
- function formatSigmaTimestampForSqlLiteral(date) {
1085
- return date.toISOString().replace("T", " ").replace("Z", "");
1086
- }
1087
- function decodeSigmaCursorValues(spec, cursor) {
1088
- const prefix = `v${spec.version}${SIGMA_CURSOR_DELIM}`;
1089
- if (!cursor.startsWith(prefix)) {
1090
- throw new Error(
1091
- `Unrecognized Sigma cursor format (expected prefix ${JSON.stringify(prefix)}): ${cursor}`
1092
- );
1093
- }
1094
- const parts = cursor.split(SIGMA_CURSOR_DELIM);
1095
- const expected = 1 + spec.columns.length;
1096
- if (parts.length !== expected) {
1097
- throw new Error(`Malformed Sigma cursor: expected ${expected} parts, got ${parts.length}`);
1098
- }
1099
- return parts.slice(1);
1100
- }
1101
- function encodeSigmaCursor(spec, values) {
1102
- if (values.length !== spec.columns.length) {
1103
- throw new Error(
1104
- `Cannot encode Sigma cursor: expected ${spec.columns.length} values, got ${values.length}`
1105
- );
1106
- }
1107
- for (const v of values) {
1108
- if (v.includes(SIGMA_CURSOR_DELIM)) {
1109
- throw new Error("Cannot encode Sigma cursor: value contains delimiter character");
1110
- }
1111
- }
1112
- return [`v${spec.version}`, ...values].join(SIGMA_CURSOR_DELIM);
1113
- }
1114
- function sigmaSqlLiteralForCursorValue(spec, rawValue) {
1115
- switch (spec.type) {
1116
- case "timestamp": {
1117
- const d = new Date(rawValue);
1118
- if (Number.isNaN(d.getTime())) {
1119
- throw new Error(`Invalid timestamp cursor value for ${spec.column}: ${rawValue}`);
1120
- }
1121
- return `timestamp '${formatSigmaTimestampForSqlLiteral(d)}'`;
1122
- }
1123
- case "number": {
1124
- if (!/^-?\d+(\.\d+)?$/.test(rawValue)) {
1125
- throw new Error(`Invalid numeric cursor value for ${spec.column}: ${rawValue}`);
1126
- }
1127
- return rawValue;
1128
- }
1129
- case "string":
1130
- return `'${escapeSigmaSqlStringLiteral(rawValue)}'`;
1131
- }
1132
- }
1133
- function buildSigmaCursorWhereClause(spec, cursorValues) {
1134
- if (cursorValues.length !== spec.columns.length) {
1135
- throw new Error(
1136
- `Cannot build Sigma cursor predicate: expected ${spec.columns.length} values, got ${cursorValues.length}`
1137
- );
1138
- }
1139
- const cols = spec.columns.map((c) => c.column);
1140
- const lits = spec.columns.map((c, i) => sigmaSqlLiteralForCursorValue(c, cursorValues[i] ?? ""));
1141
- const ors = [];
1142
- for (let i = 0; i < cols.length; i++) {
1143
- const ands = [];
1144
- for (let j = 0; j < i; j++) {
1145
- ands.push(`${cols[j]} = ${lits[j]}`);
1146
- }
1147
- ands.push(`${cols[i]} > ${lits[i]}`);
1148
- ors.push(`(${ands.join(" AND ")})`);
1149
- }
1150
- return ors.join(" OR ");
1151
- }
1152
- function buildSigmaQuery(config, cursor) {
1153
- const select = config.select === void 0 || config.select === "*" ? "*" : config.select.join(", ");
1154
- const whereParts = [];
1155
- if (config.additionalWhere) {
1156
- whereParts.push(`(${config.additionalWhere})`);
1157
- }
1158
- if (cursor) {
1159
- const values = decodeSigmaCursorValues(config.cursor, cursor);
1160
- const predicate = buildSigmaCursorWhereClause(config.cursor, values);
1161
- whereParts.push(`(${predicate})`);
1162
- }
1163
- const whereClause = whereParts.length > 0 ? `WHERE ${whereParts.join(" AND ")}` : "";
1164
- const orderBy = config.cursor.columns.map((c) => c.column).join(", ");
1165
- return [
1166
- `SELECT ${select} FROM ${config.sigmaTable}`,
1167
- whereClause,
1168
- `ORDER BY ${orderBy} ASC`,
1169
- `LIMIT ${config.pageSize}`
1170
- ].filter(Boolean).join(" ");
1171
- }
1172
- function defaultSigmaRowToEntry(config, row) {
1173
- const out = { ...row };
1174
- for (const col of config.cursor.columns) {
1175
- const raw = row[col.column];
1176
- if (raw == null) {
1177
- throw new Error(`Sigma row missing required cursor column: ${col.column}`);
1178
- }
1179
- if (col.type === "timestamp") {
1180
- const normalized = normalizeSigmaTimestampToIso(raw);
1181
- if (!normalized) {
1182
- throw new Error(`Sigma row has invalid timestamp for ${col.column}: ${raw}`);
1183
- }
1184
- out[col.column] = normalized;
1185
- } else if (col.type === "string") {
1186
- const v = raw.trim();
1187
- if (!v) {
1188
- throw new Error(`Sigma row has empty string for required cursor column: ${col.column}`);
1189
- }
1190
- out[col.column] = v;
1191
- } else {
1192
- const v = raw.trim();
1193
- if (!v) {
1194
- throw new Error(`Sigma row has empty value for required cursor column: ${col.column}`);
1195
- }
1196
- out[col.column] = v;
1197
- }
1198
- }
1199
- return out;
1200
- }
1201
- function sigmaCursorFromEntry(config, entry) {
1202
- const values = config.cursor.columns.map((c) => {
1203
- const raw = entry[c.column];
1204
- if (raw == null) {
1205
- throw new Error(`Cannot build cursor: entry missing ${c.column}`);
1206
- }
1207
- return String(raw);
1208
- });
1209
- return encodeSigmaCursor(config.cursor, values);
1210
- }
1211
-
1212
- // src/stripeSync.ts
1213
- function getUniqueIds(entries, key) {
1214
- const set = new Set(
1215
- entries.map((subscription) => subscription?.[key]?.toString()).filter((it) => Boolean(it))
1216
- );
1217
- return Array.from(set);
1218
- }
1219
- var StripeSync = class {
1220
- constructor(config) {
1221
- this.config = config;
1222
- const baseStripe = new Stripe3(config.stripeSecretKey, {
1223
- // https://github.com/stripe/stripe-node#configuration
1224
- // @ts-ignore
1225
- apiVersion: config.stripeApiVersion,
1226
- appInfo: {
1227
- name: "Stripe Sync Engine",
1228
- version: package_default.version,
1229
- url: package_default.homepage
1230
- }
1231
- });
1232
- this.stripe = createRetryableStripeClient(baseStripe, {}, config.logger);
1233
- this.config.logger = config.logger ?? console;
1234
- this.config.logger?.info(
1235
- { autoExpandLists: config.autoExpandLists, stripeApiVersion: config.stripeApiVersion },
1236
- "StripeSync initialized"
1237
- );
1238
- const poolConfig = config.poolConfig ?? {};
1239
- if (config.databaseUrl) {
1240
- poolConfig.connectionString = config.databaseUrl;
1241
- }
1242
- if (config.maxPostgresConnections) {
1243
- poolConfig.max = config.maxPostgresConnections;
1244
- }
1245
- if (poolConfig.max === void 0) {
1246
- poolConfig.max = 10;
1247
- }
1248
- if (poolConfig.keepAlive === void 0) {
1249
- poolConfig.keepAlive = true;
1250
- }
1251
- this.postgresClient = new PostgresClient({
1252
- schema: "stripe",
1253
- poolConfig
1254
- });
1255
- }
1256
- stripe;
1257
- postgresClient;
1258
- /**
1259
- * Get the Stripe account ID. Delegates to getCurrentAccount() for the actual lookup.
1260
- */
1261
- async getAccountId(objectAccountId) {
1262
- const account = await this.getCurrentAccount(objectAccountId);
1263
- if (!account) {
1264
- throw new Error("Failed to retrieve Stripe account. Please ensure API key is valid.");
1265
- }
1266
- return account.id;
1267
- }
1268
- /**
1269
- * Upsert Stripe account information to the database
1270
- * @param account - Stripe account object
1271
- * @param apiKeyHash - SHA-256 hash of API key to store for fast lookups
1272
- */
1273
- async upsertAccount(account, apiKeyHash) {
1274
- try {
1275
- await this.postgresClient.upsertAccount(
1276
- {
1277
- id: account.id,
1278
- raw_data: account
1279
- },
1280
- apiKeyHash
1281
- );
1282
- } catch (error) {
1283
- this.config.logger?.error(error, "Failed to upsert account to database");
1284
- const errorMessage = error instanceof Error ? error.message : "Unknown error";
1285
- throw new Error(`Failed to upsert account to database: ${errorMessage}`);
1286
- }
1287
- }
1288
- /**
1289
- * Get the current account being synced. Uses database lookup by API key hash,
1290
- * with fallback to Stripe API if not found (first-time setup or new API key).
1291
- * @param objectAccountId - Optional account ID from event data (Connect scenarios)
1292
- */
1293
- async getCurrentAccount(objectAccountId) {
1294
- const apiKeyHash = hashApiKey(this.config.stripeSecretKey);
1295
- try {
1296
- const account = await this.postgresClient.getAccountByApiKeyHash(apiKeyHash);
1297
- if (account) {
1298
- return account;
1299
- }
1300
- } catch (error) {
1301
- this.config.logger?.warn(
1302
- error,
1303
- "Failed to lookup account by API key hash, falling back to API"
1304
- );
1305
- }
1306
- try {
1307
- const accountIdParam = objectAccountId || this.config.stripeAccountId;
1308
- const account = accountIdParam ? await this.stripe.accounts.retrieve(accountIdParam) : await this.stripe.accounts.retrieve();
1309
- await this.upsertAccount(account, apiKeyHash);
1310
- return account;
1311
- } catch (error) {
1312
- this.config.logger?.error(error, "Failed to retrieve account from Stripe API");
1313
- return null;
1314
- }
1315
- }
1316
- /**
1317
- * Get all accounts that have been synced to the database
1318
- */
1319
- async getAllSyncedAccounts() {
1320
- try {
1321
- const accountsData = await this.postgresClient.getAllAccounts();
1322
- return accountsData;
1323
- } catch (error) {
1324
- this.config.logger?.error(error, "Failed to retrieve accounts from database");
1325
- throw new Error("Failed to retrieve synced accounts from database");
1326
- }
1327
- }
1328
- /**
1329
- * DANGEROUS: Delete an account and all associated data from the database
1330
- * This operation cannot be undone!
1331
- *
1332
- * @param accountId - The Stripe account ID to delete
1333
- * @param options - Options for deletion behavior
1334
- * @param options.dryRun - If true, only count records without deleting (default: false)
1335
- * @param options.useTransaction - If true, use transaction for atomic deletion (default: true)
1336
- * @returns Deletion summary with counts and warnings
1337
- */
1338
- async dangerouslyDeleteSyncedAccountData(accountId, options) {
1339
- const dryRun = options?.dryRun ?? false;
1340
- const useTransaction = options?.useTransaction ?? true;
1341
- this.config.logger?.info(
1342
- `${dryRun ? "Preview" : "Deleting"} account ${accountId} (transaction: ${useTransaction})`
1343
- );
1344
- try {
1345
- const counts = await this.postgresClient.getAccountRecordCounts(accountId);
1346
- const warnings = [];
1347
- let totalRecords = 0;
1348
- for (const [table, count] of Object.entries(counts)) {
1349
- if (count > 0) {
1350
- totalRecords += count;
1351
- warnings.push(`Will delete ${count} ${table} record${count !== 1 ? "s" : ""}`);
1352
- }
1353
- }
1354
- if (totalRecords > 1e5) {
1355
- warnings.push(
1356
- `Large dataset detected (${totalRecords} total records). Consider using useTransaction: false for better performance.`
1357
- );
1358
- }
1359
- if (dryRun) {
1360
- this.config.logger?.info(`Dry-run complete: ${totalRecords} total records would be deleted`);
1361
- return {
1362
- deletedAccountId: accountId,
1363
- deletedRecordCounts: counts,
1364
- warnings
1365
- };
1366
- }
1367
- const deletionCounts = await this.postgresClient.deleteAccountWithCascade(
1368
- accountId,
1369
- useTransaction
1370
- );
1371
- this.config.logger?.info(
1372
- `Successfully deleted account ${accountId} with ${totalRecords} total records`
1373
- );
1374
- return {
1375
- deletedAccountId: accountId,
1376
- deletedRecordCounts: deletionCounts,
1377
- warnings
1378
- };
1379
- } catch (error) {
1380
- this.config.logger?.error(error, `Failed to delete account ${accountId}`);
1381
- const errorMessage = error instanceof Error ? error.message : "Unknown error";
1382
- throw new Error(`Failed to delete account ${accountId}: ${errorMessage}`);
1383
- }
1384
- }
1385
- async processWebhook(payload, signature) {
1386
- let webhookSecret = this.config.stripeWebhookSecret;
1387
- if (!webhookSecret) {
1388
- const accountId = await this.getAccountId();
1389
- const result = await this.postgresClient.query(
1390
- `SELECT secret FROM "stripe"."_managed_webhooks" WHERE account_id = $1 LIMIT 1`,
1391
- [accountId]
1392
- );
1393
- if (result.rows.length > 0) {
1394
- webhookSecret = result.rows[0].secret;
1395
- }
1396
- }
1397
- if (!webhookSecret) {
1398
- throw new Error(
1399
- "No webhook secret provided. Either create a managed webhook or configure stripeWebhookSecret."
1400
- );
1401
- }
1402
- const event = await this.stripe.webhooks.constructEventAsync(payload, signature, webhookSecret);
1403
- return this.processEvent(event);
1404
- }
1405
- // Event handler registry - maps event types to handler functions
1406
- // Note: Uses 'any' for event parameter to allow handlers with specific Stripe event types
1407
- // (e.g., CustomerDeletedEvent, ProductDeletedEvent) which TypeScript won't accept
1408
- // as contravariant parameters when using the base Stripe.Event type
1409
- eventHandlers = {
1410
- "charge.captured": this.handleChargeEvent.bind(this),
1411
- "charge.expired": this.handleChargeEvent.bind(this),
1412
- "charge.failed": this.handleChargeEvent.bind(this),
1413
- "charge.pending": this.handleChargeEvent.bind(this),
1414
- "charge.refunded": this.handleChargeEvent.bind(this),
1415
- "charge.succeeded": this.handleChargeEvent.bind(this),
1416
- "charge.updated": this.handleChargeEvent.bind(this),
1417
- "customer.deleted": this.handleCustomerDeletedEvent.bind(this),
1418
- "customer.created": this.handleCustomerEvent.bind(this),
1419
- "customer.updated": this.handleCustomerEvent.bind(this),
1420
- "checkout.session.async_payment_failed": this.handleCheckoutSessionEvent.bind(this),
1421
- "checkout.session.async_payment_succeeded": this.handleCheckoutSessionEvent.bind(this),
1422
- "checkout.session.completed": this.handleCheckoutSessionEvent.bind(this),
1423
- "checkout.session.expired": this.handleCheckoutSessionEvent.bind(this),
1424
- "customer.subscription.created": this.handleSubscriptionEvent.bind(this),
1425
- "customer.subscription.deleted": this.handleSubscriptionEvent.bind(this),
1426
- "customer.subscription.paused": this.handleSubscriptionEvent.bind(this),
1427
- "customer.subscription.pending_update_applied": this.handleSubscriptionEvent.bind(this),
1428
- "customer.subscription.pending_update_expired": this.handleSubscriptionEvent.bind(this),
1429
- "customer.subscription.trial_will_end": this.handleSubscriptionEvent.bind(this),
1430
- "customer.subscription.resumed": this.handleSubscriptionEvent.bind(this),
1431
- "customer.subscription.updated": this.handleSubscriptionEvent.bind(this),
1432
- "customer.tax_id.updated": this.handleTaxIdEvent.bind(this),
1433
- "customer.tax_id.created": this.handleTaxIdEvent.bind(this),
1434
- "customer.tax_id.deleted": this.handleTaxIdDeletedEvent.bind(this),
1435
- "invoice.created": this.handleInvoiceEvent.bind(this),
1436
- "invoice.deleted": this.handleInvoiceEvent.bind(this),
1437
- "invoice.finalized": this.handleInvoiceEvent.bind(this),
1438
- "invoice.finalization_failed": this.handleInvoiceEvent.bind(this),
1439
- "invoice.paid": this.handleInvoiceEvent.bind(this),
1440
- "invoice.payment_action_required": this.handleInvoiceEvent.bind(this),
1441
- "invoice.payment_failed": this.handleInvoiceEvent.bind(this),
1442
- "invoice.payment_succeeded": this.handleInvoiceEvent.bind(this),
1443
- "invoice.upcoming": this.handleInvoiceEvent.bind(this),
1444
- "invoice.sent": this.handleInvoiceEvent.bind(this),
1445
- "invoice.voided": this.handleInvoiceEvent.bind(this),
1446
- "invoice.marked_uncollectible": this.handleInvoiceEvent.bind(this),
1447
- "invoice.updated": this.handleInvoiceEvent.bind(this),
1448
- "product.created": this.handleProductEvent.bind(this),
1449
- "product.updated": this.handleProductEvent.bind(this),
1450
- "product.deleted": this.handleProductDeletedEvent.bind(this),
1451
- "price.created": this.handlePriceEvent.bind(this),
1452
- "price.updated": this.handlePriceEvent.bind(this),
1453
- "price.deleted": this.handlePriceDeletedEvent.bind(this),
1454
- "plan.created": this.handlePlanEvent.bind(this),
1455
- "plan.updated": this.handlePlanEvent.bind(this),
1456
- "plan.deleted": this.handlePlanDeletedEvent.bind(this),
1457
- "setup_intent.canceled": this.handleSetupIntentEvent.bind(this),
1458
- "setup_intent.created": this.handleSetupIntentEvent.bind(this),
1459
- "setup_intent.requires_action": this.handleSetupIntentEvent.bind(this),
1460
- "setup_intent.setup_failed": this.handleSetupIntentEvent.bind(this),
1461
- "setup_intent.succeeded": this.handleSetupIntentEvent.bind(this),
1462
- "subscription_schedule.aborted": this.handleSubscriptionScheduleEvent.bind(this),
1463
- "subscription_schedule.canceled": this.handleSubscriptionScheduleEvent.bind(this),
1464
- "subscription_schedule.completed": this.handleSubscriptionScheduleEvent.bind(this),
1465
- "subscription_schedule.created": this.handleSubscriptionScheduleEvent.bind(this),
1466
- "subscription_schedule.expiring": this.handleSubscriptionScheduleEvent.bind(this),
1467
- "subscription_schedule.released": this.handleSubscriptionScheduleEvent.bind(this),
1468
- "subscription_schedule.updated": this.handleSubscriptionScheduleEvent.bind(this),
1469
- "payment_method.attached": this.handlePaymentMethodEvent.bind(this),
1470
- "payment_method.automatically_updated": this.handlePaymentMethodEvent.bind(this),
1471
- "payment_method.detached": this.handlePaymentMethodEvent.bind(this),
1472
- "payment_method.updated": this.handlePaymentMethodEvent.bind(this),
1473
- "charge.dispute.created": this.handleDisputeEvent.bind(this),
1474
- "charge.dispute.funds_reinstated": this.handleDisputeEvent.bind(this),
1475
- "charge.dispute.funds_withdrawn": this.handleDisputeEvent.bind(this),
1476
- "charge.dispute.updated": this.handleDisputeEvent.bind(this),
1477
- "charge.dispute.closed": this.handleDisputeEvent.bind(this),
1478
- "payment_intent.amount_capturable_updated": this.handlePaymentIntentEvent.bind(this),
1479
- "payment_intent.canceled": this.handlePaymentIntentEvent.bind(this),
1480
- "payment_intent.created": this.handlePaymentIntentEvent.bind(this),
1481
- "payment_intent.partially_funded": this.handlePaymentIntentEvent.bind(this),
1482
- "payment_intent.payment_failed": this.handlePaymentIntentEvent.bind(this),
1483
- "payment_intent.processing": this.handlePaymentIntentEvent.bind(this),
1484
- "payment_intent.requires_action": this.handlePaymentIntentEvent.bind(this),
1485
- "payment_intent.succeeded": this.handlePaymentIntentEvent.bind(this),
1486
- "credit_note.created": this.handleCreditNoteEvent.bind(this),
1487
- "credit_note.updated": this.handleCreditNoteEvent.bind(this),
1488
- "credit_note.voided": this.handleCreditNoteEvent.bind(this),
1489
- "radar.early_fraud_warning.created": this.handleEarlyFraudWarningEvent.bind(this),
1490
- "radar.early_fraud_warning.updated": this.handleEarlyFraudWarningEvent.bind(this),
1491
- "refund.created": this.handleRefundEvent.bind(this),
1492
- "refund.failed": this.handleRefundEvent.bind(this),
1493
- "refund.updated": this.handleRefundEvent.bind(this),
1494
- "charge.refund.updated": this.handleRefundEvent.bind(this),
1495
- "review.closed": this.handleReviewEvent.bind(this),
1496
- "review.opened": this.handleReviewEvent.bind(this),
1497
- "entitlements.active_entitlement_summary.updated": this.handleEntitlementSummaryEvent.bind(this)
1498
- };
1499
- // Resource registry - maps SyncObject → list/upsert operations for processNext()
1500
- // Complements eventHandlers which maps event types → handlers for webhooks
1501
- // Both registries share the same underlying upsert methods
1502
- // Order field determines backfill sequence - parents before children for FK dependencies
1503
- resourceRegistry = {
1504
- product: {
1505
- order: 1,
1506
- // No dependencies
1507
- listFn: (p) => this.stripe.products.list(p),
1508
- upsertFn: (items, id) => this.upsertProducts(items, id),
1509
- supportsCreatedFilter: true
1510
- },
1511
- price: {
1512
- order: 2,
1513
- // Depends on product
1514
- listFn: (p) => this.stripe.prices.list(p),
1515
- upsertFn: (items, id, bf) => this.upsertPrices(items, id, bf),
1516
- supportsCreatedFilter: true
1517
- },
1518
- plan: {
1519
- order: 3,
1520
- // Depends on product
1521
- listFn: (p) => this.stripe.plans.list(p),
1522
- upsertFn: (items, id, bf) => this.upsertPlans(items, id, bf),
1523
- supportsCreatedFilter: true
1524
- },
1525
- customer: {
1526
- order: 4,
1527
- // No dependencies
1528
- listFn: (p) => this.stripe.customers.list(p),
1529
- upsertFn: (items, id) => this.upsertCustomers(items, id),
1530
- supportsCreatedFilter: true
1531
- },
1532
- subscription: {
1533
- order: 5,
1534
- // Depends on customer, price
1535
- listFn: (p) => this.stripe.subscriptions.list(p),
1536
- upsertFn: (items, id, bf) => this.upsertSubscriptions(items, id, bf),
1537
- supportsCreatedFilter: true
1538
- },
1539
- subscription_schedules: {
1540
- order: 6,
1541
- // Depends on customer
1542
- listFn: (p) => this.stripe.subscriptionSchedules.list(p),
1543
- upsertFn: (items, id, bf) => this.upsertSubscriptionSchedules(items, id, bf),
1544
- supportsCreatedFilter: true
1545
- },
1546
- invoice: {
1547
- order: 7,
1548
- // Depends on customer, subscription
1549
- listFn: (p) => this.stripe.invoices.list(p),
1550
- upsertFn: (items, id, bf) => this.upsertInvoices(items, id, bf),
1551
- supportsCreatedFilter: true
1552
- },
1553
- charge: {
1554
- order: 8,
1555
- // Depends on customer, invoice
1556
- listFn: (p) => this.stripe.charges.list(p),
1557
- upsertFn: (items, id, bf) => this.upsertCharges(items, id, bf),
1558
- supportsCreatedFilter: true
1559
- },
1560
- setup_intent: {
1561
- order: 9,
1562
- // Depends on customer
1563
- listFn: (p) => this.stripe.setupIntents.list(p),
1564
- upsertFn: (items, id, bf) => this.upsertSetupIntents(items, id, bf),
1565
- supportsCreatedFilter: true
1566
- },
1567
- payment_method: {
1568
- order: 10,
1569
- // Depends on customer (special: iterates customers)
1570
- listFn: (p) => this.stripe.paymentMethods.list(p),
1571
- upsertFn: (items, id, bf) => this.upsertPaymentMethods(items, id, bf),
1572
- supportsCreatedFilter: false
1573
- // Requires customer param, can't filter by created
1574
- },
1575
- payment_intent: {
1576
- order: 11,
1577
- // Depends on customer
1578
- listFn: (p) => this.stripe.paymentIntents.list(p),
1579
- upsertFn: (items, id, bf) => this.upsertPaymentIntents(items, id, bf),
1580
- supportsCreatedFilter: true
1581
- },
1582
- tax_id: {
1583
- order: 12,
1584
- // Depends on customer
1585
- listFn: (p) => this.stripe.taxIds.list(p),
1586
- upsertFn: (items, id, bf) => this.upsertTaxIds(items, id, bf),
1587
- supportsCreatedFilter: false
1588
- // taxIds don't support created filter
1589
- },
1590
- credit_note: {
1591
- order: 13,
1592
- // Depends on invoice
1593
- listFn: (p) => this.stripe.creditNotes.list(p),
1594
- upsertFn: (items, id, bf) => this.upsertCreditNotes(items, id, bf),
1595
- supportsCreatedFilter: true
1596
- // credit_notes support created filter
1597
- },
1598
- dispute: {
1599
- order: 14,
1600
- // Depends on charge
1601
- listFn: (p) => this.stripe.disputes.list(p),
1602
- upsertFn: (items, id, bf) => this.upsertDisputes(items, id, bf),
1603
- supportsCreatedFilter: true
1604
- },
1605
- early_fraud_warning: {
1606
- order: 15,
1607
- // Depends on charge
1608
- listFn: (p) => this.stripe.radar.earlyFraudWarnings.list(p),
1609
- upsertFn: (items, id) => this.upsertEarlyFraudWarning(items, id),
1610
- supportsCreatedFilter: true
1611
- },
1612
- refund: {
1613
- order: 16,
1614
- // Depends on charge
1615
- listFn: (p) => this.stripe.refunds.list(p),
1616
- upsertFn: (items, id, bf) => this.upsertRefunds(items, id, bf),
1617
- supportsCreatedFilter: true
1618
- },
1619
- checkout_sessions: {
1620
- order: 17,
1621
- // Depends on customer (optional)
1622
- listFn: (p) => this.stripe.checkout.sessions.list(p),
1623
- upsertFn: (items, id) => this.upsertCheckoutSessions(items, id),
1624
- supportsCreatedFilter: true
1625
- },
1626
- // Sigma-backed resources
1627
- subscription_item_change_events_v2_beta: {
1628
- order: 18,
1629
- supportsCreatedFilter: false,
1630
- sigma: SIGMA_INGESTION_CONFIGS.subscription_item_change_events_v2_beta
1631
- },
1632
- exchange_rates_from_usd: {
1633
- order: 19,
1634
- supportsCreatedFilter: false,
1635
- sigma: SIGMA_INGESTION_CONFIGS.exchange_rates_from_usd
1636
- }
1637
- };
1638
- async processEvent(event) {
1639
- const objectAccountId = event.data?.object && typeof event.data.object === "object" && "account" in event.data.object ? event.data.object.account : void 0;
1640
- const accountId = await this.getAccountId(objectAccountId);
1641
- await this.getCurrentAccount();
1642
- const handler = this.eventHandlers[event.type];
1643
- if (handler) {
1644
- const entityId = event.data?.object && typeof event.data.object === "object" && "id" in event.data.object ? event.data.object.id : "unknown";
1645
- this.config.logger?.info(`Received webhook ${event.id}: ${event.type} for ${entityId}`);
1646
- await handler(event, accountId);
1647
- } else {
1648
- this.config.logger?.warn(
1649
- `Received unhandled webhook event: ${event.type} (${event.id}). Ignoring.`
1650
- );
1651
- }
1652
- }
1653
- /**
1654
- * Returns an array of all webhook event types that this sync engine can handle.
1655
- * Useful for configuring webhook endpoints with specific event subscriptions.
1656
- */
1657
- getSupportedEventTypes() {
1658
- return Object.keys(
1659
- this.eventHandlers
1660
- ).sort();
1661
- }
1662
- /**
1663
- * Returns an array of all object types that can be synced via processNext/processUntilDone.
1664
- * Ordered for backfill: parents before children (products before prices, customers before subscriptions).
1665
- * Order is determined by the `order` field in resourceRegistry.
1666
- */
1667
- getSupportedSyncObjects() {
1668
- const all = Object.entries(this.resourceRegistry).sort(([, a], [, b]) => a.order - b.order).map(([key]) => key);
1669
- if (!this.config.enableSigma) {
1670
- return all.filter(
1671
- (o) => o !== "subscription_item_change_events_v2_beta" && o !== "exchange_rates_from_usd"
1672
- );
1673
- }
1674
- return all;
1675
- }
1676
- // Event handler methods
1677
- async handleChargeEvent(event, accountId) {
1678
- const { entity: charge, refetched } = await this.fetchOrUseWebhookData(
1679
- event.data.object,
1680
- (id) => this.stripe.charges.retrieve(id),
1681
- (charge2) => charge2.status === "failed" || charge2.status === "succeeded"
1682
- );
1683
- await this.upsertCharges([charge], accountId, false, this.getSyncTimestamp(event, refetched));
1684
- }
1685
- async handleCustomerDeletedEvent(event, accountId) {
1686
- const customer = {
1687
- id: event.data.object.id,
1688
- object: "customer",
1689
- deleted: true
1690
- };
1691
- await this.upsertCustomers([customer], accountId, this.getSyncTimestamp(event, false));
1692
- }
1693
- async handleCustomerEvent(event, accountId) {
1694
- const { entity: customer, refetched } = await this.fetchOrUseWebhookData(
1695
- event.data.object,
1696
- (id) => this.stripe.customers.retrieve(id),
1697
- (customer2) => customer2.deleted === true
1698
- );
1699
- await this.upsertCustomers([customer], accountId, this.getSyncTimestamp(event, refetched));
1700
- }
1701
- async handleCheckoutSessionEvent(event, accountId) {
1702
- const { entity: checkoutSession, refetched } = await this.fetchOrUseWebhookData(
1703
- event.data.object,
1704
- (id) => this.stripe.checkout.sessions.retrieve(id)
1705
- );
1706
- await this.upsertCheckoutSessions(
1707
- [checkoutSession],
1708
- accountId,
1709
- false,
1710
- this.getSyncTimestamp(event, refetched)
1711
- );
1712
- }
1713
- async handleSubscriptionEvent(event, accountId) {
1714
- const { entity: subscription, refetched } = await this.fetchOrUseWebhookData(
1715
- event.data.object,
1716
- (id) => this.stripe.subscriptions.retrieve(id),
1717
- (subscription2) => subscription2.status === "canceled" || subscription2.status === "incomplete_expired"
1718
- );
1719
- await this.upsertSubscriptions(
1720
- [subscription],
1721
- accountId,
1722
- false,
1723
- this.getSyncTimestamp(event, refetched)
1724
- );
1725
- }
1726
- async handleTaxIdEvent(event, accountId) {
1727
- const { entity: taxId, refetched } = await this.fetchOrUseWebhookData(
1728
- event.data.object,
1729
- (id) => this.stripe.taxIds.retrieve(id)
1730
- );
1731
- await this.upsertTaxIds([taxId], accountId, false, this.getSyncTimestamp(event, refetched));
1732
- }
1733
- async handleTaxIdDeletedEvent(event, _accountId) {
1734
- const taxId = event.data.object;
1735
- await this.deleteTaxId(taxId.id);
1736
- }
1737
- async handleInvoiceEvent(event, accountId) {
1738
- const { entity: invoice, refetched } = await this.fetchOrUseWebhookData(
1739
- event.data.object,
1740
- (id) => this.stripe.invoices.retrieve(id),
1741
- (invoice2) => invoice2.status === "void"
1742
- );
1743
- await this.upsertInvoices([invoice], accountId, false, this.getSyncTimestamp(event, refetched));
1744
- }
1745
- async handleProductEvent(event, accountId) {
1746
- try {
1747
- const { entity: product, refetched } = await this.fetchOrUseWebhookData(
1748
- event.data.object,
1749
- (id) => this.stripe.products.retrieve(id)
1750
- );
1751
- await this.upsertProducts([product], accountId, this.getSyncTimestamp(event, refetched));
1752
- } catch (err) {
1753
- if (err instanceof Stripe3.errors.StripeAPIError && err.code === "resource_missing") {
1754
- const product = event.data.object;
1755
- await this.deleteProduct(product.id);
1756
- } else {
1757
- throw err;
1758
- }
1759
- }
1760
- }
1761
- async handleProductDeletedEvent(event, _accountId) {
1762
- const product = event.data.object;
1763
- await this.deleteProduct(product.id);
1764
- }
1765
- async handlePriceEvent(event, accountId) {
1766
- try {
1767
- const { entity: price, refetched } = await this.fetchOrUseWebhookData(
1768
- event.data.object,
1769
- (id) => this.stripe.prices.retrieve(id)
1770
- );
1771
- await this.upsertPrices([price], accountId, false, this.getSyncTimestamp(event, refetched));
1772
- } catch (err) {
1773
- if (err instanceof Stripe3.errors.StripeAPIError && err.code === "resource_missing") {
1774
- const price = event.data.object;
1775
- await this.deletePrice(price.id);
1776
- } else {
1777
- throw err;
1778
- }
1779
- }
1780
- }
1781
- async handlePriceDeletedEvent(event, _accountId) {
1782
- const price = event.data.object;
1783
- await this.deletePrice(price.id);
1784
- }
1785
- async handlePlanEvent(event, accountId) {
1786
- try {
1787
- const { entity: plan, refetched } = await this.fetchOrUseWebhookData(
1788
- event.data.object,
1789
- (id) => this.stripe.plans.retrieve(id)
1790
- );
1791
- await this.upsertPlans([plan], accountId, false, this.getSyncTimestamp(event, refetched));
1792
- } catch (err) {
1793
- if (err instanceof Stripe3.errors.StripeAPIError && err.code === "resource_missing") {
1794
- const plan = event.data.object;
1795
- await this.deletePlan(plan.id);
1796
- } else {
1797
- throw err;
1798
- }
1799
- }
1800
- }
1801
- async handlePlanDeletedEvent(event, _accountId) {
1802
- const plan = event.data.object;
1803
- await this.deletePlan(plan.id);
1804
- }
1805
- async handleSetupIntentEvent(event, accountId) {
1806
- const { entity: setupIntent, refetched } = await this.fetchOrUseWebhookData(
1807
- event.data.object,
1808
- (id) => this.stripe.setupIntents.retrieve(id),
1809
- (setupIntent2) => setupIntent2.status === "canceled" || setupIntent2.status === "succeeded"
1810
- );
1811
- await this.upsertSetupIntents(
1812
- [setupIntent],
1813
- accountId,
1814
- false,
1815
- this.getSyncTimestamp(event, refetched)
1816
- );
1817
- }
1818
- async handleSubscriptionScheduleEvent(event, accountId) {
1819
- const { entity: subscriptionSchedule, refetched } = await this.fetchOrUseWebhookData(
1820
- event.data.object,
1821
- (id) => this.stripe.subscriptionSchedules.retrieve(id),
1822
- (schedule) => schedule.status === "canceled" || schedule.status === "completed"
1823
- );
1824
- await this.upsertSubscriptionSchedules(
1825
- [subscriptionSchedule],
1826
- accountId,
1827
- false,
1828
- this.getSyncTimestamp(event, refetched)
1829
- );
1830
- }
1831
- async handlePaymentMethodEvent(event, accountId) {
1832
- const { entity: paymentMethod, refetched } = await this.fetchOrUseWebhookData(
1833
- event.data.object,
1834
- (id) => this.stripe.paymentMethods.retrieve(id)
1835
- );
1836
- await this.upsertPaymentMethods(
1837
- [paymentMethod],
1838
- accountId,
1839
- false,
1840
- this.getSyncTimestamp(event, refetched)
1841
- );
1842
- }
1843
- async handleDisputeEvent(event, accountId) {
1844
- const { entity: dispute, refetched } = await this.fetchOrUseWebhookData(
1845
- event.data.object,
1846
- (id) => this.stripe.disputes.retrieve(id),
1847
- (dispute2) => dispute2.status === "won" || dispute2.status === "lost"
1848
- );
1849
- await this.upsertDisputes([dispute], accountId, false, this.getSyncTimestamp(event, refetched));
1850
- }
1851
- async handlePaymentIntentEvent(event, accountId) {
1852
- const { entity: paymentIntent, refetched } = await this.fetchOrUseWebhookData(
1853
- event.data.object,
1854
- (id) => this.stripe.paymentIntents.retrieve(id),
1855
- // Final states - do not re-fetch from API
1856
- (entity) => entity.status === "canceled" || entity.status === "succeeded"
1857
- );
1858
- await this.upsertPaymentIntents(
1859
- [paymentIntent],
1860
- accountId,
1861
- false,
1862
- this.getSyncTimestamp(event, refetched)
1863
- );
1864
- }
1865
- async handleCreditNoteEvent(event, accountId) {
1866
- const { entity: creditNote, refetched } = await this.fetchOrUseWebhookData(
1867
- event.data.object,
1868
- (id) => this.stripe.creditNotes.retrieve(id),
1869
- (creditNote2) => creditNote2.status === "void"
1870
- );
1871
- await this.upsertCreditNotes(
1872
- [creditNote],
1873
- accountId,
1874
- false,
1875
- this.getSyncTimestamp(event, refetched)
1876
- );
1877
- }
1878
- async handleEarlyFraudWarningEvent(event, accountId) {
1879
- const { entity: earlyFraudWarning, refetched } = await this.fetchOrUseWebhookData(
1880
- event.data.object,
1881
- (id) => this.stripe.radar.earlyFraudWarnings.retrieve(id)
1882
- );
1883
- await this.upsertEarlyFraudWarning(
1884
- [earlyFraudWarning],
1885
- accountId,
1886
- false,
1887
- this.getSyncTimestamp(event, refetched)
1888
- );
1889
- }
1890
- async handleRefundEvent(event, accountId) {
1891
- const { entity: refund, refetched } = await this.fetchOrUseWebhookData(
1892
- event.data.object,
1893
- (id) => this.stripe.refunds.retrieve(id)
1894
- );
1895
- await this.upsertRefunds([refund], accountId, false, this.getSyncTimestamp(event, refetched));
1896
- }
1897
- async handleReviewEvent(event, accountId) {
1898
- const { entity: review, refetched } = await this.fetchOrUseWebhookData(
1899
- event.data.object,
1900
- (id) => this.stripe.reviews.retrieve(id)
1901
- );
1902
- await this.upsertReviews([review], accountId, false, this.getSyncTimestamp(event, refetched));
1903
- }
1904
- async handleEntitlementSummaryEvent(event, accountId) {
1905
- const activeEntitlementSummary = event.data.object;
1906
- let entitlements = activeEntitlementSummary.entitlements;
1907
- let refetched = false;
1908
- if (this.config.revalidateObjectsViaStripeApi?.includes("entitlements")) {
1909
- const { lastResponse, ...rest } = await this.stripe.entitlements.activeEntitlements.list({
1910
- customer: activeEntitlementSummary.customer
1911
- });
1912
- entitlements = rest;
1913
- refetched = true;
1914
- }
1915
- await this.deleteRemovedActiveEntitlements(
1916
- activeEntitlementSummary.customer,
1917
- entitlements.data.map((entitlement) => entitlement.id)
1918
- );
1919
- await this.upsertActiveEntitlements(
1920
- activeEntitlementSummary.customer,
1921
- entitlements.data,
1922
- accountId,
1923
- false,
1924
- this.getSyncTimestamp(event, refetched)
1925
- );
1926
- }
1927
- getSyncTimestamp(event, refetched) {
1928
- return refetched ? (/* @__PURE__ */ new Date()).toISOString() : new Date(event.created * 1e3).toISOString();
1929
- }
1930
- shouldRefetchEntity(entity) {
1931
- return this.config.revalidateObjectsViaStripeApi?.includes(entity.object);
1932
- }
1933
- async fetchOrUseWebhookData(entity, fetchFn, entityInFinalState) {
1934
- if (!entity.id) return { entity, refetched: false };
1935
- if (entityInFinalState && entityInFinalState(entity)) return { entity, refetched: false };
1936
- if (this.shouldRefetchEntity(entity)) {
1937
- const fetchedEntity = await fetchFn(entity.id);
1938
- return { entity: fetchedEntity, refetched: true };
1939
- }
1940
- return { entity, refetched: false };
1941
- }
1942
- async syncSingleEntity(stripeId) {
1943
- const accountId = await this.getAccountId();
1944
- if (stripeId.startsWith("cus_")) {
1945
- return this.stripe.customers.retrieve(stripeId).then((it) => {
1946
- if (!it || it.deleted) return;
1947
- return this.upsertCustomers([it], accountId);
1948
- });
1949
- } else if (stripeId.startsWith("in_")) {
1950
- return this.stripe.invoices.retrieve(stripeId).then((it) => this.upsertInvoices([it], accountId));
1951
- } else if (stripeId.startsWith("price_")) {
1952
- return this.stripe.prices.retrieve(stripeId).then((it) => this.upsertPrices([it], accountId));
1953
- } else if (stripeId.startsWith("prod_")) {
1954
- return this.stripe.products.retrieve(stripeId).then((it) => this.upsertProducts([it], accountId));
1955
- } else if (stripeId.startsWith("sub_")) {
1956
- return this.stripe.subscriptions.retrieve(stripeId).then((it) => this.upsertSubscriptions([it], accountId));
1957
- } else if (stripeId.startsWith("seti_")) {
1958
- return this.stripe.setupIntents.retrieve(stripeId).then((it) => this.upsertSetupIntents([it], accountId));
1959
- } else if (stripeId.startsWith("pm_")) {
1960
- return this.stripe.paymentMethods.retrieve(stripeId).then((it) => this.upsertPaymentMethods([it], accountId));
1961
- } else if (stripeId.startsWith("dp_") || stripeId.startsWith("du_")) {
1962
- return this.stripe.disputes.retrieve(stripeId).then((it) => this.upsertDisputes([it], accountId));
1963
- } else if (stripeId.startsWith("ch_")) {
1964
- return this.stripe.charges.retrieve(stripeId).then((it) => this.upsertCharges([it], accountId, true));
1965
- } else if (stripeId.startsWith("pi_")) {
1966
- return this.stripe.paymentIntents.retrieve(stripeId).then((it) => this.upsertPaymentIntents([it], accountId));
1967
- } else if (stripeId.startsWith("txi_")) {
1968
- return this.stripe.taxIds.retrieve(stripeId).then((it) => this.upsertTaxIds([it], accountId));
1969
- } else if (stripeId.startsWith("cn_")) {
1970
- return this.stripe.creditNotes.retrieve(stripeId).then((it) => this.upsertCreditNotes([it], accountId));
1971
- } else if (stripeId.startsWith("issfr_")) {
1972
- return this.stripe.radar.earlyFraudWarnings.retrieve(stripeId).then((it) => this.upsertEarlyFraudWarning([it], accountId));
1973
- } else if (stripeId.startsWith("prv_")) {
1974
- return this.stripe.reviews.retrieve(stripeId).then((it) => this.upsertReviews([it], accountId));
1975
- } else if (stripeId.startsWith("re_")) {
1976
- return this.stripe.refunds.retrieve(stripeId).then((it) => this.upsertRefunds([it], accountId));
1977
- } else if (stripeId.startsWith("feat_")) {
1978
- return this.stripe.entitlements.features.retrieve(stripeId).then((it) => this.upsertFeatures([it], accountId));
1979
- } else if (stripeId.startsWith("cs_")) {
1980
- return this.stripe.checkout.sessions.retrieve(stripeId).then((it) => this.upsertCheckoutSessions([it], accountId));
1981
- }
1982
- }
1983
- /**
1984
- * Process one page of items for the specified object type.
1985
- * Returns the number of items processed and whether there are more pages.
1986
- *
1987
- * This method is designed for queue-based consumption where each page
1988
- * is processed as a separate job. Uses the observable sync system for tracking.
1989
- *
1990
- * @param object - The Stripe object type to sync (e.g., 'customer', 'product')
1991
- * @param params - Optional parameters for filtering and run context
1992
- * @returns ProcessNextResult with processed count, hasMore flag, and runStartedAt
1993
- *
1994
- * @example
1995
- * ```typescript
1996
- * // Queue worker
1997
- * const { hasMore, runStartedAt } = await stripeSync.processNext('customer')
1998
- * if (hasMore) {
1999
- * await queue.send({ object: 'customer', runStartedAt })
2000
- * }
2001
- * ```
2002
- */
2003
- async processNext(object, params) {
2004
- try {
2005
- await this.getCurrentAccount();
2006
- const accountId = await this.getAccountId();
2007
- const resourceName = this.getResourceName(object);
2008
- let runStartedAt;
2009
- if (params?.runStartedAt) {
2010
- runStartedAt = params.runStartedAt;
2011
- } else {
2012
- const { runKey } = await this.joinOrCreateSyncRun(params?.triggeredBy ?? "processNext");
2013
- runStartedAt = runKey.runStartedAt;
2014
- }
2015
- await this.postgresClient.createObjectRuns(accountId, runStartedAt, [resourceName]);
2016
- const objRun = await this.postgresClient.getObjectRun(accountId, runStartedAt, resourceName);
2017
- if (objRun?.status === "complete" || objRun?.status === "error") {
2018
- return {
2019
- processed: 0,
2020
- hasMore: false,
2021
- runStartedAt
2022
- };
2023
- }
2024
- if (objRun?.status === "pending") {
2025
- const started = await this.postgresClient.tryStartObjectSync(
2026
- accountId,
2027
- runStartedAt,
2028
- resourceName
2029
- );
2030
- if (!started) {
2031
- return {
2032
- processed: 0,
2033
- hasMore: true,
2034
- runStartedAt
2035
- };
2036
- }
2037
- }
2038
- let cursor = null;
2039
- if (!params?.created) {
2040
- const lastCursor = await this.postgresClient.getLastCursorBeforeRun(
2041
- accountId,
2042
- resourceName,
2043
- runStartedAt
2044
- );
2045
- cursor = lastCursor ?? null;
2046
- }
2047
- const result = await this.fetchOnePage(
2048
- object,
2049
- accountId,
2050
- resourceName,
2051
- runStartedAt,
2052
- cursor,
2053
- objRun?.pageCursor ?? null,
2054
- params
2055
- );
2056
- return result;
2057
- } catch (error) {
2058
- throw this.appendMigrationHint(error);
2059
- }
2060
- }
2061
- appendMigrationHint(error) {
2062
- const hint = "Error occurred. Make sure you are up to date with DB migrations which can sometimes help with this. Details:";
2063
- const withHint = (message) => message.includes(hint) ? message : `${hint}
2064
- ${message}`;
2065
- if (error instanceof Error) {
2066
- const { stack } = error;
2067
- error.message = withHint(error.message);
2068
- if (stack) error.stack = stack;
2069
- return error;
2070
- }
2071
- return new Error(withHint(String(error)));
2072
- }
2073
- /**
2074
- * Get the database resource name for a SyncObject type
2075
- */
2076
- getResourceName(object) {
2077
- const mapping = {
2078
- customer: "customers",
2079
- invoice: "invoices",
2080
- price: "prices",
2081
- product: "products",
2082
- subscription: "subscriptions",
2083
- subscription_schedules: "subscription_schedules",
2084
- setup_intent: "setup_intents",
2085
- payment_method: "payment_methods",
2086
- dispute: "disputes",
2087
- charge: "charges",
2088
- payment_intent: "payment_intents",
2089
- plan: "plans",
2090
- tax_id: "tax_ids",
2091
- credit_note: "credit_notes",
2092
- early_fraud_warning: "early_fraud_warnings",
2093
- refund: "refunds",
2094
- checkout_sessions: "checkout_sessions"
2095
- };
2096
- return mapping[object] || object;
2097
- }
2098
- /**
2099
- * Fetch one page of items from Stripe and upsert to database.
2100
- * Uses resourceRegistry for DRY list/upsert operations.
2101
- * Uses the observable sync system for tracking progress.
2102
- */
2103
- async fetchOnePage(object, accountId, resourceName, runStartedAt, cursor, pageCursor, params) {
2104
- const limit = 100;
2105
- if (object === "payment_method" || object === "tax_id") {
2106
- this.config.logger?.warn(`processNext for ${object} requires customer context`);
2107
- await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
2108
- return { processed: 0, hasMore: false, runStartedAt };
2109
- }
2110
- const config = this.resourceRegistry[object];
2111
- if (!config) {
2112
- throw new Error(`Unsupported object type for processNext: ${object}`);
2113
- }
2114
- try {
2115
- if (config.sigma) {
2116
- return await this.fetchOneSigmaPage(
2117
- accountId,
2118
- resourceName,
2119
- runStartedAt,
2120
- cursor,
2121
- config.sigma
2122
- );
2123
- }
2124
- const listParams = { limit };
2125
- if (config.supportsCreatedFilter) {
2126
- const created = params?.created ?? (cursor && /^\d+$/.test(cursor) ? { gte: Number.parseInt(cursor, 10) } : void 0);
2127
- if (created) {
2128
- listParams.created = created;
2129
- }
2130
- }
2131
- if (pageCursor) {
2132
- listParams.starting_after = pageCursor;
2133
- }
2134
- const response = await config.listFn(listParams);
2135
- if (response.data.length === 0 && response.has_more) {
2136
- const message = `Stripe returned has_more=true with empty page for ${resourceName}. Aborting to avoid infinite loop.`;
2137
- this.config.logger?.warn(message);
2138
- await this.postgresClient.failObjectSync(accountId, runStartedAt, resourceName, message);
2139
- return { processed: 0, hasMore: false, runStartedAt };
2140
- }
2141
- if (response.data.length > 0) {
2142
- this.config.logger?.info(`processNext: upserting ${response.data.length} ${resourceName}`);
2143
- await config.upsertFn(response.data, accountId, params?.backfillRelatedEntities);
2144
- await this.postgresClient.incrementObjectProgress(
2145
- accountId,
2146
- runStartedAt,
2147
- resourceName,
2148
- response.data.length
2149
- );
2150
- const maxCreated = Math.max(
2151
- ...response.data.map((i) => i.created || 0)
2152
- );
2153
- if (maxCreated > 0) {
2154
- await this.postgresClient.updateObjectCursor(
2155
- accountId,
2156
- runStartedAt,
2157
- resourceName,
2158
- String(maxCreated)
2159
- );
2160
- }
2161
- const lastId = response.data[response.data.length - 1].id;
2162
- if (response.has_more) {
2163
- await this.postgresClient.updateObjectPageCursor(
2164
- accountId,
2165
- runStartedAt,
2166
- resourceName,
2167
- lastId
2168
- );
2169
- }
2170
- }
2171
- if (!response.has_more) {
2172
- await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
2173
- }
2174
- return {
2175
- processed: response.data.length,
2176
- hasMore: response.has_more,
2177
- runStartedAt
2178
- };
2179
- } catch (error) {
2180
- await this.postgresClient.failObjectSync(
2181
- accountId,
2182
- runStartedAt,
2183
- resourceName,
2184
- error instanceof Error ? error.message : "Unknown error"
2185
- );
2186
- throw error;
2187
- }
2188
- }
2189
- async getSigmaFallbackCursorFromDestination(accountId, sigmaConfig) {
2190
- const cursorCols = sigmaConfig.cursor.columns;
2191
- const selectCols = cursorCols.map((c) => `"${c.column}"`).join(", ");
2192
- const orderBy = cursorCols.map((c) => `"${c.column}" DESC`).join(", ");
2193
- const result = await this.postgresClient.query(
2194
- `SELECT ${selectCols}
2195
- FROM "stripe"."${sigmaConfig.destinationTable}"
2196
- WHERE "_account_id" = $1
2197
- ORDER BY ${orderBy}
2198
- LIMIT 1`,
2199
- [accountId]
2200
- );
2201
- if (result.rows.length === 0) return null;
2202
- const row = result.rows[0];
2203
- const entryForCursor = {};
2204
- for (const c of cursorCols) {
2205
- const v = row[c.column];
2206
- if (v == null) {
2207
- throw new Error(
2208
- `Sigma fallback cursor query returned null for ${sigmaConfig.destinationTable}.${c.column}`
2209
- );
2210
- }
2211
- if (c.type === "timestamp") {
2212
- const d = v instanceof Date ? v : new Date(String(v));
2213
- if (Number.isNaN(d.getTime())) {
2214
- throw new Error(
2215
- `Sigma fallback cursor query returned invalid timestamp for ${sigmaConfig.destinationTable}.${c.column}: ${String(
2216
- v
2217
- )}`
2218
- );
2219
- }
2220
- entryForCursor[c.column] = d.toISOString();
2221
- } else {
2222
- entryForCursor[c.column] = String(v);
2223
- }
2224
- }
2225
- return sigmaCursorFromEntry(sigmaConfig, entryForCursor);
2226
- }
2227
- async fetchOneSigmaPage(accountId, resourceName, runStartedAt, cursor, sigmaConfig) {
2228
- if (!this.config.stripeSecretKey) {
2229
- throw new Error("Sigma sync requested but stripeSecretKey is not configured.");
2230
- }
2231
- if (resourceName !== sigmaConfig.destinationTable) {
2232
- throw new Error(
2233
- `Sigma sync config mismatch: resourceName=${resourceName} destinationTable=${sigmaConfig.destinationTable}`
2234
- );
2235
- }
2236
- const effectiveCursor = cursor ?? await this.getSigmaFallbackCursorFromDestination(accountId, sigmaConfig);
2237
- const sigmaSql = buildSigmaQuery(sigmaConfig, effectiveCursor);
2238
- this.config.logger?.info(
2239
- { object: resourceName, pageSize: sigmaConfig.pageSize, hasCursor: Boolean(effectiveCursor) },
2240
- "Sigma sync: running query"
2241
- );
2242
- const { queryRunId, fileId, csv } = await runSigmaQueryAndDownloadCsv({
2243
- apiKey: this.config.stripeSecretKey,
2244
- sql: sigmaSql,
2245
- logger: this.config.logger
2246
- });
2247
- const rows = parseCsvObjects(csv);
2248
- if (rows.length === 0) {
2249
- await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
2250
- return { processed: 0, hasMore: false, runStartedAt };
2251
- }
2252
- const entries = rows.map(
2253
- (row) => defaultSigmaRowToEntry(sigmaConfig, row)
2254
- );
2255
- this.config.logger?.info(
2256
- { object: resourceName, rows: entries.length, queryRunId, fileId },
2257
- "Sigma sync: upserting rows"
2258
- );
2259
- await this.postgresClient.upsertManyWithTimestampProtection(
2260
- entries,
2261
- resourceName,
2262
- accountId,
2263
- void 0,
2264
- sigmaConfig.upsert
2265
- );
2266
- await this.postgresClient.incrementObjectProgress(
2267
- accountId,
2268
- runStartedAt,
2269
- resourceName,
2270
- entries.length
2271
- );
2272
- const newCursor = sigmaCursorFromEntry(sigmaConfig, entries[entries.length - 1]);
2273
- await this.postgresClient.updateObjectCursor(accountId, runStartedAt, resourceName, newCursor);
2274
- const hasMore = rows.length === sigmaConfig.pageSize;
2275
- if (!hasMore) {
2276
- await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
2277
- }
2278
- return { processed: entries.length, hasMore, runStartedAt };
2279
- }
2280
- /**
2281
- * Process all pages for all (or specified) object types until complete.
2282
- *
2283
- * @param params - Optional parameters for filtering and specifying object types
2284
- * @returns SyncBackfill with counts for each synced resource type
2285
- */
2286
- /**
2287
- * Process all pages for a single object type until complete.
2288
- * Loops processNext() internally until hasMore is false.
2289
- *
2290
- * @param object - The object type to sync
2291
- * @param runStartedAt - The sync run to use (for sharing across objects)
2292
- * @param params - Optional sync parameters
2293
- * @returns Sync result with count of synced items
2294
- */
2295
- async processObjectUntilDone(object, runStartedAt, params) {
2296
- let totalSynced = 0;
2297
- while (true) {
2298
- const result = await this.processNext(object, {
2299
- ...params,
2300
- runStartedAt,
2301
- triggeredBy: "processUntilDone"
2302
- });
2303
- totalSynced += result.processed;
2304
- if (!result.hasMore) {
2305
- break;
2306
- }
2307
- }
2308
- return { synced: totalSynced };
2309
- }
2310
- /**
2311
- * Join existing sync run or create a new one.
2312
- * Returns sync run key and list of supported objects to sync.
2313
- *
2314
- * Cooperative behavior: If a sync run already exists, joins it instead of failing.
2315
- * This is used by workers and background processes that should cooperate.
2316
- *
2317
- * @param triggeredBy - What triggered this sync (for observability)
2318
- * @param objectFilter - Optional specific object to sync (e.g. 'payment_intent'). If 'all' or undefined, syncs all objects.
2319
- * @returns Run key and list of objects to sync
2320
- */
2321
- async joinOrCreateSyncRun(triggeredBy = "worker", objectFilter) {
2322
- await this.getCurrentAccount();
2323
- const accountId = await this.getAccountId();
2324
- const result = await this.postgresClient.getOrCreateSyncRun(accountId, triggeredBy);
2325
- const objects = objectFilter === "all" || objectFilter === void 0 ? this.getSupportedSyncObjects() : [objectFilter];
2326
- if (!result) {
2327
- const activeRun = await this.postgresClient.getActiveSyncRun(accountId);
2328
- if (!activeRun) {
2329
- throw new Error("Failed to get or create sync run");
2330
- }
2331
- await this.postgresClient.createObjectRuns(
2332
- activeRun.accountId,
2333
- activeRun.runStartedAt,
2334
- objects.map((obj) => this.getResourceName(obj))
2335
- );
2336
- return {
2337
- runKey: { accountId: activeRun.accountId, runStartedAt: activeRun.runStartedAt },
2338
- objects
2339
- };
2340
- }
2341
- const { accountId: runAccountId, runStartedAt } = result;
2342
- await this.postgresClient.createObjectRuns(
2343
- runAccountId,
2344
- runStartedAt,
2345
- objects.map((obj) => this.getResourceName(obj))
2346
- );
2347
- return {
2348
- runKey: { accountId: runAccountId, runStartedAt },
2349
- objects
2350
- };
2351
- }
2352
- async processUntilDone(params) {
2353
- const { object } = params ?? { object: "all" };
2354
- const { runKey } = await this.joinOrCreateSyncRun("processUntilDone", object);
2355
- return this.processUntilDoneWithRun(runKey.runStartedAt, object, params);
2356
- }
2357
- /**
2358
- * Internal implementation of processUntilDone with an existing run.
2359
- */
2360
- async processUntilDoneWithRun(runStartedAt, object, params) {
2361
- const accountId = await this.getAccountId();
2362
- const results = {};
2363
- try {
2364
- const objectsToSync = object === "all" || object === void 0 ? this.getSupportedSyncObjects() : [object];
2365
- for (const obj of objectsToSync) {
2366
- this.config.logger?.info(`Syncing ${obj}`);
2367
- if (obj === "payment_method") {
2368
- results.paymentMethods = await this.syncPaymentMethodsWithRun(runStartedAt, params);
2369
- } else {
2370
- const result = await this.processObjectUntilDone(obj, runStartedAt, params);
2371
- switch (obj) {
2372
- case "product":
2373
- results.products = result;
2374
- break;
2375
- case "price":
2376
- results.prices = result;
2377
- break;
2378
- case "plan":
2379
- results.plans = result;
2380
- break;
2381
- case "customer":
2382
- results.customers = result;
2383
- break;
2384
- case "subscription":
2385
- results.subscriptions = result;
2386
- break;
2387
- case "subscription_schedules":
2388
- results.subscriptionSchedules = result;
2389
- break;
2390
- case "invoice":
2391
- results.invoices = result;
2392
- break;
2393
- case "charge":
2394
- results.charges = result;
2395
- break;
2396
- case "setup_intent":
2397
- results.setupIntents = result;
2398
- break;
2399
- case "payment_intent":
2400
- results.paymentIntents = result;
2401
- break;
2402
- case "tax_id":
2403
- results.taxIds = result;
2404
- break;
2405
- case "credit_note":
2406
- results.creditNotes = result;
2407
- break;
2408
- case "dispute":
2409
- results.disputes = result;
2410
- break;
2411
- case "early_fraud_warning":
2412
- results.earlyFraudWarnings = result;
2413
- break;
2414
- case "refund":
2415
- results.refunds = result;
2416
- break;
2417
- case "checkout_sessions":
2418
- results.checkoutSessions = result;
2419
- break;
2420
- case "subscription_item_change_events_v2_beta":
2421
- results.subscriptionItemChangeEventsV2Beta = result;
2422
- break;
2423
- case "exchange_rates_from_usd":
2424
- results.exchangeRatesFromUsd = result;
2425
- break;
2426
- }
2427
- }
2428
- }
2429
- await this.postgresClient.closeSyncRun(accountId, runStartedAt);
2430
- return results;
2431
- } catch (error) {
2432
- await this.postgresClient.closeSyncRun(accountId, runStartedAt);
2433
- throw error;
2434
- }
2435
- }
2436
- /**
2437
- * Sync payment methods with an existing run (special case - iterates customers)
2438
- */
2439
- async syncPaymentMethodsWithRun(runStartedAt, syncParams) {
2440
- const accountId = await this.getAccountId();
2441
- const resourceName = "payment_methods";
2442
- await this.postgresClient.createObjectRuns(accountId, runStartedAt, [resourceName]);
2443
- await this.postgresClient.tryStartObjectSync(accountId, runStartedAt, resourceName);
2444
- try {
2445
- const prepared = sql2(
2446
- `select id from "stripe"."customers" WHERE COALESCE(deleted, false) <> true;`
2447
- )([]);
2448
- const customerIds = await this.postgresClient.query(prepared.text, prepared.values).then(({ rows }) => rows.map((it) => it.id));
2449
- this.config.logger?.info(`Getting payment methods for ${customerIds.length} customers`);
2450
- let synced = 0;
2451
- const chunkSize = this.config.maxConcurrentCustomers ?? 10;
2452
- for (const customerIdChunk of chunkArray(customerIds, chunkSize)) {
2453
- await Promise.all(
2454
- customerIdChunk.map(async (customerId) => {
2455
- const CHECKPOINT_SIZE = 100;
2456
- let currentBatch = [];
2457
- let hasMore = true;
2458
- let startingAfter = void 0;
2459
- while (hasMore) {
2460
- const response = await this.stripe.paymentMethods.list({
2461
- limit: 100,
2462
- customer: customerId,
2463
- ...startingAfter ? { starting_after: startingAfter } : {}
2464
- });
2465
- for (const item of response.data) {
2466
- currentBatch.push(item);
2467
- if (currentBatch.length >= CHECKPOINT_SIZE) {
2468
- await this.upsertPaymentMethods(
2469
- currentBatch,
2470
- accountId,
2471
- syncParams?.backfillRelatedEntities
2472
- );
2473
- synced += currentBatch.length;
2474
- await this.postgresClient.incrementObjectProgress(
2475
- accountId,
2476
- runStartedAt,
2477
- resourceName,
2478
- currentBatch.length
2479
- );
2480
- currentBatch = [];
2481
- }
2482
- }
2483
- hasMore = response.has_more;
2484
- if (response.data.length > 0) {
2485
- startingAfter = response.data[response.data.length - 1].id;
2486
- }
2487
- }
2488
- if (currentBatch.length > 0) {
2489
- await this.upsertPaymentMethods(
2490
- currentBatch,
2491
- accountId,
2492
- syncParams?.backfillRelatedEntities
2493
- );
2494
- synced += currentBatch.length;
2495
- await this.postgresClient.incrementObjectProgress(
2496
- accountId,
2497
- runStartedAt,
2498
- resourceName,
2499
- currentBatch.length
2500
- );
2501
- }
2502
- })
2503
- );
2504
- }
2505
- await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
2506
- return { synced };
2507
- } catch (error) {
2508
- await this.postgresClient.failObjectSync(
2509
- accountId,
2510
- runStartedAt,
2511
- resourceName,
2512
- error instanceof Error ? error.message : "Unknown error"
2513
- );
2514
- throw error;
2515
- }
2516
- }
2517
- async syncProducts(syncParams) {
2518
- this.config.logger?.info("Syncing products");
2519
- return this.withSyncRun("products", "syncProducts", async (cursor, runStartedAt) => {
2520
- const accountId = await this.getAccountId();
2521
- const params = { limit: 100 };
2522
- if (syncParams?.created) {
2523
- params.created = syncParams.created;
2524
- } else if (cursor) {
2525
- params.created = { gte: cursor };
2526
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2527
- }
2528
- return this.fetchAndUpsert(
2529
- (pagination) => this.stripe.products.list({ ...params, ...pagination }),
2530
- (products) => this.upsertProducts(products, accountId),
2531
- accountId,
2532
- "products",
2533
- runStartedAt
2534
- );
2535
- });
2536
- }
2537
- async syncPrices(syncParams) {
2538
- this.config.logger?.info("Syncing prices");
2539
- return this.withSyncRun("prices", "syncPrices", async (cursor, runStartedAt) => {
2540
- const accountId = await this.getAccountId();
2541
- const params = { limit: 100 };
2542
- if (syncParams?.created) {
2543
- params.created = syncParams.created;
2544
- } else if (cursor) {
2545
- params.created = { gte: cursor };
2546
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2547
- }
2548
- return this.fetchAndUpsert(
2549
- (pagination) => this.stripe.prices.list({ ...params, ...pagination }),
2550
- (prices) => this.upsertPrices(prices, accountId, syncParams?.backfillRelatedEntities),
2551
- accountId,
2552
- "prices",
2553
- runStartedAt
2554
- );
2555
- });
2556
- }
2557
- async syncPlans(syncParams) {
2558
- this.config.logger?.info("Syncing plans");
2559
- return this.withSyncRun("plans", "syncPlans", async (cursor, runStartedAt) => {
2560
- const accountId = await this.getAccountId();
2561
- const params = { limit: 100 };
2562
- if (syncParams?.created) {
2563
- params.created = syncParams.created;
2564
- } else if (cursor) {
2565
- params.created = { gte: cursor };
2566
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2567
- }
2568
- return this.fetchAndUpsert(
2569
- (pagination) => this.stripe.plans.list({ ...params, ...pagination }),
2570
- (plans) => this.upsertPlans(plans, accountId, syncParams?.backfillRelatedEntities),
2571
- accountId,
2572
- "plans",
2573
- runStartedAt
2574
- );
2575
- });
2576
- }
2577
- async syncCustomers(syncParams) {
2578
- this.config.logger?.info("Syncing customers");
2579
- return this.withSyncRun("customers", "syncCustomers", async (cursor, runStartedAt) => {
2580
- const accountId = await this.getAccountId();
2581
- const params = { limit: 100 };
2582
- if (syncParams?.created) {
2583
- params.created = syncParams.created;
2584
- } else if (cursor) {
2585
- params.created = { gte: cursor };
2586
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2587
- }
2588
- return this.fetchAndUpsert(
2589
- (pagination) => this.stripe.customers.list({ ...params, ...pagination }),
2590
- // @ts-expect-error
2591
- (items) => this.upsertCustomers(items, accountId),
2592
- accountId,
2593
- "customers",
2594
- runStartedAt
2595
- );
2596
- });
2597
- }
2598
- async syncSubscriptions(syncParams) {
2599
- this.config.logger?.info("Syncing subscriptions");
2600
- return this.withSyncRun("subscriptions", "syncSubscriptions", async (cursor, runStartedAt) => {
2601
- const accountId = await this.getAccountId();
2602
- const params = { status: "all", limit: 100 };
2603
- if (syncParams?.created) {
2604
- params.created = syncParams.created;
2605
- } else if (cursor) {
2606
- params.created = { gte: cursor };
2607
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2608
- }
2609
- return this.fetchAndUpsert(
2610
- (pagination) => this.stripe.subscriptions.list({ ...params, ...pagination }),
2611
- (items) => this.upsertSubscriptions(items, accountId, syncParams?.backfillRelatedEntities),
2612
- accountId,
2613
- "subscriptions",
2614
- runStartedAt
2615
- );
2616
- });
2617
- }
2618
- async syncSubscriptionSchedules(syncParams) {
2619
- this.config.logger?.info("Syncing subscription schedules");
2620
- return this.withSyncRun(
2621
- "subscription_schedules",
2622
- "syncSubscriptionSchedules",
2623
- async (cursor, runStartedAt) => {
2624
- const accountId = await this.getAccountId();
2625
- const params = { limit: 100 };
2626
- if (syncParams?.created) {
2627
- params.created = syncParams.created;
2628
- } else if (cursor) {
2629
- params.created = { gte: cursor };
2630
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2631
- }
2632
- return this.fetchAndUpsert(
2633
- (pagination) => this.stripe.subscriptionSchedules.list({ ...params, ...pagination }),
2634
- (items) => this.upsertSubscriptionSchedules(items, accountId, syncParams?.backfillRelatedEntities),
2635
- accountId,
2636
- "subscription_schedules",
2637
- runStartedAt
2638
- );
2639
- }
2640
- );
2641
- }
2642
- async syncInvoices(syncParams) {
2643
- this.config.logger?.info("Syncing invoices");
2644
- return this.withSyncRun("invoices", "syncInvoices", async (cursor, runStartedAt) => {
2645
- const accountId = await this.getAccountId();
2646
- const params = { limit: 100 };
2647
- if (syncParams?.created) {
2648
- params.created = syncParams.created;
2649
- } else if (cursor) {
2650
- params.created = { gte: cursor };
2651
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2652
- }
2653
- return this.fetchAndUpsert(
2654
- (pagination) => this.stripe.invoices.list({ ...params, ...pagination }),
2655
- (items) => this.upsertInvoices(items, accountId, syncParams?.backfillRelatedEntities),
2656
- accountId,
2657
- "invoices",
2658
- runStartedAt
2659
- );
2660
- });
2661
- }
2662
- async syncCharges(syncParams) {
2663
- this.config.logger?.info("Syncing charges");
2664
- return this.withSyncRun("charges", "syncCharges", async (cursor, runStartedAt) => {
2665
- const accountId = await this.getAccountId();
2666
- const params = { limit: 100 };
2667
- if (syncParams?.created) {
2668
- params.created = syncParams.created;
2669
- } else if (cursor) {
2670
- params.created = { gte: cursor };
2671
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2672
- }
2673
- return this.fetchAndUpsert(
2674
- (pagination) => this.stripe.charges.list({ ...params, ...pagination }),
2675
- (items) => this.upsertCharges(items, accountId, syncParams?.backfillRelatedEntities),
2676
- accountId,
2677
- "charges",
2678
- runStartedAt
2679
- );
2680
- });
2681
- }
2682
- async syncSetupIntents(syncParams) {
2683
- this.config.logger?.info("Syncing setup_intents");
2684
- return this.withSyncRun("setup_intents", "syncSetupIntents", async (cursor, runStartedAt) => {
2685
- const accountId = await this.getAccountId();
2686
- const params = { limit: 100 };
2687
- if (syncParams?.created) {
2688
- params.created = syncParams.created;
2689
- } else if (cursor) {
2690
- params.created = { gte: cursor };
2691
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2692
- }
2693
- return this.fetchAndUpsert(
2694
- (pagination) => this.stripe.setupIntents.list({ ...params, ...pagination }),
2695
- (items) => this.upsertSetupIntents(items, accountId, syncParams?.backfillRelatedEntities),
2696
- accountId,
2697
- "setup_intents",
2698
- runStartedAt
2699
- );
2700
- });
2701
- }
2702
- async syncPaymentIntents(syncParams) {
2703
- this.config.logger?.info("Syncing payment_intents");
2704
- return this.withSyncRun(
2705
- "payment_intents",
2706
- "syncPaymentIntents",
2707
- async (cursor, runStartedAt) => {
2708
- const accountId = await this.getAccountId();
2709
- const params = { limit: 100 };
2710
- if (syncParams?.created) {
2711
- params.created = syncParams.created;
2712
- } else if (cursor) {
2713
- params.created = { gte: cursor };
2714
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2715
- }
2716
- return this.fetchAndUpsert(
2717
- (pagination) => this.stripe.paymentIntents.list({ ...params, ...pagination }),
2718
- (items) => this.upsertPaymentIntents(items, accountId, syncParams?.backfillRelatedEntities),
2719
- accountId,
2720
- "payment_intents",
2721
- runStartedAt
2722
- );
2723
- }
2724
- );
2725
- }
2726
- async syncTaxIds(syncParams) {
2727
- this.config.logger?.info("Syncing tax_ids");
2728
- return this.withSyncRun("tax_ids", "syncTaxIds", async (_cursor, runStartedAt) => {
2729
- const accountId = await this.getAccountId();
2730
- const params = { limit: 100 };
2731
- return this.fetchAndUpsert(
2732
- (pagination) => this.stripe.taxIds.list({ ...params, ...pagination }),
2733
- (items) => this.upsertTaxIds(items, accountId, syncParams?.backfillRelatedEntities),
2734
- accountId,
2735
- "tax_ids",
2736
- runStartedAt
2737
- );
2738
- });
2739
- }
2740
- async syncPaymentMethods(syncParams) {
2741
- this.config.logger?.info("Syncing payment method");
2742
- return this.withSyncRun(
2743
- "payment_methods",
2744
- "syncPaymentMethods",
2745
- async (_cursor, runStartedAt) => {
2746
- const accountId = await this.getAccountId();
2747
- const prepared = sql2(
2748
- `select id from "stripe"."customers" WHERE COALESCE(deleted, false) <> true;`
2749
- )([]);
2750
- const customerIds = await this.postgresClient.query(prepared.text, prepared.values).then(({ rows }) => rows.map((it) => it.id));
2751
- this.config.logger?.info(`Getting payment methods for ${customerIds.length} customers`);
2752
- let synced = 0;
2753
- const chunkSize = this.config.maxConcurrentCustomers ?? 3;
2754
- for (const customerIdChunk of chunkArray(customerIds, chunkSize)) {
2755
- await Promise.all(
2756
- customerIdChunk.map(async (customerId) => {
2757
- const CHECKPOINT_SIZE = 100;
2758
- let currentBatch = [];
2759
- let hasMore = true;
2760
- let startingAfter = void 0;
2761
- while (hasMore) {
2762
- const response = await this.stripe.paymentMethods.list({
2763
- limit: 100,
2764
- customer: customerId,
2765
- ...startingAfter ? { starting_after: startingAfter } : {}
2766
- });
2767
- for (const item of response.data) {
2768
- currentBatch.push(item);
2769
- if (currentBatch.length >= CHECKPOINT_SIZE) {
2770
- await this.upsertPaymentMethods(
2771
- currentBatch,
2772
- accountId,
2773
- syncParams?.backfillRelatedEntities
2774
- );
2775
- synced += currentBatch.length;
2776
- await this.postgresClient.incrementObjectProgress(
2777
- accountId,
2778
- runStartedAt,
2779
- "payment_methods",
2780
- currentBatch.length
2781
- );
2782
- currentBatch = [];
2783
- }
2784
- }
2785
- hasMore = response.has_more;
2786
- if (response.data.length > 0) {
2787
- startingAfter = response.data[response.data.length - 1].id;
2788
- }
2789
- }
2790
- if (currentBatch.length > 0) {
2791
- await this.upsertPaymentMethods(
2792
- currentBatch,
2793
- accountId,
2794
- syncParams?.backfillRelatedEntities
2795
- );
2796
- synced += currentBatch.length;
2797
- await this.postgresClient.incrementObjectProgress(
2798
- accountId,
2799
- runStartedAt,
2800
- "payment_methods",
2801
- currentBatch.length
2802
- );
2803
- }
2804
- })
2805
- );
2806
- }
2807
- await this.postgresClient.completeObjectSync(accountId, runStartedAt, "payment_methods");
2808
- return { synced };
2809
- }
2810
- );
2811
- }
2812
- async syncDisputes(syncParams) {
2813
- this.config.logger?.info("Syncing disputes");
2814
- return this.withSyncRun("disputes", "syncDisputes", async (cursor, runStartedAt) => {
2815
- const accountId = await this.getAccountId();
2816
- const params = { limit: 100 };
2817
- if (syncParams?.created) {
2818
- params.created = syncParams.created;
2819
- } else if (cursor) {
2820
- params.created = { gte: cursor };
2821
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2822
- }
2823
- return this.fetchAndUpsert(
2824
- (pagination) => this.stripe.disputes.list({ ...params, ...pagination }),
2825
- (items) => this.upsertDisputes(items, accountId, syncParams?.backfillRelatedEntities),
2826
- accountId,
2827
- "disputes",
2828
- runStartedAt
2829
- );
2830
- });
2831
- }
2832
- async syncEarlyFraudWarnings(syncParams) {
2833
- this.config.logger?.info("Syncing early fraud warnings");
2834
- return this.withSyncRun(
2835
- "early_fraud_warnings",
2836
- "syncEarlyFraudWarnings",
2837
- async (cursor, runStartedAt) => {
2838
- const accountId = await this.getAccountId();
2839
- const params = { limit: 100 };
2840
- if (syncParams?.created) {
2841
- params.created = syncParams.created;
2842
- } else if (cursor) {
2843
- params.created = { gte: cursor };
2844
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2845
- }
2846
- return this.fetchAndUpsert(
2847
- (pagination) => this.stripe.radar.earlyFraudWarnings.list({ ...params, ...pagination }),
2848
- (items) => this.upsertEarlyFraudWarning(items, accountId, syncParams?.backfillRelatedEntities),
2849
- accountId,
2850
- "early_fraud_warnings",
2851
- runStartedAt
2852
- );
2853
- }
2854
- );
2855
- }
2856
- async syncRefunds(syncParams) {
2857
- this.config.logger?.info("Syncing refunds");
2858
- return this.withSyncRun("refunds", "syncRefunds", async (cursor, runStartedAt) => {
2859
- const accountId = await this.getAccountId();
2860
- const params = { limit: 100 };
2861
- if (syncParams?.created) {
2862
- params.created = syncParams.created;
2863
- } else if (cursor) {
2864
- params.created = { gte: cursor };
2865
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2866
- }
2867
- return this.fetchAndUpsert(
2868
- (pagination) => this.stripe.refunds.list({ ...params, ...pagination }),
2869
- (items) => this.upsertRefunds(items, accountId, syncParams?.backfillRelatedEntities),
2870
- accountId,
2871
- "refunds",
2872
- runStartedAt
2873
- );
2874
- });
2875
- }
2876
- async syncCreditNotes(syncParams) {
2877
- this.config.logger?.info("Syncing credit notes");
2878
- return this.withSyncRun("credit_notes", "syncCreditNotes", async (cursor, runStartedAt) => {
2879
- const accountId = await this.getAccountId();
2880
- const params = { limit: 100 };
2881
- if (syncParams?.created) {
2882
- params.created = syncParams.created;
2883
- } else if (cursor) {
2884
- params.created = { gte: cursor };
2885
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2886
- }
2887
- return this.fetchAndUpsert(
2888
- (pagination) => this.stripe.creditNotes.list({ ...params, ...pagination }),
2889
- (creditNotes) => this.upsertCreditNotes(creditNotes, accountId),
2890
- accountId,
2891
- "credit_notes",
2892
- runStartedAt
2893
- );
2894
- });
2895
- }
2896
- async syncFeatures(syncParams) {
2897
- this.config.logger?.info("Syncing features");
2898
- return this.withSyncRun("features", "syncFeatures", async (cursor, runStartedAt) => {
2899
- const accountId = await this.getAccountId();
2900
- const params = {
2901
- limit: 100,
2902
- ...syncParams?.pagination
2903
- };
2904
- return this.fetchAndUpsert(
2905
- () => this.stripe.entitlements.features.list(params),
2906
- (features) => this.upsertFeatures(features, accountId),
2907
- accountId,
2908
- "features",
2909
- runStartedAt
2910
- );
2911
- });
2912
- }
2913
- async syncEntitlements(customerId, syncParams) {
2914
- this.config.logger?.info("Syncing entitlements");
2915
- return this.withSyncRun(
2916
- "active_entitlements",
2917
- "syncEntitlements",
2918
- async (cursor, runStartedAt) => {
2919
- const accountId = await this.getAccountId();
2920
- const params = {
2921
- customer: customerId,
2922
- limit: 100,
2923
- ...syncParams?.pagination
2924
- };
2925
- return this.fetchAndUpsert(
2926
- () => this.stripe.entitlements.activeEntitlements.list(params),
2927
- (entitlements) => this.upsertActiveEntitlements(customerId, entitlements, accountId),
2928
- accountId,
2929
- "active_entitlements",
2930
- runStartedAt
2931
- );
2932
- }
2933
- );
2934
- }
2935
- async syncCheckoutSessions(syncParams) {
2936
- this.config.logger?.info("Syncing checkout sessions");
2937
- return this.withSyncRun(
2938
- "checkout_sessions",
2939
- "syncCheckoutSessions",
2940
- async (cursor, runStartedAt) => {
2941
- const accountId = await this.getAccountId();
2942
- const params = { limit: 100 };
2943
- if (syncParams?.created) {
2944
- params.created = syncParams.created;
2945
- } else if (cursor) {
2946
- params.created = { gte: cursor };
2947
- this.config.logger?.info(`Incremental sync from cursor: ${cursor}`);
2948
- }
2949
- return this.fetchAndUpsert(
2950
- (pagination) => this.stripe.checkout.sessions.list({ ...params, ...pagination }),
2951
- (items) => this.upsertCheckoutSessions(items, accountId, syncParams?.backfillRelatedEntities),
2952
- accountId,
2953
- "checkout_sessions",
2954
- runStartedAt
2955
- );
2956
- }
2957
- );
2958
- }
2959
- /**
2960
- * Helper to wrap a sync operation in the observable sync system.
2961
- * Creates/gets a sync run, sets up the object run, gets cursor, and handles completion.
2962
- *
2963
- * @param resourceName - The resource being synced (e.g., 'products', 'customers')
2964
- * @param triggeredBy - What triggered this sync (for observability)
2965
- * @param fn - The sync function to execute, receives cursor and runStartedAt
2966
- * @returns The result of the sync function
2967
- */
2968
- async withSyncRun(resourceName, triggeredBy, fn) {
2969
- const accountId = await this.getAccountId();
2970
- const lastCursor = await this.postgresClient.getLastCompletedCursor(accountId, resourceName);
2971
- const cursor = lastCursor ? parseInt(lastCursor) : null;
2972
- const runKey = await this.postgresClient.getOrCreateSyncRun(accountId, triggeredBy);
2973
- if (!runKey) {
2974
- const activeRun = await this.postgresClient.getActiveSyncRun(accountId);
2975
- if (!activeRun) {
2976
- throw new Error("Failed to get or create sync run");
2977
- }
2978
- throw new Error("Another sync is already running for this account");
2979
- }
2980
- const { runStartedAt } = runKey;
2981
- await this.postgresClient.createObjectRuns(accountId, runStartedAt, [resourceName]);
2982
- await this.postgresClient.tryStartObjectSync(accountId, runStartedAt, resourceName);
2983
- try {
2984
- const result = await fn(cursor, runStartedAt);
2985
- await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
2986
- return result;
2987
- } catch (error) {
2988
- await this.postgresClient.failObjectSync(
2989
- accountId,
2990
- runStartedAt,
2991
- resourceName,
2992
- error instanceof Error ? error.message : "Unknown error"
2993
- );
2994
- throw error;
2995
- }
2996
- }
2997
- async fetchAndUpsert(fetch2, upsert, accountId, resourceName, runStartedAt) {
2998
- const CHECKPOINT_SIZE = 100;
2999
- let totalSynced = 0;
3000
- let currentBatch = [];
3001
- try {
3002
- this.config.logger?.info("Fetching items to sync from Stripe");
3003
- try {
3004
- let hasMore = true;
3005
- let startingAfter = void 0;
3006
- while (hasMore) {
3007
- const response = await fetch2(
3008
- startingAfter ? { starting_after: startingAfter } : void 0
3009
- );
3010
- for (const item of response.data) {
3011
- currentBatch.push(item);
3012
- if (currentBatch.length >= CHECKPOINT_SIZE) {
3013
- this.config.logger?.info(`Upserting batch of ${currentBatch.length} items`);
3014
- await upsert(currentBatch, accountId);
3015
- totalSynced += currentBatch.length;
3016
- await this.postgresClient.incrementObjectProgress(
3017
- accountId,
3018
- runStartedAt,
3019
- resourceName,
3020
- currentBatch.length
3021
- );
3022
- const maxCreated = Math.max(
3023
- ...currentBatch.map((i) => i.created || 0)
3024
- );
3025
- if (maxCreated > 0) {
3026
- await this.postgresClient.updateObjectCursor(
3027
- accountId,
3028
- runStartedAt,
3029
- resourceName,
3030
- String(maxCreated)
3031
- );
3032
- this.config.logger?.info(`Checkpoint: cursor updated to ${maxCreated}`);
3033
- }
3034
- currentBatch = [];
3035
- }
3036
- }
3037
- hasMore = response.has_more;
3038
- if (response.data.length > 0) {
3039
- startingAfter = response.data[response.data.length - 1].id;
3040
- }
3041
- }
3042
- if (currentBatch.length > 0) {
3043
- this.config.logger?.info(`Upserting final batch of ${currentBatch.length} items`);
3044
- await upsert(currentBatch, accountId);
3045
- totalSynced += currentBatch.length;
3046
- await this.postgresClient.incrementObjectProgress(
3047
- accountId,
3048
- runStartedAt,
3049
- resourceName,
3050
- currentBatch.length
3051
- );
3052
- const maxCreated = Math.max(
3053
- ...currentBatch.map((i) => i.created || 0)
3054
- );
3055
- if (maxCreated > 0) {
3056
- await this.postgresClient.updateObjectCursor(
3057
- accountId,
3058
- runStartedAt,
3059
- resourceName,
3060
- String(maxCreated)
3061
- );
3062
- }
3063
- }
3064
- } catch (error) {
3065
- if (currentBatch.length > 0) {
3066
- this.config.logger?.info(
3067
- `Error occurred, saving partial progress: ${currentBatch.length} items`
3068
- );
3069
- await upsert(currentBatch, accountId);
3070
- totalSynced += currentBatch.length;
3071
- await this.postgresClient.incrementObjectProgress(
3072
- accountId,
3073
- runStartedAt,
3074
- resourceName,
3075
- currentBatch.length
3076
- );
3077
- const maxCreated = Math.max(
3078
- ...currentBatch.map((i) => i.created || 0)
3079
- );
3080
- if (maxCreated > 0) {
3081
- await this.postgresClient.updateObjectCursor(
3082
- accountId,
3083
- runStartedAt,
3084
- resourceName,
3085
- String(maxCreated)
3086
- );
3087
- }
3088
- }
3089
- throw error;
3090
- }
3091
- await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
3092
- this.config.logger?.info(`Sync complete: ${totalSynced} items synced`);
3093
- return { synced: totalSynced };
3094
- } catch (error) {
3095
- await this.postgresClient.failObjectSync(
3096
- accountId,
3097
- runStartedAt,
3098
- resourceName,
3099
- error instanceof Error ? error.message : "Unknown error"
3100
- );
3101
- throw error;
3102
- }
3103
- }
3104
- async upsertCharges(charges, accountId, backfillRelatedEntities, syncTimestamp) {
3105
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3106
- await Promise.all([
3107
- this.backfillCustomers(getUniqueIds(charges, "customer"), accountId),
3108
- this.backfillInvoices(getUniqueIds(charges, "invoice"), accountId)
3109
- ]);
3110
- }
3111
- await this.expandEntity(
3112
- charges,
3113
- "refunds",
3114
- (id) => this.stripe.refunds.list({ charge: id, limit: 100 })
3115
- );
3116
- return this.postgresClient.upsertManyWithTimestampProtection(
3117
- charges,
3118
- "charges",
3119
- accountId,
3120
- syncTimestamp
3121
- );
3122
- }
3123
- async backfillCharges(chargeIds, accountId) {
3124
- const missingChargeIds = await this.postgresClient.findMissingEntries("charges", chargeIds);
3125
- await this.fetchMissingEntities(
3126
- missingChargeIds,
3127
- (id) => this.stripe.charges.retrieve(id)
3128
- ).then((charges) => this.upsertCharges(charges, accountId));
3129
- }
3130
- async backfillPaymentIntents(paymentIntentIds, accountId) {
3131
- const missingIds = await this.postgresClient.findMissingEntries(
3132
- "payment_intents",
3133
- paymentIntentIds
3134
- );
3135
- await this.fetchMissingEntities(
3136
- missingIds,
3137
- (id) => this.stripe.paymentIntents.retrieve(id)
3138
- ).then((paymentIntents) => this.upsertPaymentIntents(paymentIntents, accountId));
3139
- }
3140
- async upsertCreditNotes(creditNotes, accountId, backfillRelatedEntities, syncTimestamp) {
3141
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3142
- await Promise.all([
3143
- this.backfillCustomers(getUniqueIds(creditNotes, "customer"), accountId),
3144
- this.backfillInvoices(getUniqueIds(creditNotes, "invoice"), accountId)
3145
- ]);
3146
- }
3147
- await this.expandEntity(
3148
- creditNotes,
3149
- "lines",
3150
- (id) => this.stripe.creditNotes.listLineItems(id, { limit: 100 })
3151
- );
3152
- return this.postgresClient.upsertManyWithTimestampProtection(
3153
- creditNotes,
3154
- "credit_notes",
3155
- accountId,
3156
- syncTimestamp
3157
- );
3158
- }
3159
- async upsertCheckoutSessions(checkoutSessions, accountId, backfillRelatedEntities, syncTimestamp) {
3160
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3161
- await Promise.all([
3162
- this.backfillCustomers(getUniqueIds(checkoutSessions, "customer"), accountId),
3163
- this.backfillSubscriptions(getUniqueIds(checkoutSessions, "subscription"), accountId),
3164
- this.backfillPaymentIntents(getUniqueIds(checkoutSessions, "payment_intent"), accountId),
3165
- this.backfillInvoices(getUniqueIds(checkoutSessions, "invoice"), accountId)
3166
- ]);
3167
- }
3168
- const rows = await this.postgresClient.upsertManyWithTimestampProtection(
3169
- checkoutSessions,
3170
- "checkout_sessions",
3171
- accountId,
3172
- syncTimestamp
3173
- );
3174
- await this.fillCheckoutSessionsLineItems(
3175
- checkoutSessions.map((cs) => cs.id),
3176
- accountId,
3177
- syncTimestamp
3178
- );
3179
- return rows;
3180
- }
3181
- async upsertEarlyFraudWarning(earlyFraudWarnings, accountId, backfillRelatedEntities, syncTimestamp) {
3182
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3183
- await Promise.all([
3184
- this.backfillPaymentIntents(getUniqueIds(earlyFraudWarnings, "payment_intent"), accountId),
3185
- this.backfillCharges(getUniqueIds(earlyFraudWarnings, "charge"), accountId)
3186
- ]);
3187
- }
3188
- return this.postgresClient.upsertManyWithTimestampProtection(
3189
- earlyFraudWarnings,
3190
- "early_fraud_warnings",
3191
- accountId,
3192
- syncTimestamp
3193
- );
3194
- }
3195
- async upsertRefunds(refunds, accountId, backfillRelatedEntities, syncTimestamp) {
3196
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3197
- await Promise.all([
3198
- this.backfillPaymentIntents(getUniqueIds(refunds, "payment_intent"), accountId),
3199
- this.backfillCharges(getUniqueIds(refunds, "charge"), accountId)
3200
- ]);
3201
- }
3202
- return this.postgresClient.upsertManyWithTimestampProtection(
3203
- refunds,
3204
- "refunds",
3205
- accountId,
3206
- syncTimestamp
3207
- );
3208
- }
3209
- async upsertReviews(reviews, accountId, backfillRelatedEntities, syncTimestamp) {
3210
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3211
- await Promise.all([
3212
- this.backfillPaymentIntents(getUniqueIds(reviews, "payment_intent"), accountId),
3213
- this.backfillCharges(getUniqueIds(reviews, "charge"), accountId)
3214
- ]);
3215
- }
3216
- return this.postgresClient.upsertManyWithTimestampProtection(
3217
- reviews,
3218
- "reviews",
3219
- accountId,
3220
- syncTimestamp
3221
- );
3222
- }
3223
- async upsertCustomers(customers, accountId, syncTimestamp) {
3224
- const deletedCustomers = customers.filter((customer) => customer.deleted);
3225
- const nonDeletedCustomers = customers.filter((customer) => !customer.deleted);
3226
- await this.postgresClient.upsertManyWithTimestampProtection(
3227
- nonDeletedCustomers,
3228
- "customers",
3229
- accountId,
3230
- syncTimestamp
3231
- );
3232
- await this.postgresClient.upsertManyWithTimestampProtection(
3233
- deletedCustomers,
3234
- "customers",
3235
- accountId,
3236
- syncTimestamp
3237
- );
3238
- return customers;
3239
- }
3240
- async backfillCustomers(customerIds, accountId) {
3241
- const missingIds = await this.postgresClient.findMissingEntries("customers", customerIds);
3242
- await this.fetchMissingEntities(missingIds, (id) => this.stripe.customers.retrieve(id)).then((entries) => this.upsertCustomers(entries, accountId)).catch((err) => {
3243
- this.config.logger?.error(err, "Failed to backfill");
3244
- throw err;
3245
- });
3246
- }
3247
- async upsertDisputes(disputes, accountId, backfillRelatedEntities, syncTimestamp) {
3248
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3249
- await this.backfillCharges(getUniqueIds(disputes, "charge"), accountId);
3250
- }
3251
- return this.postgresClient.upsertManyWithTimestampProtection(
3252
- disputes,
3253
- "disputes",
3254
- accountId,
3255
- syncTimestamp
3256
- );
3257
- }
3258
- async upsertInvoices(invoices, accountId, backfillRelatedEntities, syncTimestamp) {
3259
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3260
- await Promise.all([
3261
- this.backfillCustomers(getUniqueIds(invoices, "customer"), accountId),
3262
- this.backfillSubscriptions(getUniqueIds(invoices, "subscription"), accountId)
3263
- ]);
3264
- }
3265
- await this.expandEntity(
3266
- invoices,
3267
- "lines",
3268
- (id) => this.stripe.invoices.listLineItems(id, { limit: 100 })
3269
- );
3270
- return this.postgresClient.upsertManyWithTimestampProtection(
3271
- invoices,
3272
- "invoices",
3273
- accountId,
3274
- syncTimestamp
3275
- );
3276
- }
3277
- backfillInvoices = async (invoiceIds, accountId) => {
3278
- const missingIds = await this.postgresClient.findMissingEntries("invoices", invoiceIds);
3279
- await this.fetchMissingEntities(missingIds, (id) => this.stripe.invoices.retrieve(id)).then(
3280
- (entries) => this.upsertInvoices(entries, accountId)
3281
- );
3282
- };
3283
- backfillPrices = async (priceIds, accountId) => {
3284
- const missingIds = await this.postgresClient.findMissingEntries("prices", priceIds);
3285
- await this.fetchMissingEntities(missingIds, (id) => this.stripe.prices.retrieve(id)).then(
3286
- (entries) => this.upsertPrices(entries, accountId)
3287
- );
3288
- };
3289
- async upsertPlans(plans, accountId, backfillRelatedEntities, syncTimestamp) {
3290
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3291
- await this.backfillProducts(getUniqueIds(plans, "product"), accountId);
3292
- }
3293
- return this.postgresClient.upsertManyWithTimestampProtection(
3294
- plans,
3295
- "plans",
3296
- accountId,
3297
- syncTimestamp
3298
- );
3299
- }
3300
- async deletePlan(id) {
3301
- return this.postgresClient.delete("plans", id);
3302
- }
3303
- async upsertPrices(prices, accountId, backfillRelatedEntities, syncTimestamp) {
3304
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3305
- await this.backfillProducts(getUniqueIds(prices, "product"), accountId);
3306
- }
3307
- return this.postgresClient.upsertManyWithTimestampProtection(
3308
- prices,
3309
- "prices",
3310
- accountId,
3311
- syncTimestamp
3312
- );
3313
- }
3314
- async deletePrice(id) {
3315
- return this.postgresClient.delete("prices", id);
3316
- }
3317
- async upsertProducts(products, accountId, syncTimestamp) {
3318
- return this.postgresClient.upsertManyWithTimestampProtection(
3319
- products,
3320
- "products",
3321
- accountId,
3322
- syncTimestamp
3323
- );
3324
- }
3325
- async deleteProduct(id) {
3326
- return this.postgresClient.delete("products", id);
3327
- }
3328
- async backfillProducts(productIds, accountId) {
3329
- const missingProductIds = await this.postgresClient.findMissingEntries("products", productIds);
3330
- await this.fetchMissingEntities(
3331
- missingProductIds,
3332
- (id) => this.stripe.products.retrieve(id)
3333
- ).then((products) => this.upsertProducts(products, accountId));
3334
- }
3335
- async upsertPaymentIntents(paymentIntents, accountId, backfillRelatedEntities, syncTimestamp) {
3336
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3337
- await Promise.all([
3338
- this.backfillCustomers(getUniqueIds(paymentIntents, "customer"), accountId),
3339
- this.backfillInvoices(getUniqueIds(paymentIntents, "invoice"), accountId)
3340
- ]);
3341
- }
3342
- return this.postgresClient.upsertManyWithTimestampProtection(
3343
- paymentIntents,
3344
- "payment_intents",
3345
- accountId,
3346
- syncTimestamp
3347
- );
3348
- }
3349
- async upsertPaymentMethods(paymentMethods, accountId, backfillRelatedEntities = false, syncTimestamp) {
3350
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3351
- await this.backfillCustomers(getUniqueIds(paymentMethods, "customer"), accountId);
3352
- }
3353
- return this.postgresClient.upsertManyWithTimestampProtection(
3354
- paymentMethods,
3355
- "payment_methods",
3356
- accountId,
3357
- syncTimestamp
3358
- );
3359
- }
3360
- async upsertSetupIntents(setupIntents, accountId, backfillRelatedEntities, syncTimestamp) {
3361
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3362
- await this.backfillCustomers(getUniqueIds(setupIntents, "customer"), accountId);
3363
- }
3364
- return this.postgresClient.upsertManyWithTimestampProtection(
3365
- setupIntents,
3366
- "setup_intents",
3367
- accountId,
3368
- syncTimestamp
3369
- );
3370
- }
3371
- async upsertTaxIds(taxIds, accountId, backfillRelatedEntities, syncTimestamp) {
3372
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3373
- await this.backfillCustomers(getUniqueIds(taxIds, "customer"), accountId);
3374
- }
3375
- return this.postgresClient.upsertManyWithTimestampProtection(
3376
- taxIds,
3377
- "tax_ids",
3378
- accountId,
3379
- syncTimestamp
3380
- );
3381
- }
3382
- async deleteTaxId(id) {
3383
- return this.postgresClient.delete("tax_ids", id);
3384
- }
3385
- async upsertSubscriptionItems(subscriptionItems, accountId, syncTimestamp) {
3386
- const modifiedSubscriptionItems = subscriptionItems.map((subscriptionItem) => {
3387
- const priceId = subscriptionItem.price.id.toString();
3388
- const deleted = subscriptionItem.deleted;
3389
- const quantity = subscriptionItem.quantity;
3390
- return {
3391
- ...subscriptionItem,
3392
- price: priceId,
3393
- deleted: deleted ?? false,
3394
- quantity: quantity ?? null
3395
- };
3396
- });
3397
- await this.postgresClient.upsertManyWithTimestampProtection(
3398
- modifiedSubscriptionItems,
3399
- "subscription_items",
3400
- accountId,
3401
- syncTimestamp
3402
- );
3403
- }
3404
- async fillCheckoutSessionsLineItems(checkoutSessionIds, accountId, syncTimestamp) {
3405
- for (const checkoutSessionId of checkoutSessionIds) {
3406
- const lineItemResponses = [];
3407
- let hasMore = true;
3408
- let startingAfter = void 0;
3409
- while (hasMore) {
3410
- const response = await this.stripe.checkout.sessions.listLineItems(checkoutSessionId, {
3411
- limit: 100,
3412
- ...startingAfter ? { starting_after: startingAfter } : {}
3413
- });
3414
- lineItemResponses.push(...response.data);
3415
- hasMore = response.has_more;
3416
- if (response.data.length > 0) {
3417
- startingAfter = response.data[response.data.length - 1].id;
3418
- }
3419
- }
3420
- await this.upsertCheckoutSessionLineItems(
3421
- lineItemResponses,
3422
- checkoutSessionId,
3423
- accountId,
3424
- syncTimestamp
3425
- );
3426
- }
3427
- }
3428
- async upsertCheckoutSessionLineItems(lineItems, checkoutSessionId, accountId, syncTimestamp) {
3429
- await this.backfillPrices(
3430
- lineItems.map((lineItem) => lineItem.price?.id?.toString() ?? void 0).filter((id) => id !== void 0),
3431
- accountId
3432
- );
3433
- const modifiedLineItems = lineItems.map((lineItem) => {
3434
- const priceId = typeof lineItem.price === "object" && lineItem.price?.id ? lineItem.price.id.toString() : lineItem.price?.toString() || null;
3435
- return {
3436
- ...lineItem,
3437
- price: priceId,
3438
- checkout_session: checkoutSessionId
3439
- };
3440
- });
3441
- await this.postgresClient.upsertManyWithTimestampProtection(
3442
- modifiedLineItems,
3443
- "checkout_session_line_items",
3444
- accountId,
3445
- syncTimestamp
3446
- );
3447
- }
3448
- async markDeletedSubscriptionItems(subscriptionId, currentSubItemIds) {
3449
- let prepared = sql2(`
3450
- select id from "stripe"."subscription_items"
3451
- where subscription = :subscriptionId and COALESCE(deleted, false) = false;
3452
- `)({ subscriptionId });
3453
- const { rows } = await this.postgresClient.query(prepared.text, prepared.values);
3454
- const deletedIds = rows.filter(
3455
- ({ id }) => currentSubItemIds.includes(id) === false
3456
- );
3457
- if (deletedIds.length > 0) {
3458
- const ids = deletedIds.map(({ id }) => id);
3459
- prepared = sql2(`
3460
- update "stripe"."subscription_items"
3461
- set _raw_data = jsonb_set(_raw_data, '{deleted}', 'true'::jsonb)
3462
- where id=any(:ids::text[]);
3463
- `)({ ids });
3464
- const { rowCount } = await this.postgresClient.query(prepared.text, prepared.values);
3465
- return { rowCount: rowCount || 0 };
3466
- } else {
3467
- return { rowCount: 0 };
3468
- }
3469
- }
3470
- async upsertSubscriptionSchedules(subscriptionSchedules, accountId, backfillRelatedEntities, syncTimestamp) {
3471
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3472
- const customerIds = getUniqueIds(subscriptionSchedules, "customer");
3473
- await this.backfillCustomers(customerIds, accountId);
3474
- }
3475
- const rows = await this.postgresClient.upsertManyWithTimestampProtection(
3476
- subscriptionSchedules,
3477
- "subscription_schedules",
3478
- accountId,
3479
- syncTimestamp
3480
- );
3481
- return rows;
3482
- }
3483
- async upsertSubscriptions(subscriptions, accountId, backfillRelatedEntities, syncTimestamp) {
3484
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3485
- const customerIds = getUniqueIds(subscriptions, "customer");
3486
- await this.backfillCustomers(customerIds, accountId);
3487
- }
3488
- await this.expandEntity(
3489
- subscriptions,
3490
- "items",
3491
- (id) => this.stripe.subscriptionItems.list({ subscription: id, limit: 100 })
3492
- );
3493
- const rows = await this.postgresClient.upsertManyWithTimestampProtection(
3494
- subscriptions,
3495
- "subscriptions",
3496
- accountId,
3497
- syncTimestamp
3498
- );
3499
- const allSubscriptionItems = subscriptions.flatMap((subscription) => subscription.items.data);
3500
- await this.upsertSubscriptionItems(allSubscriptionItems, accountId, syncTimestamp);
3501
- const markSubscriptionItemsDeleted = [];
3502
- for (const subscription of subscriptions) {
3503
- const subscriptionItems = subscription.items.data;
3504
- const subItemIds = subscriptionItems.map((x) => x.id);
3505
- markSubscriptionItemsDeleted.push(
3506
- this.markDeletedSubscriptionItems(subscription.id, subItemIds)
3507
- );
3508
- }
3509
- await Promise.all(markSubscriptionItemsDeleted);
3510
- return rows;
3511
- }
3512
- async deleteRemovedActiveEntitlements(customerId, currentActiveEntitlementIds) {
3513
- const prepared = sql2(`
3514
- delete from "stripe"."active_entitlements"
3515
- where customer = :customerId and id <> ALL(:currentActiveEntitlementIds::text[]);
3516
- `)({ customerId, currentActiveEntitlementIds });
3517
- const { rowCount } = await this.postgresClient.query(prepared.text, prepared.values);
3518
- return { rowCount: rowCount || 0 };
3519
- }
3520
- async upsertFeatures(features, accountId, syncTimestamp) {
3521
- return this.postgresClient.upsertManyWithTimestampProtection(
3522
- features,
3523
- "features",
3524
- accountId,
3525
- syncTimestamp
3526
- );
3527
- }
3528
- async backfillFeatures(featureIds, accountId) {
3529
- const missingFeatureIds = await this.postgresClient.findMissingEntries("features", featureIds);
3530
- await this.fetchMissingEntities(
3531
- missingFeatureIds,
3532
- (id) => this.stripe.entitlements.features.retrieve(id)
3533
- ).then((features) => this.upsertFeatures(features, accountId)).catch((err) => {
3534
- this.config.logger?.error(err, "Failed to backfill features");
3535
- throw err;
3536
- });
3537
- }
3538
- async upsertActiveEntitlements(customerId, activeEntitlements, accountId, backfillRelatedEntities, syncTimestamp) {
3539
- if (backfillRelatedEntities ?? this.config.backfillRelatedEntities) {
3540
- await Promise.all([
3541
- this.backfillCustomers(getUniqueIds(activeEntitlements, "customer"), accountId),
3542
- this.backfillFeatures(getUniqueIds(activeEntitlements, "feature"), accountId)
3543
- ]);
3544
- }
3545
- const entitlements = activeEntitlements.map((entitlement) => ({
3546
- id: entitlement.id,
3547
- object: entitlement.object,
3548
- feature: typeof entitlement.feature === "string" ? entitlement.feature : entitlement.feature.id,
3549
- customer: customerId,
3550
- livemode: entitlement.livemode,
3551
- lookup_key: entitlement.lookup_key
3552
- }));
3553
- return this.postgresClient.upsertManyWithTimestampProtection(
3554
- entitlements,
3555
- "active_entitlements",
3556
- accountId,
3557
- syncTimestamp
3558
- );
3559
- }
3560
- async findOrCreateManagedWebhook(url, params) {
3561
- const webhookParams = {
3562
- enabled_events: this.getSupportedEventTypes(),
3563
- ...params
3564
- };
3565
- const accountId = await this.getAccountId();
3566
- const lockKey = `webhook:${accountId}:${url}`;
3567
- return this.postgresClient.withAdvisoryLock(lockKey, async () => {
3568
- const existingWebhook = await this.getManagedWebhookByUrl(url);
3569
- if (existingWebhook) {
3570
- try {
3571
- const stripeWebhook = await this.stripe.webhookEndpoints.retrieve(existingWebhook.id);
3572
- if (stripeWebhook.status === "enabled") {
3573
- return stripeWebhook;
3574
- }
3575
- this.config.logger?.info(
3576
- { webhookId: existingWebhook.id },
3577
- "Webhook is disabled, deleting and will recreate"
3578
- );
3579
- await this.stripe.webhookEndpoints.del(existingWebhook.id);
3580
- await this.postgresClient.delete("_managed_webhooks", existingWebhook.id);
3581
- } catch (error) {
3582
- const stripeError = error;
3583
- if (stripeError?.statusCode === 404 || stripeError?.code === "resource_missing") {
3584
- this.config.logger?.warn(
3585
- { error, webhookId: existingWebhook.id },
3586
- "Webhook not found in Stripe (404), removing from database"
3587
- );
3588
- await this.postgresClient.delete("_managed_webhooks", existingWebhook.id);
3589
- } else {
3590
- this.config.logger?.error(
3591
- { error, webhookId: existingWebhook.id },
3592
- "Error retrieving webhook from Stripe, keeping in database"
3593
- );
3594
- throw error;
3595
- }
3596
- }
3597
- }
3598
- const allDbWebhooks = await this.listManagedWebhooks();
3599
- for (const dbWebhook of allDbWebhooks) {
3600
- if (dbWebhook.url !== url) {
3601
- this.config.logger?.info(
3602
- { webhookId: dbWebhook.id, oldUrl: dbWebhook.url, newUrl: url },
3603
- "Webhook URL mismatch, deleting"
3604
- );
3605
- try {
3606
- await this.stripe.webhookEndpoints.del(dbWebhook.id);
3607
- } catch (error) {
3608
- this.config.logger?.warn(
3609
- { error, webhookId: dbWebhook.id },
3610
- "Failed to delete old webhook from Stripe"
3611
- );
3612
- }
3613
- await this.postgresClient.delete("_managed_webhooks", dbWebhook.id);
3614
- }
3615
- }
3616
- try {
3617
- const stripeWebhooks = await this.stripe.webhookEndpoints.list({ limit: 100 });
3618
- for (const stripeWebhook of stripeWebhooks.data) {
3619
- const isManagedByMetadata = stripeWebhook.metadata?.managed_by?.toLowerCase().replace(/[\s\-]+/g, "") === "stripesync";
3620
- const normalizedDescription = stripeWebhook.description?.toLowerCase().replace(/[\s\-]+/g, "") || "";
3621
- const isManagedByDescription = normalizedDescription.includes("stripesync");
3622
- if (isManagedByMetadata || isManagedByDescription) {
3623
- const existsInDb = allDbWebhooks.some((dbWebhook) => dbWebhook.id === stripeWebhook.id);
3624
- if (!existsInDb) {
3625
- this.config.logger?.warn(
3626
- { webhookId: stripeWebhook.id, url: stripeWebhook.url },
3627
- "Found orphaned managed webhook in Stripe, deleting"
3628
- );
3629
- await this.stripe.webhookEndpoints.del(stripeWebhook.id);
3630
- }
3631
- }
3632
- }
3633
- } catch (error) {
3634
- this.config.logger?.warn({ error }, "Failed to check for orphaned webhooks");
3635
- }
3636
- const webhook = await this.stripe.webhookEndpoints.create({
3637
- ...webhookParams,
3638
- url,
3639
- // Always set metadata to identify managed webhooks
3640
- metadata: {
3641
- ...webhookParams.metadata,
3642
- managed_by: "stripe-sync",
3643
- version: package_default.version
3644
- }
3645
- });
3646
- const accountId2 = await this.getAccountId();
3647
- await this.upsertManagedWebhooks([webhook], accountId2);
3648
- return webhook;
3649
- });
3650
- }
3651
- async getManagedWebhook(id) {
3652
- const accountId = await this.getAccountId();
3653
- const result = await this.postgresClient.query(
3654
- `SELECT * FROM "stripe"."_managed_webhooks" WHERE id = $1 AND "account_id" = $2`,
3655
- [id, accountId]
3656
- );
3657
- return result.rows.length > 0 ? result.rows[0] : null;
3658
- }
3659
- /**
3660
- * Get a managed webhook by URL and account ID.
3661
- * Used for race condition recovery: when createManagedWebhook hits a unique constraint
3662
- * violation (another instance created the webhook), we need to fetch the existing webhook
3663
- * by URL since we only know the URL, not the ID of the webhook that won the race.
3664
- */
3665
- async getManagedWebhookByUrl(url) {
3666
- const accountId = await this.getAccountId();
3667
- const result = await this.postgresClient.query(
3668
- `SELECT * FROM "stripe"."_managed_webhooks" WHERE url = $1 AND "account_id" = $2`,
3669
- [url, accountId]
3670
- );
3671
- return result.rows.length > 0 ? result.rows[0] : null;
3672
- }
3673
- async listManagedWebhooks() {
3674
- const accountId = await this.getAccountId();
3675
- const result = await this.postgresClient.query(
3676
- `SELECT * FROM "stripe"."_managed_webhooks" WHERE "account_id" = $1 ORDER BY created DESC`,
3677
- [accountId]
3678
- );
3679
- return result.rows;
3680
- }
3681
- async updateManagedWebhook(id, params) {
3682
- const webhook = await this.stripe.webhookEndpoints.update(id, params);
3683
- const accountId = await this.getAccountId();
3684
- await this.upsertManagedWebhooks([webhook], accountId);
3685
- return webhook;
3686
- }
3687
- async deleteManagedWebhook(id) {
3688
- await this.stripe.webhookEndpoints.del(id);
3689
- return this.postgresClient.delete("_managed_webhooks", id);
3690
- }
3691
- async upsertManagedWebhooks(webhooks, accountId, syncTimestamp) {
3692
- const filteredWebhooks = webhooks.map((webhook) => {
3693
- const filtered = {};
3694
- for (const prop of managedWebhookSchema.properties) {
3695
- if (prop in webhook) {
3696
- filtered[prop] = webhook[prop];
3697
- }
3698
- }
3699
- return filtered;
3700
- });
3701
- return this.postgresClient.upsertManyWithTimestampProtection(
3702
- filteredWebhooks,
3703
- "_managed_webhooks",
3704
- accountId,
3705
- syncTimestamp
3706
- );
3707
- }
3708
- async backfillSubscriptions(subscriptionIds, accountId) {
3709
- const missingSubscriptionIds = await this.postgresClient.findMissingEntries(
3710
- "subscriptions",
3711
- subscriptionIds
3712
- );
3713
- await this.fetchMissingEntities(
3714
- missingSubscriptionIds,
3715
- (id) => this.stripe.subscriptions.retrieve(id)
3716
- ).then((subscriptions) => this.upsertSubscriptions(subscriptions, accountId));
3717
- }
3718
- backfillSubscriptionSchedules = async (subscriptionIds, accountId) => {
3719
- const missingSubscriptionIds = await this.postgresClient.findMissingEntries(
3720
- "subscription_schedules",
3721
- subscriptionIds
3722
- );
3723
- await this.fetchMissingEntities(
3724
- missingSubscriptionIds,
3725
- (id) => this.stripe.subscriptionSchedules.retrieve(id)
3726
- ).then(
3727
- (subscriptionSchedules) => this.upsertSubscriptionSchedules(subscriptionSchedules, accountId)
3728
- );
3729
- };
3730
- /**
3731
- * Stripe only sends the first 10 entries by default, the option will actively fetch all entries.
3732
- * Uses manual pagination - each fetch() gets automatic retry protection.
3733
- */
3734
- async expandEntity(entities, property, listFn) {
3735
- if (!this.config.autoExpandLists) return;
3736
- for (const entity of entities) {
3737
- if (entity[property]?.has_more) {
3738
- const allData = [];
3739
- let hasMore = true;
3740
- let startingAfter = void 0;
3741
- while (hasMore) {
3742
- const response = await listFn(
3743
- entity.id,
3744
- startingAfter ? { starting_after: startingAfter } : void 0
3745
- );
3746
- allData.push(...response.data);
3747
- hasMore = response.has_more;
3748
- if (response.data.length > 0) {
3749
- startingAfter = response.data[response.data.length - 1].id;
3750
- }
3751
- }
3752
- entity[property] = {
3753
- ...entity[property],
3754
- data: allData,
3755
- has_more: false
3756
- };
3757
- }
3758
- }
3759
- }
3760
- async fetchMissingEntities(ids, fetch2) {
3761
- if (!ids.length) return [];
3762
- const entities = [];
3763
- for (const id of ids) {
3764
- const entity = await fetch2(id);
3765
- entities.push(entity);
3766
- }
3767
- return entities;
3768
- }
3769
- /**
3770
- * Closes the database connection pool and cleans up resources.
3771
- * Call this when you're done using the StripeSync instance.
3772
- */
3773
- async close() {
3774
- await this.postgresClient.pool.end();
3775
- }
3776
- };
3777
- function chunkArray(array, chunkSize) {
3778
- const result = [];
3779
- for (let i = 0; i < array.length; i += chunkSize) {
3780
- result.push(array.slice(i, i + chunkSize));
3781
- }
3782
- return result;
3783
- }
3784
-
3785
- // src/database/migrate.ts
3786
- import { Client } from "pg";
3787
- import { migrate } from "pg-node-migrations";
3788
- import fs from "fs";
3789
- import path from "path";
3790
- import { fileURLToPath } from "url";
3791
- var __filename2 = fileURLToPath(import.meta.url);
3792
- var __dirname2 = path.dirname(__filename2);
3793
- async function doesTableExist(client, schema, tableName) {
3794
- const result = await client.query(
3795
- `SELECT EXISTS (
3796
- SELECT 1
3797
- FROM information_schema.tables
3798
- WHERE table_schema = $1
3799
- AND table_name = $2
3800
- )`,
3801
- [schema, tableName]
3802
- );
3803
- return result.rows[0]?.exists || false;
3804
- }
3805
- async function renameMigrationsTableIfNeeded(client, schema = "stripe", logger) {
3806
- const oldTableExists = await doesTableExist(client, schema, "migrations");
3807
- const newTableExists = await doesTableExist(client, schema, "_migrations");
3808
- if (oldTableExists && !newTableExists) {
3809
- logger?.info("Renaming migrations table to _migrations");
3810
- await client.query(`ALTER TABLE "${schema}"."migrations" RENAME TO "_migrations"`);
3811
- logger?.info("Successfully renamed migrations table");
3812
- }
3813
- }
3814
- async function cleanupSchema(client, schema, logger) {
3815
- logger?.warn(`Migrations table is empty - dropping and recreating schema "${schema}"`);
3816
- await client.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
3817
- await client.query(`CREATE SCHEMA "${schema}"`);
3818
- logger?.info(`Schema "${schema}" has been reset`);
3819
- }
3820
- async function connectAndMigrate(client, migrationsDirectory, config, logOnError = false) {
3821
- if (!fs.existsSync(migrationsDirectory)) {
3822
- config.logger?.info(`Migrations directory ${migrationsDirectory} not found, skipping`);
3823
- return;
3824
- }
3825
- const optionalConfig = {
3826
- schemaName: "stripe",
3827
- tableName: "_migrations"
3828
- };
3829
- try {
3830
- await migrate({ client }, migrationsDirectory, optionalConfig);
3831
- } catch (error) {
3832
- if (logOnError && error instanceof Error) {
3833
- config.logger?.error(error, "Migration error:");
3834
- } else {
3835
- throw error;
3836
- }
3837
- }
3838
- }
3839
- async function runMigrations(config) {
3840
- const client = new Client({
3841
- connectionString: config.databaseUrl,
3842
- ssl: config.ssl,
3843
- connectionTimeoutMillis: 1e4
3844
- });
3845
- const schema = "stripe";
3846
- try {
3847
- await client.connect();
3848
- await client.query(`CREATE SCHEMA IF NOT EXISTS ${schema};`);
3849
- await renameMigrationsTableIfNeeded(client, schema, config.logger);
3850
- const tableExists = await doesTableExist(client, schema, "_migrations");
3851
- if (tableExists) {
3852
- const migrationCount = await client.query(
3853
- `SELECT COUNT(*) as count FROM "${schema}"."_migrations"`
3854
- );
3855
- const isEmpty = migrationCount.rows[0]?.count === "0";
3856
- if (isEmpty) {
3857
- await cleanupSchema(client, schema, config.logger);
3858
- }
3859
- }
3860
- config.logger?.info("Running migrations");
3861
- await connectAndMigrate(client, path.resolve(__dirname2, "./migrations"), config);
3862
- } catch (err) {
3863
- config.logger?.error(err, "Error running migrations");
3864
- throw err;
3865
- } finally {
3866
- await client.end();
3867
- config.logger?.info("Finished migrations");
3868
- }
3869
- }
3870
-
3871
- // src/websocket-client.ts
3872
- import WebSocket from "ws";
3873
- var CLI_VERSION = "1.33.0";
3874
- var PONG_WAIT = 10 * 1e3;
3875
- var PING_PERIOD = PONG_WAIT * 9 / 10;
3876
- var CONNECT_ATTEMPT_WAIT = 10 * 1e3;
3877
- var DEFAULT_RECONNECT_INTERVAL = 60 * 1e3;
3878
- function getClientUserAgent() {
3879
- return JSON.stringify({
3880
- name: "stripe-cli",
3881
- version: CLI_VERSION,
3882
- publisher: "stripe",
3883
- os: process.platform
3884
- });
3885
- }
3886
- async function createCliSession(stripeApiKey) {
3887
- const params = new URLSearchParams();
3888
- params.append("device_name", "stripe-sync-engine");
3889
- params.append("websocket_features[]", "webhooks");
3890
- const response = await fetch("https://api.stripe.com/v1/stripecli/sessions", {
3891
- method: "POST",
3892
- headers: {
3893
- Authorization: `Bearer ${stripeApiKey}`,
3894
- "Content-Type": "application/x-www-form-urlencoded",
3895
- "User-Agent": `Stripe/v1 stripe-cli/${CLI_VERSION}`,
3896
- "X-Stripe-Client-User-Agent": getClientUserAgent()
3897
- },
3898
- body: params.toString()
3899
- });
3900
- if (!response.ok) {
3901
- const error = await response.text();
3902
- throw new Error(`Failed to create CLI session: ${error}`);
3903
- }
3904
- return await response.json();
3905
- }
3906
- function sleep3(ms) {
3907
- return new Promise((resolve) => setTimeout(resolve, ms));
3908
- }
3909
- async function createStripeWebSocketClient(options) {
3910
- const { stripeApiKey, onEvent, onReady, onError, onClose } = options;
3911
- const session = await createCliSession(stripeApiKey);
3912
- const reconnectInterval = session.reconnect_delay ? session.reconnect_delay * 1e3 : DEFAULT_RECONNECT_INTERVAL;
3913
- let ws = null;
3914
- let pingInterval = null;
3915
- let reconnectTimer = null;
3916
- let connected = false;
3917
- let shouldRun = true;
3918
- let lastPongReceived = Date.now();
3919
- let notifyCloseResolve = null;
3920
- let stopResolve = null;
3921
- function cleanupConnection() {
3922
- if (pingInterval) {
3923
- clearInterval(pingInterval);
3924
- pingInterval = null;
3925
- }
3926
- if (reconnectTimer) {
3927
- clearTimeout(reconnectTimer);
3928
- reconnectTimer = null;
3929
- }
3930
- if (ws) {
3931
- ws.removeAllListeners();
3932
- if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
3933
- ws.close(1e3, "Resetting connection");
3934
- }
3935
- ws = null;
3936
- }
3937
- connected = false;
3938
- }
3939
- function setupWebSocket() {
3940
- return new Promise((resolve, reject) => {
3941
- lastPongReceived = Date.now();
3942
- const wsUrl = `${session.websocket_url}?websocket_feature=${encodeURIComponent(session.websocket_authorized_feature)}`;
3943
- ws = new WebSocket(wsUrl, {
3944
- headers: {
3945
- "Accept-Encoding": "identity",
3946
- "User-Agent": `Stripe/v1 stripe-cli/${CLI_VERSION}`,
3947
- "X-Stripe-Client-User-Agent": getClientUserAgent(),
3948
- "Websocket-Id": session.websocket_id
3949
- }
3950
- });
3951
- const connectionTimeout = setTimeout(() => {
3952
- if (ws && ws.readyState === WebSocket.CONNECTING) {
3953
- ws.terminate();
3954
- reject(new Error("WebSocket connection timeout"));
3955
- }
3956
- }, CONNECT_ATTEMPT_WAIT);
3957
- ws.on("pong", () => {
3958
- lastPongReceived = Date.now();
3959
- });
3960
- ws.on("open", () => {
3961
- clearTimeout(connectionTimeout);
3962
- connected = true;
3963
- pingInterval = setInterval(() => {
3964
- if (ws && ws.readyState === WebSocket.OPEN) {
3965
- const timeSinceLastPong = Date.now() - lastPongReceived;
3966
- if (timeSinceLastPong > PONG_WAIT) {
3967
- if (onError) {
3968
- onError(new Error(`WebSocket stale: no pong in ${timeSinceLastPong}ms`));
3969
- }
3970
- if (notifyCloseResolve) {
3971
- notifyCloseResolve();
3972
- notifyCloseResolve = null;
3973
- }
3974
- ws.terminate();
3975
- return;
3976
- }
3977
- ws.ping();
3978
- }
3979
- }, PING_PERIOD);
3980
- if (onReady) {
3981
- onReady(session.secret);
3982
- }
3983
- resolve();
3984
- });
3985
- ws.on("message", async (data) => {
3986
- try {
3987
- const message = JSON.parse(data.toString());
3988
- const ack = {
3989
- type: "event_ack",
3990
- event_id: message.webhook_id,
3991
- webhook_conversation_id: message.webhook_conversation_id,
3992
- webhook_id: message.webhook_id
3993
- };
3994
- if (ws && ws.readyState === WebSocket.OPEN) {
3995
- ws.send(JSON.stringify(ack));
3996
- }
3997
- let response;
3998
- try {
3999
- const result = await onEvent(message);
4000
- response = {
4001
- type: "webhook_response",
4002
- webhook_id: message.webhook_id,
4003
- webhook_conversation_id: message.webhook_conversation_id,
4004
- forward_url: "stripe-sync-engine",
4005
- status: result?.status ?? 200,
4006
- http_headers: {},
4007
- body: JSON.stringify({
4008
- event_type: result?.event_type,
4009
- event_id: result?.event_id,
4010
- database_url: result?.databaseUrl,
4011
- error: result?.error
4012
- }),
4013
- request_headers: message.http_headers,
4014
- request_body: message.event_payload,
4015
- notification_id: message.webhook_id
4016
- };
4017
- } catch (err) {
4018
- const errorMessage = err instanceof Error ? err.message : String(err);
4019
- response = {
4020
- type: "webhook_response",
4021
- webhook_id: message.webhook_id,
4022
- webhook_conversation_id: message.webhook_conversation_id,
4023
- forward_url: "stripe-sync-engine",
4024
- status: 500,
4025
- http_headers: {},
4026
- body: JSON.stringify({ error: errorMessage }),
4027
- request_headers: message.http_headers,
4028
- request_body: message.event_payload,
4029
- notification_id: message.webhook_id
4030
- };
4031
- if (onError) {
4032
- onError(err instanceof Error ? err : new Error(errorMessage));
4033
- }
4034
- }
4035
- if (ws && ws.readyState === WebSocket.OPEN) {
4036
- ws.send(JSON.stringify(response));
4037
- }
4038
- } catch (err) {
4039
- if (onError) {
4040
- onError(err instanceof Error ? err : new Error(String(err)));
4041
- }
4042
- }
4043
- });
4044
- ws.on("error", (error) => {
4045
- clearTimeout(connectionTimeout);
4046
- if (onError) {
4047
- onError(error);
4048
- }
4049
- if (!connected) {
4050
- reject(error);
4051
- }
4052
- });
4053
- ws.on("close", (code, reason) => {
4054
- clearTimeout(connectionTimeout);
4055
- connected = false;
4056
- if (pingInterval) {
4057
- clearInterval(pingInterval);
4058
- pingInterval = null;
4059
- }
4060
- if (onClose) {
4061
- onClose(code, reason.toString());
4062
- }
4063
- if (notifyCloseResolve) {
4064
- notifyCloseResolve();
4065
- notifyCloseResolve = null;
4066
- }
4067
- });
4068
- });
4069
- }
4070
- async function runLoop() {
4071
- while (shouldRun) {
4072
- connected = false;
4073
- let connectError = null;
4074
- do {
4075
- try {
4076
- await setupWebSocket();
4077
- connectError = null;
4078
- } catch (err) {
4079
- connectError = err instanceof Error ? err : new Error(String(err));
4080
- if (onError) {
4081
- onError(connectError);
4082
- }
4083
- if (shouldRun) {
4084
- await sleep3(CONNECT_ATTEMPT_WAIT);
4085
- }
4086
- }
4087
- } while (connectError && shouldRun);
4088
- if (!shouldRun) break;
4089
- await new Promise((resolve) => {
4090
- notifyCloseResolve = resolve;
4091
- stopResolve = resolve;
4092
- reconnectTimer = setTimeout(() => {
4093
- cleanupConnection();
4094
- resolve();
4095
- }, reconnectInterval);
4096
- });
4097
- if (reconnectTimer) {
4098
- clearTimeout(reconnectTimer);
4099
- reconnectTimer = null;
4100
- }
4101
- notifyCloseResolve = null;
4102
- stopResolve = null;
4103
- }
4104
- cleanupConnection();
4105
- }
4106
- runLoop();
4107
- return {
4108
- close: () => {
4109
- shouldRun = false;
4110
- if (stopResolve) {
4111
- stopResolve();
4112
- stopResolve = null;
4113
- }
4114
- cleanupConnection();
4115
- },
4116
- isConnected: () => connected
4117
- };
4118
- }
4119
-
4120
- // src/index.ts
4121
- var VERSION = package_default.version;
4122
-
4123
- export {
4124
- PostgresClient,
4125
- hashApiKey,
4126
- StripeSync,
4127
- runMigrations,
4128
- createStripeWebSocketClient,
4129
- VERSION
4130
- };