Skip to content

Commit 47a5fc7

Browse files
authored
fix: allow organizations to sign up with existing usernames (calcom#25941)
* fix: allow organizations to sign up with existing usernames - Update usernameCheck to check organization context when currentOrgDomain is provided - Organizations can now use usernames that exist in other orgs or global namespace - Only checks username availability within the specific organization - Fixes issue where org signups were blocked by global username conflicts Refs calcom#25800 * Update username.ts
1 parent a1a5f24 commit 47a5fc7

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

packages/lib/server/username.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { NextResponse } from "next/server";
22

3+
import { ErrorCode } from "@calcom/lib/errorCodes";
4+
import { ErrorWithCode } from "@calcom/lib/errors";
35
import slugify from "@calcom/lib/slugify";
46
import prisma from "@calcom/prisma";
57
import { RedirectType } from "@calcom/prisma/enums";
@@ -116,11 +118,36 @@ const usernameCheck = async (usernameRaw: string, currentOrgDomain?: string | nu
116118

117119
const username = slugify(usernameRaw);
118120

121+
let organizationId: number | null = null;
122+
if (currentOrgDomain) {
123+
// Get organizationId from orgSlug
124+
const organization = await prisma.team.findFirst({
125+
where: {
126+
isOrganization: true,
127+
OR: [
128+
{ slug: currentOrgDomain },
129+
{ metadata: { path: ["requestedSlug"], equals: currentOrgDomain } },
130+
],
131+
},
132+
select: {
133+
id: true,
134+
},
135+
});
136+
if (!organization) {
137+
throw new ErrorWithCode(
138+
ErrorCode.NotFound,
139+
`Organization with domain "${currentOrgDomain}" not found`,
140+
{ currentOrgDomain }
141+
);
142+
}
143+
organizationId = organization.id;
144+
}
145+
119146
const user = await prisma.user.findFirst({
120147
where: {
121148
username,
122-
// Simply remove it when we drop organizationId column
123-
organizationId: null,
149+
// Check in the specific organization context, or global namespace if no org
150+
organizationId: organizationId ?? null,
124151
},
125152
select: {
126153
id: true,
@@ -140,12 +167,13 @@ const usernameCheck = async (usernameRaw: string, currentOrgDomain?: string | nu
140167
response.premium = true;
141168
}
142169

143-
// get list of similar usernames in the db
170+
// get list of similar usernames in the db (scoped to organization if checking in org context)
144171
const users = await prisma.user.findMany({
145172
where: {
146173
username: {
147174
contains: username,
148175
},
176+
...(organizationId !== null ? { organizationId } : { organizationId: null }),
149177
},
150178
select: {
151179
username: true,

0 commit comments

Comments
 (0)