Skip to content

Commit

Permalink
validating Room object projections
Browse files Browse the repository at this point in the history
  • Loading branch information
cabaceira committed Oct 24, 2023
1 parent 96cf998 commit 284740d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,8 @@ export const useQuickActions = (): {
});

const omnichannelRouteConfig = useOmnichannelRouteConfig();

const manualOnHoldAllowed = useSetting('Livechat_allow_manual_on_hold');
const hasManagerRole = useRole('livechat-manager');

const roomOpen = room?.open && (room.u?._id === uid || hasManagerRole) && room?.lastMessage?.t !== 'livechat-close';
const canMoveQueue = !!omnichannelRouteConfig?.returnQueue && room?.u !== undefined;
const canForwardGuest = usePermission('transfer-livechat-guest');
Expand All @@ -309,8 +308,8 @@ export const useQuickActions = (): {
const canCloseOthersRoom = usePermission('close-others-livechat-room');
const restrictedOnHold = useSetting('Livechat_allow_manual_on_hold_upon_agent_engagement_only');
const canRoomBePlacedOnHold = !room.onHold && room.u;
const canAgentPlaceOnHold = !room.lastMessage.token;
const canPlaceChatOnHold = canRoomBePlacedOnHold && (!restrictedOnHold || canAgentPlaceOnHold);
const canAgentPlaceOnHold = !room.lastMessage?.token;
const canPlaceChatOnHold = manualOnHoldAllowed && canRoomBePlacedOnHold && (!restrictedOnHold || canAgentPlaceOnHold);

const hasPermissionButtons = (id: string): boolean => {
switch (id) {
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/ee/app/livechat-enterprise/server/api/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ API.v1.addRoute(
async post() {
const { roomId } = this.bodyParams;

type Room = Pick<IOmnichannelRoom, '_id' | 't' | 'open' | 'onHold' | 'lastMessage' | 'servedBy'>;
type Room = Pick<IOmnichannelRoom, '_id' | 't' | 'open' | 'onHold' | 'u' | 'lastMessage' | 'servedBy'>;

const room = await LivechatRooms.findOneById<Room>(roomId, {
projection: { _id: 1, t: 1, open: 1, onHold: 1, lastMessage: 1, servedBy: 1 },
projection: { _id: 1, t: 1, open: 1, onHold: 1, u: 1, lastMessage: 1, servedBy: 1 },
});
if (!room) {
throw new Error('error-invalid-room');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { LivechatRooms, Subscriptions, LivechatInquiry } from '@rocket.chat/mode
import { dispatchAgentDelegated } from '../../../../../app/livechat/server/lib/Helper';
import { queueInquiry } from '../../../../../app/livechat/server/lib/QueueManager';
import { RoutingManager } from '../../../../../app/livechat/server/lib/RoutingManager';
import { settings } from '../../../../../app/settings/server';
import { callbacks } from '../../../../../lib/callbacks';
import { settings } from '../../../settings/server';

export class OmnichannelEE extends ServiceClassInternal implements IOmnichannelEEService {
protected name = 'omnichannel-ee';
Expand All @@ -33,11 +33,8 @@ export class OmnichannelEE extends ServiceClassInternal implements IOmnichannelE
const { _id: roomId } = room;
const restrictedOnHold = settings.get('Livechat_allow_manual_on_hold_upon_agent_engagement_only');
const canRoomBePlacedOnHold = !room.onHold;
const canAgentPlaceOnHold = !room.lastMessage.token;
const canAgentPlaceOnHold = !room.lastMessage?.token;
const canPlaceChatOnHold = canRoomBePlacedOnHold && (!restrictedOnHold || canAgentPlaceOnHold);
if (!canPlaceChatOnHold) {
throw new Error('error-contact-sent-last-message-so-cannot-place-on-hold');
}

if (!room || !isOmnichannelRoom(room)) {
throw new Error('error-invalid-room');
Expand All @@ -48,7 +45,9 @@ export class OmnichannelEE extends ServiceClassInternal implements IOmnichannelE
if (room.onHold) {
throw new Error('error-room-is-already-on-hold');
}

if (!canPlaceChatOnHold) {
throw new Error('error-contact-sent-last-message-so-cannot-place-on-hold');
}
if (!room.servedBy) {
throw new Error('error-unserved-rooms-cannot-be-placed-onhold');
}
Expand All @@ -60,8 +59,6 @@ export class OmnichannelEE extends ServiceClassInternal implements IOmnichannelE
]);

await callbacks.run('livechat:afterOnHold', room);

this.logger.debug(`Room ${room._id} set on hold successfully`);
}

async resumeRoomOnHold(
Expand Down Expand Up @@ -111,8 +108,6 @@ export class OmnichannelEE extends ServiceClassInternal implements IOmnichannelE
]);

await callbacks.run('livechat:afterOnHoldChatResumed', room);

this.logger.debug(`Room ${room._id} resumed successfully`);
}

private async attemptToAssignRoomToServingAgentElseQueueIt({
Expand Down Expand Up @@ -140,28 +135,23 @@ export class OmnichannelEE extends ServiceClassInternal implements IOmnichannelE

return;
} catch (e) {
this.logger.debug(`Agent ${servingAgent._id} is not available to take the inquiry ${inquiry._id}`, e);
this.logger.error(`Agent ${servingAgent._id} is not available to take the inquiry ${inquiry._id}`, e);
if (clientAction) {
// if the action was triggered by the client, we should throw the error
// so the client can handle it and show the error message to the user
throw e;
}
}

this.logger.debug(`Attempting to queue inquiry ${inquiry._id}`);

await this.removeCurrentAgentFromRoom({ room, inquiry });

const { _id: inquiryId } = inquiry;
const newInquiry = await LivechatInquiry.findOneById(inquiryId);

if (!newInquiry) {
this.logger.error(`No inquiry found for id ${inquiryId}`);
throw new Error('error-invalid-inquiry');
}
await queueInquiry(newInquiry);

this.logger.debug('Room queued successfully');
}

private async removeCurrentAgentFromRoom({
Expand All @@ -184,7 +174,5 @@ export class OmnichannelEE extends ServiceClassInternal implements IOmnichannelE
]);

await dispatchAgentDelegated(roomId);

this.logger.debug(`Current agent removed from room ${room._id} successfully`);
}
}

0 comments on commit 284740d

Please sign in to comment.