diff --git a/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts b/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts index a8414816030f..bed3dfa62693 100644 --- a/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts +++ b/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts @@ -20,18 +20,22 @@ callbacks.add( if (message.token) { return message; } - if (room.responseBy) { - await LivechatRooms.setAgentLastMessageTs(room._id); - } - // Return MMMM-YY from moment + // Return YYYY-MM from moment const monthYear = moment().format('YYYY-MM'); - if (!(await LivechatVisitors.isVisitorActiveOnPeriod(room.v._id, monthYear))) { + const isVisitorActive = await LivechatVisitors.isVisitorActiveOnPeriod(room.v._id, monthYear); + if (!isVisitorActive) { await LivechatVisitors.markVisitorActiveForPeriod(room.v._id, monthYear); } + await LivechatRooms.markVisitorActiveForPeriod(room._id, monthYear); + + if (room.responseBy) { + await LivechatRooms.setAgentLastMessageTs(room._id); + } + // check if room is yet awaiting for response - if (!(typeof room.t !== 'undefined' && room.t === 'l' && room.waitingResponse)) { + if (!room.waitingResponse) { return message; } diff --git a/apps/meteor/server/models/raw/LivechatRooms.ts b/apps/meteor/server/models/raw/LivechatRooms.ts index 1687c0d5cb87..40f6de9a2fba 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -2452,6 +2452,20 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive return this.updateOne(query, update); } + markVisitorActiveForPeriod(rid: string, period: string): Promise { + const query = { + _id: rid, + }; + + const update = { + $addToSet: { + 'v.activity': period, + }, + }; + + return this.updateOne(query, update); + } + async unsetAllPredictedVisitorAbandonment(): Promise { throw new Error('Method not implemented.'); } diff --git a/apps/meteor/server/models/raw/LivechatVisitors.ts b/apps/meteor/server/models/raw/LivechatVisitors.ts index 087801acbca4..21e155b4bab2 100644 --- a/apps/meteor/server/models/raw/LivechatVisitors.ts +++ b/apps/meteor/server/models/raw/LivechatVisitors.ts @@ -34,6 +34,7 @@ export class LivechatVisitorsRaw extends BaseRaw implements IL { key: { 'livechatData.$**': 1 } }, { key: { activity: 1 } }, { key: { disabled: 1 }, partialFilterExpression: { disabled: { $exists: true } } }, + { key: { activity: 1 }, partialFilterExpression: { activity: { $exists: true } } }, ]; } diff --git a/packages/core-typings/src/IRoom.ts b/packages/core-typings/src/IRoom.ts index 618c24a4fd3a..348fc31a1432 100644 --- a/packages/core-typings/src/IRoom.ts +++ b/packages/core-typings/src/IRoom.ts @@ -151,7 +151,11 @@ export enum OmnichannelSourceType { export interface IOmnichannelGenericRoom extends Omit { t: 'l' | 'v'; - v: Pick & { lastMessageTs?: Date; phone?: string }; + v: Pick & { + lastMessageTs?: Date; + phone?: string; + activity?: string[]; + }; email?: { // Data used when the room is created from an email, via email Integration. inbox: string; diff --git a/packages/model-typings/src/models/ILivechatRoomsModel.ts b/packages/model-typings/src/models/ILivechatRoomsModel.ts index b2af0add69a7..d460ad8382dd 100644 --- a/packages/model-typings/src/models/ILivechatRoomsModel.ts +++ b/packages/model-typings/src/models/ILivechatRoomsModel.ts @@ -234,4 +234,5 @@ export interface ILivechatRoomsModel extends IBaseModel { setVisitorInactivityInSecondsById(roomId: string, visitorInactivity: any): Promise; changeVisitorByRoomId(roomId: string, visitor: { _id: string; username: string; token: string }): Promise; unarchiveOneById(roomId: string): Promise; + markVisitorActiveForPeriod(rid: string, period: string): Promise; }