fix: Only update online & unavailable agents when opening business hours (#29540)

pull/29709/head
Kevin Aleman 3 years ago committed by GitHub
parent 7765526938
commit 94477bd9f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      .changeset/thirty-lobsters-vanish.md
  2. 3
      apps/meteor/app/livechat/server/business-hour/Helper.ts
  3. 12
      apps/meteor/app/livechat/server/business-hour/Single.ts
  4. 1
      apps/meteor/ee/app/livechat-enterprise/server/business-hour/Helper.ts
  5. 27
      apps/meteor/server/models/raw/Users.js
  6. 4
      packages/model-typings/src/models/IUsersModel.ts

@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/model-typings": patch
---
Update database query to only update online & unavailable agents when opening & closing business hours

@ -55,6 +55,9 @@ export const openBusinessHourDefault = async (): Promise<void> => {
const businessHoursToOpenIds = (await filterBusinessHoursThatMustBeOpened(activeBusinessHours)).map((businessHour) => businessHour._id);
businessHourLogger.debug({ msg: 'Opening default business hours', businessHoursToOpenIds });
await Users.openAgentsBusinessHoursByBusinessHourId(businessHoursToOpenIds);
if (businessHoursToOpenIds.length) {
await Users.makeAgentsWithinBusinessHourAvailable();
}
await Users.updateLivechatStatusBasedOnBusinessHours();
businessHourLogger.debug('Done opening default business hours');
};

@ -6,13 +6,9 @@ import { openBusinessHourDefault } from './Helper';
import { businessHourLogger } from '../lib/logger';
export class SingleBusinessHourBehavior extends AbstractBusinessHourBehavior implements IBusinessHourBehavior {
async openBusinessHoursByDayAndHour(day: string, hour: string): Promise<void> {
const businessHoursIds = (
await this.BusinessHourRepository.findActiveBusinessHoursToOpen(day, hour, LivechatBusinessHourTypes.DEFAULT, {
projection: { _id: 1 },
})
).map((businessHour) => businessHour._id);
this.UsersRepository.openAgentsBusinessHoursByBusinessHourId(businessHoursIds);
async openBusinessHoursByDayAndHour(): Promise<void> {
businessHourLogger.debug('opening single business hour');
return openBusinessHourDefault();
}
async closeBusinessHoursByDayAndHour(day: string, hour: string): Promise<void> {
@ -22,7 +18,7 @@ export class SingleBusinessHourBehavior extends AbstractBusinessHourBehavior imp
})
).map((businessHour) => businessHour._id);
await this.UsersRepository.closeAgentsBusinessHoursByBusinessHourIds(businessHoursIds);
this.UsersRepository.updateLivechatStatusBasedOnBusinessHours();
await this.UsersRepository.updateLivechatStatusBasedOnBusinessHours();
}
async onStartBusinessHours(): Promise<void> {

@ -78,6 +78,7 @@ export const openBusinessHour = async (
});
await Users.addBusinessHourByAgentIds(agentIds, businessHour._id);
await Users.makeAgentsWithinBusinessHourAvailable(agentIds);
if (updateLivechatStatus) {
await Users.updateLivechatStatusBasedOnBusinessHours();
}

@ -857,9 +857,6 @@ export class UsersRaw extends BaseRaw {
};
const update = {
$set: {
statusLivechat: 'available',
},
$addToSet: {
openBusinessHours: { $each: businessHourIds },
},
@ -883,6 +880,25 @@ export class UsersRaw extends BaseRaw {
return this.updateMany(query, update);
}
makeAgentsWithinBusinessHourAvailable(agentIds) {
const query = {
...(agentIds && { _id: { $in: agentIds } }),
roles: 'livechat-agent',
// Exclude away users
status: 'online',
// Exclude users that are already available, maybe due to other business hour
statusLivechat: 'not-available',
};
const update = {
$set: {
statusLivechat: 'available',
},
};
return this.updateMany(query, update);
}
removeBusinessHourByAgentIds(agentIds = [], businessHourId) {
const query = {
_id: { $in: agentIds },
@ -904,9 +920,6 @@ export class UsersRaw extends BaseRaw {
};
const update = {
$set: {
statusLivechat: 'available',
},
$addToSet: {
openBusinessHours: businessHourId,
},
@ -947,6 +960,8 @@ export class UsersRaw extends BaseRaw {
const query = {
$or: [{ openBusinessHours: { $exists: false } }, { openBusinessHours: { $size: 0 } }],
roles: 'livechat-agent',
// Avoid unnecessary updates
statusLivechat: 'available',
...(Array.isArray(userIds) && userIds.length > 0 && { _id: { $in: userIds } }),
};

@ -128,10 +128,12 @@ export interface IUsersModel extends IBaseModel<IUser> {
openAgentsBusinessHoursByBusinessHourId(businessHourIds: any): any;
openAgentBusinessHoursByBusinessHourIdsAndAgentId(businessHourIds: any, agentId: any): any;
openAgentBusinessHoursByBusinessHourIdsAndAgentId(businessHourIds: string[], agentId: string): Promise<UpdateResult | Document>;
addBusinessHourByAgentIds(agentIds: string[], businessHourId: string): any;
makeAgentsWithinBusinessHourAvailable(agentIds?: string[]): Promise<UpdateResult | Document>;
removeBusinessHourByAgentIds(agentIds: any, businessHourId: any): any;
openBusinessHourToAgentsWithoutDepartment(agentIdsWithDepartment: any, businessHourId: any): any;

Loading…
Cancel
Save