Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions apps/meteor/app/api/server/v1/call-history.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CallHistory as CallHistoryService } from '@rocket.chat/core-services';
import type { CallHistoryItem, CallHistoryItemState, IMediaCall } from '@rocket.chat/core-typings';
import { CallHistory, MediaCalls } from '@rocket.chat/models';
import type { PaginatedRequest, PaginatedResult } from '@rocket.chat/rest-typings';
Expand All @@ -9,7 +10,6 @@ import {
validateUnauthorizedErrorResponse,
validateForbiddenErrorResponse,
} from '@rocket.chat/rest-typings';
import { escapeRegExp } from '@rocket.chat/string-helpers';

import { ensureArray } from '../../../../lib/utils/arrayUtils';
import type { ExtractRoutesFromAPI } from '../ApiClass';
Expand Down Expand Up @@ -104,37 +104,17 @@ const callHistoryListEndpoints = API.v1.get(

const { direction, state, filter } = this.queryParams;

const filterText = typeof filter === 'string' && filter.trim();

const stateFilter = state && ensureArray(state);
const query = {
uid: this.userId,
...(direction && { direction }),
...(stateFilter?.length && { state: { $in: stateFilter } }),
...(filterText && {
$or: [
{
external: false,
contactName: { $regex: escapeRegExp(filterText), $options: 'i' },
},
{
external: false,
contactUsername: { $regex: escapeRegExp(filterText), $options: 'i' },
},
{
external: true,
contactExtension: { $regex: escapeRegExp(filterText), $options: 'i' },
},
],
}),
};
const searchTerm = typeof filter === 'string' && filter.trim();

const { cursor, totalCount } = CallHistory.findPaginated(query, {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
});
const [items, total] = await Promise.all([cursor.toArray(), totalCount]);
const { items, total } = await CallHistoryService.search(
this.userId,
{
...(searchTerm && { searchTerm }),
...(direction && { direction }),
...(state && { inStates: ensureArray(state) }),
},
{ count, offset, sort },
);

return API.v1.success({
items,
Expand Down
39 changes: 39 additions & 0 deletions apps/meteor/server/services/call-history/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ServiceClassInternal, type ICallHistoryService } from '@rocket.chat/core-services';
import type { IUser, CallHistoryItem } from '@rocket.chat/core-typings';
import { CallHistory } from '@rocket.chat/models';

export class CallHistoryService extends ServiceClassInternal implements ICallHistoryService {
protected name = 'call-history';

public async search(
uid: IUser['_id'],
filters: {
searchTerm?: string;
direction?: CallHistoryItem['direction'];
inStates?: CallHistoryItem['state'][];
},
pagination: {
count: number;
offset: number;
sort?: Record<string, 1 | -1>;
},
): Promise<{ items: CallHistoryItem[]; total: number }> {
const { offset, count, sort } = pagination || {};

const { cursor, totalCount } = CallHistory.findAllByUserIdAndSearchFilters(
uid,
{ ...filters },
{
sort: sort || { ts: -1 },
skip: offset,
limit: count,
},
);
const [items, total] = await Promise.all([cursor.toArray(), totalCount]);

return {
items,
total,
};
}
}
2 changes: 2 additions & 0 deletions apps/meteor/server/services/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AnalyticsService } from './analytics/service';
import { AppsEngineService } from './apps-engine/service';
import { BannerService } from './banner/service';
import { CalendarService } from './calendar/service';
import { CallHistoryService } from './call-history/service';
import { DeviceManagementService } from './device-management/service';
import { MediaService } from './image/service';
import { ImportService } from './import/service';
Expand Down Expand Up @@ -59,6 +60,7 @@ export const registerServices = async (): Promise<void> => {
api.registerService(new OmnichannelAnalyticsService());
api.registerService(new UserService());
api.registerService(new MediaCallService());
api.registerService(new CallHistoryService());

// if the process is running in micro services mode we don't need to register services that will run separately
if (!isRunningMs()) {
Expand Down
3 changes: 3 additions & 0 deletions packages/core-services/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { IAuthorization, RoomAccessValidator } from './types/IAuthorization
import type { IAuthorizationLivechat } from './types/IAuthorizationLivechat';
import type { IBannerService } from './types/IBannerService';
import type { ICalendarService } from './types/ICalendarService';
import type { ICallHistoryService } from './types/ICallHistoryService';
import type { IDeviceManagementService } from './types/IDeviceManagementService';
import type { IEnterpriseSettings } from './types/IEnterpriseSettings';
import type { IFederationMatrixService } from './types/IFederationMatrixService';
Expand Down Expand Up @@ -143,6 +144,7 @@ export type {
IUploadFileParams,
IUploadService,
ICalendarService,
ICallHistoryService,
IOmnichannelTranscriptService,
IQueueWorkerService,
HealthAggResult,
Expand Down Expand Up @@ -178,6 +180,7 @@ export const DeviceManagement = proxify<IDeviceManagementService>('device-manage
export const VideoConf = proxify<IVideoConfService>('video-conference');
export const Upload = proxify<IUploadService>('upload');
export const Calendar = proxify<ICalendarService>('calendar');
export const CallHistory = proxify<ICallHistoryService>('call-history');
export const QueueWorker = proxify<IQueueWorkerService>('queue-worker');
export const OmnichannelTranscript = proxify<IOmnichannelTranscriptService>('omnichannel-transcript');
export const Message = proxify<IMessageService>('message');
Expand Down
17 changes: 17 additions & 0 deletions packages/core-services/src/types/ICallHistoryService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { CallHistoryItem, IUser } from '@rocket.chat/core-typings';

export interface ICallHistoryService {
search(
uid: IUser['_id'],
filters: {
searchTerm?: string;
direction?: CallHistoryItem['direction'];
inStates?: CallHistoryItem['state'][];
},
pagination: {
count: number;
offset: number;
sort?: Record<string, 1 | -1>;
},
): Promise<{ items: CallHistoryItem[]; total: number }>;
}
17 changes: 14 additions & 3 deletions packages/model-typings/src/models/ICallHistoryModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CallHistoryItem, IRegisterUser } from '@rocket.chat/core-typings';
import type { FindOptions } from 'mongodb';
import type { CallHistoryItem, IRegisterUser, IUser } from '@rocket.chat/core-typings';
import type { FindCursor, FindOptions } from 'mongodb';

import type { IBaseModel } from './IBaseModel';
import type { FindPaginated, IBaseModel } from './IBaseModel';

export interface ICallHistoryModel extends IBaseModel<CallHistoryItem> {
findOneByIdAndUid(
Expand All @@ -16,5 +16,16 @@ export interface ICallHistoryModel extends IBaseModel<CallHistoryItem> {
options?: FindOptions<CallHistoryItem>,
): Promise<CallHistoryItem | null>;

findAllByUserIdAndSearchFilters(
uid: IUser['_id'],
filters: {
type?: CallHistoryItem['type'];
searchTerm?: string;
direction?: CallHistoryItem['direction'];
inStates?: CallHistoryItem['state'][];
},
options: FindOptions<CallHistoryItem>,
): FindPaginated<FindCursor<CallHistoryItem>>;

updateUserReferences(userId: IRegisterUser['_id'], username: IRegisterUser['username'], name?: IRegisterUser['name']): Promise<void>;
}
46 changes: 43 additions & 3 deletions packages/models/src/models/CallHistory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CallHistoryItem, IRegisterUser } from '@rocket.chat/core-typings';
import type { ICallHistoryModel } from '@rocket.chat/model-typings';
import type { Db, FindOptions, IndexDescription } from 'mongodb';
import type { CallHistoryItem, IRegisterUser, IUser } from '@rocket.chat/core-typings';
import type { FindPaginated, ICallHistoryModel } from '@rocket.chat/model-typings';
import { escapeRegExp } from '@rocket.chat/string-helpers';
import type { Db, Filter, FindCursor, FindOptions, IndexDescription } from 'mongodb';

import { BaseRaw } from './BaseRaw';

Expand Down Expand Up @@ -46,4 +47,43 @@ export class CallHistoryRaw extends BaseRaw<CallHistoryItem> implements ICallHis
},
);
}

public findAllByUserIdAndSearchFilters(
uid: IUser['_id'],
filters: {
type?: CallHistoryItem['type'];
searchTerm?: string;
direction?: CallHistoryItem['direction'];
inStates?: CallHistoryItem['state'][];
},
options: FindOptions<CallHistoryItem>,
): FindPaginated<FindCursor<CallHistoryItem>> {
const { type, direction, inStates, searchTerm } = filters;

const textSearch = searchTerm ? { $regex: escapeRegExp(searchTerm), $options: 'i' } : null;

const query: Filter<CallHistoryItem> = {
uid,
...(type && { type }),
...(direction && { direction }),
...(inStates?.length && { state: { $in: inStates } }),
...(textSearch && {
$or: [
{
contactName: textSearch,
},
{
external: false,
contactUsername: textSearch,
},
{
external: true,
contactExtension: textSearch,
},
],
}),
};

return this.findPaginated(query, options);
}
}
Loading