From 4eed56d3dc8cb67b1264ae8284de4130d3c78579 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Mon, 11 Sep 2023 09:39:15 -0600 Subject: [PATCH 1/2] Add activity to room --- .../livechat/server/hooks/markRoomResponded.ts | 16 ++++++++++------ apps/meteor/server/models/raw/LivechatRooms.ts | 17 +++++++++++++++++ .../server/models/raw/LivechatVisitors.ts | 2 +- packages/core-typings/src/IRoom.ts | 6 +++++- .../src/models/ILivechatRoomsModel.ts | 1 + 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts b/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts index a8414816030f..19b18e0559e4 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))) { - await LivechatVisitors.markVisitorActiveForPeriod(room.v._id, monthYear); + await Promise.all([ + LivechatVisitors.markVisitorActiveForPeriod(room.v._id, monthYear), + 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..fa0b988e4462 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -2452,6 +2452,23 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive return this.updateOne(query, update); } + markVisitorActiveForPeriod(rid: string, period: string): Promise { + const query = { + _id: rid, + }; + + const update = { + $push: { + 'v.activity': { + $each: [period], + $slice: -12, + }, + }, + }; + + 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 cb0addb69553..6c0ecf82b720 100644 --- a/apps/meteor/server/models/raw/LivechatVisitors.ts +++ b/apps/meteor/server/models/raw/LivechatVisitors.ts @@ -32,7 +32,7 @@ export class LivechatVisitorsRaw extends BaseRaw implements IL { key: { username: 1 } }, { key: { 'contactMananger.username': 1 }, sparse: true }, { key: { 'livechatData.$**': 1 } }, - { key: { activity: 1 } }, + { 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; } From 79a69e01749538e7693b665fff01296e50e16cab Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Mon, 11 Sep 2023 12:52:31 -0600 Subject: [PATCH 2/2] room - addToSet --- .../app/livechat/server/hooks/markRoomResponded.ts | 10 +++++----- apps/meteor/server/models/raw/LivechatRooms.ts | 7 ++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts b/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts index 19b18e0559e4..bed3dfa62693 100644 --- a/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts +++ b/apps/meteor/app/livechat/server/hooks/markRoomResponded.ts @@ -23,13 +23,13 @@ callbacks.add( // Return YYYY-MM from moment const monthYear = moment().format('YYYY-MM'); - if (!(await LivechatVisitors.isVisitorActiveOnPeriod(room.v._id, monthYear))) { - await Promise.all([ - LivechatVisitors.markVisitorActiveForPeriod(room.v._id, monthYear), - LivechatRooms.markVisitorActiveForPeriod(room._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); } diff --git a/apps/meteor/server/models/raw/LivechatRooms.ts b/apps/meteor/server/models/raw/LivechatRooms.ts index fa0b988e4462..40f6de9a2fba 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -2458,11 +2458,8 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive }; const update = { - $push: { - 'v.activity': { - $each: [period], - $slice: -12, - }, + $addToSet: { + 'v.activity': period, }, };