Skip to content

Handle membership updates in Stripe#5992

Merged
pumfleet merged 3 commits intomainfrom
zomars/cal-240-allow-teams-to-edit-of-seats-for
Dec 13, 2022
Merged

Handle membership updates in Stripe#5992
pumfleet merged 3 commits intomainfrom
zomars/cal-240-allow-teams-to-edit-of-seats-for

Conversation

@zomars
Copy link
Copy Markdown
Contributor

@zomars zomars commented Dec 13, 2022

What does this PR do?

  • Adds missing logic for handling removing/adding membership on Stripe when team billing is enabled.

Type of change

  • Chore (refactoring code, technical debt, workflow improvements)

How should this be tested?

  • Add/remove members with team billing enabled
  • Add/remove members with team billing disabled

@linear
Copy link
Copy Markdown

linear Bot commented Dec 13, 2022

@vercel
Copy link
Copy Markdown

vercel Bot commented Dec 13, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
cal ❌ Failed (Inspect) Dec 13, 2022 at 0:47AM (UTC)

Copy link
Copy Markdown
Contributor Author

@zomars zomars left a comment

Choose a reason for hiding this comment

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

Self review done

Comment on lines +9 to +13
const teamPaymentMetadataSchema = z.object({
paymentId: z.string(),
subscriptionId: z.string(),
subscriptionItemId: z.string(),
});
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Duplicated this schema. In this context these values shouldn't be null or undefined. If they do it means the team isn't paid.

Comment on lines +67 to +74
const getTeamWithPaymentMetadata = async (teamId: number) => {
const team = await prisma.team.findUniqueOrThrow({
where: { id: teamId },
select: { metadata: true, members: true },
});
const metadata = teamPaymentMetadataSchema.parse(team.metadata);
return { ...team, metadata };
};
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Extracted reused logic.

Comment on lines +88 to +100
export const updateQuantitySubscriptionFromStripe = async (teamId: number) => {
try {
const team = await getTeamWithPaymentMetadata(teamId);
const { subscriptionId, subscriptionItemId } = team.metadata;
await stripe.subscriptions.update(subscriptionId, {
items: [{ quantity: team.members.length, id: subscriptionItemId }],
});
} catch (error) {
let message = "Unknown error on updateQuantitySubscriptionFromStripe";
if (error instanceof Error) message = error.message;
console.error(message);
}
};
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Instead of +1 / -1 memberships we recalculate the new quantity and update the stripe subscription. This will also help as a callback if we decide to implement invites/removals in bulk.

Comment on lines -395 to -398
// TODO: disable if not hosted by Cal
// @TODO: Update with new logic
// if (teamOwner) await removeSeat(teamOwner.userId, input.teamId, ctx.user.id);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We do this on invite/removal so no need to rerun on invite acceptance.

Comment on lines +90 to +95
const { url } = await checkIfTeamPaymentRequired({ teamId });
/**
* If there's no pending checkout URL it means that this team has not been paid.
* We cannot update the subscription yet, this will be handled on publish/checkout.
**/
if (!url) return;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added explainer

Comment on lines +98 to +100
await stripe.subscriptions.update(subscriptionId, {
items: [{ quantity: team.members.length, id: subscriptionItemId }],
});
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Instead of +1 / -1 we check the new membership count and update. This will allow to use this callback in case we want to add bulk invite/removal in the future.

@zomars zomars marked this pull request as ready for review December 13, 2022 01:19
@zomars zomars requested a review from a team December 13, 2022 01:20
@pumfleet pumfleet merged commit da0bdfa into main Dec 13, 2022
@pumfleet pumfleet deleted the zomars/cal-240-allow-teams-to-edit-of-seats-for branch December 13, 2022 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants