From 68d2a97e92d03b4d23db65ed11cf4acd9b19cf66 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Wed, 9 Aug 2023 12:56:53 -0400 Subject: [PATCH] fix: Orgs publishing with billing (#10662) Co-authored-by: Leo Giovanetti --- packages/features/ee/teams/api/upgrade.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/features/ee/teams/api/upgrade.ts b/packages/features/ee/teams/api/upgrade.ts index 5d6270e3f6..a7ab349c4b 100644 --- a/packages/features/ee/teams/api/upgrade.ts +++ b/packages/features/ee/teams/api/upgrade.ts @@ -36,12 +36,24 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { if (!team) { const prevTeam = await prisma.team.findFirstOrThrow({ where: { id } }); - const metadata = teamMetadataSchema.parse(prevTeam.metadata); + + const metadata = teamMetadataSchema.safeParse(prevTeam.metadata); + if (!metadata.success) throw new HttpError({ statusCode: 400, message: "Invalid team metadata" }); + + if (!metadata.data?.requestedSlug) { + throw new HttpError({ + statusCode: 400, + message: "Can't publish team/org without `requestedSlug`", + }); + } + + const { requestedSlug, ...newMetadata } = metadata.data; /** We save the metadata first to prevent duplicate payments */ team = await prisma.team.update({ where: { id }, data: { metadata: { + ...newMetadata, paymentId: checkoutSession.id, subscriptionId: subscription.id || null, subscriptionItemId: subscription.items.data[0].id || null, @@ -49,7 +61,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { }, }); /** Legacy teams already have a slug, this will allow them to upgrade as well */ - const slug = prevTeam.slug || metadata?.requestedSlug; + const slug = prevTeam.slug || requestedSlug; if (slug) { try { /** Then we try to upgrade the slug, which may fail if a conflict came up since team creation */