Skip to content

Commit

Permalink
return manager information on contacts.search
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc committed Nov 6, 2024
1 parent 7f46d97 commit a738619
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 20 additions & 5 deletions apps/meteor/app/livechat/server/lib/contacts/getContacts.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -11,7 +11,7 @@ export type GetContactsParams = {
unknown?: boolean;
};

export async function getContacts(params: GetContactsParams): Promise<PaginatedResult<{ contacts: ILivechatContact[] }>> {
export async function getContacts(params: GetContactsParams): Promise<PaginatedResult<{ contacts: ILivechatContactWithManagerData[] }>> {
const { searchText, count, offset, sort, unknown } = params;

const { cursor, totalCount } = LivechatContacts.findPaginatedContacts(
Expand All @@ -23,7 +23,22 @@ export async function getContacts(params: GetContactsParams): Promise<PaginatedR
},
);

const [contacts, total] = await Promise.all([cursor.toArray(), totalCount]);
const [rawContacts, total] = await Promise.all([cursor.toArray(), totalCount]);

const managerIds = [...new Set(rawContacts.map(({ contactManager }) => contactManager))];
const managersData = await Users.findByIds<Pick<IUser, '_id' | 'name' | 'username'>>(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,
Expand Down
7 changes: 6 additions & 1 deletion packages/rest-typings/src/v1/omnichannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -3571,6 +3572,10 @@ const LivechatTriggerWebhookCallParamsSchema = {

export const isLivechatTriggerWebhookCallParams = ajv.compile<LivechatTriggerWebhookCallParams>(LivechatTriggerWebhookCallParamsSchema);

export type ILivechatContactWithManagerData = Omit<ILivechatContact, 'contactManager'> & {
contactManager?: Pick<IUser, '_id' | 'name' | 'username'>;
};

export type OmnichannelEndpoints = {
'/v1/livechat/appearance': {
GET: () => {
Expand Down Expand Up @@ -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[] }>;
Expand Down

0 comments on commit a738619

Please sign in to comment.