Scheduling infrastructure for absolutely everyone.
fix: allow organizations to sign up with existing usernames (#25941)
Rakshit Sisodiya47a5fc7bf719
diff --git a/packages/lib/server/username.ts b/packages/lib/server/username.tsindex aaaf261a837..a02bd972027 100644--- a/packages/lib/server/username.ts+++ b/packages/lib/server/username.ts@@ -1,5 +1,7 @@import type { NextResponse } from "next/server";+import { ErrorCode } from "@calcom/lib/errorCodes";+import { ErrorWithCode } from "@calcom/lib/errors";import slugify from "@calcom/lib/slugify";import prisma from "@calcom/prisma";import { RedirectType } from "@calcom/prisma/enums";@@ -116,11 +118,36 @@ const usernameCheck = async (usernameRaw: string, currentOrgDomain?: string | nuconst username = slugify(usernameRaw);+ let organizationId: number | null = null;+ if (currentOrgDomain) {+ // Get organizationId from orgSlug+ const organization = await prisma.team.findFirst({+ where: {+ isOrganization: true,+ OR: [+ { slug: currentOrgDomain },+ { metadata: { path: ["requestedSlug"], equals: currentOrgDomain } },+ ],+ },+ select: {+ id: true,+ },+ });+ if (!organization) {+ throw new ErrorWithCode(+ ErrorCode.NotFound,+ `Organization with domain "${currentOrgDomain}" not found`,+ { currentOrgDomain }+ );+ }+ organizationId = organization.id;+ }+const user = await prisma.user.findFirst({where: {username,- // Simply remove it when we drop organizationId column- organizationId: null,+ // Check in the specific organization context, or global namespace if no org+ organizationId: organizationId ?? null,},select: {id: true,@@ -140,12 +167,13 @@ const usernameCheck = async (usernameRaw: string, currentOrgDomain?: string | nuresponse.premium = true;}- // get list of similar usernames in the db+ // get list of similar usernames in the db (scoped to organization if checking in org context)const users = await prisma.user.findMany({where: {username: {contains: username,},+ ...(organizationId !== null ? { organizationId } : { organizationId: null }),},select: {username: true,