From 83854d6305dbbc9ee9eee5a1c1faa812bb01c868 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 5 Mar 2024 14:23:24 -0600 Subject: [PATCH 1/6] Show always all rooms when requesting chat history --- .../contactHistory/useHistoryList.ts | 2 +- .../meteor/server/models/raw/LivechatRooms.ts | 4 +- .../end-to-end/api/livechat/09-visitors.ts | 83 +++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/apps/meteor/client/views/omnichannel/contactHistory/useHistoryList.ts b/apps/meteor/client/views/omnichannel/contactHistory/useHistoryList.ts index f62af76d3d94c..62d304b39d135 100644 --- a/apps/meteor/client/views/omnichannel/contactHistory/useHistoryList.ts +++ b/apps/meteor/client/views/omnichannel/contactHistory/useHistoryList.ts @@ -37,7 +37,7 @@ export const useHistoryList = ( const { history, total } = await getHistory({ ...(options.filter && { searchText: options.filter }), closedChatsOnly: 'true', - servedChatsOnly: 'true', + servedChatsOnly: 'false', offset: start, count: end + start, }); diff --git a/apps/meteor/server/models/raw/LivechatRooms.ts b/apps/meteor/server/models/raw/LivechatRooms.ts index 1423476a708bd..32779fe5bc30b 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -1130,8 +1130,8 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive const match: Document = { $match: { 'v._id': visitorId, - ...(open !== undefined && { open: { $exists: open } }), - ...(served !== undefined && { servedBy: { $exists: served } }), + ...(open !== undefined && open && { open: { $exists: open } }), + ...(served !== undefined && served && { servedBy: { $exists: served } }), ...(source && { $or: [{ 'source.type': new RegExp(escapeRegExp(source), 'i') }, { 'source.alias': new RegExp(escapeRegExp(source), 'i') }], }), diff --git a/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts b/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts index 372f7ddf5d7b4..088c8082bb78f 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts @@ -14,6 +14,7 @@ import { createLivechatRoom, createVisitor, startANewLivechatRoomAndTakeIt, + closeOmnichannelRoom, } from '../../../data/livechat/rooms'; import { getRandomVisitorToken } from '../../../data/livechat/users'; import { getLivechatVisitorByToken } from '../../../data/livechat/visitor'; @@ -590,6 +591,88 @@ describe('LIVECHAT - visitors', function () { expect(res.body.history[0]).to.have.property('v'); }); }); + + it('should return only closed chats when closedChatsOnly is true', async () => { + const { + room: { _id: roomId }, + visitor: { _id: visitorId }, + } = await startANewLivechatRoomAndTakeIt(); + + await closeOmnichannelRoom(roomId); + + await request + .get(api(`livechat/visitors.searchChats/room/${roomId}/visitor/${visitorId}?closedChatsOnly=true&servedChatsOnly=false`)) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res: Response) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('history'); + expect(res.body.history).to.be.an('array'); + expect(res.body.history.find((chat: any) => chat._id === roomId)).to.be.an('object'); + }); + }); + + it('should return only served chats when servedChatsOnly is true', async () => { + const visitor = await createVisitor(); + const room = await createLivechatRoom(visitor.token); + + await request + .get(api(`livechat/visitors.searchChats/room/${room._id}/visitor/${visitor._id}?closedChatsOnly=false&servedChatsOnly=true`)) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res: Response) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('history'); + expect(res.body.history).to.be.an('array'); + expect(res.body.history.find((chat: any) => chat._id === room._id)).to.be.undefined; + }); + }); + + it('should return open rooms when closedChatsOnly is false', async () => { + const { + room: { _id: roomId }, + visitor: { _id: visitorId, token }, + } = await startANewLivechatRoomAndTakeIt(); + await closeOmnichannelRoom(roomId); + const room2 = await createLivechatRoom(token); + await closeOmnichannelRoom(room2._id); + + await request + .get(api(`livechat/visitors.searchChats/room/${roomId}/visitor/${visitorId}?closedChatsOnly=true&servedChatsOnly=false`)) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res: Response) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('history'); + expect(res.body.history).to.be.an('array'); + expect(res.body.history.find((chat: any) => chat._id === roomId)).to.be.an('object'); + expect(res.body.history.find((chat: any) => chat._id === room2._id)).to.be.an('object'); + }); + }); + + it('should return all closed chats (even unserved ones) when closedChatsOnly is true & servedChatsOnly is false', async () => { + const visitor = await createVisitor(); + const room = await createLivechatRoom(visitor.token); + await closeOmnichannelRoom(room._id); + const room2 = await createLivechatRoom(visitor.token); + await closeOmnichannelRoom(room2._id); + + await request + .get(api(`livechat/visitors.searchChats/room/${room._id}/visitor/${visitor._id}?closedChatsOnly=true&servedChatsOnly=false`)) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res: Response) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('history'); + expect(res.body.history).to.be.an('array'); + expect(res.body.history.find((chat: any) => chat._id === room._id)).to.be.an('object'); + expect(res.body.history.find((chat: any) => chat._id === room2._id)).to.be.an('object'); + }); + }); }); describe('livechat/visitor.status', () => { From 76157b69b0bcc34e36cd764c77edf970b3340b42 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 5 Mar 2024 14:24:12 -0600 Subject: [PATCH 2/6] Create spotty-students-warn.md --- .changeset/spotty-students-warn.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spotty-students-warn.md diff --git a/.changeset/spotty-students-warn.md b/.changeset/spotty-students-warn.md new file mode 100644 index 0000000000000..2fe78fd2b50fb --- /dev/null +++ b/.changeset/spotty-students-warn.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +fix: Show always all rooms when requesting chat history, even unserved ones From 51f27a34a453c5d0a33f7e686dcb686ee89562a0 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Wed, 6 Mar 2024 11:53:40 -0600 Subject: [PATCH 3/6] Update spotty-students-warn.md --- .changeset/spotty-students-warn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/spotty-students-warn.md b/.changeset/spotty-students-warn.md index 2fe78fd2b50fb..f3a5808df4b3e 100644 --- a/.changeset/spotty-students-warn.md +++ b/.changeset/spotty-students-warn.md @@ -2,4 +2,4 @@ "@rocket.chat/meteor": patch --- -fix: Show always all rooms when requesting chat history, even unserved ones +fix: Show always all rooms when requesting chat history, even unserved ones. A faulty condition caused an issue where chat history was only able to present either served or unserved chats at once, without a proper way to get both. Now, the Chat history feature will showcase all closed rooms for the requested visitor. From 9b5ae27d9ca3faea6a526dcfeac1db71f81fb012 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 12 Mar 2024 08:31:56 -0600 Subject: [PATCH 4/6] another shot --- .../meteor/server/models/raw/LivechatRooms.ts | 3 ++- .../end-to-end/api/livechat/09-visitors.ts | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/apps/meteor/server/models/raw/LivechatRooms.ts b/apps/meteor/server/models/raw/LivechatRooms.ts index 32779fe5bc30b..2a1cde0c4385c 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -1130,7 +1130,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive const match: Document = { $match: { 'v._id': visitorId, - ...(open !== undefined && open && { open: { $exists: open } }), + ...(open !== undefined && !open && { closedAt: { $exists: true } }), ...(served !== undefined && served && { servedBy: { $exists: served } }), ...(source && { $or: [{ 'source.type': new RegExp(escapeRegExp(source), 'i') }, { 'source.alias': new RegExp(escapeRegExp(source), 'i') }], @@ -1196,6 +1196,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive params.push({ $limit: options.limit }); } + console.log(JSON.stringify(params, null, 2)); return this.col.aggregate(params, { readPreference: readSecondaryPreferred() }); } diff --git a/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts b/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts index 088c8082bb78f..7a9f68869a5a7 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts @@ -673,6 +673,27 @@ describe('LIVECHAT - visitors', function () { expect(res.body.history.find((chat: any) => chat._id === room2._id)).to.be.an('object'); }); }); + + it('should return all chats when both closed & served flags are false', async () => { + const visitor = await createVisitor(); + const room = await createLivechatRoom(visitor.token); + await closeOmnichannelRoom(room._id); + const room2 = await createLivechatRoom(visitor.token); + await closeOmnichannelRoom(room2._id); + await createLivechatRoom(visitor.token); + + const { body } = await request + .get(api(`livechat/visitors.searchChats/room/${room._id}/visitor/${visitor._id}?closedChatsOnly=false&servedChatsOnly=false`)) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(200); + + expect(body).to.have.property('success', true); + expect(body.count).to.be.equal(3); + expect(body.history.filter((chat: any) => !!chat.closedAt).length === 2).to.be.true; + expect(body.history.filter((chat: any) => !chat.closedAt).length === 1).to.be.true; + expect(body.total).to.be.equal(3); + }); }); describe('livechat/visitor.status', () => { From 4c85c4417bacd63d1714f33422418f180944de3a Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 12 Mar 2024 11:05:32 -0600 Subject: [PATCH 5/6] Update 09-visitors.ts --- .../end-to-end/api/livechat/09-visitors.ts | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts b/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts index 7a9f68869a5a7..cfa976fc1d97b 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/09-visitors.ts @@ -630,7 +630,7 @@ describe('LIVECHAT - visitors', function () { }); }); - it('should return open rooms when closedChatsOnly is false', async () => { + it('should return closed rooms (served & unserved) when `closedChatsOnly` is true & `servedChatsOnly` is false', async () => { const { room: { _id: roomId }, visitor: { _id: visitorId, token }, @@ -653,27 +653,6 @@ describe('LIVECHAT - visitors', function () { }); }); - it('should return all closed chats (even unserved ones) when closedChatsOnly is true & servedChatsOnly is false', async () => { - const visitor = await createVisitor(); - const room = await createLivechatRoom(visitor.token); - await closeOmnichannelRoom(room._id); - const room2 = await createLivechatRoom(visitor.token); - await closeOmnichannelRoom(room2._id); - - await request - .get(api(`livechat/visitors.searchChats/room/${room._id}/visitor/${visitor._id}?closedChatsOnly=true&servedChatsOnly=false`)) - .set(credentials) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res: Response) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('history'); - expect(res.body.history).to.be.an('array'); - expect(res.body.history.find((chat: any) => chat._id === room._id)).to.be.an('object'); - expect(res.body.history.find((chat: any) => chat._id === room2._id)).to.be.an('object'); - }); - }); - it('should return all chats when both closed & served flags are false', async () => { const visitor = await createVisitor(); const room = await createLivechatRoom(visitor.token); From e8abd174cf6b6e1f3ba91ba1d40cc77f80dd54ca Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 12 Mar 2024 11:05:42 -0600 Subject: [PATCH 6/6] Update apps/meteor/server/models/raw/LivechatRooms.ts Co-authored-by: Marcos Spessatto Defendi --- apps/meteor/server/models/raw/LivechatRooms.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/meteor/server/models/raw/LivechatRooms.ts b/apps/meteor/server/models/raw/LivechatRooms.ts index 2a1cde0c4385c..8af1a26a8157d 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -1196,7 +1196,6 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive params.push({ $limit: options.limit }); } - console.log(JSON.stringify(params, null, 2)); return this.col.aggregate(params, { readPreference: readSecondaryPreferred() }); }