Skip to content

Commit 6a5eb79

Browse files
chore: Adds deprecation warning for livechat:saveUnit (RocketChat#37255)
Co-authored-by: Douglas Fabris <27704687+dougfabris@users.noreply.github.com>
1 parent 4b8d0df commit 6a5eb79

8 files changed

Lines changed: 76 additions & 40 deletions

File tree

.changeset/cool-yaks-perform.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rocket.chat/meteor": patch
3+
---
4+
5+
Adds deprecation warning for `livechat:saveUnit`

apps/meteor/client/omnichannel/units/UnitEdit.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import type { ILivechatDepartment, ILivechatUnitMonitor, Serialized, IOmnichannelBusinessUnit } from '@rocket.chat/core-typings';
1+
import type {
2+
ILivechatDepartment,
3+
ILivechatUnitMonitor,
4+
Serialized,
5+
IOmnichannelBusinessUnit,
6+
OmnichannelBusinessUnitPayload,
7+
} from '@rocket.chat/core-typings';
28
import type { SelectOption } from '@rocket.chat/fuselage';
39
import { FieldError, Field, TextInput, Button, Select, ButtonGroup, FieldGroup, Box, FieldLabel, FieldRow } from '@rocket.chat/fuselage';
410
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
5-
import { useToastMessageDispatch, useMethod, useTranslation } from '@rocket.chat/ui-contexts';
11+
import { useToastMessageDispatch, useTranslation, useEndpoint } from '@rocket.chat/ui-contexts';
612
import { useQueryClient } from '@tanstack/react-query';
713
import { useId, useMemo } from 'react';
814
import { useForm, Controller } from 'react-hook-form';
@@ -34,13 +40,14 @@ type UnitEditProps = {
3440
unitData?: Serialized<IOmnichannelBusinessUnit>;
3541
unitMonitors?: Serialized<ILivechatUnitMonitor>[];
3642
unitDepartments?: Serialized<ILivechatDepartment>[];
37-
onClose: () => void;
43+
onUpdate?: (params: OmnichannelBusinessUnitPayload) => void;
3844
onDelete?: () => void;
45+
onClose: () => void;
3946
};
4047

41-
const UnitEdit = ({ unitData, unitMonitors, unitDepartments, onClose, onDelete }: UnitEditProps) => {
48+
const UnitEdit = ({ unitData, unitMonitors, unitDepartments, onUpdate, onDelete, onClose }: UnitEditProps) => {
4249
const t = useTranslation();
43-
const saveUnit = useMethod('livechat:saveUnit');
50+
const saveUnit = useEndpoint('POST', '/v1/livechat/units');
4451
const dispatchToastMessage = useToastMessageDispatch();
4552
const queryClient = useQueryClient();
4653

@@ -94,8 +101,19 @@ const UnitEdit = ({ unitData, unitMonitors, unitDepartments, onClose, onDelete }
94101
username: monitor.label,
95102
}));
96103

104+
const payload = {
105+
unitData: { name, visibility },
106+
unitMonitors: monitorsData,
107+
unitDepartments: departmentsData,
108+
};
109+
97110
try {
98-
await saveUnit(_id as unknown as string, { name, visibility }, monitorsData, departmentsData);
111+
if (_id && onUpdate) {
112+
await onUpdate(payload);
113+
} else {
114+
await saveUnit(payload);
115+
}
116+
99117
dispatchToastMessage({ type: 'success', message: t('Saved') });
100118
queryClient.invalidateQueries({
101119
queryKey: ['livechat-units'],

apps/meteor/client/omnichannel/units/UnitEditWithData.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const UnitEditWithData = ({ unitId, onClose }: { unitId: IOmnichannelBusinessUni
1414
const getUnitById = useEndpoint('GET', '/v1/livechat/units/:id', { id: unitId });
1515
const getMonitorsByUnitId = useEndpoint('GET', '/v1/livechat/units/:unitId/monitors', { unitId });
1616
const getDepartmentsByUnitId = useEndpoint('GET', '/v1/livechat/units/:unitId/departments', { unitId });
17+
const editUnit = useEndpoint('POST', '/v1/livechat/units/:id', { id: unitId });
1718
const removeUnit = useRemoveUnit(unitId);
1819

1920
const {
@@ -61,8 +62,9 @@ const UnitEditWithData = ({ unitId, onClose }: { unitId: IOmnichannelBusinessUni
6162
unitData={unitData}
6263
unitMonitors={unitMonitors?.monitors}
6364
unitDepartments={unitDepartments?.departments}
64-
onClose={onClose}
65+
onUpdate={editUnit}
6566
onDelete={removeUnit}
67+
onClose={onClose}
6668
/>
6769
);
6870
};

apps/meteor/ee/app/livechat-enterprise/server/api/units.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ILivechatUnitMonitor, IOmnichannelBusinessUnit } from '@rocket.chat/core-typings';
1+
import type { ILivechatUnitMonitor, IOmnichannelBusinessUnit, OmnichannelBusinessUnitPayload } from '@rocket.chat/core-typings';
22
import type { PaginatedResult, PaginatedRequest } from '@rocket.chat/rest-typings';
33

44
import { API } from '../../../../../app/api/server';
@@ -16,11 +16,11 @@ declare module '@rocket.chat/rest-typings' {
1616
};
1717
'/v1/livechat/units': {
1818
GET: (params: PaginatedRequest<{ text?: string }>) => PaginatedResult & { units: IOmnichannelBusinessUnit[] };
19-
POST: (params: { unitData: string; unitMonitors: string; unitDepartments: string }) => Omit<IOmnichannelBusinessUnit, '_updatedAt'>;
19+
POST: (params: OmnichannelBusinessUnitPayload) => Omit<IOmnichannelBusinessUnit, '_updatedAt'>;
2020
};
2121
'/v1/livechat/units/:id': {
2222
GET: () => IOmnichannelBusinessUnit;
23-
POST: (params: { unitData: string; unitMonitors: string; unitDepartments: string }) => Omit<IOmnichannelBusinessUnit, '_updatedAt'>;
23+
POST: (params: OmnichannelBusinessUnitPayload) => Omit<IOmnichannelBusinessUnit, '_updatedAt'>;
2424
DELETE: () => number;
2525
};
2626
}

apps/meteor/ee/app/livechat-enterprise/server/methods/saveUnit.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ServerMethods } from '@rocket.chat/ddp-client';
33
import { Meteor } from 'meteor/meteor';
44

55
import { hasPermissionAsync } from '../../../../../app/authorization/server/functions/hasPermission';
6+
import { methodDeprecationLogger } from '../../../../../app/lib/server/lib/deprecationWarningLogger';
67
import { LivechatEnterprise } from '../lib/LivechatEnterprise';
78

89
declare module '@rocket.chat/ddp-client' {
@@ -14,6 +15,7 @@ declare module '@rocket.chat/ddp-client' {
1415

1516
Meteor.methods<ServerMethods>({
1617
async 'livechat:saveUnit'(_id, unitData, unitMonitors, unitDepartments) {
18+
methodDeprecationLogger.method('livechat:saveUnit', '8.0.0', '/v1/livechat/units');
1719
const uid = Meteor.userId();
1820
if (!uid || !(await hasPermissionAsync(uid, 'manage-livechat-units'))) {
1921
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveUnit' });

apps/meteor/tests/data/livechat/units.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,21 @@ export const createUnit = async (
3535
): Promise<IOmnichannelBusinessUnit> => {
3636
return new Promise((resolve, reject) => {
3737
void request
38-
.post(methodCall(`livechat:saveUnit`))
38+
.post(api('livechat/units'))
3939
.set(credentials)
4040
.send({
41-
message: JSON.stringify({
42-
method: 'livechat:saveUnit',
43-
params: [
44-
null,
45-
{
46-
name: name || `${faker.person.firstName()} ${faker.string.uuid()}`,
47-
visibility: faker.helpers.arrayElement(['public', 'private']),
48-
},
49-
[{ monitorId, username }, ...extraMonitor],
50-
departmentIds.map((departmentId) => ({ departmentId })),
51-
],
52-
id: '101',
53-
msg: 'method',
54-
}),
41+
unitData: {
42+
name: name || `${faker.person.firstName()} ${faker.string.uuid()}`,
43+
visibility: faker.helpers.arrayElement(['public', 'private']),
44+
},
45+
unitMonitors: [{ monitorId, username }, ...extraMonitor],
46+
unitDepartments: departmentIds.map((departmentId) => ({ departmentId })),
5547
})
56-
.end((err: Error, res: DummyResponse<string, 'wrapped'>) => {
48+
.end((err: Error, res: DummyResponse<IOmnichannelBusinessUnit, 'not-wrapped'>) => {
5749
if (err) {
5850
return reject(err);
5951
}
60-
resolve(JSON.parse(res.body.message).result);
52+
resolve(res.body);
6153
});
6254
});
6355
};

apps/meteor/tests/e2e/utils/omnichannel/units.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { faker } from '@faker-js/faker';
2-
import type { IOmnichannelBusinessUnit } from '@rocket.chat/core-typings';
32

4-
import { parseMeteorResponse } from '../parseMeteorResponse';
53
import type { BaseTest } from '../test';
64

75
type CreateUnitParams = {
@@ -21,21 +19,27 @@ export const createOrUpdateUnit = async (
2119
api: BaseTest['api'],
2220
{ id = null, name, visibility, monitors, departments }: CreateUnitParams = {},
2321
) => {
24-
const response = await api.post('/method.call/livechat:saveUnit', {
25-
message: JSON.stringify({
26-
msg: 'method',
27-
id: '34',
28-
method: 'livechat:saveUnit',
29-
params: [id, { name: name || faker.string.uuid(), visibility: visibility || 'public' }, monitors, departments],
30-
}),
31-
});
22+
const unitPayload = {
23+
unitData: {
24+
name: name ?? faker.string.uuid(),
25+
visibility: visibility ?? 'public',
26+
},
27+
unitMonitors: monitors,
28+
unitDepartments: departments,
29+
};
30+
31+
const response = id ? await api.post(`/livechat/units/${id}`, unitPayload) : await api.post('/livechat/units', unitPayload);
32+
33+
if (response.status() !== 200) {
34+
throw new Error(`Failed to create or update unit [http status: ${response.status()}]`);
35+
}
3236

33-
const unit = await parseMeteorResponse<IOmnichannelBusinessUnit>(response);
37+
const data = await response.json();
3438

3539
return {
3640
response,
37-
data: unit,
38-
delete: async () => removeUnit(api, unit?._id),
41+
data,
42+
delete: async () => removeUnit(api, data?._id),
3943
};
4044
};
4145

packages/core-typings/src/IOmnichannelBusinessUnit.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,16 @@ export interface IOmnichannelBusinessUnit extends IRocketChatRecord {
1010
// Units don't have ancestors per se, but we need TS to know it can access the property from the collection
1111
ancestors?: string[];
1212
}
13+
14+
export type OmnichannelBusinessUnitPayload = {
15+
unitData: {
16+
name: string;
17+
visibility: string;
18+
enabled?: boolean;
19+
description?: string;
20+
email?: string;
21+
showOnOfflineForm?: boolean;
22+
};
23+
unitMonitors: { monitorId: string; username: string }[];
24+
unitDepartments: { departmentId: string }[];
25+
};

0 commit comments

Comments
 (0)