diff --git a/.changeset/dirty-crabs-deliver.md b/.changeset/dirty-crabs-deliver.md new file mode 100644 index 000000000000..374d10e37530 --- /dev/null +++ b/.changeset/dirty-crabs-deliver.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +fix: Prevent app's bridges from overriding the lastMsg prop which further was affecting Omni-Visitor abandonment feature for app diff --git a/apps/meteor/app/apps/server/converters/rooms.js b/apps/meteor/app/apps/server/converters/rooms.js index 8fc74ac21c9a..44d513bd591d 100644 --- a/apps/meteor/app/apps/server/converters/rooms.js +++ b/apps/meteor/app/apps/server/converters/rooms.js @@ -38,11 +38,16 @@ export class AppRoomsConverter { let v; if (room.visitor) { const visitor = await LivechatVisitors.findOneById(room.visitor.id); + + const lastMessageTs = room?.visitor?.lastMessageTs; + const phone = room?.visitor?.channelPhone; v = { _id: visitor._id, username: visitor.username, token: visitor.token, status: visitor.status || 'online', + ...(lastMessageTs && { lastMessageTs }), + ...(phone && { phone }), }; } @@ -172,9 +177,21 @@ export class AppRoomsConverter { return undefined; } + const { lastMessageTs, phone } = v; + delete room.v; - return this.orch.getConverters().get('visitors').convertById(v._id); + return { + ...(await this.orch.getConverters().get('visitors').convertById(v._id)), + // Note: room.v is not just visitor, it also contains channel related visitor data + // so we need to pass this data to the converter + // So suppose you have a contact whom we're contacting using SMS via 2 phone no's, + // let's call X and Y. Then if the contact sends a message using X phone number, + // then room.v.phoneNo would be X and correspondingly we'll store the timestamp of + // the last message from this visitor from X phone no on room.v.lastMessageTs + ...(phone && { channelPhone: phone }), + ...(lastMessageTs && { lastMessageTs }), + }; }, department: async (room) => { const { departmentId } = room; diff --git a/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts b/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts index b6273c0f3e74..1dabee3b8c03 100644 --- a/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts +++ b/apps/meteor/app/livechat/server/hooks/saveLastVisitorMessageTs.ts @@ -9,6 +9,9 @@ callbacks.add( if (!(isOmnichannelRoom(room) && room.v.token)) { return message; } + if (message.t) { + return message; + } if (message.token) { await LivechatRooms.setVisitorLastMessageTimestampByRoomId(room._id, message.ts); } diff --git a/packages/core-typings/src/IInquiry.ts b/packages/core-typings/src/IInquiry.ts index 26ba2bc32188..f3dba1e9d907 100644 --- a/packages/core-typings/src/IInquiry.ts +++ b/packages/core-typings/src/IInquiry.ts @@ -18,6 +18,9 @@ export enum LivechatInquiryStatus { OPEN = 'open', } +// This is a subset of the IVisitor interface + channel related fields +// IMPORTANT: If you're adding a new field here, make sure to update the +// apps-engine's room converter to include it too export interface IVisitor { _id: string; username: string;