Skip to content

Commit

Permalink
Adapt endpoints to use new AJV validations
Browse files Browse the repository at this point in the history
  • Loading branch information
murtaza98 committed Jun 6, 2022
1 parent d69b8f0 commit d332b81
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
18 changes: 5 additions & 13 deletions apps/meteor/app/api/server/v1/voip/rooms.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Match, check } from 'meteor/check';
import { Random } from 'meteor/random';
import type { ILivechatAgent, IVoipRoom } from '@rocket.chat/core-typings';
import { isVoipRoomProps } from '@rocket.chat/rest-typings/dist/v1/voip';
import { isVoipRoomProps, isVoipRoomsProps } from '@rocket.chat/rest-typings/dist/v1/voip';

import { API } from '../../api';
import { VoipRoom, LivechatVisitors, Users } from '../../../../models/server/raw';
Expand All @@ -25,7 +25,6 @@ const validateDateParams = (property: string, date: DateParam = {}): DateParam =
const parseAndValidate = (property: string, date?: string): DateParam => {
return validateDateParams(property, parseDateParams(date));
};
const VoipRoomDirectionValidator = Match.Where((value: string) => ['inbound', 'outbound'].includes(value));

/**
* @openapi
Expand Down Expand Up @@ -100,8 +99,6 @@ API.v1.addRoute(
}

if (!rid) {
check(direction, VoipRoomDirectionValidator);

const room = await VoipRoom.findOneOpenByVisitorToken(token, { projection: API.v1.defaultFieldsToExclude });
if (room) {
return API.v1.success({ room, newRoom: false });
Expand Down Expand Up @@ -136,21 +133,15 @@ API.v1.addRoute(

API.v1.addRoute(
'voip/rooms',
{ authRequired: true },
{ authRequired: true, validateParams: isVoipRoomsProps },
{
async get() {
const { offset, count } = this.getPaginationItems();

const { sort, fields } = this.parseJsonQuery();
const { agents, open, tags, queue, visitorId, direction } = this.requestParams();
const { agents, open, tags, queue, visitorId, direction, roomName } = this.requestParams();
const { createdAt: createdAtParam, closedAt: closedAtParam } = this.requestParams();

check(agents, Match.Maybe([String]));
check(open, Match.Maybe(String));
check(tags, Match.Maybe([String]));
check(queue, Match.Maybe(String));
check(visitorId, Match.Maybe(String));
check(direction, Match.Maybe(VoipRoomDirectionValidator));

// Reusing same L room permissions for simplicity
const hasAdminAccess = hasPermission(this.userId, 'view-livechat-rooms');
const hasAgentAccess = hasPermission(this.userId, 'view-l-room') && agents?.includes(this.userId) && agents?.length === 1;
Expand All @@ -171,6 +162,7 @@ API.v1.addRoute(
createdAt,
closedAt,
direction,
roomName,
options: { sort, offset, count, fields },
}),
);
Expand Down
6 changes: 6 additions & 0 deletions apps/meteor/app/models/server/raw/VoipRooms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FilterQuery, WithoutProjection, FindOneOptions, WriteOpResult, Cursor } from 'mongodb';
import type { IVoipRoom, IRoomClosingInfo } from '@rocket.chat/core-typings';
import { escapeRegExp } from '@rocket.chat/string-helpers';

import { BaseRaw } from './BaseRaw';
import { Logger } from '../../../../server/lib/logger/Logger';
Expand Down Expand Up @@ -109,6 +110,7 @@ export class VoipRoomsRaw extends BaseRaw<IVoipRoom> {
queue,
visitorId,
direction,
roomName,
options = {},
}: {
agents?: string[];
Expand All @@ -119,6 +121,7 @@ export class VoipRoomsRaw extends BaseRaw<IVoipRoom> {
queue?: string;
visitorId?: string;
direction?: IVoipRoom['direction'];
roomName?: string;
options?: {
sort?: Record<string, unknown>;
count?: number;
Expand Down Expand Up @@ -166,6 +169,9 @@ export class VoipRoomsRaw extends BaseRaw<IVoipRoom> {
if (direction) {
query.direction = direction;
}
if (roomName) {
query.name = new RegExp(escapeRegExp(roomName), 'i');
}

return this.find(query, {
sort: options.sort || { name: 1 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export type FindVoipRoomsParams = {
offset?: number;
};
direction?: IVoipRoom['direction'];
roomName?: string;
};
2 changes: 2 additions & 0 deletions apps/meteor/server/services/omnichannel-voip/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ export class OmnichannelVoipService extends ServiceClassInternal implements IOmn
tags,
queue,
direction,
roomName,
options: { offset = 0, count, fields, sort } = {},
}: FindVoipRoomsParams): Promise<PaginatedResult<{ rooms: IVoipRoom[] }>> {
const cursor = this.voipRoom.findRoomsWithCriteria({
Expand All @@ -409,6 +410,7 @@ export class OmnichannelVoipService extends ServiceClassInternal implements IOmn
queue,
visitorId,
direction,
roomName,
options: {
sort: sort || { ts: -1 },
offset,
Expand Down
27 changes: 24 additions & 3 deletions packages/rest-typings/src/v1/voip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,17 @@ const VoipCallServerCheckConnectionSchema: JSONSchemaType<VoipCallServerCheckCon

export const isVoipCallServerCheckConnectionProps = ajv.compile<VoipCallServerCheckConnection>(VoipCallServerCheckConnectionSchema);

type VoipRooms = {
type VoipRooms = PaginatedRequest<{
agents?: string[];
open?: 'true' | 'false';
createdAt?: string;
closedAt?: string;
tags?: string[];
queue?: string;
visitorId?: string;
roomName?: string;
direction?: IVoipRoom['direction'];
};
}>;

const VoipRoomsSchema: JSONSchemaType<VoipRooms> = {
type: 'object',
Expand Down Expand Up @@ -463,6 +464,26 @@ const VoipRoomsSchema: JSONSchemaType<VoipRooms> = {
enum: ['inbound', 'outbound'],
nullable: true,
},
roomName: {
type: 'string',
nullable: true,
},
count: {
type: 'number',
nullable: true,
},
offset: {
type: 'number',
nullable: true,
},
sort: {
type: 'string',
nullable: true,
},
query: {
type: 'string',
nullable: true,
},
},
required: [],
additionalProperties: false,
Expand Down Expand Up @@ -492,7 +513,7 @@ const VoipRoomCloseSchema: JSONSchemaType<VoipRoomClose> = {
nullable: true,
},
},
required: ['rid', 'token', 'comment'],
required: ['rid', 'token'],
additionalProperties: false,
};

Expand Down

0 comments on commit d332b81

Please sign in to comment.