Skip to content

Commit 0b4f3d3

Browse files
authored
fix: Missing notifiers for priorities & some inquiry changes (#36449)
1 parent 975b4b1 commit 0b4f3d3

File tree

13 files changed

+81
-18
lines changed

13 files changed

+81
-18
lines changed

.changeset/sweet-dingos-decide.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@rocket.chat/meteor": patch
3+
"@rocket.chat/model-typings": patch
4+
"@rocket.chat/models": patch
5+
---
6+
7+
Fixes priorities, sla changes & inquiries not being propagated when change streams were not being used

apps/meteor/app/lib/server/lib/notifyListener.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,24 @@ export const notifyOnLivechatInquiryChanged = withDbWatcherCheck(
250250

251251
export const notifyOnLivechatInquiryChangedById = withDbWatcherCheck(
252252
async (
253-
id: ILivechatInquiryRecord['_id'],
253+
ids: ILivechatInquiryRecord['_id'] | ILivechatInquiryRecord['_id'][],
254254
clientAction: ClientAction = 'updated',
255255
diff?: Partial<Record<keyof ILivechatInquiryRecord, unknown> & { queuedAt: unknown; takenAt: unknown }>,
256256
): Promise<void> => {
257-
const inquiry = clientAction === 'removed' ? await LivechatInquiry.trashFindOneById(id) : await LivechatInquiry.findOneById(id);
257+
const eligibleIds = Array.isArray(ids) ? ids : [ids];
258258

259-
if (!inquiry) {
259+
const items =
260+
clientAction === 'removed'
261+
? LivechatInquiry.trashFind({ _id: { $in: eligibleIds } })
262+
: LivechatInquiry.find({ _id: { $in: eligibleIds } });
263+
264+
if (!items) {
260265
return;
261266
}
262267

263-
void api.broadcast('watch.inquiries', { clientAction, inquiry, diff });
268+
for await (const inquiry of items) {
269+
void api.broadcast('watch.inquiries', { clientAction, inquiry, diff });
270+
}
264271
},
265272
);
266273

@@ -280,17 +287,24 @@ export const notifyOnLivechatInquiryChangedByVisitorIds = withDbWatcherCheck(
280287

281288
export const notifyOnLivechatInquiryChangedByRoom = withDbWatcherCheck(
282289
async (
283-
rid: ILivechatInquiryRecord['rid'],
290+
rids: ILivechatInquiryRecord['rid'] | ILivechatInquiryRecord['rid'][],
284291
clientAction: ClientAction = 'updated',
285292
diff?: Partial<Record<keyof ILivechatInquiryRecord, unknown> & { queuedAt: unknown; takenAt: unknown }>,
286293
): Promise<void> => {
287-
const inquiry = await LivechatInquiry.findOneByRoomId(rid, {});
294+
const eligibleIds = Array.isArray(rids) ? rids : [rids];
288295

289-
if (!inquiry) {
296+
const items =
297+
clientAction === 'removed'
298+
? LivechatInquiry.trashFind({ rid: { $in: eligibleIds } })
299+
: LivechatInquiry.find({ rid: { $in: eligibleIds } });
300+
301+
if (!items) {
290302
return;
291303
}
292304

293-
void api.broadcast('watch.inquiries', { clientAction, inquiry, diff });
305+
for await (const inquiry of items) {
306+
void api.broadcast('watch.inquiries', { clientAction, inquiry, diff });
307+
}
294308
},
295309
);
296310

apps/meteor/app/livechat/server/lib/Helper.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import {
5555
notifyOnSubscriptionChangedById,
5656
notifyOnSubscriptionChangedByRoomId,
5757
notifyOnSubscriptionChanged,
58+
notifyOnRoomChangedById,
59+
notifyOnLivechatInquiryChangedByRoom,
5860
} from '../../../lib/server/lib/notifyListener';
5961
import { settings } from '../../../settings/server';
6062

@@ -555,6 +557,12 @@ export const updateChatDepartment = async ({
555557
Subscriptions.changeDepartmentByRoomId(rid, newDepartmentId),
556558
]);
557559

560+
if (responses[0].modifiedCount) {
561+
void notifyOnRoomChangedById(rid);
562+
}
563+
if (responses[1].modifiedCount) {
564+
void notifyOnLivechatInquiryChangedByRoom(rid);
565+
}
558566
if (responses[2].modifiedCount) {
559567
void notifyOnSubscriptionChangedByRoomId(rid);
560568
}

apps/meteor/app/livechat/server/lib/closeRoom.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { callbacks } from '../../../../lib/callbacks';
1212
import { client, shouldRetryTransaction } from '../../../../server/database/utils';
1313
import {
1414
notifyOnLivechatInquiryChanged,
15+
notifyOnRoomChanged,
1516
notifyOnRoomChangedById,
1617
notifyOnSubscriptionChanged,
1718
} from '../../../lib/server/lib/notifyListener';
@@ -179,6 +180,9 @@ async function doCloseRoom(
179180
if (!params.forceClose && removedInquiry && removedInquiry.deletedCount !== 1) {
180181
throw new Error('Error removing inquiry');
181182
}
183+
if (removedInquiry.deletedCount) {
184+
void notifyOnLivechatInquiryChanged(inquiry!, 'removed');
185+
}
182186

183187
const updatedRoom = await LivechatRooms.closeRoomById(rid, closeData, { session });
184188
if (!params.forceClose && (!updatedRoom || updatedRoom.modifiedCount !== 1)) {
@@ -207,6 +211,7 @@ async function doCloseRoom(
207211
throw new Error('Error: Room not found');
208212
}
209213

214+
void notifyOnRoomChanged(newRoom, 'updated');
210215
return { room: newRoom, closedBy: closeData.closedBy, removedInquiry: inquiry };
211216
}
212217

apps/meteor/app/livechat/server/lib/rooms.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
notifyOnRoomChangedById,
3838
notifyOnLivechatInquiryChanged,
3939
notifyOnSubscriptionChanged,
40+
notifyOnRoomChanged,
4041
} from '../../../lib/server/lib/notifyListener';
4142
import { settings } from '../../../settings/server';
4243
import { i18n } from '../../../utils/lib/i18n';
@@ -278,6 +279,9 @@ export async function removeOmnichannelRoom(rid: string) {
278279
if (result[3]?.status === 'fulfilled' && result[3].value?.deletedCount && inquiry) {
279280
void notifyOnLivechatInquiryChanged(inquiry, 'removed');
280281
}
282+
if (result[4]?.status === 'fulfilled' && result[4].value?.deletedCount) {
283+
void notifyOnRoomChanged(room, 'removed');
284+
}
281285

282286
for (const r of result) {
283287
if (r.status === 'rejected') {

apps/meteor/app/livechat/server/roomAccessValidator.compatibility.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const validators: OmnichannelRoomAccessValidator[] = [
6868
],
6969
};
7070

71+
// TODO: findone filtering if the inquiry is queued instead of checking here
7172
const inquiry = await LivechatInquiry.findOne(filter, { projection: { status: 1 } });
7273
return inquiry && inquiry.status === 'queued';
7374
},

apps/meteor/ee/app/livechat-enterprise/server/api/lib/priorities.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { PaginatedResult } from '@rocket.chat/rest-typings';
55
import { escapeRegExp } from '@rocket.chat/string-helpers';
66
import type { FindOptions } from 'mongodb';
77

8+
import { notifyOnLivechatInquiryChangedByRoom, notifyOnRoomChanged } from '../../../../../../app/lib/server/lib/notifyListener';
89
import { logger } from '../../lib/logger';
910

1011
type FindPriorityParams = {
@@ -24,7 +25,7 @@ export async function findPriority({
2425
...(text && { $or: [{ name: new RegExp(escapeRegExp(text), 'i') }, { description: new RegExp(escapeRegExp(text), 'i') }] }),
2526
};
2627

27-
const { cursor, totalCount } = await LivechatPriority.findPaginated(query, {
28+
const { cursor, totalCount } = LivechatPriority.findPaginated(query, {
2829
sort: sort || { name: 1 },
2930
skip: offset,
3031
limit: count,
@@ -64,7 +65,7 @@ export const updateRoomPriority = async (
6465
user: Required<Pick<IUser, '_id' | 'username' | 'name'>>,
6566
priorityId: string,
6667
): Promise<void> => {
67-
const room = await LivechatRooms.findOneById(rid, { projection: { _id: 1 } });
68+
const room = await LivechatRooms.findOneById(rid);
6869
if (!room) {
6970
throw new Error('error-room-does-not-exist');
7071
}
@@ -79,10 +80,15 @@ export const updateRoomPriority = async (
7980
LivechatInquiry.setPriorityForRoom(rid, priority),
8081
addPriorityChangeHistoryToRoom(room._id, user, priority),
8182
]);
83+
84+
void notifyOnRoomChanged({ ...room, priorityId: priority._id, priorityWeight: priority.sortItem }, 'updated');
85+
void notifyOnLivechatInquiryChangedByRoom(rid, 'updated');
8286
};
8387

8488
export const removePriorityFromRoom = async (rid: string, user: Required<Pick<IUser, '_id' | 'username' | 'name'>>): Promise<void> => {
85-
const room = await LivechatRooms.findOneById<Pick<IOmnichannelRoom, '_id'>>(rid, { projection: { _id: 1 } });
89+
const room = await LivechatRooms.findOneById<Omit<IOmnichannelRoom, 'priorityId' | 'priorityWeight'>>(rid, {
90+
projection: { priorityId: 0, priorityWeight: 0 },
91+
});
8692
if (!room) {
8793
throw new Error('error-room-does-not-exist');
8894
}
@@ -92,6 +98,9 @@ export const removePriorityFromRoom = async (rid: string, user: Required<Pick<IU
9298
LivechatInquiry.unsetPriorityForRoom(rid),
9399
addPriorityChangeHistoryToRoom(rid, user),
94100
]);
101+
102+
void notifyOnRoomChanged(room, 'updated');
103+
void notifyOnLivechatInquiryChangedByRoom(rid, 'updated');
95104
};
96105

97106
const addPriorityChangeHistoryToRoom = async (

apps/meteor/ee/app/livechat-enterprise/server/api/lib/sla.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function findSLA({
2929
...(text && { $or: [{ name: new RegExp(escapeRegExp(text), 'i') }, { description: new RegExp(escapeRegExp(text), 'i') }] }),
3030
};
3131

32-
const { cursor, totalCount } = await OmnichannelServiceLevelAgreements.findPaginated(query, {
32+
const { cursor, totalCount } = OmnichannelServiceLevelAgreements.findPaginated(query, {
3333
sort: sort || { name: 1 },
3434
skip: offset,
3535
limit: count,

apps/meteor/ee/app/livechat-enterprise/server/hooks/beforeRoutingChat.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ILivechatInquiryRecord, SelectedAgent, ILivechatDepartment } from '@rocket.chat/core-typings';
22
import { LivechatDepartment, LivechatInquiry, LivechatRooms } from '@rocket.chat/models';
33

4-
import { notifyOnLivechatInquiryChanged } from '../../../../../app/lib/server/lib/notifyListener';
4+
import { notifyOnLivechatInquiryChanged, notifyOnRoomChangedById } from '../../../../../app/lib/server/lib/notifyListener';
55
import { allowAgentSkipQueue } from '../../../../../app/livechat/server/lib/Helper';
66
import { saveQueueInquiry } from '../../../../../app/livechat/server/lib/QueueManager';
77
import { setDepartmentForGuest } from '../../../../../app/livechat/server/lib/departmentsLib';
@@ -54,6 +54,8 @@ beforeRouteChat.patch(
5454
void notifyOnLivechatInquiryChanged(updatedLivechatInquiry, 'updated', { department: updatedLivechatInquiry.department });
5555
}
5656

57+
void notifyOnRoomChangedById(inquiry.rid, 'updated');
58+
5759
inquiry = updatedLivechatInquiry ?? inquiry;
5860
}
5961
}

apps/meteor/ee/app/livechat-enterprise/server/lib/SlaHelper.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ import { Message } from '@rocket.chat/core-services';
22
import type { IOmnichannelServiceLevelAgreements, IUser } from '@rocket.chat/core-typings';
33
import { LivechatInquiry, LivechatRooms } from '@rocket.chat/models';
44

5+
import {
6+
notifyOnLivechatInquiryChanged,
7+
notifyOnRoomChangedById,
8+
notifyOnLivechatInquiryChangedByRoom,
9+
} from '../../../../../app/lib/server/lib/notifyListener';
510
import { callbacks } from '../../../../../lib/callbacks';
611

712
export const removeSLAFromRooms = async (slaId: string, userId: string) => {
813
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {}, { userId });
914
const openRooms = await LivechatRooms.findOpenBySlaId(slaId, { projection: { _id: 1 } }, extraQuery).toArray();
15+
const openRoomIds: string[] = openRooms.map(({ _id }) => _id);
1016
if (openRooms.length) {
11-
const openRoomIds: string[] = openRooms.map(({ _id }) => _id);
1217
await LivechatInquiry.bulkUnsetSla(openRoomIds);
18+
void notifyOnLivechatInquiryChangedByRoom(openRoomIds, 'updated');
1319
}
1420

1521
await LivechatRooms.bulkRemoveSlaFromRoomsById(slaId);
22+
void notifyOnRoomChangedById(openRoomIds, 'updated');
1623
};
1724

1825
export const updateInquiryQueueSla = async (roomId: string, sla: Pick<IOmnichannelServiceLevelAgreements, 'dueTimeInMinutes' | '_id'>) => {
@@ -29,6 +36,8 @@ export const updateInquiryQueueSla = async (roomId: string, sla: Pick<IOmnichann
2936
slaId,
3037
estimatedWaitingTimeQueue,
3138
});
39+
40+
void notifyOnLivechatInquiryChanged({ ...inquiry, slaId, estimatedWaitingTimeQueue }, 'updated');
3241
};
3342

3443
export const updateRoomSlaWeights = async (roomId: string, sla: Pick<IOmnichannelServiceLevelAgreements, 'dueTimeInMinutes' | '_id'>) => {

0 commit comments

Comments
 (0)