From ef7cc1f2bd694028b8c094354856acf22229b0b0 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 14 Jun 2022 16:21:05 -0300 Subject: [PATCH] [FIX] use urlParams on omnichannel/agent/extension/ --- .../app/api/server/v1/voip/omnichannel.ts | 73 ++++++++++--------- .../groups/voip/RemoveAgentButton.tsx | 4 +- packages/rest-typings/src/v1/voip.ts | 6 +- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/apps/meteor/app/api/server/v1/voip/omnichannel.ts b/apps/meteor/app/api/server/v1/voip/omnichannel.ts index 521da77aa256..12918c884367 100644 --- a/apps/meteor/app/api/server/v1/voip/omnichannel.ts +++ b/apps/meteor/app/api/server/v1/voip/omnichannel.ts @@ -33,38 +33,6 @@ API.v1.addRoute( 'omnichannel/agent/extension', { authRequired: true }, { - // Get the extensions associated with the agent passed as request params. - async get() { - if (!hasPermission(this.userId, 'view-agent-extension-association')) { - return API.v1.unauthorized(); - } - check( - this.requestParams(), - Match.ObjectIncluding({ - username: String, - }), - ); - const { username } = this.requestParams(); - const user = await Users.findOneByAgentUsername(username, { - projection: { _id: 1 }, - }); - if (!user) { - return API.v1.notFound('User not found'); - } - const extension = await Users.getVoipExtensionByUserId(user._id, { - projection: { - _id: 1, - username: 1, - extension: 1, - }, - }); - if (!extension) { - return API.v1.notFound('Extension not found'); - } - return API.v1.success({ extension }); - }, - - // Create agent-extension association. async post() { if (!hasPermission(this.userId, 'manage-agent-extension-association')) { return API.v1.unauthorized(); @@ -121,18 +89,55 @@ API.v1.addRoute( return API.v1.failure(`extension already in use ${extension}`); } }, + }, +); + +API.v1.addRoute( + 'omnichannel/agent/extension/:username', + { authRequired: true }, + { + // Get the extensions associated with the agent passed as request params. + async get() { + if (!hasPermission(this.userId, 'view-agent-extension-association')) { + return API.v1.unauthorized(); + } + check( + this.urlParams, + Match.ObjectIncluding({ + username: String, + }), + ); + const { username } = this.urlParams; + const user = await Users.findOneByAgentUsername(username, { + projection: { _id: 1 }, + }); + if (!user) { + return API.v1.notFound('User not found'); + } + const extension = await Users.getVoipExtensionByUserId(user._id, { + projection: { + _id: 1, + username: 1, + extension: 1, + }, + }); + if (!extension) { + return API.v1.notFound('Extension not found'); + } + return API.v1.success({ extension }); + }, async delete() { if (!hasPermission(this.userId, 'manage-agent-extension-association')) { return API.v1.unauthorized(); } check( - this.requestParams(), + this.urlParams, Match.ObjectIncluding({ username: String, }), ); - const { username } = this.requestParams(); + const { username } = this.urlParams; const user = await Users.findOneByAgentUsername(username, { projection: { _id: 1, diff --git a/apps/meteor/client/views/admin/settings/groups/voip/RemoveAgentButton.tsx b/apps/meteor/client/views/admin/settings/groups/voip/RemoveAgentButton.tsx index a08c6da2f02d..526f46098da2 100644 --- a/apps/meteor/client/views/admin/settings/groups/voip/RemoveAgentButton.tsx +++ b/apps/meteor/client/views/admin/settings/groups/voip/RemoveAgentButton.tsx @@ -6,14 +6,14 @@ import React, { FC } from 'react'; import GenericModal from '../../../../../components/GenericModal'; const RemoveAgentButton: FC<{ username: string; reload: () => void }> = ({ username, reload }) => { - const removeAgent = useEndpoint('DELETE', '/v1/omnichannel/agent/extension'); + const removeAgent = useEndpoint('DELETE', `/v1/omnichannel/agent/extension/${username}`); const setModal = useSetModal(); const dispatchToastMessage = useToastMessageDispatch(); const t = useTranslation(); const handleRemoveClick = useMutableCallback(async () => { try { - await removeAgent({ username }); + await removeAgent(); } catch (error: any) { dispatchToastMessage({ type: 'error', message: error }); } diff --git a/packages/rest-typings/src/v1/voip.ts b/packages/rest-typings/src/v1/voip.ts index 9ec925154779..b01f9dafbfd1 100644 --- a/packages/rest-typings/src/v1/voip.ts +++ b/packages/rest-typings/src/v1/voip.ts @@ -516,9 +516,11 @@ export type VoipEndpoints = { }; }; '/v1/omnichannel/agent/extension': { - GET: (params: OmnichannelAgentExtensionGET) => { extension: Pick }; POST: (params: OmnichannelAgentExtensionPOST) => void; - DELETE: (params: OmnichannelAgentExtensionDELETE) => void; + }; + '/v1/omnichannel/agent/extension/:username': { + GET: () => { extension: Pick }; + DELETE: () => void; }; '/v1/omnichannel/agents/available': { GET: (params: OmnichannelAgentsAvailable) => PaginatedResult<{ agents: ILivechatAgent[] }>;