From e90954eda826d9b11a0a3c4be7568efe3834284a Mon Sep 17 00:00:00 2001 From: Pierre Lehnen <55164754+pierre-lehnen-rc@users.noreply.github.com> Date: Thu, 18 Apr 2024 15:04:01 -0300 Subject: [PATCH] chore: remove references to EE code from livechat business hour feature (#32006) --- .../business-hour/BusinessHourManager.ts | 2 +- .../server/business-hour/closeBusinessHour.ts | 25 ++++++++ .../getAgentIdsForBusinessHour.ts | 45 ++++++++++++++ .../server/business-hour/Helper.ts | 60 +------------------ .../server/business-hour/Multiple.ts | 3 +- .../ee/server/patches/closeBusinessHour.ts | 7 +++ apps/meteor/ee/server/patches/index.ts | 1 + 7 files changed, 84 insertions(+), 59 deletions(-) create mode 100644 apps/meteor/app/livechat/server/business-hour/closeBusinessHour.ts create mode 100644 apps/meteor/app/livechat/server/business-hour/getAgentIdsForBusinessHour.ts create mode 100644 apps/meteor/ee/server/patches/closeBusinessHour.ts diff --git a/apps/meteor/app/livechat/server/business-hour/BusinessHourManager.ts b/apps/meteor/app/livechat/server/business-hour/BusinessHourManager.ts index c541e5f7b2c3..55b1724730f6 100644 --- a/apps/meteor/app/livechat/server/business-hour/BusinessHourManager.ts +++ b/apps/meteor/app/livechat/server/business-hour/BusinessHourManager.ts @@ -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 = new Map(); diff --git a/apps/meteor/app/livechat/server/business-hour/closeBusinessHour.ts b/apps/meteor/app/livechat/server/business-hour/closeBusinessHour.ts new file mode 100644 index 000000000000..a2295b529272 --- /dev/null +++ b/apps/meteor/app/livechat/server/business-hour/closeBusinessHour.ts @@ -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 => { + 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): Promise => { + const agentIds = await getAgentIdsForBusinessHour(); + return closeBusinessHourByAgentIds(businessHour._id, agentIds); +}); diff --git a/apps/meteor/app/livechat/server/business-hour/getAgentIdsForBusinessHour.ts b/apps/meteor/app/livechat/server/business-hour/getAgentIdsForBusinessHour.ts new file mode 100644 index 000000000000..849975f45d60 --- /dev/null +++ b/apps/meteor/app/livechat/server/business-hour/getAgentIdsForBusinessHour.ts @@ -0,0 +1,45 @@ +import type { IUser } from '@rocket.chat/core-typings'; +import { LivechatDepartment, LivechatDepartmentAgents, Users } from '@rocket.chat/models'; + +const getAllAgentIdsWithoutDepartment = async (): Promise => { + // 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 => { + 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 => { + const [withoutDepartment, withDepartmentNotConnectedToBusinessHour] = await Promise.all([ + getAllAgentIdsWithoutDepartment(), + getAllAgentIdsWithDepartmentNotConnectedToBusinessHour(), + ]); + + return [...new Set([...withoutDepartment, ...withDepartmentNotConnectedToBusinessHour])]; +}; diff --git a/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Helper.ts b/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Helper.ts index d1cafcc06e54..cd4af235270c 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Helper.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Helper.ts @@ -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 => { - // 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 => { - 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 => { - const [withoutDepartment, withDepartmentNotConnectedToBusinessHour] = await Promise.all([ - getAllAgentIdsWithoutDepartment(), - getAllAgentIdsWithDepartmentNotConnectedToBusinessHour(), - ]); - - return [...new Set([...withoutDepartment, ...withDepartmentNotConnectedToBusinessHour])]; -}; - -const getAgentIdsToHandle = async (businessHour: Pick): Promise => { +export const getAgentIdsToHandle = async (businessHour: Pick): Promise => { if (businessHour.type === LivechatBusinessHourTypes.DEFAULT) { - return getAllAgentIdsForDefaultBusinessHour(); + return getAgentIdsForBusinessHour(); } const departmentIds = ( await LivechatDepartment.findEnabledByBusinessHourId(businessHour._id, { @@ -82,18 +40,6 @@ export const openBusinessHour = async ( } }; -export const closeBusinessHour = async (businessHour: Pick): Promise => { - 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 => { if (!agentIds.length) { return; diff --git a/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Multiple.ts b/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Multiple.ts index 46ca7cb38cf4..d2cd3fb17240 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Multiple.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Multiple.ts @@ -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; diff --git a/apps/meteor/ee/server/patches/closeBusinessHour.ts b/apps/meteor/ee/server/patches/closeBusinessHour.ts new file mode 100644 index 000000000000..7163f66ee34b --- /dev/null +++ b/apps/meteor/ee/server/patches/closeBusinessHour.ts @@ -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); +}); diff --git a/apps/meteor/ee/server/patches/index.ts b/apps/meteor/ee/server/patches/index.ts index 98ce91475494..ab3f4bf147c2 100644 --- a/apps/meteor/ee/server/patches/index.ts +++ b/apps/meteor/ee/server/patches/index.ts @@ -1,2 +1,3 @@ +import './closeBusinessHour'; import './getInstanceList'; import './isDepartmentCreationAvailable';