import { ILivechatAgentStatus, LivechatBusinessHourTypes } from '@rocket.chat/core-typings'; import { LivechatBusinessHours, Users } from '@rocket.chat/models'; import type { IBusinessHourBehavior } from './AbstractBusinessHour'; import { AbstractBusinessHourBehavior } from './AbstractBusinessHour'; import { filterBusinessHoursThatMustBeOpened, makeAgentsUnavailableBasedOnBusinessHour, openBusinessHourDefault } from './Helper'; import { notifyOnUserChange } from '../../notifyListener'; import { businessHourLogger } from '../logger'; export class SingleBusinessHourBehavior extends AbstractBusinessHourBehavior implements IBusinessHourBehavior { async openBusinessHoursByDayAndHour(): Promise { await openBusinessHourDefault(); } async closeBusinessHoursByDayAndHour(day: string, hour: string): Promise { const businessHoursIds = ( await this.BusinessHourRepository.findActiveBusinessHoursToClose(day, hour, LivechatBusinessHourTypes.DEFAULT, { projection: { _id: 1 }, }) ).map((businessHour) => businessHour._id); await this.UsersRepository.closeAgentsBusinessHoursByBusinessHourIds(businessHoursIds); await makeAgentsUnavailableBasedOnBusinessHour(); } async onStartBusinessHours(): Promise { await openBusinessHourDefault(); } async onNewAgentCreated(agentId: string): Promise { const defaultBusinessHour = await LivechatBusinessHours.findOneDefaultBusinessHour(); if (!defaultBusinessHour) { businessHourLogger.debug({ msg: 'No default business hour found for agentId', agentId, }); return; } const businessHourToOpen = await filterBusinessHoursThatMustBeOpened([defaultBusinessHour]); if (!businessHourToOpen.length) { businessHourLogger.debug({ msg: 'No business hours found. Moving agent to NOT_AVAILABLE status', agentId, newStatus: ILivechatAgentStatus.NOT_AVAILABLE, }); const { modifiedCount } = await Users.setLivechatStatus(agentId, ILivechatAgentStatus.NOT_AVAILABLE); if (modifiedCount > 0) { void notifyOnUserChange({ id: agentId, clientAction: 'updated', diff: { statusLivechat: ILivechatAgentStatus.NOT_AVAILABLE, livechatStatusSystemModified: false, }, }); } return; } await Users.addBusinessHourByAgentIds([agentId], defaultBusinessHour._id); businessHourLogger.debug({ msg: 'Business hours found. Moving agent to AVAILABLE status', agentId, newStatus: ILivechatAgentStatus.AVAILABLE, }); } afterSaveBusinessHours(): Promise { return openBusinessHourDefault(); } removeBusinessHourById(): Promise { return Promise.resolve(); } onAddAgentToDepartment(): Promise { return Promise.resolve(); } onRemoveAgentFromDepartment(): Promise { return Promise.resolve(); } onRemoveDepartment(): Promise { return Promise.resolve(); } onDepartmentDisabled(): Promise { return Promise.resolve(); } onDepartmentArchived(): Promise { return Promise.resolve(); } }