@paymentsdb/sync-engine 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-LB4HG4Q6.js → chunk-2KB2ISYF.js} +471 -6
- package/dist/{chunk-HXSDJSKR.js → chunk-E6BGC7CB.js} +1 -1
- package/dist/{chunk-Q3AGYUHN.js → chunk-GXIMCH5Y.js} +1 -1
- package/dist/{chunk-DZMKGCU5.js → chunk-WHNAH5B7.js} +2 -2
- package/dist/cli/index.cjs +471 -6
- package/dist/cli/index.js +4 -4
- package/dist/cli/lib.cjs +471 -6
- package/dist/cli/lib.js +4 -4
- package/dist/index.cjs +471 -6
- package/dist/index.d.cts +40 -1
- package/dist/index.d.ts +40 -1
- package/dist/index.js +2 -2
- package/dist/supabase/index.cjs +1 -1
- package/dist/supabase/index.js +2 -2
- package/package.json +1 -1
package/dist/cli/index.cjs
CHANGED
|
@@ -33,7 +33,7 @@ var import_commander = require("commander");
|
|
|
33
33
|
// package.json
|
|
34
34
|
var package_default = {
|
|
35
35
|
name: "@paymentsdb/sync-engine",
|
|
36
|
-
version: "0.0.
|
|
36
|
+
version: "0.0.5",
|
|
37
37
|
private: false,
|
|
38
38
|
description: "Stripe Sync Engine to sync Stripe data to Postgres",
|
|
39
39
|
type: "module",
|
|
@@ -45501,6 +45501,13 @@ var StripeSync = class {
|
|
|
45501
45501
|
listFn: (p) => this.stripe.checkout.sessions.list(p),
|
|
45502
45502
|
upsertFn: (items, id) => this.upsertCheckoutSessions(items, id),
|
|
45503
45503
|
supportsCreatedFilter: true
|
|
45504
|
+
},
|
|
45505
|
+
_event_catchup: {
|
|
45506
|
+
order: 99,
|
|
45507
|
+
// Always runs last — catches missed webhook events
|
|
45508
|
+
listFn: (_p) => Promise.resolve({ data: [], has_more: false }),
|
|
45509
|
+
upsertFn: async () => [],
|
|
45510
|
+
supportsCreatedFilter: true
|
|
45504
45511
|
}
|
|
45505
45512
|
};
|
|
45506
45513
|
const maxOrder = Math.max(...Object.values(core).map((cfg) => cfg.order));
|
|
@@ -46267,7 +46274,8 @@ ${message}`;
|
|
|
46267
46274
|
credit_note: "credit_notes",
|
|
46268
46275
|
early_fraud_warning: "early_fraud_warnings",
|
|
46269
46276
|
refund: "refunds",
|
|
46270
|
-
checkout_sessions: "checkout_sessions"
|
|
46277
|
+
checkout_sessions: "checkout_sessions",
|
|
46278
|
+
_event_catchup: "_event_catchup"
|
|
46271
46279
|
};
|
|
46272
46280
|
return mapping[object] || object;
|
|
46273
46281
|
}
|
|
@@ -46278,10 +46286,11 @@ ${message}`;
|
|
|
46278
46286
|
*/
|
|
46279
46287
|
async fetchOnePage(object, accountId, resourceName, runStartedAt, cursor, pageCursor, params) {
|
|
46280
46288
|
const limit = 100;
|
|
46281
|
-
if (object === "payment_method"
|
|
46282
|
-
this.
|
|
46283
|
-
|
|
46284
|
-
|
|
46289
|
+
if (object === "payment_method") {
|
|
46290
|
+
return this.fetchOnePagePaymentMethods(accountId, resourceName, runStartedAt, cursor, pageCursor, params);
|
|
46291
|
+
}
|
|
46292
|
+
if (object === "_event_catchup") {
|
|
46293
|
+
return this.fetchOnePageEventCatchup(accountId, resourceName, runStartedAt, cursor, pageCursor);
|
|
46285
46294
|
}
|
|
46286
46295
|
const config = this.resourceRegistry[object];
|
|
46287
46296
|
if (!config) {
|
|
@@ -46459,6 +46468,435 @@ ${message}`;
|
|
|
46459
46468
|
}
|
|
46460
46469
|
return { processed: entries.length, hasMore, runStartedAt };
|
|
46461
46470
|
}
|
|
46471
|
+
/**
|
|
46472
|
+
* Fetch payment methods by iterating customers in batches.
|
|
46473
|
+
*
|
|
46474
|
+
* Most customers have 1-2 PMs, so processing one customer per processNext call
|
|
46475
|
+
* would be extremely wasteful (10,000 customers = 10,000 worker round-trips).
|
|
46476
|
+
* Instead, each call fetches a batch of customers (sized by maxConcurrentCustomers,
|
|
46477
|
+
* default 10) and lists+upserts PMs for all of them concurrently.
|
|
46478
|
+
*
|
|
46479
|
+
* Cursor semantics:
|
|
46480
|
+
* - cursor: max customer `created` timestamp — filters to only new customers on subsequent runs
|
|
46481
|
+
* - pageCursor: last processed customer ID — tracks position in customer iteration
|
|
46482
|
+
*/
|
|
46483
|
+
async fetchOnePagePaymentMethods(accountId, resourceName, runStartedAt, cursor, pageCursor, params) {
|
|
46484
|
+
const batchSize = this.config.maxConcurrentCustomers ?? 10;
|
|
46485
|
+
try {
|
|
46486
|
+
const customerBatch = await this.findNextCustomerBatchForPmSync(
|
|
46487
|
+
accountId,
|
|
46488
|
+
pageCursor,
|
|
46489
|
+
cursor,
|
|
46490
|
+
batchSize
|
|
46491
|
+
);
|
|
46492
|
+
if (customerBatch.length === 0) {
|
|
46493
|
+
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
46494
|
+
return { processed: 0, hasMore: false, runStartedAt };
|
|
46495
|
+
}
|
|
46496
|
+
this.config.logger?.info(
|
|
46497
|
+
`processNext: fetching payment_methods for ${customerBatch.length} customers`
|
|
46498
|
+
);
|
|
46499
|
+
let totalProcessed = 0;
|
|
46500
|
+
await Promise.all(
|
|
46501
|
+
customerBatch.map(async (customer) => {
|
|
46502
|
+
const allPms = [];
|
|
46503
|
+
let hasMore = true;
|
|
46504
|
+
let startingAfter;
|
|
46505
|
+
while (hasMore) {
|
|
46506
|
+
const response = await this.stripe.paymentMethods.list({
|
|
46507
|
+
customer: customer.id,
|
|
46508
|
+
limit: 100,
|
|
46509
|
+
...startingAfter ? { starting_after: startingAfter } : {}
|
|
46510
|
+
});
|
|
46511
|
+
allPms.push(...response.data);
|
|
46512
|
+
hasMore = response.has_more;
|
|
46513
|
+
if (response.data.length > 0) {
|
|
46514
|
+
startingAfter = response.data[response.data.length - 1].id;
|
|
46515
|
+
}
|
|
46516
|
+
}
|
|
46517
|
+
if (allPms.length > 0) {
|
|
46518
|
+
await this.upsertPaymentMethods(allPms, accountId, params?.backfillRelatedEntities);
|
|
46519
|
+
totalProcessed += allPms.length;
|
|
46520
|
+
}
|
|
46521
|
+
})
|
|
46522
|
+
);
|
|
46523
|
+
if (totalProcessed > 0) {
|
|
46524
|
+
await this.postgresClient.incrementObjectProgress(
|
|
46525
|
+
accountId,
|
|
46526
|
+
runStartedAt,
|
|
46527
|
+
resourceName,
|
|
46528
|
+
totalProcessed
|
|
46529
|
+
);
|
|
46530
|
+
}
|
|
46531
|
+
const maxCreated = Math.max(...customerBatch.map((c) => c.created).filter((c) => c != null));
|
|
46532
|
+
if (maxCreated > 0) {
|
|
46533
|
+
await this.postgresClient.updateObjectCursor(
|
|
46534
|
+
accountId,
|
|
46535
|
+
runStartedAt,
|
|
46536
|
+
resourceName,
|
|
46537
|
+
String(maxCreated)
|
|
46538
|
+
);
|
|
46539
|
+
}
|
|
46540
|
+
const lastCustomerId = customerBatch[customerBatch.length - 1].id;
|
|
46541
|
+
const nextBatch = await this.findNextCustomerBatchForPmSync(
|
|
46542
|
+
accountId,
|
|
46543
|
+
lastCustomerId,
|
|
46544
|
+
cursor,
|
|
46545
|
+
1
|
|
46546
|
+
);
|
|
46547
|
+
if (nextBatch.length > 0) {
|
|
46548
|
+
await this.postgresClient.updateObjectPageCursor(
|
|
46549
|
+
accountId,
|
|
46550
|
+
runStartedAt,
|
|
46551
|
+
resourceName,
|
|
46552
|
+
lastCustomerId
|
|
46553
|
+
);
|
|
46554
|
+
return { processed: totalProcessed, hasMore: true, runStartedAt };
|
|
46555
|
+
}
|
|
46556
|
+
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
46557
|
+
return { processed: totalProcessed, hasMore: false, runStartedAt };
|
|
46558
|
+
} catch (error) {
|
|
46559
|
+
await this.postgresClient.failObjectSync(
|
|
46560
|
+
accountId,
|
|
46561
|
+
runStartedAt,
|
|
46562
|
+
resourceName,
|
|
46563
|
+
error instanceof Error ? error.message : "Unknown error"
|
|
46564
|
+
);
|
|
46565
|
+
throw error;
|
|
46566
|
+
}
|
|
46567
|
+
}
|
|
46568
|
+
/**
|
|
46569
|
+
* Fetch the next batch of non-deleted customers for PM sync.
|
|
46570
|
+
* Returns customer IDs and created timestamps, ordered by id ASC.
|
|
46571
|
+
*/
|
|
46572
|
+
async findNextCustomerBatchForPmSync(accountId, afterCustomerId, cursor, limit) {
|
|
46573
|
+
let query = `SELECT id, created FROM stripe.customers WHERE _account_id = $1 AND COALESCE(deleted, false) <> true`;
|
|
46574
|
+
const params = [accountId];
|
|
46575
|
+
if (afterCustomerId) {
|
|
46576
|
+
params.push(afterCustomerId);
|
|
46577
|
+
query += ` AND id > $${params.length}`;
|
|
46578
|
+
}
|
|
46579
|
+
if (cursor && /^\d+$/.test(cursor)) {
|
|
46580
|
+
params.push(Number.parseInt(cursor, 10));
|
|
46581
|
+
query += ` AND created >= $${params.length}`;
|
|
46582
|
+
}
|
|
46583
|
+
params.push(limit);
|
|
46584
|
+
query += ` ORDER BY id ASC LIMIT $${params.length}`;
|
|
46585
|
+
const result = await this.postgresClient.query(query, params);
|
|
46586
|
+
return result.rows;
|
|
46587
|
+
}
|
|
46588
|
+
/**
|
|
46589
|
+
* Fetch one page of events from the Stripe Events API and reconcile affected entities.
|
|
46590
|
+
*
|
|
46591
|
+
* Instead of replaying events (which can resurrect deleted objects due to newest-first ordering),
|
|
46592
|
+
* we deduplicate by entity and re-fetch current state from Stripe for each affected entity.
|
|
46593
|
+
*
|
|
46594
|
+
* Cursor: event `created` timestamp. On first run, starts from the sync run's startedAt.
|
|
46595
|
+
* On subsequent runs, picks up where the last completed run left off.
|
|
46596
|
+
*/
|
|
46597
|
+
async fetchOnePageEventCatchup(accountId, resourceName, runStartedAt, cursor, pageCursor) {
|
|
46598
|
+
try {
|
|
46599
|
+
let createdGte;
|
|
46600
|
+
if (cursor && /^\d+$/.test(cursor)) {
|
|
46601
|
+
createdGte = Number.parseInt(cursor, 10);
|
|
46602
|
+
} else {
|
|
46603
|
+
createdGte = Math.floor(runStartedAt.getTime() / 1e3);
|
|
46604
|
+
}
|
|
46605
|
+
const thirtyDaysAgo = Math.floor(Date.now() / 1e3) - 30 * 24 * 60 * 60;
|
|
46606
|
+
if (createdGte < thirtyDaysAgo) {
|
|
46607
|
+
this.config.logger?.warn(
|
|
46608
|
+
`_event_catchup: cursor ${createdGte} is older than 30 days, clamping to ${thirtyDaysAgo}`
|
|
46609
|
+
);
|
|
46610
|
+
createdGte = thirtyDaysAgo;
|
|
46611
|
+
}
|
|
46612
|
+
const listParams = {
|
|
46613
|
+
limit: 100,
|
|
46614
|
+
created: { gte: createdGte }
|
|
46615
|
+
};
|
|
46616
|
+
if (pageCursor) {
|
|
46617
|
+
listParams.starting_after = pageCursor;
|
|
46618
|
+
}
|
|
46619
|
+
const response = await this.stripe.events.list(listParams);
|
|
46620
|
+
if (response.data.length === 0) {
|
|
46621
|
+
await this.postgresClient.updateObjectCursor(
|
|
46622
|
+
accountId,
|
|
46623
|
+
runStartedAt,
|
|
46624
|
+
resourceName,
|
|
46625
|
+
String(createdGte)
|
|
46626
|
+
);
|
|
46627
|
+
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
46628
|
+
return { processed: 0, hasMore: false, runStartedAt };
|
|
46629
|
+
}
|
|
46630
|
+
const entityMap = /* @__PURE__ */ new Map();
|
|
46631
|
+
for (const event of response.data) {
|
|
46632
|
+
const obj = event.data.object;
|
|
46633
|
+
if (!obj?.id || !obj?.object) continue;
|
|
46634
|
+
const key = `${obj.object}:${obj.id}`;
|
|
46635
|
+
const existing = entityMap.get(key);
|
|
46636
|
+
if (!existing || event.created > existing.created) {
|
|
46637
|
+
entityMap.set(key, event);
|
|
46638
|
+
}
|
|
46639
|
+
}
|
|
46640
|
+
const hardDeleteEventTypes = /* @__PURE__ */ new Set([
|
|
46641
|
+
"product.deleted",
|
|
46642
|
+
"price.deleted",
|
|
46643
|
+
"plan.deleted",
|
|
46644
|
+
"customer.deleted",
|
|
46645
|
+
"customer.tax_id.deleted"
|
|
46646
|
+
]);
|
|
46647
|
+
let processed = 0;
|
|
46648
|
+
let skipped = 0;
|
|
46649
|
+
for (const [, event] of entityMap) {
|
|
46650
|
+
const obj = event.data.object;
|
|
46651
|
+
const eventType = event.type;
|
|
46652
|
+
try {
|
|
46653
|
+
if (!hardDeleteEventTypes.has(eventType)) {
|
|
46654
|
+
const tableName = eventObjectTypeToTable(obj.object);
|
|
46655
|
+
if (tableName) {
|
|
46656
|
+
const localRecord = await this.postgresClient.query(
|
|
46657
|
+
`SELECT "_last_synced_at" FROM stripe."${tableName}"
|
|
46658
|
+
WHERE id = $1 AND "_account_id" = $2 LIMIT 1`,
|
|
46659
|
+
[obj.id, accountId]
|
|
46660
|
+
);
|
|
46661
|
+
if (localRecord.rows.length > 0 && localRecord.rows[0]._last_synced_at != null) {
|
|
46662
|
+
const syncedAt = new Date(localRecord.rows[0]._last_synced_at).getTime() / 1e3;
|
|
46663
|
+
if (syncedAt >= event.created) {
|
|
46664
|
+
skipped++;
|
|
46665
|
+
continue;
|
|
46666
|
+
}
|
|
46667
|
+
}
|
|
46668
|
+
}
|
|
46669
|
+
}
|
|
46670
|
+
if (hardDeleteEventTypes.has(eventType)) {
|
|
46671
|
+
await this.handleEventCatchupDelete(obj.object, obj.id, accountId);
|
|
46672
|
+
} else {
|
|
46673
|
+
await this.handleEventCatchupUpsert(obj.object, obj.id, accountId);
|
|
46674
|
+
}
|
|
46675
|
+
processed++;
|
|
46676
|
+
} catch (err) {
|
|
46677
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
46678
|
+
this.config.logger?.warn(
|
|
46679
|
+
`_event_catchup: failed to process ${obj.object}:${obj.id} (event ${event.id}): ${errMsg}`
|
|
46680
|
+
);
|
|
46681
|
+
}
|
|
46682
|
+
}
|
|
46683
|
+
if (skipped > 0) {
|
|
46684
|
+
this.config.logger?.info(
|
|
46685
|
+
`_event_catchup: skipped ${skipped} entities already up-to-date, processed ${processed}`
|
|
46686
|
+
);
|
|
46687
|
+
}
|
|
46688
|
+
if (processed > 0) {
|
|
46689
|
+
await this.postgresClient.incrementObjectProgress(
|
|
46690
|
+
accountId,
|
|
46691
|
+
runStartedAt,
|
|
46692
|
+
resourceName,
|
|
46693
|
+
processed
|
|
46694
|
+
);
|
|
46695
|
+
}
|
|
46696
|
+
const maxCreated = Math.max(...response.data.map((e) => e.created));
|
|
46697
|
+
await this.postgresClient.updateObjectCursor(
|
|
46698
|
+
accountId,
|
|
46699
|
+
runStartedAt,
|
|
46700
|
+
resourceName,
|
|
46701
|
+
String(maxCreated)
|
|
46702
|
+
);
|
|
46703
|
+
const lastEventId = response.data[response.data.length - 1].id;
|
|
46704
|
+
if (response.has_more) {
|
|
46705
|
+
await this.postgresClient.updateObjectPageCursor(
|
|
46706
|
+
accountId,
|
|
46707
|
+
runStartedAt,
|
|
46708
|
+
resourceName,
|
|
46709
|
+
lastEventId
|
|
46710
|
+
);
|
|
46711
|
+
}
|
|
46712
|
+
if (!response.has_more) {
|
|
46713
|
+
await this.postgresClient.completeObjectSync(accountId, runStartedAt, resourceName);
|
|
46714
|
+
}
|
|
46715
|
+
return { processed, hasMore: response.has_more, runStartedAt };
|
|
46716
|
+
} catch (error) {
|
|
46717
|
+
await this.postgresClient.failObjectSync(
|
|
46718
|
+
accountId,
|
|
46719
|
+
runStartedAt,
|
|
46720
|
+
resourceName,
|
|
46721
|
+
error instanceof Error ? error.message : "Unknown error"
|
|
46722
|
+
);
|
|
46723
|
+
throw error;
|
|
46724
|
+
}
|
|
46725
|
+
}
|
|
46726
|
+
/**
|
|
46727
|
+
* Handle a delete for an entity discovered via event catch-up.
|
|
46728
|
+
* Maps Stripe object types to the appropriate delete method.
|
|
46729
|
+
*/
|
|
46730
|
+
async handleEventCatchupDelete(objectType, entityId, accountId) {
|
|
46731
|
+
switch (objectType) {
|
|
46732
|
+
case "product":
|
|
46733
|
+
await this.deleteProduct(entityId);
|
|
46734
|
+
break;
|
|
46735
|
+
case "price":
|
|
46736
|
+
await this.deletePrice(entityId);
|
|
46737
|
+
break;
|
|
46738
|
+
case "plan":
|
|
46739
|
+
await this.deletePlan(entityId);
|
|
46740
|
+
break;
|
|
46741
|
+
case "customer": {
|
|
46742
|
+
const deletedCustomer = {
|
|
46743
|
+
id: entityId,
|
|
46744
|
+
object: "customer",
|
|
46745
|
+
deleted: true
|
|
46746
|
+
};
|
|
46747
|
+
await this.upsertCustomers([deletedCustomer], accountId);
|
|
46748
|
+
break;
|
|
46749
|
+
}
|
|
46750
|
+
case "tax_id":
|
|
46751
|
+
await this.deleteTaxId(entityId);
|
|
46752
|
+
break;
|
|
46753
|
+
default:
|
|
46754
|
+
this.config.logger?.warn(
|
|
46755
|
+
`_event_catchup: no delete handler for object type "${objectType}", skipping ${entityId}`
|
|
46756
|
+
);
|
|
46757
|
+
}
|
|
46758
|
+
}
|
|
46759
|
+
/**
|
|
46760
|
+
* Handle an upsert for an entity discovered via event catch-up.
|
|
46761
|
+
* Re-fetches the current state from Stripe and upserts it.
|
|
46762
|
+
* If the entity has been deleted (404), falls back to the delete handler.
|
|
46763
|
+
*/
|
|
46764
|
+
async handleEventCatchupUpsert(objectType, entityId, accountId) {
|
|
46765
|
+
try {
|
|
46766
|
+
switch (objectType) {
|
|
46767
|
+
case "product": {
|
|
46768
|
+
const product = await this.stripe.products.retrieve(entityId);
|
|
46769
|
+
await this.upsertProducts([product], accountId);
|
|
46770
|
+
break;
|
|
46771
|
+
}
|
|
46772
|
+
case "price": {
|
|
46773
|
+
const price = await this.stripe.prices.retrieve(entityId);
|
|
46774
|
+
await this.upsertPrices([price], accountId);
|
|
46775
|
+
break;
|
|
46776
|
+
}
|
|
46777
|
+
case "plan": {
|
|
46778
|
+
const plan = await this.stripe.plans.retrieve(entityId);
|
|
46779
|
+
await this.upsertPlans([plan], accountId);
|
|
46780
|
+
break;
|
|
46781
|
+
}
|
|
46782
|
+
case "customer": {
|
|
46783
|
+
const customer = await this.stripe.customers.retrieve(entityId);
|
|
46784
|
+
await this.upsertCustomers([customer], accountId);
|
|
46785
|
+
break;
|
|
46786
|
+
}
|
|
46787
|
+
case "subscription": {
|
|
46788
|
+
const sub = await this.stripe.subscriptions.retrieve(entityId);
|
|
46789
|
+
await this.upsertSubscriptions([sub], accountId);
|
|
46790
|
+
break;
|
|
46791
|
+
}
|
|
46792
|
+
case "subscription_schedule": {
|
|
46793
|
+
const schedule = await this.stripe.subscriptionSchedules.retrieve(entityId);
|
|
46794
|
+
await this.upsertSubscriptionSchedules([schedule], accountId);
|
|
46795
|
+
break;
|
|
46796
|
+
}
|
|
46797
|
+
case "invoice": {
|
|
46798
|
+
const invoice = await this.stripe.invoices.retrieve(entityId);
|
|
46799
|
+
await this.upsertInvoices([invoice], accountId);
|
|
46800
|
+
break;
|
|
46801
|
+
}
|
|
46802
|
+
case "charge": {
|
|
46803
|
+
const charge = await this.stripe.charges.retrieve(entityId, {
|
|
46804
|
+
expand: ["balance_transaction"]
|
|
46805
|
+
});
|
|
46806
|
+
if (charge.balance_transaction && typeof charge.balance_transaction === "object") {
|
|
46807
|
+
await this.upsertBalanceTransactions(
|
|
46808
|
+
[charge.balance_transaction],
|
|
46809
|
+
accountId
|
|
46810
|
+
);
|
|
46811
|
+
}
|
|
46812
|
+
await this.upsertCharges([charge], accountId);
|
|
46813
|
+
break;
|
|
46814
|
+
}
|
|
46815
|
+
case "payment_intent": {
|
|
46816
|
+
const pi = await this.stripe.paymentIntents.retrieve(entityId);
|
|
46817
|
+
await this.upsertPaymentIntents([pi], accountId);
|
|
46818
|
+
break;
|
|
46819
|
+
}
|
|
46820
|
+
case "payment_method": {
|
|
46821
|
+
const pm = await this.stripe.paymentMethods.retrieve(entityId);
|
|
46822
|
+
await this.upsertPaymentMethods([pm], accountId);
|
|
46823
|
+
break;
|
|
46824
|
+
}
|
|
46825
|
+
case "setup_intent": {
|
|
46826
|
+
const si = await this.stripe.setupIntents.retrieve(entityId);
|
|
46827
|
+
await this.upsertSetupIntents([si], accountId);
|
|
46828
|
+
break;
|
|
46829
|
+
}
|
|
46830
|
+
case "dispute": {
|
|
46831
|
+
const dispute = await this.stripe.disputes.retrieve(entityId, {
|
|
46832
|
+
expand: ["balance_transactions"]
|
|
46833
|
+
});
|
|
46834
|
+
if (dispute.balance_transactions && Array.isArray(dispute.balance_transactions)) {
|
|
46835
|
+
const expandedBts = dispute.balance_transactions.filter(
|
|
46836
|
+
(bt) => typeof bt === "object"
|
|
46837
|
+
);
|
|
46838
|
+
if (expandedBts.length > 0) {
|
|
46839
|
+
await this.upsertBalanceTransactions(expandedBts, accountId);
|
|
46840
|
+
}
|
|
46841
|
+
}
|
|
46842
|
+
await this.upsertDisputes([dispute], accountId);
|
|
46843
|
+
break;
|
|
46844
|
+
}
|
|
46845
|
+
case "credit_note": {
|
|
46846
|
+
const cn = await this.stripe.creditNotes.retrieve(entityId);
|
|
46847
|
+
await this.upsertCreditNotes([cn], accountId);
|
|
46848
|
+
break;
|
|
46849
|
+
}
|
|
46850
|
+
case "refund": {
|
|
46851
|
+
const refund = await this.stripe.refunds.retrieve(entityId, {
|
|
46852
|
+
expand: ["balance_transaction"]
|
|
46853
|
+
});
|
|
46854
|
+
if (refund.balance_transaction && typeof refund.balance_transaction === "object") {
|
|
46855
|
+
await this.upsertBalanceTransactions(
|
|
46856
|
+
[refund.balance_transaction],
|
|
46857
|
+
accountId
|
|
46858
|
+
);
|
|
46859
|
+
}
|
|
46860
|
+
await this.upsertRefunds([refund], accountId);
|
|
46861
|
+
break;
|
|
46862
|
+
}
|
|
46863
|
+
case "tax_id": {
|
|
46864
|
+
const taxId = await this.stripe.taxIds.retrieve(entityId);
|
|
46865
|
+
await this.upsertTaxIds([taxId], accountId);
|
|
46866
|
+
break;
|
|
46867
|
+
}
|
|
46868
|
+
case "balance_transaction": {
|
|
46869
|
+
const bt = await this.stripe.balanceTransactions.retrieve(entityId);
|
|
46870
|
+
await this.upsertBalanceTransactions([bt], accountId);
|
|
46871
|
+
break;
|
|
46872
|
+
}
|
|
46873
|
+
case "checkout.session": {
|
|
46874
|
+
const session = await this.stripe.checkout.sessions.retrieve(entityId);
|
|
46875
|
+
await this.upsertCheckoutSessions([session], accountId);
|
|
46876
|
+
break;
|
|
46877
|
+
}
|
|
46878
|
+
case "radar.early_fraud_warning": {
|
|
46879
|
+
const efw = await this.stripe.radar.earlyFraudWarnings.retrieve(entityId);
|
|
46880
|
+
await this.upsertEarlyFraudWarning([efw], accountId);
|
|
46881
|
+
break;
|
|
46882
|
+
}
|
|
46883
|
+
default:
|
|
46884
|
+
this.config.logger?.warn(
|
|
46885
|
+
`_event_catchup: no upsert handler for object type "${objectType}", skipping ${entityId}`
|
|
46886
|
+
);
|
|
46887
|
+
}
|
|
46888
|
+
} catch (err) {
|
|
46889
|
+
const isNotFound = (err instanceof import_stripe3.default.errors.StripeInvalidRequestError || err instanceof import_stripe3.default.errors.StripeAPIError) && (err.code === "resource_missing" || err.statusCode === 404);
|
|
46890
|
+
if (isNotFound) {
|
|
46891
|
+
this.config.logger?.info(
|
|
46892
|
+
`_event_catchup: ${objectType}:${entityId} not found on Stripe, treating as deleted`
|
|
46893
|
+
);
|
|
46894
|
+
await this.handleEventCatchupDelete(objectType, entityId, accountId);
|
|
46895
|
+
return;
|
|
46896
|
+
}
|
|
46897
|
+
throw err;
|
|
46898
|
+
}
|
|
46899
|
+
}
|
|
46462
46900
|
/**
|
|
46463
46901
|
* Process all pages for all (or specified) object types until complete.
|
|
46464
46902
|
*
|
|
@@ -46532,6 +46970,7 @@ ${message}`;
|
|
|
46532
46970
|
};
|
|
46533
46971
|
}
|
|
46534
46972
|
applySyncBackfillResult(results, object, result) {
|
|
46973
|
+
if (object === "_event_catchup") return;
|
|
46535
46974
|
if (this.isSigmaResource(object)) {
|
|
46536
46975
|
results.sigma = results.sigma ?? {};
|
|
46537
46976
|
results.sigma[object] = result;
|
|
@@ -46567,6 +47006,9 @@ ${message}`;
|
|
|
46567
47006
|
case "setup_intent":
|
|
46568
47007
|
results.setupIntents = result;
|
|
46569
47008
|
break;
|
|
47009
|
+
case "payment_method":
|
|
47010
|
+
results.paymentMethods = result;
|
|
47011
|
+
break;
|
|
46570
47012
|
case "payment_intent":
|
|
46571
47013
|
results.paymentIntents = result;
|
|
46572
47014
|
break;
|
|
@@ -48074,6 +48516,29 @@ function chunkArray(array, chunkSize) {
|
|
|
48074
48516
|
}
|
|
48075
48517
|
return result;
|
|
48076
48518
|
}
|
|
48519
|
+
function eventObjectTypeToTable(objectType) {
|
|
48520
|
+
const mapping = {
|
|
48521
|
+
product: "products",
|
|
48522
|
+
price: "prices",
|
|
48523
|
+
plan: "plans",
|
|
48524
|
+
customer: "customers",
|
|
48525
|
+
subscription: "subscriptions",
|
|
48526
|
+
subscription_schedule: "subscription_schedules",
|
|
48527
|
+
invoice: "invoices",
|
|
48528
|
+
charge: "charges",
|
|
48529
|
+
balance_transaction: "balance_transactions",
|
|
48530
|
+
payment_intent: "payment_intents",
|
|
48531
|
+
payment_method: "payment_methods",
|
|
48532
|
+
setup_intent: "setup_intents",
|
|
48533
|
+
dispute: "disputes",
|
|
48534
|
+
credit_note: "credit_notes",
|
|
48535
|
+
refund: "refunds",
|
|
48536
|
+
tax_id: "tax_ids",
|
|
48537
|
+
"checkout.session": "checkout_sessions",
|
|
48538
|
+
"radar.early_fraud_warning": "early_fraud_warnings"
|
|
48539
|
+
};
|
|
48540
|
+
return mapping[objectType] ?? null;
|
|
48541
|
+
}
|
|
48077
48542
|
|
|
48078
48543
|
// src/database/migrate.ts
|
|
48079
48544
|
var import_pg2 = require("pg");
|
package/dist/cli/index.js
CHANGED
|
@@ -5,12 +5,12 @@ import {
|
|
|
5
5
|
migrateCommand,
|
|
6
6
|
syncCommand,
|
|
7
7
|
uninstallCommand
|
|
8
|
-
} from "../chunk-
|
|
9
|
-
import "../chunk-
|
|
10
|
-
import "../chunk-
|
|
8
|
+
} from "../chunk-WHNAH5B7.js";
|
|
9
|
+
import "../chunk-2KB2ISYF.js";
|
|
10
|
+
import "../chunk-GXIMCH5Y.js";
|
|
11
11
|
import {
|
|
12
12
|
package_default
|
|
13
|
-
} from "../chunk-
|
|
13
|
+
} from "../chunk-E6BGC7CB.js";
|
|
14
14
|
|
|
15
15
|
// src/cli/index.ts
|
|
16
16
|
import { Command } from "commander";
|