Skip to content

Commit c15a47e

Browse files
chore: Adds deprecation warning on livechat:takeInquiry (RocketChat#36993)
1 parent 40e2421 commit c15a47e

6 files changed

Lines changed: 110 additions & 67 deletions

File tree

.changeset/nice-balloons-relax.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:removeRoom`, use `livechat/inquiries.take` instead

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { API } from '../../../../api/server';
1111
import { getPaginationItems } from '../../../../api/server/helpers/getPaginationItems';
1212
import { findInquiries, findOneInquiryByRoomId } from '../../../server/api/lib/inquiries';
13-
import { takeInquiry } from '../../../server/methods/takeInquiry';
13+
import { takeInquiry } from '../../../server/lib/takeInquiry';
1414

1515
API.v1.addRoute(
1616
'livechat/inquiries.list',
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Omnichannel } from '@rocket.chat/core-services';
2+
import { LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models';
3+
import { Meteor } from 'meteor/meteor';
4+
5+
import { RoutingManager } from './RoutingManager';
6+
import { isAgentAvailableToTakeContactInquiry } from './contacts/isAgentAvailableToTakeContactInquiry';
7+
import { migrateVisitorIfMissingContact } from './contacts/migrateVisitorIfMissingContact';
8+
import { settings } from '../../../settings/server';
9+
10+
export const takeInquiry = async (
11+
userId: string,
12+
inquiryId: string,
13+
options?: { clientAction: boolean; forwardingToDepartment?: { oldDepartmentId: string; transferData: any } },
14+
): Promise<void> => {
15+
const inquiry = await LivechatInquiry.findOneById(inquiryId);
16+
17+
if (!inquiry) {
18+
throw new Meteor.Error('error-not-found', 'Inquiry not found', {
19+
method: 'livechat:takeInquiry',
20+
});
21+
}
22+
23+
if (inquiry.status === 'taken') {
24+
throw new Meteor.Error('error-inquiry-taken', 'Inquiry already taken', {
25+
method: 'livechat:takeInquiry',
26+
});
27+
}
28+
29+
const user = await Users.findOneOnlineAgentById(userId, settings.get<boolean>('Livechat_enabled_when_agent_idle'));
30+
if (!user) {
31+
throw new Meteor.Error('error-agent-status-service-offline', 'Agent status is offline or Omnichannel service is not active', {
32+
method: 'livechat:takeInquiry',
33+
});
34+
}
35+
36+
const room = await LivechatRooms.findOneById(inquiry.rid);
37+
if (!room || !(await Omnichannel.isWithinMACLimit(room))) {
38+
throw new Meteor.Error('error-mac-limit-reached');
39+
}
40+
41+
const contactId = room.contactId ?? (await migrateVisitorIfMissingContact(room.v._id, room.source));
42+
if (contactId) {
43+
const isAgentAvailableToTakeContactInquiryResult = await isAgentAvailableToTakeContactInquiry(inquiry.v._id, room.source, contactId);
44+
if (!isAgentAvailableToTakeContactInquiryResult.value) {
45+
throw new Meteor.Error(isAgentAvailableToTakeContactInquiryResult.error);
46+
}
47+
}
48+
49+
const agent = {
50+
agentId: user._id,
51+
username: user.username,
52+
};
53+
54+
try {
55+
await RoutingManager.takeInquiry(inquiry, agent, options ?? {}, room);
56+
} catch (e: any) {
57+
throw new Meteor.Error(e.message);
58+
}
59+
};
Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { Omnichannel } from '@rocket.chat/core-services';
21
import type { ServerMethods } from '@rocket.chat/ddp-client';
3-
import { LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models';
42
import { Meteor } from 'meteor/meteor';
53

64
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
7-
import { settings } from '../../../settings/server';
8-
import { RoutingManager } from '../lib/RoutingManager';
9-
import { isAgentAvailableToTakeContactInquiry } from '../lib/contacts/isAgentAvailableToTakeContactInquiry';
10-
import { migrateVisitorIfMissingContact } from '../lib/contacts/migrateVisitorIfMissingContact';
5+
import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
6+
import { takeInquiry } from '../lib/takeInquiry';
117

128
declare module '@rocket.chat/ddp-client' {
139
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -19,72 +15,22 @@ declare module '@rocket.chat/ddp-client' {
1915
}
2016
}
2117

22-
export const takeInquiry = async (
23-
userId: string,
24-
inquiryId: string,
25-
options?: { clientAction: boolean; forwardingToDepartment?: { oldDepartmentId: string; transferData: any } },
26-
): Promise<void> => {
27-
if (!userId || !(await hasPermissionAsync(userId, 'view-l-room'))) {
28-
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
29-
method: 'livechat:takeInquiry',
30-
});
31-
}
32-
33-
const inquiry = await LivechatInquiry.findOneById(inquiryId);
34-
35-
if (!inquiry) {
36-
throw new Meteor.Error('error-not-found', 'Inquiry not found', {
37-
method: 'livechat:takeInquiry',
38-
});
39-
}
40-
41-
if (inquiry.status === 'taken') {
42-
throw new Meteor.Error('error-inquiry-taken', 'Inquiry already taken', {
43-
method: 'livechat:takeInquiry',
44-
});
45-
}
46-
47-
const user = await Users.findOneOnlineAgentById(userId, settings.get<boolean>('Livechat_enabled_when_agent_idle'));
48-
if (!user) {
49-
throw new Meteor.Error('error-agent-status-service-offline', 'Agent status is offline or Omnichannel service is not active', {
50-
method: 'livechat:takeInquiry',
51-
});
52-
}
53-
54-
const room = await LivechatRooms.findOneById(inquiry.rid);
55-
if (!room || !(await Omnichannel.isWithinMACLimit(room))) {
56-
throw new Meteor.Error('error-mac-limit-reached');
57-
}
58-
59-
const contactId = room.contactId ?? (await migrateVisitorIfMissingContact(room.v._id, room.source));
60-
if (contactId) {
61-
const isAgentAvailableToTakeContactInquiryResult = await isAgentAvailableToTakeContactInquiry(inquiry.v._id, room.source, contactId);
62-
if (!isAgentAvailableToTakeContactInquiryResult.value) {
63-
throw new Meteor.Error(isAgentAvailableToTakeContactInquiryResult.error);
64-
}
65-
}
66-
67-
const agent = {
68-
agentId: user._id,
69-
username: user.username,
70-
};
71-
72-
try {
73-
await RoutingManager.takeInquiry(inquiry, agent, options ?? {}, room);
74-
} catch (e: any) {
75-
throw new Meteor.Error(e.message);
76-
}
77-
};
78-
7918
Meteor.methods<ServerMethods>({
8019
async 'livechat:takeInquiry'(inquiryId, options) {
20+
methodDeprecationLogger.method('livechat:takeInquiry', '8.0.0', '/v1/livechat/inquiries.take');
8121
const uid = Meteor.userId();
8222
if (!uid) {
8323
throw new Meteor.Error('error-not-allowed', 'Invalid User', {
8424
method: 'livechat:takeInquiry',
8525
});
8626
}
8727

28+
if (!(await hasPermissionAsync(uid, 'view-l-room'))) {
29+
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
30+
method: 'livechat:takeInquiry',
31+
});
32+
}
33+
8834
return takeInquiry(uid, inquiryId, options);
8935
},
9036
});

apps/meteor/client/views/room/composer/ComposerOmnichannel/ComposerOmnichannelInquiry.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MessageFooterCallout, MessageFooterCalloutAction, MessageFooterCalloutContent } from '@rocket.chat/ui-composer';
2-
import { useEndpoint, useMethod, useToastMessageDispatch, useTranslation, useUser } from '@rocket.chat/ui-contexts';
2+
import { useEndpoint, useToastMessageDispatch, useTranslation, useUser } from '@rocket.chat/ui-contexts';
33
import { useQuery } from '@tanstack/react-query';
44
import type { ReactElement } from 'react';
55
import { useMemo } from 'react';
@@ -23,7 +23,7 @@ export const ComposerOmnichannelInquiry = (): ReactElement => {
2323
}),
2424
});
2525

26-
const takeInquiry = useMethod('livechat:takeInquiry');
26+
const takeInquiry = useEndpoint('POST', '/v1/livechat/inquiries.take');
2727

2828
const handleTakeInquiry = async (): Promise<void> => {
2929
if (!result.isSuccess) {
@@ -33,7 +33,7 @@ export const ComposerOmnichannelInquiry = (): ReactElement => {
3333
return;
3434
}
3535
try {
36-
await takeInquiry(result.data.inquiry._id, { clientAction: true });
36+
await takeInquiry({ inquiryId: result.data.inquiry._id, options: { clientAction: true } });
3737
} catch (error) {
3838
dispatchToastMessage({ type: 'error', message: error });
3939
}

packages/rest-typings/src/v1/omnichannel.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,6 +3366,13 @@ export const isGETLivechatInquiriesListParams = ajv.compile<GETLivechatInquiries
33663366
type POSTLivechatInquiriesTakeParams = {
33673367
inquiryId: string;
33683368
userId?: string;
3369+
options?: {
3370+
clientAction: boolean;
3371+
forwardingToDepartment?: {
3372+
oldDepartmentId: string;
3373+
transferData: unknown;
3374+
};
3375+
};
33693376
};
33703377

33713378
const POSTLivechatInquiriesTakeParamsSchema = {
@@ -3378,6 +3385,31 @@ const POSTLivechatInquiriesTakeParamsSchema = {
33783385
type: 'string',
33793386
nullable: true,
33803387
},
3388+
options: {
3389+
type: 'object',
3390+
nullable: true,
3391+
properties: {
3392+
clientAction: {
3393+
type: 'boolean',
3394+
},
3395+
forwardingToDepartment: {
3396+
type: 'object',
3397+
nullable: true,
3398+
properties: {
3399+
oldDepartmentId: {
3400+
type: 'string',
3401+
},
3402+
transferData: {
3403+
type: 'object',
3404+
},
3405+
},
3406+
required: ['oldDepartmentId', 'transferData'],
3407+
additionalProperties: false,
3408+
},
3409+
},
3410+
required: ['clientAction'],
3411+
additionalProperties: false,
3412+
},
33813413
},
33823414
additionalProperties: false,
33833415
required: ['inquiryId'],

0 commit comments

Comments
 (0)