{"record":{"id":"6de9b9b3bcc413f6","repo":"RocketChat/Rocket.Chat","slug":"room-name-already-exists","errorCode":null,"errorMessage":"room-name-already-exists","messagePattern":"room-name-already-exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/services/team/service.ts","lineNumber":49,"sourceCode":"import { addUserToRoom } from '../../lib/rooms/addUserToRoom';\nimport { getSubscribedRoomsForUserWithDetails } from '../../lib/rooms/getRoomsWithSingleOwner';\nimport { removeUserFromRoom } from '../../lib/rooms/removeUserFromRoom';\nimport { saveRoomName } from '../../lib/rooms/settings';\nimport { saveRoomType } from '../../lib/rooms/settings/saveRoomType';\nimport { checkUsernameAvailability } from '../../lib/users/checkUsernameAvailability';\nimport { settings } from '../../settings';\n\nexport class TeamService extends ServiceClassInternal implements ITeamService {\n\tprotected name = 'team';\n\n\tasync create(uid: string, { team, room = { name: team.name, extraData: {} }, members, owner }: ITeamCreateParams): Promise<ITeam> {\n\t\tif (!(await checkUsernameAvailability(team.name, 'room'))) {\n\t\t\tthrow new Error('team-name-already-exists');\n\t\t}\n\n\t\tconst existingRoom = await Rooms.findOneByName(team.name, { projection: { _id: 1 } });\n\t\tif (existingRoom && existingRoom._id !== room.id) {\n\t\t\tthrow new Error('room-name-already-exists');\n\t\t}\n\n\t\tconst createdBy = await Users.findOneById<Pick<IUser, 'username' | '_id'>>(uid, {\n\t\t\tprojection: { username: 1 },\n\t\t});\n\t\tif (!createdBy) {\n\t\t\tthrow new Error('invalid-user');\n\t\t}\n\n\t\t// TODO add validations to `data` and `members`\n\n\t\tconst membersResult =\n\t\t\t!members || !Array.isArray(members) || members.length === 0\n\t\t\t\t? []\n\t\t\t\t: await Users.findActiveByIdsOrUsernames(members, {\n\t\t\t\t\t\tprojection: { username: 1 },\n\t\t\t\t\t}).toArray();\n\t\tconst memberUsernames = membersResult.map(({ username }) => username);","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/services/team/service.ts#L31-L67","documentation":"After the name-availability check, TeamService.create() also runs Rooms.findOneByName(team.name); if a room with that name already exists under a different id than the provided room.id, it throws room-name-already-exists — the team's channel would collide with an existing room name.","triggerScenarios":"Creating a team when a channel/private room with the same name already exists (and the caller did not pass that exact room.id); races where two creates pick the same name at once.","commonSituations":"A pre-existing channel shares the desired team name; a deleted team left its channel behind; users re-running a failed create whose channel was actually created.","solutions":["Rename the team, or delete/rename the colliding room first","If the existing room is intended to back the team, pass its id as room.id in the create params","Pre-check Rooms.findOneByName(team.name) before submitting"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const existingRoom = await Rooms.findOneByName(team.name, { projections: { _id: 1 } });\nif (existingRoom && existingRoom._id !== intendedRoomId) {\n  // a room already uses this name: pick another team name or pass this room.id deliberately\n  return markRoomNameTaken(team.name);\n}","typeGuard":"const isRoomNameFreeForTeam = async (name: string, roomId?: string): Promise<boolean> => {\n  const existing = await Rooms.findOneByName(name, { projections: { _id: 1 } });\n  return !existing || existing._id === roomId;\n};","tryCatchPattern":"try {\n  await teamService.create(uid, params);\n} catch (err) {\n  if (err instanceof Error && err.message === 'room-name-already-exists') {\n    // rename the team or delete/rename the colliding room; retrying the same name always fails\n    return suggestAlternativeNames(params.team.name);\n  }\n  throw err;\n}","preventionTips":["Check Rooms.findOneByName before creating teams whose channel names collide","If reusing an existing room as the team channel, pass its exact room.id","Remove orphaned channels left by failed team creates before retrying"],"tags":["teams","rooms","naming","duplicate-name","rocket-chat"],"backgroundTag":"duplicate-resource-name","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}