-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup department props from room once a department is removed
- Loading branch information
Showing
13 changed files
with
80 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { LivechatDepartment, LivechatDepartmentAgents, LivechatRooms } from '@rocket.chat/models'; | ||
|
||
import { callbacks } from '../../../../lib/callbacks'; | ||
import { Logger } from '../../../logger/server'; | ||
|
||
class DepartmentHelperClass { | ||
logger = new Logger('Omnichannel:DepartmentHelper'); | ||
|
||
async removeDepartment(departmentId: string) { | ||
this.logger.debug(`Removing department: ${departmentId}`); | ||
|
||
const department = await LivechatDepartment.findOneById(departmentId, { projection: { _id: 1 } }); | ||
if (!department) { | ||
this.logger.debug(`Department not found: ${departmentId}`); | ||
throw new Error('error-department-not-found'); | ||
} | ||
|
||
const { _id } = department; | ||
|
||
const ret = await LivechatDepartment.removeById(_id); | ||
if (ret.acknowledged !== true) { | ||
this.logger.error(`Department record not removed: ${_id}. Result from db: ${ret}`); | ||
throw new Error('error-failed-to-delete-department'); | ||
} | ||
this.logger.debug(`Department record removed: ${_id}`); | ||
|
||
const agentsIds = LivechatDepartmentAgents.findAgentsByDepartmentId(department._id).cursor.map((agent) => agent.agentId); | ||
|
||
this.logger.debug( | ||
`Performing post-department-removal actions: ${_id}. Removing department agents, unsetting fallback department and removing department from rooms`, | ||
); | ||
|
||
await Promise.all([ | ||
LivechatDepartmentAgents.removeByDepartmentId(_id), | ||
LivechatDepartment.unsetFallbackDepartmentByDepartmentId(_id), | ||
LivechatRooms.bulkRemoveDepartmentFromRooms(_id), | ||
]); | ||
|
||
this.logger.debug(`Post-department-removal actions completed: ${_id}. Notifying callbacks with department and agentsIds`); | ||
|
||
Meteor.defer(() => { | ||
callbacks.run('livechat.afterRemoveDepartment', { department, agentsIds }); | ||
}); | ||
|
||
return ret; | ||
} | ||
} | ||
|
||
export const DepartmentHelper = new DepartmentHelperClass(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
import { Meteor } from 'meteor/meteor'; | ||
import { check } from 'meteor/check'; | ||
|
||
import { hasPermission } from '../../../authorization'; | ||
import { Livechat } from '../lib/Livechat'; | ||
import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger'; | ||
import { DepartmentHelper } from '../lib/DepartmentsHelper'; | ||
|
||
Meteor.methods({ | ||
'livechat:removeDepartment'(_id) { | ||
methodDeprecationLogger.warn('livechat:removeDepartment will be deprecated in future versions of Rocket.Chat'); | ||
|
||
check(_id, String); | ||
|
||
if (!Meteor.userId() || !hasPermission(Meteor.userId(), 'manage-livechat-departments')) { | ||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { | ||
method: 'livechat:removeDepartment', | ||
}); | ||
} | ||
|
||
return Livechat.removeDepartment(_id); | ||
return DepartmentHelper.removeDepartment(_id); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters