From d62fd4a0af2bf6d8fb7a31aea1d6907bdfdd3ccf Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Thu, 21 Sep 2023 14:10:14 -0600 Subject: [PATCH] block meteor methods --- apps/meteor/app/lib/server/methods/sendMessage.ts | 6 +++--- apps/meteor/app/livechat/server/hooks/checkMAC.ts | 3 ++- apps/meteor/app/livechat/server/lib/LivechatTyped.ts | 6 +++++- .../meteor/app/livechat/server/methods/returnAsInquiry.ts | 5 +++++ apps/meteor/app/livechat/server/methods/transfer.ts | 5 +++++ apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx | 2 +- apps/meteor/server/services/omnichannel/service.ts | 8 +++++--- 7 files changed, 26 insertions(+), 9 deletions(-) diff --git a/apps/meteor/app/lib/server/methods/sendMessage.ts b/apps/meteor/app/lib/server/methods/sendMessage.ts index ebdcdfd43d9bd..e12ebc2d47e92 100644 --- a/apps/meteor/app/lib/server/methods/sendMessage.ts +++ b/apps/meteor/app/lib/server/methods/sendMessage.ts @@ -82,7 +82,7 @@ export async function executeSendMessage(uid: IUser['_id'], message: AtLeast({ - sendMessage(message, previewUrls) { + async sendMessage(message, previewUrls) { check(message, Object); const uid = Meteor.userId(); @@ -118,7 +118,7 @@ Meteor.methods({ } try { - return executeSendMessage(uid, message, previewUrls); + return await executeSendMessage(uid, message, previewUrls); } catch (error: any) { if ((error.error || error.message) === 'error-not-allowed') { throw new Meteor.Error(error.error || error.message, error.reason, { diff --git a/apps/meteor/app/livechat/server/hooks/checkMAC.ts b/apps/meteor/app/livechat/server/hooks/checkMAC.ts index 56d5b26a15980..82878861d6d4c 100644 --- a/apps/meteor/app/livechat/server/hooks/checkMAC.ts +++ b/apps/meteor/app/livechat/server/hooks/checkMAC.ts @@ -1,4 +1,5 @@ import { Omnichannel } from '@rocket.chat/core-services'; +import type { IOmnichannelRoom } from '@rocket.chat/core-typings'; import { isEditedMessage } from '@rocket.chat/core-typings'; import { callbacks } from '../../../../lib/callbacks'; @@ -16,7 +17,7 @@ callbacks.add('beforeSaveMessage', async (message, room) => { return message; } - const canSendMessage = await Omnichannel.isRoomEnabled(room); + const canSendMessage = await Omnichannel.isRoomEnabled(room as IOmnichannelRoom); if (!canSendMessage) { throw new Error('error-mac-limit-reached'); } diff --git a/apps/meteor/app/livechat/server/lib/LivechatTyped.ts b/apps/meteor/app/livechat/server/lib/LivechatTyped.ts index 1c60a257d3192..86bffed76884e 100644 --- a/apps/meteor/app/livechat/server/lib/LivechatTyped.ts +++ b/apps/meteor/app/livechat/server/lib/LivechatTyped.ts @@ -1,4 +1,4 @@ -import { Message } from '@rocket.chat/core-services'; +import { Message, Omnichannel } from '@rocket.chat/core-services'; import type { IOmnichannelRoom, IOmnichannelRoomClosingInfo, @@ -463,6 +463,10 @@ class LivechatClass { throw new Error('error-invalid-room'); } + if (!(await Omnichannel.isRoomEnabled(room))) { + throw new Error('error-mac-limit-reached'); + } + const showAgentInfo = settings.get('Livechat_show_agent_info'); const closingMessage = await Messages.findLivechatClosingMessage(rid, { projection: { ts: 1 } }); const ignoredMessageTypes: MessageTypesValues[] = [ diff --git a/apps/meteor/app/livechat/server/methods/returnAsInquiry.ts b/apps/meteor/app/livechat/server/methods/returnAsInquiry.ts index 57a2b0afa3d5d..4c942def6f993 100644 --- a/apps/meteor/app/livechat/server/methods/returnAsInquiry.ts +++ b/apps/meteor/app/livechat/server/methods/returnAsInquiry.ts @@ -1,3 +1,4 @@ +import { Omnichannel } from '@rocket.chat/core-services'; import type { ILivechatDepartment, IRoom } from '@rocket.chat/core-typings'; import { LivechatRooms } from '@rocket.chat/models'; import type { ServerMethods } from '@rocket.chat/ui-contexts'; @@ -29,6 +30,10 @@ Meteor.methods({ }); } + if (!(await Omnichannel.isRoomEnabled(room))) { + throw new Meteor.Error('error-mac-limit-reached', 'MAC limit reached', { method: 'livechat:returnAsInquiry' }); + } + if (!room.open) { throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:returnAsInquiry' }); } diff --git a/apps/meteor/app/livechat/server/methods/transfer.ts b/apps/meteor/app/livechat/server/methods/transfer.ts index 2dc796fc6c94a..bfeaa7f95709f 100644 --- a/apps/meteor/app/livechat/server/methods/transfer.ts +++ b/apps/meteor/app/livechat/server/methods/transfer.ts @@ -1,3 +1,4 @@ +import { Omnichannel } from '@rocket.chat/core-services'; import type { IUser } from '@rocket.chat/core-typings'; import { LivechatVisitors, LivechatRooms, Subscriptions, Users } from '@rocket.chat/models'; import type { ServerMethods } from '@rocket.chat/ui-contexts'; @@ -49,6 +50,10 @@ Meteor.methods({ throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:transfer' }); } + if (!(await Omnichannel.isRoomEnabled(room))) { + throw new Meteor.Error('error-mac-limit-reached', 'MAC limit reached', { method: 'livechat:transfer' }); + } + const subscription = await Subscriptions.findOneByRoomIdAndUserId(room._id, uid, { projection: { _id: 1 }, }); diff --git a/apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx b/apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx index b57a35b0ce282..a7f00a7e883d5 100644 --- a/apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx +++ b/apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx @@ -6,7 +6,7 @@ export const useIsRoomActive = (room: IOmnichannelRoom) => { const { activity = [] } = room.v; // TODO: add activity to IOmnichannelRoom['v'] const isContactActive = useMemo(() => { const date = new Date(); - const currentPeriod = `${date.getFullYear()}-${date.getMonth() + 1}`; + const currentPeriod = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`; return activity.includes(currentPeriod); }, [activity]); diff --git a/apps/meteor/server/services/omnichannel/service.ts b/apps/meteor/server/services/omnichannel/service.ts index 123a8d6fea1c6..a01fcf73dd378 100644 --- a/apps/meteor/server/services/omnichannel/service.ts +++ b/apps/meteor/server/services/omnichannel/service.ts @@ -1,6 +1,7 @@ import { ServiceClassInternal } from '@rocket.chat/core-services'; import type { IOmnichannelService } from '@rocket.chat/core-services'; import type { AtLeast, IOmnichannelQueue, IOmnichannelRoom } from '@rocket.chat/core-typings'; +import moment from 'moment'; import { Livechat } from '../../../app/livechat/server'; import { RoutingManager } from '../../../app/livechat/server/lib/RoutingManager'; @@ -51,10 +52,11 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha return this.queueWorker; } - async isRoomEnabled(_room: AtLeast): Promise { - // const currentMonth = moment.utc().format('YYYY-MM'); + async isRoomEnabled(room: AtLeast): Promise { + const currentMonth = moment.utc().format('YYYY-MM'); // return license.isMacOnLimit() || room.v.activity.includes(currentMonth) - return true; + // @ts-expect-error - v.activity + return false || room.v?.activity?.includes(currentMonth); } async checkMACLimit(): Promise {