Skip to content

Commit

Permalink
chore: remove references to EE code from livechat business hour featu…
Browse files Browse the repository at this point in the history
…re (#32006)
  • Loading branch information
pierre-lehnen-rc authored Apr 18, 2024
1 parent 66b070e commit e90954e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { AgendaCronJobs } from '@rocket.chat/cron';
import { LivechatDepartment, Users } from '@rocket.chat/models';
import moment from 'moment';

import { closeBusinessHour } from '../../../../ee/app/livechat-enterprise/server/business-hour/Helper';
import { callbacks } from '../../../../lib/callbacks';
import { settings } from '../../../settings/server';
import type { IBusinessHourBehavior, IBusinessHourType } from './AbstractBusinessHour';
import { closeBusinessHour } from './closeBusinessHour';

export class BusinessHourManager {
private types: Map<string, IBusinessHourType> = new Map();
Expand Down
25 changes: 25 additions & 0 deletions apps/meteor/app/livechat/server/business-hour/closeBusinessHour.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ILivechatBusinessHour, IUser } from '@rocket.chat/core-typings';
import { Users } from '@rocket.chat/models';
import { makeFunction } from '@rocket.chat/patch-injection';

import { businessHourLogger } from '../lib/logger';
import { getAgentIdsForBusinessHour } from './getAgentIdsForBusinessHour';

export const closeBusinessHourByAgentIds = async (
businessHourId: ILivechatBusinessHour['_id'],
agentIds: IUser['_id'][],
): Promise<void> => {
businessHourLogger.debug({
msg: 'Closing business hour',
businessHour: businessHourId,
totalAgents: agentIds.length,
top10AgentIds: agentIds.slice(0, 10),
});
await Users.removeBusinessHourByAgentIds(agentIds, businessHourId);
await Users.updateLivechatStatusBasedOnBusinessHours();
};

export const closeBusinessHour = makeFunction(async (businessHour: Pick<ILivechatBusinessHour, '_id' | 'type'>): Promise<void> => {
const agentIds = await getAgentIdsForBusinessHour();
return closeBusinessHourByAgentIds(businessHour._id, agentIds);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { IUser } from '@rocket.chat/core-typings';
import { LivechatDepartment, LivechatDepartmentAgents, Users } from '@rocket.chat/models';

const getAllAgentIdsWithoutDepartment = async (): Promise<string[]> => {
// Fetch departments with agents excluding archived ones (disabled ones still can be tied to business hours)
// Then find the agents that are not in any of those departments

const departmentIds = (await LivechatDepartment.findNotArchived({ projection: { _id: 1 } }).toArray()).map(({ _id }) => _id);

const agentIdsWithDepartment = await LivechatDepartmentAgents.findAllAgentsConnectedToListOfDepartments(departmentIds);

const agentIdsWithoutDepartment = (
await Users.findUsersInRolesWithQuery(
'livechat-agent',
{
_id: { $nin: agentIdsWithDepartment },
},
{ projection: { _id: 1 } },
).toArray()
).map((user) => user._id);

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;
};

export const getAgentIdsForBusinessHour = async (): Promise<IUser['_id'][]> => {
const [withoutDepartment, withDepartmentNotConnectedToBusinessHour] = await Promise.all([
getAllAgentIdsWithoutDepartment(),
getAllAgentIdsWithDepartmentNotConnectedToBusinessHour(),
]);

return [...new Set([...withoutDepartment, ...withDepartmentNotConnectedToBusinessHour])];
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,12 @@ import type { ILivechatBusinessHour } from '@rocket.chat/core-typings';
import { LivechatBusinessHourTypes } from '@rocket.chat/core-typings';
import { LivechatDepartment, LivechatDepartmentAgents, Users } from '@rocket.chat/models';

import { getAgentIdsForBusinessHour } from '../../../../../app/livechat/server/business-hour/getAgentIdsForBusinessHour';
import { businessHourLogger } from '../../../../../app/livechat/server/lib/logger';

const getAllAgentIdsWithoutDepartment = async (): Promise<string[]> => {
// Fetch departments with agents excluding archived ones (disabled ones still can be tied to business hours)
// Then find the agents that are not in any of those departments

const departmentIds = (await LivechatDepartment.findNotArchived({ projection: { _id: 1 } }).toArray()).map(({ _id }) => _id);

const agentIdsWithDepartment = await LivechatDepartmentAgents.findAllAgentsConnectedToListOfDepartments(departmentIds);

const agentIdsWithoutDepartment = (
await Users.findUsersInRolesWithQuery(
'livechat-agent',
{
_id: { $nin: agentIdsWithDepartment },
},
{ projection: { _id: 1 } },
).toArray()
).map((user) => user._id);

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: Pick<ILivechatBusinessHour, '_id' | 'type'>): Promise<string[]> => {
export const getAgentIdsToHandle = async (businessHour: Pick<ILivechatBusinessHour, '_id' | 'type'>): Promise<string[]> => {
if (businessHour.type === LivechatBusinessHourTypes.DEFAULT) {
return getAllAgentIdsForDefaultBusinessHour();
return getAgentIdsForBusinessHour();
}
const departmentIds = (
await LivechatDepartment.findEnabledByBusinessHourId(businessHour._id, {
Expand Down Expand Up @@ -82,18 +40,6 @@ export const openBusinessHour = async (
}
};

export const closeBusinessHour = async (businessHour: Pick<ILivechatBusinessHour, '_id' | 'type'>): Promise<void> => {
const agentIds: string[] = await getAgentIdsToHandle(businessHour);
businessHourLogger.debug({
msg: 'Closing business hour',
businessHour: businessHour._id,
totalAgents: agentIds.length,
top10AgentIds: agentIds.slice(0, 10),
});
await Users.removeBusinessHourByAgentIds(agentIds, businessHour._id);
await Users.updateLivechatStatusBasedOnBusinessHours();
};

export const removeBusinessHourByAgentIds = async (agentIds: string[], businessHourId: string): Promise<void> => {
if (!agentIds.length) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
filterBusinessHoursThatMustBeOpened,
filterBusinessHoursThatMustBeOpenedByDay,
} from '../../../../../app/livechat/server/business-hour/Helper';
import { closeBusinessHour } from '../../../../../app/livechat/server/business-hour/closeBusinessHour';
import { settings } from '../../../../../app/settings/server';
import { isTruthy } from '../../../../../lib/isTruthy';
import { bhLogger } from '../lib/logger';
import { closeBusinessHour, openBusinessHour, removeBusinessHourByAgentIds } from './Helper';
import { openBusinessHour, removeBusinessHourByAgentIds } from './Helper';

interface IBusinessHoursExtraProperties extends ILivechatBusinessHour {
timezoneName: string;
Expand Down
7 changes: 7 additions & 0 deletions apps/meteor/ee/server/patches/closeBusinessHour.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { closeBusinessHour, closeBusinessHourByAgentIds } from '../../../app/livechat/server/business-hour/closeBusinessHour';
import { getAgentIdsToHandle } from '../../app/livechat-enterprise/server/business-hour/Helper';

closeBusinessHour.patch(async (_next, businessHour) => {
const agentIds = await getAgentIdsToHandle(businessHour);
return closeBusinessHourByAgentIds(businessHour._id, agentIds);
});
1 change: 1 addition & 0 deletions apps/meteor/ee/server/patches/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import './closeBusinessHour';
import './getInstanceList';
import './isDepartmentCreationAvailable';

0 comments on commit e90954e

Please sign in to comment.