{"record":{"id":"9ec2190c05ba7268","repo":"RocketChat/Rocket.Chat","slug":"error-business-hour-name-already-in-use","errorCode":null,"errorMessage":"error-business-hour-name-already-in-use","messagePattern":"error-business-hour-name-already-in-use","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/ee/server/lib/omnichannel/business-hour/Custom.ts","lineNumber":44,"sourceCode":"\n\t\tconst businessHour = await this.BusinessHourRepository.findOneById(id);\n\t\tif (!businessHour) {\n\t\t\treturn null;\n\t\t}\n\n\t\tbusinessHour.departments = await LivechatDepartment.findByBusinessHourId(businessHour._id, {\n\t\t\tprojection: { name: 1 },\n\t\t}).toArray();\n\t\treturn businessHour;\n\t}\n\n\tasync saveBusinessHour(businessHour: ILivechatBusinessHour & IBusinessHoursExtraProperties): Promise<ILivechatBusinessHour> {\n\t\tconst existingBusinessHour = (await this.BusinessHourRepository.findOne(\n\t\t\t{ name: businessHour.name },\n\t\t\t{ projection: { _id: 1 } },\n\t\t)) as ILivechatBusinessHour;\n\t\tif (existingBusinessHour && existingBusinessHour._id !== businessHour._id) {\n\t\t\tthrow new Error('error-business-hour-name-already-in-use');\n\t\t}\n\t\tconst { timezoneName, departmentsToApplyBusinessHour, ...businessHourData } = businessHour;\n\t\tbusinessHourData.timezone = {\n\t\t\tname: timezoneName,\n\t\t\tutc: this.getUTCFromTimezone(timezoneName),\n\t\t};\n\t\tconst businessHourToReturn = { ...businessHourData, departmentsToApplyBusinessHour };\n\t\tdelete businessHourData.departments;\n\n\t\tconst businessHourId = await this.baseSaveBusinessHour(businessHourData);\n\n\t\t// Internal callers (e.g. the DST verifier) re-save business hours without the\n\t\t// departments field; only reconcile department links when it is provided,\n\t\t// otherwise an internal re-save would unlink every department.\n\t\tif (departmentsToApplyBusinessHour !== undefined) {\n\t\t\tconst departments = departmentsToApplyBusinessHour.split(',').filter(Boolean);\n\t\t\tconst currentDepartments = (\n\t\t\t\tawait LivechatDepartment.findByBusinessHourId(businessHourId, {","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/ee/server/lib/omnichannel/business-hour/Custom.ts#L26-L62","documentation":"Thrown by CustomBusinessHour.saveBusinessHour in Custom.ts:44 when a different business hour already uses the requested name. It looks up by {name} and only allows reuse when the found _id equals the one being saved (i.e. renaming to your own name is fine). NOTE: plain `new Error('error-business-hour-name-already-in-use')` — no Meteor.Error, so the code lives only in the message string.","triggerScenarios":"Creating a business hour with a name already taken, or renaming an existing hour to another hour's name. The check ignores _id equality for new records (businessHour._id undefined/falsy).","commonSituations":"Default 'Business Hours' name reused; admin copy operation that does not rename; CI fixture collision.","solutions":["Choose a unique business-hour name.","Pre-list business hours and validate the name client-side before save.","Match by message string 'error-business-hour-name-already-in-use' in the catch (no code available)."],"exampleFix":"// before\nawait businessHour.saveBusinessHour(payload);\n\n// after\nconst clash = await LivechatBusinessHours.findOne({ name: payload.name }, { projection: { _id: 1 } });\nif (clash && clash._id !== payload._id) {\n  throw new Error('name taken');\n}\nawait businessHour.saveBusinessHour(payload);","handlingStrategy":"validation","validationCode":"const clash = await LivechatBusinessHours.findOne({ name: payload.name }, { projection: { _id: 1 } });\nif (clash && clash._id !== payload._id) throw new Error('name taken');","typeGuard":"const isNameUnique = async (name: string, selfId?: string) => {\n  const c = await LivechatBusinessHours.findOne({ name }, { projection: { _id: 1 } });\n  return !c || c._id === selfId;\n};","tryCatchPattern":"try { await bh.saveBusinessHour(payload); }\ncatch (e) {\n  if (e instanceof Error && e.message === 'error-business-hour-name-already-in-use') { /* rename */ return; }\n  throw e;\n}","preventionTips":["Validate the name is unique before save.","Show existing business-hour names in the form for quick comparison."],"tags":["omnichannel","business-hour","uniqueness","plain-error"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}