From a4c200f4471dee7aadeb8ffeba0a6c06806e0ce9 Mon Sep 17 00:00:00 2001 From: Lucas Pelegrino Date: Thu, 18 Sep 2025 18:27:43 -0300 Subject: [PATCH 1/9] chore: adds deprecation warning to livechat:takeInquiry --- .../livechat/server/methods/takeInquiry.ts | 67 +------------------ 1 file changed, 3 insertions(+), 64 deletions(-) diff --git a/apps/meteor/app/livechat/server/methods/takeInquiry.ts b/apps/meteor/app/livechat/server/methods/takeInquiry.ts index 19a08761c6145..439fc03de5950 100644 --- a/apps/meteor/app/livechat/server/methods/takeInquiry.ts +++ b/apps/meteor/app/livechat/server/methods/takeInquiry.ts @@ -1,13 +1,8 @@ -import { Omnichannel } from '@rocket.chat/core-services'; import type { ServerMethods } from '@rocket.chat/ddp-client'; -import { LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; -import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission'; -import { settings } from '../../../settings/server'; -import { RoutingManager } from '../lib/RoutingManager'; -import { isAgentAvailableToTakeContactInquiry } from '../lib/contacts/isAgentAvailableToTakeContactInquiry'; -import { migrateVisitorIfMissingContact } from '../lib/contacts/migrateVisitorIfMissingContact'; +import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger'; +import { takeInquiry } from '../lib/takeInquiry'; declare module '@rocket.chat/ddp-client' { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -19,65 +14,9 @@ declare module '@rocket.chat/ddp-client' { } } -export const takeInquiry = async ( - userId: string, - inquiryId: string, - options?: { clientAction: boolean; forwardingToDepartment?: { oldDepartmentId: string; transferData: any } }, -): Promise => { - if (!userId || !(await hasPermissionAsync(userId, 'view-l-room'))) { - throw new Meteor.Error('error-not-allowed', 'Not allowed', { - method: 'livechat:takeInquiry', - }); - } - - const inquiry = await LivechatInquiry.findOneById(inquiryId); - - if (!inquiry) { - throw new Meteor.Error('error-not-found', 'Inquiry not found', { - method: 'livechat:takeInquiry', - }); - } - - if (inquiry.status === 'taken') { - throw new Meteor.Error('error-inquiry-taken', 'Inquiry already taken', { - method: 'livechat:takeInquiry', - }); - } - - const user = await Users.findOneOnlineAgentById(userId, settings.get('Livechat_enabled_when_agent_idle')); - if (!user) { - throw new Meteor.Error('error-agent-status-service-offline', 'Agent status is offline or Omnichannel service is not active', { - method: 'livechat:takeInquiry', - }); - } - - const room = await LivechatRooms.findOneById(inquiry.rid); - if (!room || !(await Omnichannel.isWithinMACLimit(room))) { - throw new Meteor.Error('error-mac-limit-reached'); - } - - const contactId = room.contactId ?? (await migrateVisitorIfMissingContact(room.v._id, room.source)); - if (contactId) { - const isAgentAvailableToTakeContactInquiryResult = await isAgentAvailableToTakeContactInquiry(inquiry.v._id, room.source, contactId); - if (!isAgentAvailableToTakeContactInquiryResult.value) { - throw new Meteor.Error(isAgentAvailableToTakeContactInquiryResult.error); - } - } - - const agent = { - agentId: user._id, - username: user.username, - }; - - try { - await RoutingManager.takeInquiry(inquiry, agent, options ?? {}, room); - } catch (e: any) { - throw new Meteor.Error(e.message); - } -}; - Meteor.methods({ async 'livechat:takeInquiry'(inquiryId, options) { + methodDeprecationLogger.method('livechat:takeInquiry', '8.0.0', '/v1/livechat/inquiries.take'); const uid = Meteor.userId(); if (!uid) { throw new Meteor.Error('error-not-allowed', 'Invalid User', { From a00d88f7e56a8c0b0dfa1e1ec003445138a4efbe Mon Sep 17 00:00:00 2001 From: Lucas Pelegrino Date: Thu, 18 Sep 2025 18:28:42 -0300 Subject: [PATCH 2/9] chore: move takeInquiry func to a single file under lib/ --- .../app/livechat/server/lib/takeInquiry.ts | 67 +++++++++++++++++++ packages/rest-typings/src/v1/omnichannel.ts | 32 +++++++++ 2 files changed, 99 insertions(+) create mode 100644 apps/meteor/app/livechat/server/lib/takeInquiry.ts diff --git a/apps/meteor/app/livechat/server/lib/takeInquiry.ts b/apps/meteor/app/livechat/server/lib/takeInquiry.ts new file mode 100644 index 0000000000000..612ac1e044a63 --- /dev/null +++ b/apps/meteor/app/livechat/server/lib/takeInquiry.ts @@ -0,0 +1,67 @@ +import { Omnichannel } from '@rocket.chat/core-services'; +import { LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models'; + +import { Meteor } from 'meteor/meteor'; + +import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission'; +import { settings } from '../../../settings/server'; +import { RoutingManager } from '../lib/RoutingManager'; +import { isAgentAvailableToTakeContactInquiry } from '../lib/contacts/isAgentAvailableToTakeContactInquiry'; +import { migrateVisitorIfMissingContact } from '../lib/contacts/migrateVisitorIfMissingContact'; + +export const takeInquiry = async ( + userId: string, + inquiryId: string, + options?: { clientAction: boolean; forwardingToDepartment?: { oldDepartmentId: string; transferData: any } }, +): Promise => { + if (!userId || !(await hasPermissionAsync(userId, 'view-l-room'))) { + throw new Meteor.Error('error-not-allowed', 'Not allowed', { + method: 'livechat:takeInquiry', + }); + } + + const inquiry = await LivechatInquiry.findOneById(inquiryId); + + if (!inquiry) { + throw new Meteor.Error('error-not-found', 'Inquiry not found', { + method: 'livechat:takeInquiry', + }); + } + + if (inquiry.status === 'taken') { + throw new Meteor.Error('error-inquiry-taken', 'Inquiry already taken', { + method: 'livechat:takeInquiry', + }); + } + + const user = await Users.findOneOnlineAgentById(userId, settings.get('Livechat_enabled_when_agent_idle')); + if (!user) { + throw new Meteor.Error('error-agent-status-service-offline', 'Agent status is offline or Omnichannel service is not active', { + method: 'livechat:takeInquiry', + }); + } + + const room = await LivechatRooms.findOneById(inquiry.rid); + if (!room || !(await Omnichannel.isWithinMACLimit(room))) { + throw new Meteor.Error('error-mac-limit-reached'); + } + + const contactId = room.contactId ?? (await migrateVisitorIfMissingContact(room.v._id, room.source)); + if (contactId) { + const isAgentAvailableToTakeContactInquiryResult = await isAgentAvailableToTakeContactInquiry(inquiry.v._id, room.source, contactId); + if (!isAgentAvailableToTakeContactInquiryResult.value) { + throw new Meteor.Error(isAgentAvailableToTakeContactInquiryResult.error); + } + } + + const agent = { + agentId: user._id, + username: user.username, + }; + + try { + await RoutingManager.takeInquiry(inquiry, agent, options ?? {}, room); + } catch (e: any) { + throw new Meteor.Error(e.message); + } +}; diff --git a/packages/rest-typings/src/v1/omnichannel.ts b/packages/rest-typings/src/v1/omnichannel.ts index e5db53a53a785..bf3c01a8a58f3 100644 --- a/packages/rest-typings/src/v1/omnichannel.ts +++ b/packages/rest-typings/src/v1/omnichannel.ts @@ -3324,6 +3324,13 @@ export const isGETLivechatInquiriesListParams = ajv.compile Date: Thu, 18 Sep 2025 18:29:14 -0300 Subject: [PATCH 3/9] chore: updates endpoint params to expect options --- apps/meteor/app/livechat/imports/server/rest/inquiries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/livechat/imports/server/rest/inquiries.ts b/apps/meteor/app/livechat/imports/server/rest/inquiries.ts index 2432e55054f52..13ecab3df9037 100644 --- a/apps/meteor/app/livechat/imports/server/rest/inquiries.ts +++ b/apps/meteor/app/livechat/imports/server/rest/inquiries.ts @@ -10,7 +10,7 @@ import { import { API } from '../../../../api/server'; import { getPaginationItems } from '../../../../api/server/helpers/getPaginationItems'; import { findInquiries, findOneInquiryByRoomId } from '../../../server/api/lib/inquiries'; -import { takeInquiry } from '../../../server/methods/takeInquiry'; +import { takeInquiry } from '../../../server/lib/takeInquiry'; API.v1.addRoute( 'livechat/inquiries.list', From daa8e61dfcf61b239fa83fe135745268cca010fd Mon Sep 17 00:00:00 2001 From: Lucas Pelegrino Date: Thu, 18 Sep 2025 18:29:45 -0300 Subject: [PATCH 4/9] chore: replaces use of livechat:takeInquiry for livechat/inquiries.take endpoint --- .../ComposerOmnichannel/ComposerOmnichannelInquiry.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/meteor/client/views/room/composer/ComposerOmnichannel/ComposerOmnichannelInquiry.tsx b/apps/meteor/client/views/room/composer/ComposerOmnichannel/ComposerOmnichannelInquiry.tsx index 785f274183abb..2d79085971ec3 100644 --- a/apps/meteor/client/views/room/composer/ComposerOmnichannel/ComposerOmnichannelInquiry.tsx +++ b/apps/meteor/client/views/room/composer/ComposerOmnichannel/ComposerOmnichannelInquiry.tsx @@ -1,5 +1,5 @@ import { MessageFooterCallout, MessageFooterCalloutAction, MessageFooterCalloutContent } from '@rocket.chat/ui-composer'; -import { useEndpoint, useMethod, useToastMessageDispatch, useTranslation, useUser } from '@rocket.chat/ui-contexts'; +import { useEndpoint, useToastMessageDispatch, useTranslation, useUser } from '@rocket.chat/ui-contexts'; import { useQuery } from '@tanstack/react-query'; import type { ReactElement } from 'react'; import { useMemo } from 'react'; @@ -23,7 +23,7 @@ export const ComposerOmnichannelInquiry = (): ReactElement => { }), }); - const takeInquiry = useMethod('livechat:takeInquiry'); + const takeInquiry = useEndpoint('POST', '/v1/livechat/inquiries.take'); const handleTakeInquiry = async (): Promise => { if (!result.isSuccess) { @@ -33,7 +33,7 @@ export const ComposerOmnichannelInquiry = (): ReactElement => { return; } try { - await takeInquiry(result.data.inquiry._id, { clientAction: true }); + await takeInquiry({ inquiryId: result.data.inquiry._id, options: { clientAction: true } }); } catch (error) { dispatchToastMessage({ type: 'error', message: error }); } From 3108bea73442b6737dad4a20168105af1965b0a4 Mon Sep 17 00:00:00 2001 From: Lucas Pelegrino Date: Thu, 18 Sep 2025 18:34:05 -0300 Subject: [PATCH 5/9] docs: adds changeset --- .changeset/nice-balloons-relax.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/nice-balloons-relax.md diff --git a/.changeset/nice-balloons-relax.md b/.changeset/nice-balloons-relax.md new file mode 100644 index 0000000000000..951859657182c --- /dev/null +++ b/.changeset/nice-balloons-relax.md @@ -0,0 +1,6 @@ +--- +"@rocket.chat/meteor": patch +"@rocket.chat/rest-typings": patch +--- + +Adds deprecation warning on `livechat:removeRoom`, use `livechat/inquiries.take` instead From 7b03fe5124039a9b743192d505f2f4f2af9a74e1 Mon Sep 17 00:00:00 2001 From: Lucas Pelegrino Date: Fri, 19 Sep 2025 10:31:10 -0300 Subject: [PATCH 6/9] fix: lint import paths --- apps/meteor/app/livechat/server/lib/takeInquiry.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/meteor/app/livechat/server/lib/takeInquiry.ts b/apps/meteor/app/livechat/server/lib/takeInquiry.ts index 612ac1e044a63..256ce3aa71afe 100644 --- a/apps/meteor/app/livechat/server/lib/takeInquiry.ts +++ b/apps/meteor/app/livechat/server/lib/takeInquiry.ts @@ -1,13 +1,12 @@ import { Omnichannel } from '@rocket.chat/core-services'; import { LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models'; - import { Meteor } from 'meteor/meteor'; import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission'; import { settings } from '../../../settings/server'; -import { RoutingManager } from '../lib/RoutingManager'; -import { isAgentAvailableToTakeContactInquiry } from '../lib/contacts/isAgentAvailableToTakeContactInquiry'; -import { migrateVisitorIfMissingContact } from '../lib/contacts/migrateVisitorIfMissingContact'; +import { RoutingManager } from './RoutingManager'; +import { isAgentAvailableToTakeContactInquiry } from './contacts/isAgentAvailableToTakeContactInquiry'; +import { migrateVisitorIfMissingContact } from './contacts/migrateVisitorIfMissingContact'; export const takeInquiry = async ( userId: string, From 4793ef864bea32321f2b465df2debd1f7eb91df1 Mon Sep 17 00:00:00 2001 From: Lucas Pelegrino Date: Fri, 19 Sep 2025 10:31:10 -0300 Subject: [PATCH 7/9] fix: lint import paths --- apps/meteor/app/livechat/server/lib/takeInquiry.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/meteor/app/livechat/server/lib/takeInquiry.ts b/apps/meteor/app/livechat/server/lib/takeInquiry.ts index 612ac1e044a63..ddde976f970d8 100644 --- a/apps/meteor/app/livechat/server/lib/takeInquiry.ts +++ b/apps/meteor/app/livechat/server/lib/takeInquiry.ts @@ -1,13 +1,12 @@ import { Omnichannel } from '@rocket.chat/core-services'; import { LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models'; - import { Meteor } from 'meteor/meteor'; +import { RoutingManager } from './RoutingManager'; +import { isAgentAvailableToTakeContactInquiry } from './contacts/isAgentAvailableToTakeContactInquiry'; +import { migrateVisitorIfMissingContact } from './contacts/migrateVisitorIfMissingContact'; import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission'; import { settings } from '../../../settings/server'; -import { RoutingManager } from '../lib/RoutingManager'; -import { isAgentAvailableToTakeContactInquiry } from '../lib/contacts/isAgentAvailableToTakeContactInquiry'; -import { migrateVisitorIfMissingContact } from '../lib/contacts/migrateVisitorIfMissingContact'; export const takeInquiry = async ( userId: string, From e852e695e66ab9bd9722bbc2f4b0d7e512a850d3 Mon Sep 17 00:00:00 2001 From: Lucas Pelegrino Date: Mon, 22 Sep 2025 10:40:49 -0300 Subject: [PATCH 8/9] chore: adds minor improvements to takeInquiry --- apps/meteor/app/livechat/server/lib/takeInquiry.ts | 7 ------- apps/meteor/app/livechat/server/methods/takeInquiry.ts | 8 ++++++++ packages/rest-typings/src/v1/omnichannel.ts | 6 +++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/apps/meteor/app/livechat/server/lib/takeInquiry.ts b/apps/meteor/app/livechat/server/lib/takeInquiry.ts index ddde976f970d8..c046768ea92c0 100644 --- a/apps/meteor/app/livechat/server/lib/takeInquiry.ts +++ b/apps/meteor/app/livechat/server/lib/takeInquiry.ts @@ -5,7 +5,6 @@ import { Meteor } from 'meteor/meteor'; import { RoutingManager } from './RoutingManager'; import { isAgentAvailableToTakeContactInquiry } from './contacts/isAgentAvailableToTakeContactInquiry'; import { migrateVisitorIfMissingContact } from './contacts/migrateVisitorIfMissingContact'; -import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission'; import { settings } from '../../../settings/server'; export const takeInquiry = async ( @@ -13,12 +12,6 @@ export const takeInquiry = async ( inquiryId: string, options?: { clientAction: boolean; forwardingToDepartment?: { oldDepartmentId: string; transferData: any } }, ): Promise => { - if (!userId || !(await hasPermissionAsync(userId, 'view-l-room'))) { - throw new Meteor.Error('error-not-allowed', 'Not allowed', { - method: 'livechat:takeInquiry', - }); - } - const inquiry = await LivechatInquiry.findOneById(inquiryId); if (!inquiry) { diff --git a/apps/meteor/app/livechat/server/methods/takeInquiry.ts b/apps/meteor/app/livechat/server/methods/takeInquiry.ts index 439fc03de5950..b3b34f40bbf94 100644 --- a/apps/meteor/app/livechat/server/methods/takeInquiry.ts +++ b/apps/meteor/app/livechat/server/methods/takeInquiry.ts @@ -1,6 +1,7 @@ import type { ServerMethods } from '@rocket.chat/ddp-client'; import { Meteor } from 'meteor/meteor'; +import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission'; import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger'; import { takeInquiry } from '../lib/takeInquiry'; @@ -24,6 +25,13 @@ Meteor.methods({ }); } + if (!(await hasPermissionAsync(uid, 'view-l-room'))) { + throw new Meteor.Error('error-not-allowed', 'Not allowed', { + method: 'livechat:takeInquiry', + }); + } + + return takeInquiry(uid, inquiryId, options); }, }); diff --git a/packages/rest-typings/src/v1/omnichannel.ts b/packages/rest-typings/src/v1/omnichannel.ts index bf3c01a8a58f3..7652406ed77fd 100644 --- a/packages/rest-typings/src/v1/omnichannel.ts +++ b/packages/rest-typings/src/v1/omnichannel.ts @@ -3327,7 +3327,7 @@ type POSTLivechatInquiriesTakeParams = { options?: { clientAction: boolean; forwardingToDepartment?: { - departmentId: string; + oldDepartmentId: string; transferData: unknown; }; }; @@ -3354,14 +3354,14 @@ const POSTLivechatInquiriesTakeParamsSchema = { type: 'object', nullable: true, properties: { - departmentId: { + oldDepartmentId: { type: 'string', }, transferData: { type: 'object', }, }, - required: ['departmentId', 'transferData'], + required: ['oldDepartmentId', 'transferData'], additionalProperties: false, }, }, From b9db48f9ca093c98d578b78be851a8bfe3abd639 Mon Sep 17 00:00:00 2001 From: Lucas Pelegrino Date: Mon, 22 Sep 2025 15:19:03 -0300 Subject: [PATCH 9/9] chore: removed double space --- apps/meteor/app/livechat/server/methods/takeInquiry.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/meteor/app/livechat/server/methods/takeInquiry.ts b/apps/meteor/app/livechat/server/methods/takeInquiry.ts index b3b34f40bbf94..52336e91d08e3 100644 --- a/apps/meteor/app/livechat/server/methods/takeInquiry.ts +++ b/apps/meteor/app/livechat/server/methods/takeInquiry.ts @@ -31,7 +31,6 @@ Meteor.methods({ }); } - return takeInquiry(uid, inquiryId, options); }, });