Skip to content

Commit 48d1442

Browse files
chore: Adds deprecation warning on livechat:returnAsInquiry with new endpoint replacing it (RocketChat#36973)
1 parent dca0b3a commit 48d1442

8 files changed

Lines changed: 145 additions & 98 deletions

File tree

.changeset/calm-hounds-look.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rocket.chat/meteor": patch
3+
"@rocket.chat/rest-typings": patch
4+
---
5+
6+
Adds deprecation warning on `livechat:returnAsInquiry` with new endpoint replacing it; `livechat/inquiries.returnAsInquiry`

apps/meteor/app/livechat/imports/server/rest/inquiries.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { LivechatInquiryStatus } from '@rocket.chat/core-typings';
2-
import { LivechatInquiry, LivechatDepartment, Users } from '@rocket.chat/models';
2+
import { LivechatInquiry, LivechatDepartment, Users, LivechatRooms } from '@rocket.chat/models';
33
import {
44
isGETLivechatInquiriesListParams,
55
isPOSTLivechatInquiriesTakeParams,
66
isGETLivechatInquiriesQueuedForUserParams,
77
isGETLivechatInquiriesGetOneParams,
8+
validateBadRequestErrorResponse,
9+
validateUnauthorizedErrorResponse,
10+
validateForbiddenErrorResponse,
11+
isPOSTLivechatInquiriesReturnAsInquiry,
12+
POSTLivechatInquiriesReturnAsInquirySuccessResponse,
813
} from '@rocket.chat/rest-typings';
914

1015
import { API } from '../../../../api/server';
16+
import type { ExtractRoutesFromAPI } from '../../../../api/server/ApiClass';
1117
import { getPaginationItems } from '../../../../api/server/helpers/getPaginationItems';
1218
import { findInquiries, findOneInquiryByRoomId } from '../../../server/api/lib/inquiries';
19+
import { returnRoomAsInquiry } from '../../../server/lib/rooms';
1320
import { takeInquiry } from '../../../server/lib/takeInquiry';
1421

1522
API.v1.addRoute(
@@ -108,3 +115,45 @@ API.v1.addRoute(
108115
},
109116
},
110117
);
118+
119+
const livechatInquiriesEndpoints = API.v1.post(
120+
'livechat/inquiries.returnAsInquiry',
121+
{
122+
response: {
123+
200: POSTLivechatInquiriesReturnAsInquirySuccessResponse,
124+
400: validateBadRequestErrorResponse,
125+
401: validateUnauthorizedErrorResponse,
126+
403: validateForbiddenErrorResponse,
127+
},
128+
authRequired: true,
129+
permissionsRequired: ['view-l-room'],
130+
body: isPOSTLivechatInquiriesReturnAsInquiry,
131+
},
132+
async function action() {
133+
const { roomId, departmentId } = this.bodyParams;
134+
135+
try {
136+
const room = await LivechatRooms.findOneById(roomId);
137+
if (!room) {
138+
return API.v1.failure('error-room-not-found');
139+
}
140+
141+
const result = await returnRoomAsInquiry(room, departmentId);
142+
143+
return API.v1.success({ result });
144+
} catch (error) {
145+
if (error instanceof Meteor.Error && typeof error.error === 'string') {
146+
return API.v1.failure(error.error as string);
147+
}
148+
149+
return API.v1.failure('error-returning-inquiry');
150+
}
151+
},
152+
);
153+
154+
type LivechatInquiriesEndpoints = ExtractRoutesFromAPI<typeof livechatInquiriesEndpoints>;
155+
156+
declare module '@rocket.chat/rest-typings' {
157+
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
158+
interface Endpoints extends LivechatInquiriesEndpoints {}
159+
}

