Skip to content

Commit

Permalink
departments.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Dec 7, 2023
1 parent d00d28f commit 6fccefc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 33 deletions.
8 changes: 4 additions & 4 deletions apps/meteor/app/livechat/server/lib/analytics/dashboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const getProductivityMetricsAsync = async ({
language: user?.language || settings.get('Language') || 'en',
})) || [];
const averageWaitingTime = await findAllAverageWaitingTimeAsync({
start,
end,
start: new Date(start),
end: new Date(end),
departmentId,
});

Expand Down Expand Up @@ -95,8 +95,8 @@ const getAgentsProductivityMetricsAsync = async ({
})
)[0];
const averageOfServiceTime = await findAllAverageServiceTimeAsync({
start,
end,
start: new Date(start),
end: new Date(end),
departmentId,
});
const totalizers =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { LivechatRooms, Messages } from '@rocket.chat/models';

export const findAllRoomsAsync = async ({ start, end, answered, departmentId, options = {} }) => {
type Params = {
start: Date;
end: Date;
options?: any;
departmentId?: string;
};

export const findAllRoomsAsync = async ({ start, end, answered, departmentId, options = {} }: Params & { answered?: boolean }) => {
if (!start || !end) {
throw new Error('"start" and "end" must be provided');
}
Expand All @@ -23,7 +30,7 @@ export const findAllRoomsAsync = async ({ start, end, answered, departmentId, op
};
};

export const findAllAverageOfChatDurationTimeAsync = async ({ start, end, departmentId, options = {} }) => {
export const findAllAverageOfChatDurationTimeAsync = async ({ start, end, departmentId, options = {} }: Params) => {
if (!start || !end) {
throw new Error('"start" and "end" must be provided');
}
Expand All @@ -44,7 +51,7 @@ export const findAllAverageOfChatDurationTimeAsync = async ({ start, end, depart
};
};

export const findAllAverageServiceTimeAsync = async ({ start, end, departmentId, options = {} }) => {
export const findAllAverageServiceTimeAsync = async ({ start, end, departmentId, options = {} }: Params) => {
if (!start || !end) {
throw new Error('"start" and "end" must be provided');
}
Expand All @@ -65,7 +72,7 @@ export const findAllAverageServiceTimeAsync = async ({ start, end, departmentId,
};
};

export const findAllServiceTimeAsync = async ({ start, end, departmentId, options = {} }) => {
export const findAllServiceTimeAsync = async ({ start, end, departmentId, options = {} }: Params) => {
if (!start || !end) {
throw new Error('"start" and "end" must be provided');
}
Expand All @@ -86,7 +93,7 @@ export const findAllServiceTimeAsync = async ({ start, end, departmentId, option
};
};

export const findAllAverageWaitingTimeAsync = async ({ start, end, departmentId, options = {} }) => {
export const findAllAverageWaitingTimeAsync = async ({ start, end, departmentId, options = {} }: Params) => {
if (!start || !end) {
throw new Error('"start" and "end" must be provided');
}
Expand All @@ -107,7 +114,7 @@ export const findAllAverageWaitingTimeAsync = async ({ start, end, departmentId,
};
};

export const findAllNumberOfTransferredRoomsAsync = async ({ start, end, departmentId, options = {} }) => {
export const findAllNumberOfTransferredRoomsAsync = async ({ start, end, departmentId, options = {} }: Params) => {
if (!start || !end) {
throw new Error('"start" and "end" must be provided');
}
Expand All @@ -128,7 +135,7 @@ export const findAllNumberOfTransferredRoomsAsync = async ({ start, end, departm
};
};

export const findAllNumberOfAbandonedRoomsAsync = async ({ start, end, departmentId, options = {} }) => {
export const findAllNumberOfAbandonedRoomsAsync = async ({ start, end, departmentId, options = {} }: Params) => {
if (!start || !end) {
throw new Error('"start" and "end" must be provided');
}
Expand All @@ -139,7 +146,7 @@ export const findAllNumberOfAbandonedRoomsAsync = async ({ start, end, departmen
};
};

export const findPercentageOfAbandonedRoomsAsync = async ({ start, end, departmentId, options = {} }) => {
export const findPercentageOfAbandonedRoomsAsync = async ({ start, end, departmentId, options = {} }: Params) => {
if (!start || !end) {
throw new Error('"start" and "end" must be provided');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
isLivechatAnalyticsDepartmentsTotalAbandonedChatsProps,
isLivechatAnalyticsDepartmentsPercentageAbandonedChatsProps,
} from '@rocket.chat/rest-typings';
import { Match, check } from 'meteor/check';

import { API } from '../../../../../app/api/server';
import { getPaginationItems } from '../../../../../app/api/server/helpers/getPaginationItems';
Expand Down Expand Up @@ -45,7 +44,7 @@ API.v1.addRoute(
const { departments, total } = await findAllRoomsAsync({
start: startDate,
end: endDate,
answered: answered && answered === 'true',
...(answered ? { answered: answered === 'true' } : { answered: false }),
departmentId,
options: { offset, count },
});
Expand Down Expand Up @@ -189,10 +188,6 @@ API.v1.addRoute(
const { start, end } = this.queryParams;
const { departmentId } = this.queryParams;

check(start, String);
check(end, String);
check(departmentId, Match.Maybe(String));

if (isNaN(Date.parse(start))) {
return API.v1.failure('The "start" query parameter must be a valid date.');
}
Expand Down
47 changes: 39 additions & 8 deletions apps/meteor/server/models/raw/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,47 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
return this.findPaginated(query, options);
}

findAllNumberOfTransferredRooms({
start,
end,
departmentId,
onlyCount,
options,
}: {
start: Date;
end: Date;
departmentId?: ILivechatDepartment['_id'];
onlyCount: true;
options?: PaginatedRequest;
}): AggregationCursor<{ total: number }>;

findAllNumberOfTransferredRooms({
start,
end,
departmentId,
onlyCount,
options,
}: {
start: Date;
end: Date;
departmentId?: ILivechatDepartment['_id'];
onlyCount?: false;
options?: PaginatedRequest;
}): AggregationCursor<{ _id: string | null; numberOfTransferredRooms: number }>;

findAllNumberOfTransferredRooms({
start,
end,
departmentId,
onlyCount = false,
options = {},
}: {
start: string;
end: string;
departmentId: ILivechatDepartment['_id'];
onlyCount: boolean;
options: PaginatedRequest;
}): AggregationCursor<any> {
start: Date;
end: Date;
departmentId?: ILivechatDepartment['_id'];
onlyCount?: boolean;
options?: PaginatedRequest;
}): AggregationCursor<{ total: number }> | AggregationCursor<{ _id: string | null; numberOfTransferredRooms: number }> {
// FIXME: aggregation type definitions
const match = {
$match: {
Expand Down Expand Up @@ -211,15 +239,18 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
const params = [...firstParams, group, project, sort];
if (onlyCount) {
params.push({ $count: 'total' });
return this.col.aggregate(params, { readPreference: readSecondaryPreferred() });
return this.col.aggregate<{ total: number }>(params, { readPreference: readSecondaryPreferred() });
}
if (options.offset) {
params.push({ $skip: options.offset });
}
if (options.count) {
params.push({ $limit: options.count });
}
return this.col.aggregate(params, { allowDiskUse: true, readPreference: readSecondaryPreferred() });
return this.col.aggregate<{ _id: string | null; numberOfTransferredRooms: number }>(params, {
allowDiskUse: true,
readPreference: readSecondaryPreferred(),
});
}

getTotalOfMessagesSentByDate({ start, end, options = {} }: { start: Date; end: Date; options?: PaginatedRequest }): Promise<any[]> {
Expand Down
23 changes: 16 additions & 7 deletions packages/model-typings/src/models/IMessagesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
MessageAttachment,
IMessageWithPendingFileImport,
} from '@rocket.chat/core-typings';
import type { PaginatedRequest } from '@rocket.chat/rest-typings';
import type {
AggregationCursor,
CountDocumentsOptions,
Expand Down Expand Up @@ -44,13 +45,21 @@ export interface IMessagesModel extends IBaseModel<IMessage> {

findDiscussionsByRoomAndText(rid: IRoom['_id'], text: string, options?: FindOptions<IMessage>): FindPaginated<FindCursor<IMessage>>;

findAllNumberOfTransferredRooms(params: {
start: string;
end: string;
departmentId: ILivechatDepartment['_id'];
onlyCount: boolean;
options: any;
}): AggregationCursor<any>;
findAllNumberOfTransferredRooms(p: {
start: Date;
end: Date;
departmentId?: ILivechatDepartment['_id'];
onlyCount: true;
options?: PaginatedRequest;
}): AggregationCursor<{ total: number }>;

findAllNumberOfTransferredRooms(p: {
start: Date;
end: Date;
departmentId?: ILivechatDepartment['_id'];
onlyCount?: false;
options?: PaginatedRequest;
}): AggregationCursor<{ _id: string | null; numberOfTransferredRooms: number }>;

getTotalOfMessagesSentByDate(params: { start: Date; end: Date; options?: any }): Promise<any[]>;

Expand Down

0 comments on commit 6fccefc

Please sign in to comment.