Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| const teamPaymentMetadataSchema = z.object({ | ||
| paymentId: z.string(), | ||
| subscriptionId: z.string(), | ||
| subscriptionItemId: z.string(), | ||
| }); |
There was a problem hiding this comment.
Duplicated this schema. In this context these values shouldn't be null or undefined. If they do it means the team isn't paid.
| 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 }; | ||
| }; |
There was a problem hiding this comment.
Extracted reused logic.
| 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); | ||
| } | ||
| }; |
There was a problem hiding this comment.
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.
| // TODO: disable if not hosted by Cal | ||
| // @TODO: Update with new logic | ||
| // if (teamOwner) await removeSeat(teamOwner.userId, input.teamId, ctx.user.id); | ||
|
|
There was a problem hiding this comment.
We do this on invite/removal so no need to rerun on invite acceptance.
| 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; |
| await stripe.subscriptions.update(subscriptionId, { | ||
| items: [{ quantity: team.members.length, id: subscriptionItemId }], | ||
| }); |
There was a problem hiding this comment.
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.
What does this PR do?
Type of change
How should this be tested?