Skip to content
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

fix: speed up team upgrade #9902

Merged
merged 2 commits into from
Jul 1, 2024
Merged

fix: speed up team upgrade #9902

merged 2 commits into from
Jul 1, 2024

Conversation

mattkrick
Copy link
Member

@mattkrick mattkrick commented Jun 28, 2024

Description

Upgrading via stripe takes too long!
It's still too long, but this shaves off about 2 seconds (in the video it was 6!) & now you don't need to listen to webhooks to test locally.

Demo

https://www.loom.com/share/a2482e91d4b940e19dd31f66fb7fa119?sid=67a06848-bc0e-490d-85ac-3d230073d975

Signed-off-by: Matt Krick <matt.krick@gmail.com>
Copy link
Contributor

@nickoferrall nickoferrall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

With this PR, we don't need to listen to webhooks to test the card payment locally, but the org isn't upgraded to the team tier as upgradeToTeamTier isn't called via the webhook.

Other possible low-hanging fruit could be upgrading the Stripe API from version 2020-08-27 to 2024-06-20, but I don't see anything about relevant performance improvements in their changelog, so maybe not: https://docs.stripe.com/upgrades#api-changelog

const res = await manager.attachPaymentToCustomer(customerId, paymentMethodId)
if (res instanceof Error) return standardError(res, {userId: viewerId})
// wait until the payment is attached to the customer before updating the default payment method
await manager.updateDefaultPaymentMethod(customerId, paymentMethodId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the edge case where a user is upgrading for a second time, we could use a Promise.all to speed things up a bit here? Something like:

  const [attachPaymentResult, updateDefaultPaymentResult] = await Promise.all([
    manager.attachPaymentToCustomer(customerId, paymentMethodId),
    manager.updateDefaultPaymentMethod(customerId, paymentMethodId)
  ])
  if (attachPaymentResult instanceof Error) return standardError(attachPaymentResult, {userId: viewerId})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried this...
nope, gotta attach first. ah well, not too bad since re-upgrade is such an edge case!

} else {
customer = await manager.createCustomer(orgId, email, paymentMethodId)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And we could remove the await on line 65 when updating rethinkdb?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was thinking about that! but since our DB is colocated it's usually only a couple ms so I figured probably better to be safe than sorry in case the write failed for some reason.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second look, we should completely get rid of the write to the DB here!
We should only update our DB when the payment succeeds, otherwise if we update the stripeSubscriptionId & then the 3D payment fails, the org gets stuck in a weird state where it can't upgrade because it has a subscription, but it can't downgrade because it's still on the starter tier 😬

Signed-off-by: Matt Krick <matt.krick@gmail.com>
@github-actions github-actions bot added size/m and removed size/s labels Jul 1, 2024
@mattkrick mattkrick merged commit d91f649 into master Jul 1, 2024
5 checks passed
@mattkrick mattkrick deleted the fix/upgrade-speed branch July 1, 2024 19:27
@github-actions github-actions bot mentioned this pull request Jul 2, 2024
24 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants