From 3b37fe50e2fdc0b50bd36d8913cf76fefec4f597 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 25 Aug 2023 05:40:17 -0600 Subject: [PATCH] chore: Callback smol fixes (#29942) Co-authored-by: murtaza98 --- .../livechat/server/hooks/offlineMessageToChannel.ts | 12 ++++++++---- .../app/livechat/server/hooks/saveAnalyticsData.ts | 2 +- .../server/hooks/saveLastVisitorMessageTs.ts | 7 ++++--- .../server/hooks/afterForwardChatToDepartment.ts | 5 ++++- .../server/hooks/afterRemoveDepartment.ts | 3 --- .../server/hooks/onTransferFailure.ts | 9 ++++++--- .../hooks/setPredictedVisitorAbandonmentTime.ts | 10 +++++----- 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/apps/meteor/app/livechat/server/hooks/offlineMessageToChannel.ts b/apps/meteor/app/livechat/server/hooks/offlineMessageToChannel.ts index c8166d1a7454..7cc8f8e6e9ad 100644 --- a/apps/meteor/app/livechat/server/hooks/offlineMessageToChannel.ts +++ b/apps/meteor/app/livechat/server/hooks/offlineMessageToChannel.ts @@ -1,3 +1,4 @@ +import type { ILivechatDepartment } from '@rocket.chat/core-typings'; import { isOmnichannelRoom } from '@rocket.chat/core-typings'; import { LivechatDepartment, Users, Rooms } from '@rocket.chat/models'; @@ -17,9 +18,12 @@ callbacks.add( let departmentName; const { name, email, department, message: text, host } = data; if (department && department !== '') { - const dept = await LivechatDepartment.findOneById(department, { - projection: { name: 1, offlineMessageChannelName: 1 }, - }); + const dept = await LivechatDepartment.findOneById>( + department, + { + projection: { name: 1, offlineMessageChannelName: 1 }, + }, + ); departmentName = dept?.name; if (dept?.offlineMessageChannelName) { channelName = dept.offlineMessageChannelName; @@ -30,7 +34,7 @@ callbacks.add( return data; } - const room: any = await Rooms.findOneByName(channelName, { projection: { t: 1, archived: 1 } }); + const room = await Rooms.findOneByName(channelName, { projection: { t: 1, archived: 1 } }); if (!room || room.archived || (isOmnichannelRoom(room) && room.closedAt)) { return data; } diff --git a/apps/meteor/app/livechat/server/hooks/saveAnalyticsData.ts b/apps/meteor/app/livechat/server/hooks/saveAnalyticsData.ts index 48a52aa41fe0..ec584ec001d6 100644 --- a/apps/meteor/app/livechat/server/hooks/saveAnalyticsData.ts +++ b/apps/meteor/app/livechat/server/hooks/saveAnalyticsData.ts @@ -8,12 +8,12 @@ import { callbackLogger } from '../lib/logger'; callbacks.add( 'afterSaveMessage', async (message, room) => { - callbackLogger.debug(`Calculating Omnichannel metrics for room ${room._id}`); // check if room is livechat if (!isOmnichannelRoom(room)) { return message; } + callbackLogger.debug(`Calculating Omnichannel metrics for room ${room._id}`); // skips this callback if the message was edited if (!message || isEditedMessage(message)) { return message; diff --git a/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts b/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts index 576b14722a2a..4bc28c3990ba 100644 --- a/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts +++ b/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts @@ -12,10 +12,11 @@ callbacks.add( if (message.t) { return message; } - if (message.token) { - await LivechatRooms.setVisitorLastMessageTimestampByRoomId(room._id, message.ts); + if (!message.token) { + return message; } - return message; + + await LivechatRooms.setVisitorLastMessageTimestampByRoomId(room._id, message.ts); }, callbacks.priority.HIGH, 'save-last-visitor-message-timestamp', diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterForwardChatToDepartment.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterForwardChatToDepartment.ts index f5b5dc8234f1..8babfec041c7 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterForwardChatToDepartment.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterForwardChatToDepartment.ts @@ -1,3 +1,4 @@ +import type { IOmnichannelRoom } from '@rocket.chat/core-typings'; import { LivechatRooms, LivechatDepartment } from '@rocket.chat/models'; import { callbacks } from '../../../../../lib/callbacks'; @@ -8,7 +9,9 @@ callbacks.add( async (options) => { const { rid, newDepartmentId } = options; - const room = await LivechatRooms.findOneById(rid); + const room = await LivechatRooms.findOneById>(rid, { + projection: { departmentAncestors: 1 }, + }); if (!room) { cbLogger.debug('Skipping callback. No room found'); return options; diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterRemoveDepartment.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterRemoveDepartment.ts index a2f53f39715e..be732be66297 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterRemoveDepartment.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterRemoveDepartment.ts @@ -15,9 +15,6 @@ const afterRemoveDepartment = async (options: { department: ILivechatDepartmentR cbLogger.debug(`Removing department from forward list: ${department._id}`); await LivechatDepartment.removeDepartmentFromForwardListById(department._id); - cbLogger.debug(`Removed department from forward list: ${department._id}`); - - cbLogger.debug(`Post-department-removal actions completed in EE: ${department._id}`); return options; }; diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/onTransferFailure.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/onTransferFailure.ts index d2d481c42d8b..bd33cfe12bbd 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/onTransferFailure.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/onTransferFailure.ts @@ -28,9 +28,12 @@ const onTransferFailure = async ( return false; } - const department = (await LivechatDepartment.findOneById(departmentId, { - projection: { _id: 1, name: 1, fallbackForwardDepartment: 1 }, - })) as Partial; + const department = await LivechatDepartment.findOneById>( + departmentId, + { + projection: { _id: 1, name: 1, fallbackForwardDepartment: 1 }, + }, + ); if (!department?.fallbackForwardDepartment?.length) { return false; diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/setPredictedVisitorAbandonmentTime.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/setPredictedVisitorAbandonmentTime.ts index 4efafdf09d52..0f162f887913 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/setPredictedVisitorAbandonmentTime.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/setPredictedVisitorAbandonmentTime.ts @@ -7,6 +7,10 @@ import { setPredictedVisitorAbandonmentTime } from '../lib/Helper'; callbacks.add( 'afterSaveMessage', async (message, room) => { + if (!isOmnichannelRoom(room)) { + return message; + } + if ( !settings.get('Livechat_abandoned_rooms_action') || settings.get('Livechat_abandoned_rooms_action') === 'none' || @@ -19,12 +23,8 @@ callbacks.add( return message; } - if (!isOmnichannelRoom(room)) { - return message; - } - // message valid only if it is a livechat room - if (!(typeof room.t !== 'undefined' && room.t === 'l' && room.v && room.v.token)) { + if (!room.v?.token) { return message; } // if the message has a type means it is a special message (like the closing comment), so skip it