apps/meteor/app/livechat/server/lib/rooms.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AppEvents, Apps } from '@rocket.chat/apps';
2+
import { Omnichannel } from '@rocket.chat/core-services';
23
import type {
34
ILivechatVisitor,
45
IMessage,
@@ -211,13 +212,17 @@ export async function saveRoomInfo(
211212
export async function returnRoomAsInquiry(room: IOmnichannelRoom, departmentId?: string, overrideTransferData: Partial<TransferData> = {}) {
212213
livechatLogger.debug({ msg: `Transfering room to ${departmentId ? 'department' : ''} queue`, room });
213214
if (!room.open) {
214-
throw new Meteor.Error('room-closed');
215+
throw new Meteor.Error('room-closed', 'Room closed');
215216
}
216217

217218
if (room.onHold) {
218219
throw new Meteor.Error('error-room-onHold');
219220
}
220221

222+
if (!(await Omnichannel.isWithinMACLimit(room))) {
223+
throw new Meteor.Error('error-mac-limit-reached');
224+
}
225+
221226
if (!room.servedBy) {
222227
return false;
223228
}

apps/meteor/app/livechat/server/methods/returnAsInquiry.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Omnichannel } from '@rocket.chat/core-services';
21
import type { ILivechatDepartment, IRoom } from '@rocket.chat/core-typings';
32
import type { ServerMethods } from '@rocket.chat/ddp-client';
43
import { LivechatRooms } from '@rocket.chat/models';
54
import { Meteor } from 'meteor/meteor';
65

76
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
7+
import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
88
import { returnRoomAsInquiry } from '../lib/rooms';
99

1010
declare module '@rocket.chat/ddp-client' {
@@ -16,6 +16,7 @@ declare module '@rocket.chat/ddp-client' {
1616

1717
Meteor.methods<ServerMethods>({
1818
async 'livechat:returnAsInquiry'(rid, departmentId) {
19+
methodDeprecationLogger.method('livechat:returnAsInquiry', '8.0.0', '/v1/livechat/inquiries.returnAsInquiry');
1920
const uid = Meteor.userId();
2021
if (!uid || !(await hasPermissionAsync(uid, 'view-l-room'))) {
2122
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
@@ -30,14 +31,6 @@ Meteor.methods<ServerMethods>({
3031
});
3132
}
3233

33-
if (!(await Omnichannel.isWithinMACLimit(room))) {
34-
throw new Meteor.Error('error-mac-limit-reached', 'MAC limit reached', { method: 'livechat:returnAsInquiry' });
35-
}
36-
37-
if (!room.open) {
38-
throw new Meteor.Error('room-closed', 'Room closed', { method: 'livechat:returnAsInquiry' });
39-
}
40-
4134
return returnRoomAsInquiry(room, departmentId);
4235
},
4336
});

apps/meteor/client/views/room/Header/Omnichannel/QuickActions/hooks/useReturnChatToQueueMutation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IRoom } from '@rocket.chat/core-typings';
2-
import { useMethod } from '@rocket.chat/ui-contexts';
2+
import { useEndpoint } from '@rocket.chat/ui-contexts';
33
import type { UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
44
import { useMutation, useQueryClient } from '@tanstack/react-query';
55

@@ -8,13 +8,13 @@ import { roomsQueryKeys, subscriptionsQueryKeys } from '../../../../../../lib/qu
88
export const useReturnChatToQueueMutation = (
99
options?: Omit<UseMutationOptions<void, Error, IRoom['_id']>, 'mutationFn'>,
1010
): UseMutationResult<void, Error, IRoom['_id']> => {
11-
const returnChatToQueue = useMethod('livechat:returnAsInquiry');
11+
const returnChatToQueue = useEndpoint('POST', '/v1/livechat/inquiries.returnAsInquiry');
1212

1313
const queryClient = useQueryClient();
1414

1515
return useMutation({
1616
mutationFn: async (rid) => {
17-
await returnChatToQueue(rid);
17+
await returnChatToQueue({ roomId: rid });
1818
},
1919
...options,
2020
onSuccess: async (data, rid, context) => {

apps/meteor/client/views/room/HeaderV2/Omnichannel/QuickActions/hooks/useReturnChatToQueueMutation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IRoom } from '@rocket.chat/core-typings';
2-
import { useMethod } from '@rocket.chat/ui-contexts';
2+
import { useEndpoint } from '@rocket.chat/ui-contexts';
33
import type { UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
44
import { useMutation, useQueryClient } from '@tanstack/react-query';
55

@@ -8,13 +8,13 @@ import { roomsQueryKeys, subscriptionsQueryKeys } from '../../../../../../lib/qu
88
export const useReturnChatToQueueMutation = (
99
options?: Omit<UseMutationOptions<void, Error, IRoom['_id']>, 'mutationFn'>,
1010
): UseMutationResult<void, Error, IRoom['_id']> => {
11-
const returnChatToQueue = useMethod('livechat:returnAsInquiry');
11+
const returnChatToQueue = useEndpoint('POST', '/v1/livechat/inquiries.returnAsInquiry');
1212

1313
const queryClient = useQueryClient();
1414

1515
return useMutation({
1616
mutationFn: async (rid) => {
17-
await returnChatToQueue(rid);
17+
await returnChatToQueue({ roomId: rid });
1818
},
1919
...options,
2020
onSuccess: async (data, rid, context) => {

apps/meteor/tests/end-to-end/api/livechat/05-inquiries.ts

Lines changed: 33 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai';
44
import { before, describe, it, after } from 'mocha';
55
import type { Response } from 'supertest';
66

7-
import { getCredentials, api, request, credentials, methodCall } from '../../../data/api-data';
7+
import { getCredentials, api, request, credentials } from '../../../data/api-data';
88
import { deleteDepartment } from '../../../data/livechat/department';
99
import {
1010
closeOmnichannelRoom,
@@ -19,7 +19,7 @@ import {
1919
startANewLivechatRoomAndTakeIt,
2020
takeInquiry,
2121
} from '../../../data/livechat/rooms';
22-
import { parseMethodResponse, sleep } from '../../../data/livechat/utils';
22+
import { sleep } from '../../../data/livechat/utils';
2323
import {
2424
removePermissionFromAllRoles,
2525
restorePermissionToRoles,
@@ -324,7 +324,7 @@ describe('LIVECHAT - inquiries', () => {
324324
});
325325
});
326326

327-
describe('livechat:returnAsInquiry', () => {
327+
describe('livechat/inquiries.returnAsInquiry', () => {
328328
let testUser: { user: IUser; credentials: Credentials };
329329
before(async () => {
330330
const user = await createUser();
@@ -344,81 +344,54 @@ describe('LIVECHAT - inquiries', () => {
344344
it('should throw an error if user doesnt have view-l-room permission', async () => {
345345
await removePermissionFromAllRoles('view-l-room');
346346
const { body } = await request
347-
.post(methodCall('livechat:returnAsInquiry'))
347+
.post(api('livechat/inquiries.returnAsInquiry'))
348348
.set(credentials)
349-
.send({
350-
message: JSON.stringify({
351-
method: 'livechat:returnAsInquiry',
352-
params: ['test'],
353-
id: 'id',
354-
msg: 'method',
355-
}),
356-
})
349+
.send({ roomId: 'test' })
357350
.expect('Content-Type', 'application/json')
358-
.expect(200);
359-
360-
const response = parseMethodResponse(body);
351+
.expect(403);
361352

362-
expect(response.error.error).to.be.equal('error-not-allowed');
353+
expect(body).to.have.property('success', false);
354+
expect(body.error).to.have.equal('User does not have the permissions required for this action [error-unauthorized]');
363355
});
364356
it('should fail if provided room doesnt exists', async () => {
365357
await restorePermissionToRoles('view-l-room');
366358
const { body } = await request
367-
.post(methodCall('livechat:returnAsInquiry'))
359+
.post(api('livechat/inquiries.returnAsInquiry'))
368360
.set(credentials)
369361
.send({
370-
message: JSON.stringify({
371-
method: 'livechat:returnAsInquiry',
372-
params: ['test'],
373-
id: 'id',
374-
msg: 'method',
375-
}),
362+
roomId: 'test',
376363
})
377364
.expect('Content-Type', 'application/json')
378-
.expect(200);
365+
.expect(400);
379366

380-
const response = parseMethodResponse(body);
381-
expect(response.error.error).to.be.equal('error-invalid-room');
367+
expect(body).to.have.property('success', false);
368+
expect(body).to.have.property('error', 'error-room-not-found');
382369
});
383370
it('should fail if room is not a livechat room', async () => {
384371
const { body } = await request
385-
.post(methodCall('livechat:returnAsInquiry'))
372+
.post(api('livechat/inquiries.returnAsInquiry'))
386373
.set(credentials)
387-
.send({
388-
message: JSON.stringify({
389-
method: 'livechat:returnAsInquiry',
390-
params: ['GENERAL'],
391-
id: 'id',
392-
msg: 'method',
393-
}),
394-
})
374+
.send({ roomId: 'GENERAL' })
395375
.expect('Content-Type', 'application/json')
396-
.expect(200);
376+
.expect(400);
397377

398-
const response = parseMethodResponse(body);
399-
expect(response.error.error).to.be.equal('error-invalid-room');
378+
expect(body).to.have.property('success', false);
379+
expect(body).to.have.property('error', 'error-room-not-found');
400380
});
401381
it('should fail if room is closed', async () => {
402382
const visitor = await createVisitor();
403383
const room = await createLivechatRoom(visitor.token);
404384
await closeOmnichannelRoom(room._id);
405385

406386
const { body } = await request
407-
.post(methodCall('livechat:returnAsInquiry'))
387+
.post(api('livechat/inquiries.returnAsInquiry'))
408388
.set(credentials)
409-
.send({
410-
message: JSON.stringify({
411-
method: 'livechat:returnAsInquiry',
412-
params: [room._id],
413-
id: 'id',
414-
msg: 'method',
415-
}),
416-
})
389+
.send({ roomId: room._id })
417390
.expect('Content-Type', 'application/json')
418-
.expect(200);
391+
.expect(400);
419392

420-
const response = parseMethodResponse(body);
421-
expect(response.error.error).to.be.equal('room-closed');
393+
expect(body).to.have.property('success', false);
394+
expect(body).to.have.property('error', 'room-closed');
422395
});
423396
describe('no serving', () => {
424397
let room: IOmnichannelRoom;
@@ -431,21 +404,14 @@ describe('LIVECHAT - inquiries', () => {
431404
});
432405
it('should fail if no one is serving the room', async () => {
433406
const { body } = await request
434-
.post(methodCall('livechat:returnAsInquiry'))
407+
.post(api('livechat/inquiries.returnAsInquiry'))
435408
.set(credentials)
436-
.send({
437-
message: JSON.stringify({
438-
method: 'livechat:returnAsInquiry',
439-
params: [room._id],
440-
id: 'id',
441-
msg: 'method',
442-
}),
443-
})
409+
.send({ roomId: room._id })
444410
.expect('Content-Type', 'application/json')
445411
.expect(200);
446412

447-
const response = parseMethodResponse(body);
448-
expect(response.result).to.be.false;
413+
expect(body).to.have.property('success', true);
414+
expect(body).to.have.property('result', false);
449415
});
450416
});
451417

@@ -460,21 +426,14 @@ describe('LIVECHAT - inquiries', () => {
460426
await takeInquiry(inq._id, testUser.credentials);
461427

462428
const { body } = await request
463-
.post(methodCall('livechat:returnAsInquiry'))
429+
.post(api('livechat/inquiries.returnAsInquiry'))
464430
.set(testUser.credentials)
465-
.send({
466-
message: JSON.stringify({
467-
method: 'livechat:returnAsInquiry',
468-
params: [room._id],
469-
id: 'id',
470-
msg: 'method',
471-
}),
472-
})
431+
.send({ roomId: room._id })
473432
.expect('Content-Type', 'application/json')
474433
.expect(200);
475434

476-
const response = parseMethodResponse(body);
477-
expect(response.result).to.be.true;
435+
expect(body).to.have.property('success', true);
436+
expect(body).to.have.property('result', true);
478437
});
479438
(IS_EE ? it : it.skip)('should appear on users queued elements', async () => {
480439
const { body } = await request
@@ -554,16 +513,9 @@ describe('LIVECHAT - inquiries', () => {
554513
await request.post(api('livechat/message')).send({ token: visitor.token, rid: room._id, msg: msgText }).expect(200);
555514

556515
await request
557-
.post(methodCall('livechat:returnAsInquiry'))
516+
.post(api('livechat/inquiries.returnAsInquiry'))
558517
.set(credentials)
559-
.send({
560-
message: JSON.stringify({
561-
method: 'livechat:returnAsInquiry',
562-
params: [room._id],
563-
id: 'id',
564-
msg: 'method',
565-
}),
566-
})
518+
.send({ roomId: room._id })
567519
.expect('Content-Type', 'application/json')
568520
.expect(200);
569521

0 commit comments

Comments
 (0)