From 31362ac4a878763463fe451ba19624335f460ac1 Mon Sep 17 00:00:00 2001 From: Matheus Barbosa Silva <36537004+matheusbsilva137@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:49:01 -0300 Subject: [PATCH] regression: `lastChat` property missing on contact migrated from visitor (#34016) --- .../server/lib/contacts/createContact.ts | 5 ++- .../lib/contacts/mapVisitorToContact.spec.ts | 43 +++++++++++++++++++ .../lib/contacts/mapVisitorToContact.ts | 1 + 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/apps/meteor/app/livechat/server/lib/contacts/createContact.ts b/apps/meteor/app/livechat/server/lib/contacts/createContact.ts index 2bca2aa1ee7e..efdcacdbf4ad 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/createContact.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/createContact.ts @@ -1,4 +1,4 @@ -import type { ILivechatContactChannel } from '@rocket.chat/core-typings'; +import type { ILivechatContactChannel, IVisitorLastChat } from '@rocket.chat/core-typings'; import { LivechatContacts } from '@rocket.chat/models'; import { getAllowedCustomFields } from './getAllowedCustomFields'; @@ -11,6 +11,7 @@ export type CreateContactParams = { phones?: string[]; unknown: boolean; customFields?: Record; + lastChat?: IVisitorLastChat; contactManager?: string; channels?: ILivechatContactChannel[]; importIds?: string[]; @@ -21,6 +22,7 @@ export async function createContact({ emails, phones, customFields: receivedCustomFields = {}, + lastChat, contactManager, channels = [], unknown, @@ -40,6 +42,7 @@ export async function createContact({ contactManager, channels, customFields, + lastChat, unknown, ...(importIds?.length && { importIds }), }); diff --git a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts index 04762f578a6f..65f26edfb83c 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.spec.ts @@ -13,6 +13,7 @@ const { mapVisitorToContact } = proxyquire.noCallThru().load('./mapVisitorToCont }, }); +const testDate = new Date(); const dataMap: [Partial, IOmnichannelSource, CreateContactParams][] = [ [ { @@ -87,6 +88,48 @@ const dataMap: [Partial, IOmnichannelSource, CreateContactPara contactManager: undefined, }, ], + + [ + { + _id: 'visitor1', + username: 'Username', + lastChat: { + _id: 'last-chat-id', + ts: testDate, + }, + }, + { + type: OmnichannelSourceType.WIDGET, + }, + { + name: 'Username', + emails: undefined, + phones: undefined, + unknown: false, + channels: [ + { + name: 'sms', + visitor: { + visitorId: 'visitor1', + source: { + type: OmnichannelSourceType.WIDGET, + }, + }, + blocked: false, + verified: false, + details: { + type: OmnichannelSourceType.WIDGET, + }, + }, + ], + customFields: undefined, + lastChat: { + _id: 'last-chat-id', + ts: testDate, + }, + contactManager: undefined, + }, + ], ]; describe('mapVisitorToContact', () => { diff --git a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.ts b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.ts index 74f215d6bb7e..ad1ac994ce0f 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/mapVisitorToContact.ts @@ -25,6 +25,7 @@ export async function mapVisitorToContact(visitor: ILivechatVisitor, source: IOm }, ], customFields: visitor.livechatData, + lastChat: visitor.lastChat, contactManager: visitor.contactManager?.username && (await getContactManagerIdByUsername(visitor.contactManager.username)), }; }