Skip to content

Commit

Permalink
fix: Dept w/o any BH do not adhere to the default BH rules. (RocketCh…
Browse files Browse the repository at this point in the history
  • Loading branch information
murtaza98 authored Jun 21, 2023
1 parent 7f78a29 commit ee59936
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/little-ligers-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/model-typings": patch
---

fix: Dept w/o any BH config do not adhere to the default BH rules.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,31 @@ const getAllAgentIdsWithoutDepartment = async (): Promise<string[]> => {
return agentIdsWithoutDepartment;
};

const getAllAgentIdsWithDepartmentNotConnectedToBusinessHour = async (): Promise<string[]> => {
const activeDepartmentsWithoutBusinessHour = (
await LivechatDepartment.findActiveDepartmentsWithoutBusinessHour({
projection: { _id: 1 },
}).toArray()
).map((dept) => dept._id);

const agentIdsWithDepartmentNotConnectedToBusinessHour = await LivechatDepartmentAgents.findAllAgentsConnectedToListOfDepartments(
activeDepartmentsWithoutBusinessHour,
);
return agentIdsWithDepartmentNotConnectedToBusinessHour;
};

const getAllAgentIdsForDefaultBusinessHour = async (): Promise<string[]> => {
const [withoutDepartment, withDepartmentNotConnectedToBusinessHour] = await Promise.all([
getAllAgentIdsWithoutDepartment(),
getAllAgentIdsWithDepartmentNotConnectedToBusinessHour(),
]);

return [...new Set([...withoutDepartment, ...withDepartmentNotConnectedToBusinessHour])];
};

const getAgentIdsToHandle = async (businessHour: Record<string, any>): Promise<string[]> => {
if (businessHour.type === LivechatBusinessHourTypes.DEFAULT) {
return getAllAgentIdsWithoutDepartment();
return getAllAgentIdsForDefaultBusinessHour();
}
const departmentIds = (
await LivechatDepartment.findEnabledByBusinessHourId(businessHour._id, {
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/server/models/raw/LivechatDepartment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ export class LivechatDepartmentRaw extends BaseRaw<ILivechatDepartment> implemen
return this.find(query, options);
}

findActiveDepartmentsWithoutBusinessHour(options: FindOptions<ILivechatDepartment>): FindCursor<ILivechatDepartment> {
const query = {
enabled: true,
businessHourId: { $exists: false },
};
return this.find(query, options);
}

findEnabledByListOfBusinessHourIdsAndDepartmentIds(
businessHourIds: string[],
departmentIds: string[],
Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/server/models/raw/LivechatDepartmentAgents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ export class LivechatDepartmentAgentsRaw extends BaseRaw<ILivechatDepartmentAgen
countByDepartmentId(departmentId: string): Promise<number> {
return this.col.countDocuments({ departmentId });
}

findAllAgentsConnectedToListOfDepartments(departmentIds: string[]): Promise<string[]> {
return this.col.distinct('agentId', { departmentId: { $in: departmentIds } });
}
}

const isStringValue = (value: any): value is string => typeof value === 'string';
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ export interface ILivechatDepartmentAgentsModel extends IBaseModel<ILivechatDepa
getNextBotForDepartment(departmentId: string, ignoreAgentId?: string): Promise<{ agentId: string; username: string } | undefined>;
replaceUsernameOfAgentByUserId(userId: string, username: string): Promise<UpdateResult | Document>;
countByDepartmentId(departmentId: string): Promise<number>;
findAllAgentsConnectedToListOfDepartments(departmentIds: string[]): Promise<string[]>;
}
2 changes: 2 additions & 0 deletions packages/model-typings/src/models/ILivechatDepartmentModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface ILivechatDepartmentModel extends IBaseModel<ILivechatDepartment
options: FindOptions<ILivechatDepartment>,
): FindCursor<ILivechatDepartment>;

findActiveDepartmentsWithoutBusinessHour(options: FindOptions<ILivechatDepartment>): FindCursor<ILivechatDepartment>;

addBusinessHourToDepartmentsByIds(ids: string[], businessHourId: string): Promise<Document | UpdateResult>;

removeBusinessHourFromDepartmentsByIdsAndBusinessHourId(ids: string[], businessHourId: string): Promise<Document | UpdateResult>;
Expand Down

0 comments on commit ee59936

Please sign in to comment.