Improve webhook handler robustness: upserts, final state, customer.deleted#29
Open
advaitpaliwal wants to merge 1 commit intoget-convex:masterfrom
Open
Improve webhook handler robustness: upserts, final state, customer.deleted#29advaitpaliwal wants to merge 1 commit intoget-convex:masterfrom
advaitpaliwal wants to merge 1 commit intoget-convex:masterfrom
Conversation
- handleSubscriptionUpdated now upserts: creates the subscription if it doesn't exist yet (fixes webhook ordering where updated arrives before created). Requires stripeCustomerId passed from the event. - handleInvoiceCreated now updates existing invoices, fixing invoice.finalized events silently dropping updated amounts/status when the invoice was already created by invoice.created. - handleSubscriptionDeleted now stores final state (currentPeriodEnd, cancelAt) instead of only setting status to "canceled" and discarding all other fields from the event. - Add customer.deleted webhook event handling that removes the customer record from the database. - Fix cancelAt || undefined → cancelAt ?? undefined throughout to avoid coercing a valid 0 timestamp to undefined. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Five webhook handler fixes in one PR since they're all closely related:
handleSubscriptionUpdatednow upserts — ifsubscription.updatedarrives beforesubscription.created(webhook ordering), the data was silently dropped. Now creates the record if missing, preventing lost subscriptions.handleInvoiceCreatednow updates existing invoices —invoice.finalizedcalled the same handler asinvoice.created, but it was insert-only. Updated amounts, status, and subscription links from finalization were silently dropped. Now patches existing records.handleSubscriptionDeletedstores final state — previously only setstatus: "canceled"and discardedcurrentPeriodEnd,cancelAtfrom the event. Now stores the final timestamps.customer.deletedevent handling — deletes the customer record when a customer is removed in Stripe (dashboard or API). Previously left stale records.cancelAt || undefined→cancelAt ?? undefined—||coerces a valid0timestamp toundefined. Changed to??throughout for correctness.Test plan
subscription.updatedarriving beforesubscription.creatednow creates the subscription recordinvoice.finalizedupdates existing invoice records (amount, status)subscription.deletedstorescurrentPeriodEndandcancelAtin the recordcustomer.deletedremoves the customer from the database🤖 Generated with Claude Code