From dbafda98229dcae5c13a576d3891ff29bc969c2e Mon Sep 17 00:00:00 2001 From: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Date: Wed, 10 Apr 2024 19:48:22 +0100 Subject: [PATCH] fix: org-redirect-url-admin-update (#14493) --- .../features/ee/users/server/trpc-router.ts | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/features/ee/users/server/trpc-router.ts b/packages/features/ee/users/server/trpc-router.ts index a3f8c82451..e9f1b02e59 100644 --- a/packages/features/ee/users/server/trpc-router.ts +++ b/packages/features/ee/users/server/trpc-router.ts @@ -1,7 +1,9 @@ import { z } from "zod"; +import { getOrgFullOrigin } from "@calcom/ee/organizations/lib/orgDomains"; import { WEBAPP_URL } from "@calcom/lib/constants"; import { AVATAR_FALLBACK } from "@calcom/lib/constants"; +import { RedirectType } from "@calcom/prisma/enums"; import { _UserModel as User } from "@calcom/prisma/zod"; import type { inferRouterOutputs } from "@calcom/trpc"; import { TRPCError } from "@calcom/trpc"; @@ -106,7 +108,7 @@ export const userAdminRouter = router({ // If the profile has been moved to an Org -> we can easily access the profile we need to update if (requestedUser.movedToProfileId && input.username) { - await tx.profile.update({ + const profile = await tx.profile.update({ where: { id: requestedUser.movedToProfileId, }, @@ -115,6 +117,37 @@ export const userAdminRouter = router({ }, }); + // Update all of this users tempOrgRedirectUrls + if (requestedUser.username && profile.organizationId) { + const data = await prisma.team.findUnique({ + where: { + id: profile.organizationId, + }, + select: { + slug: true, + }, + }); + + // We should never hit this + if (!data?.slug) { + throw new Error("Team has no attached slug."); + } + + const orgUrlPrefix = getOrgFullOrigin(data.slug); + + const toUrl = `${orgUrlPrefix}/${input.username}`; + + await prisma.tempOrgRedirect.updateMany({ + where: { + type: RedirectType.User, + from: requestedUser.username, // Old username + }, + data: { + toUrl, + }, + }); + } + return userInternal; }