Skip to content

Commit

Permalink
[BREAK] use urlParams on omnichannel/agent/extension/ (#25874)
Browse files Browse the repository at this point in the history
<!-- This is a pull request template, you do not need to uncomment or remove the comments, they won't show up in the PR text. -->

<!-- Your Pull Request name should start with one of the following tags
  [NEW] For new features
  [IMPROVE] For an improvement (performance or little improvements) in existing features
  [FIX] For bug fixes that affect the end-user
  [BREAK] For pull requests including breaking changes
  Chore: For small tasks
  Doc: For documentation
-->

<!-- Checklist!!! If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code. 
  - I have read the Contributing Guide - https://github.com/RocketChat/Rocket.Chat/blob/develop/.github/CONTRIBUTING.md#contributing-to-rocketchat doc
  - I have signed the CLA - https://cla-assistant.io/RocketChat/Rocket.Chat
  - Lint and unit tests pass locally with my changes
  - I have added tests that prove my fix is effective or that my feature works (if applicable)
  - I have added necessary documentation (if applicable)
  - Any dependent changes have been merged and published in downstream modules
-->

## Proposed changes (including videos or screenshots)
I've noted that our extensions api was not using the same pattern that we use for a lot of `GET`s and `DELETE`s.

This will fix an issue when trying to remove an agent from a voip extension.
  • Loading branch information
MartinSchoeler committed Jun 23, 2022
1 parent b0b1a0c commit 90f65b3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
73 changes: 39 additions & 34 deletions apps/meteor/app/api/server/v1/voip/omnichannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
6 changes: 4 additions & 2 deletions packages/rest-typings/src/v1/voip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,11 @@ export type VoipEndpoints = {
};
};
'/v1/omnichannel/agent/extension': {
GET: (params: OmnichannelAgentExtensionGET) => { extension: Pick<IUser, '_id' | 'username' | 'extension'> };
POST: (params: OmnichannelAgentExtensionPOST) => void;
DELETE: (params: OmnichannelAgentExtensionDELETE) => void;
};
'/v1/omnichannel/agent/extension/:username': {
GET: () => { extension: Pick<IUser, '_id' | 'username' | 'extension'> };
DELETE: () => void;
};
'/v1/omnichannel/agents/available': {
GET: (params: OmnichannelAgentsAvailable) => PaginatedResult<{ agents: ILivechatAgent[] }>;
Expand Down

0 comments on commit 90f65b3

Please sign in to comment.