From a7386197c6f9ebd9c57a720b63863d15594c2abf Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 6 Nov 2024 13:48:28 -0300 Subject: [PATCH] return manager information on contacts.search --- .../server/lib/contacts/getContacts.ts | 25 +++++++++++++++---- packages/rest-typings/src/v1/omnichannel.ts | 7 +++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/apps/meteor/app/livechat/server/lib/contacts/getContacts.ts b/apps/meteor/app/livechat/server/lib/contacts/getContacts.ts index 76666efd311e4..26cc93cf607fa 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/getContacts.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/getContacts.ts @@ -1,6 +1,6 @@ -import type { ILivechatContact } from '@rocket.chat/core-typings'; -import { LivechatContacts } from '@rocket.chat/models'; -import type { PaginatedResult } from '@rocket.chat/rest-typings'; +import type { IUser } from '@rocket.chat/core-typings'; +import { LivechatContacts, Users } from '@rocket.chat/models'; +import type { PaginatedResult, ILivechatContactWithManagerData } from '@rocket.chat/rest-typings'; import type { Sort } from 'mongodb'; export type GetContactsParams = { @@ -11,7 +11,7 @@ export type GetContactsParams = { unknown?: boolean; }; -export async function getContacts(params: GetContactsParams): Promise> { +export async function getContacts(params: GetContactsParams): Promise> { const { searchText, count, offset, sort, unknown } = params; const { cursor, totalCount } = LivechatContacts.findPaginatedContacts( @@ -23,7 +23,22 @@ export async function getContacts(params: GetContactsParams): Promise contactManager))]; + const managersData = await Users.findByIds>(managerIds, { + projection: { name: 1, username: 1 }, + }).toArray(); + const mappedManagers = Object.fromEntries(managersData.map((manager) => [manager._id, manager])); + + const contacts: ILivechatContactWithManagerData[] = rawContacts.map((contact) => { + const { contactManager, ...data } = contact; + + return { + ...data, + ...(contactManager ? { contactManager: mappedManagers[contactManager] } : {}), + }; + }); return { contacts, diff --git a/packages/rest-typings/src/v1/omnichannel.ts b/packages/rest-typings/src/v1/omnichannel.ts index 4f8ec15b4dc06..642953923df4b 100644 --- a/packages/rest-typings/src/v1/omnichannel.ts +++ b/packages/rest-typings/src/v1/omnichannel.ts @@ -31,6 +31,7 @@ import type { ILivechatContact, ILivechatContactVisitorAssociation, ILivechatContactChannel, + IUser, } from '@rocket.chat/core-typings'; import { ILivechatAgentStatus } from '@rocket.chat/core-typings'; import type { WithId } from 'mongodb'; @@ -3571,6 +3572,10 @@ const LivechatTriggerWebhookCallParamsSchema = { export const isLivechatTriggerWebhookCallParams = ajv.compile(LivechatTriggerWebhookCallParamsSchema); +export type ILivechatContactWithManagerData = Omit & { + contactManager?: Pick; +}; + export type OmnichannelEndpoints = { '/v1/livechat/appearance': { GET: () => { @@ -3860,7 +3865,7 @@ export type OmnichannelEndpoints = { GET: (params: GETOmnichannelContactsProps) => { contact: ILivechatContact | null }; }; '/v1/omnichannel/contacts.search': { - GET: (params: GETOmnichannelContactsSearchProps) => PaginatedResult<{ contacts: ILivechatContact[] }>; + GET: (params: GETOmnichannelContactsSearchProps) => PaginatedResult<{ contacts: ILivechatContactWithManagerData[] }>; }; '/v1/omnichannel/contacts.history': { GET: (params: GETOmnichannelContactHistoryProps) => PaginatedResult<{ history: ContactSearchChatsResult[] }>;