Skip to content

Commit

Permalink
CR suggestions about avoiding unnecessary null check
Browse files Browse the repository at this point in the history
  • Loading branch information
murtaza98 committed Jun 3, 2022
1 parent 5d550b9 commit 5d8592d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions apps/meteor/app/api/server/v1/voip/rooms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Match, check } from 'meteor/check';
import { Random } from 'meteor/random';
import type { ILivechatAgent } from '@rocket.chat/core-typings';
import type { ILivechatAgent, IVoipRoom } from '@rocket.chat/core-typings';

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

/**
* @openapi
* /voip/server/api/v1/voip/room
Expand Down Expand Up @@ -86,21 +87,25 @@ API.v1.addRoute(
{ authRequired: false, rateLimiterOptions: { numRequestsAllowed: 5, intervalTimeInMS: 60000 } },
{
async get() {
const defaultCheckParams = {
token: String,
agentId: Match.Maybe(String),
rid: Match.Maybe(String),
direction: VoipRoomDirectionValidator,
const { token, rid, agentId, direction } = this.queryParams as {
token: string;
rid?: string;
agentId?: string;
direction?: IVoipRoom['direction'];
};
check(this.queryParams, defaultCheckParams);

const { token, rid, agentId, direction } = this.queryParams;
check(token, String);
check(rid, Match.Maybe(String));
check(agentId, Match.Maybe(String));

const guest = await LivechatVisitors.getVisitorByToken(token, {});
if (!guest) {
return API.v1.failure('invalid-token');
}

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 All @@ -117,11 +122,11 @@ API.v1.addRoute(
const agent = { agentId: _id, username };
const rid = Random.id();

if (!direction) {
return API.v1.failure('no-direction-provided-for-new-room');
}

return API.v1.success(await LivechatVoip.getNewRoom(guest, agent, rid, direction, { projection: API.v1.defaultFieldsToExclude }));
return API.v1.success(
await LivechatVoip.getNewRoom(guest, agent, rid, direction as IVoipRoom['direction'], {
projection: API.v1.defaultFieldsToExclude,
}),
);
}

const room = await VoipRoom.findOneByIdAndVisitorToken(rid, token, { projection: API.v1.defaultFieldsToExclude });
Expand All @@ -148,7 +153,7 @@ API.v1.addRoute(
check(tags, Match.Maybe([String]));
check(queue, Match.Maybe(String));
check(visitorId, Match.Maybe(String));
check(direction, VoipRoomDirectionValidator);
check(direction, Match.Maybe(VoipRoomDirectionValidator));

// Reusing same L room permissions for simplicity
const hasAdminAccess = hasPermission(this.userId, 'view-livechat-rooms');
Expand Down

0 comments on commit 5d8592d

Please sign in to comment.