-
Notifications
You must be signed in to change notification settings - Fork 114
Fix entitlement pagination truncation and invoice.upcoming crash #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -233,7 +233,13 @@ export class StripeSync { | |
| case 'invoice.payment_action_required': | ||
| case 'invoice.payment_failed': | ||
| case 'invoice.payment_succeeded': | ||
| case 'invoice.upcoming': | ||
| case 'invoice.upcoming': { | ||
| // invoice.upcoming is a preview invoice with no id — it cannot be persisted. | ||
| this.config.logger?.info( | ||
| `Received webhook ${event.id}: ${event.type} — skipping (preview invoice has no id)` | ||
| ) | ||
| break | ||
| } | ||
| case 'invoice.sent': | ||
| case 'invoice.voided': | ||
| case 'invoice.marked_uncollectible': | ||
|
|
@@ -529,12 +535,28 @@ export class StripeSync { | |
| .object as Stripe.Entitlements.ActiveEntitlementSummary | ||
| let entitlements = activeEntitlementSummary.entitlements | ||
| let refetched = false | ||
| if (this.config.revalidateObjectsViaStripeApi?.includes('entitlements')) { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const { lastResponse, ...rest } = await this.stripe.entitlements.activeEntitlements.list({ | ||
| if ( | ||
| this.config.revalidateObjectsViaStripeApi?.includes('entitlements') || | ||
| entitlements.has_more | ||
| ) { | ||
| // Fetch all pages from the API — the webhook payload is capped at 10 items | ||
| // and has_more signals that there are more entitlements than were included. | ||
| const allData: Stripe.Entitlements.ActiveEntitlement[] = [] | ||
| let page = await this.stripe.entitlements.activeEntitlements.list({ | ||
| customer: activeEntitlementSummary.customer, | ||
| }) | ||
| entitlements = rest | ||
| limit: 100, | ||
| } as Stripe.Entitlements.ActiveEntitlementListParams) | ||
| allData.push(...page.data) | ||
| while (page.has_more) { | ||
| const lastId = page.data[page.data.length - 1].id | ||
| page = await this.stripe.entitlements.activeEntitlements.list({ | ||
| customer: activeEntitlementSummary.customer, | ||
| limit: 100, | ||
| starting_after: lastId, | ||
| } as Stripe.Entitlements.ActiveEntitlementListParams) | ||
| allData.push(...page.data) | ||
| } | ||
|
Comment on lines
+542
to
+558
|
||
| entitlements = { ...entitlements, data: allData, has_more: false } | ||
| refetched = true | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this
case 'invoice.upcoming'now contains executable statements and abreak, every precedinginvoice.*case label (invoice.created/deleted/finalized/etc.) will fall through into this block and be skipped. This changes behavior for all invoice webhook types, not justinvoice.upcoming. Move theinvoice.upcominghandler to its own case position before the shared invoice cases (or otherwise restructure so onlyinvoice.upcominghits thisbreak).