diff --git a/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts b/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts index bdaedfed19f2..03dcfdbf81bd 100644 --- a/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts +++ b/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts @@ -5,15 +5,14 @@ import { callbacks } from '../../../../lib/callbacks'; callbacks.add( 'afterOmnichannelSaveMessage', - async (message, { room }) => { - if (message.t) { - return message; - } - if (!isMessageFromVisitor(message)) { + async (message, { roomUpdater }) => { + if (message.t || !isMessageFromVisitor(message)) { return message; } - await LivechatRooms.setVisitorLastMessageTimestampByRoomId(room._id, message.ts); + await LivechatRooms.getVisitorLastMessageTsUpdateQueryByRoomId(message.ts, roomUpdater); + + return message; }, callbacks.priority.HIGH, 'save-last-visitor-message-timestamp', diff --git a/apps/meteor/server/models/raw/LivechatRooms.ts b/apps/meteor/server/models/raw/LivechatRooms.ts index d6a6711d06c9..b88ae1eb767a 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -2403,17 +2403,8 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive return this.deleteOne(query); } - setVisitorLastMessageTimestampByRoomId(roomId: string, lastMessageTs: Date) { - const query = { - _id: roomId, - }; - const update = { - $set: { - 'v.lastMessageTs': lastMessageTs, - }, - }; - - return this.updateOne(query, update); + getVisitorLastMessageTsUpdateQueryByRoomId(lastMessageTs: Date, updater: Updater = this.getUpdater()) { + return updater.set('v.lastMessageTs', lastMessageTs); } setVisitorInactivityInSecondsById(roomId: string, visitorInactivity: number) { diff --git a/packages/model-typings/src/models/ILivechatRoomsModel.ts b/packages/model-typings/src/models/ILivechatRoomsModel.ts index 32238ccb5602..345ecb2d768d 100644 --- a/packages/model-typings/src/models/ILivechatRoomsModel.ts +++ b/packages/model-typings/src/models/ILivechatRoomsModel.ts @@ -244,7 +244,7 @@ export interface ILivechatRoomsModel extends IBaseModel { removeAgentByRoomId(roomId: string): Promise; removeByVisitorToken(token: string): Promise; removeById(_id: string): Promise; - setVisitorLastMessageTimestampByRoomId(roomId: string, lastMessageTs: Date): Promise; + getVisitorLastMessageTsUpdateQueryByRoomId(lastMessageTs: Date, updater?: Updater): Updater; setVisitorInactivityInSecondsById(roomId: string, visitorInactivity: any): Promise; changeVisitorByRoomId(roomId: string, visitor: { _id: string; username: string; token: string }): Promise; unarchiveOneById(roomId: string): Promise;