Skip to content

Commit 0c0b252

Browse files
committed
Merge branch 'regression/visitor-name-overwrite' of github.com:RocketChat/Rocket.Chat into regression/visitor-name-overwrite
2 parents 40932a8 + 514ace1 commit 0c0b252

8 files changed

Lines changed: 50 additions & 43 deletions

File tree

apps/meteor/app/livechat/imports/server/rest/rooms.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ API.v1.addRoute(
2121
'livechat/rooms',
2222
{ authRequired: true },
2323
{
24-
get() {
24+
async get() {
2525
const { offset, count } = this.getPaginationItems();
2626
const { sort, fields } = this.parseJsonQuery();
2727
const { agents, departmentId, open, tags, roomName, onhold } = this.requestParams();
@@ -32,6 +32,7 @@ API.v1.addRoute(
3232
check(open, Match.Maybe(String));
3333
check(onhold, Match.Maybe(String));
3434
check(tags, Match.Maybe([String]));
35+
check(customFields, Match.Maybe(String));
3536

3637
createdAt = validateDateParams('createdAt', createdAt);
3738
closedAt = validateDateParams('closedAt', closedAt);
@@ -43,24 +44,29 @@ API.v1.addRoute(
4344
}
4445

4546
if (customFields) {
46-
customFields = JSON.parse(customFields);
47+
try {
48+
const parsedCustomFields = JSON.parse(customFields);
49+
check(parsedCustomFields, Object);
50+
// Model's already checking for the keys, so we don't need to do it here.
51+
customFields = parsedCustomFields;
52+
} catch (e) {
53+
throw new Error('The "customFields" query parameter must be a valid JSON.');
54+
}
4755
}
4856

4957
return API.v1.success(
50-
Promise.await(
51-
findRooms({
52-
agents,
53-
roomName,
54-
departmentId,
55-
open: open && open === 'true',
56-
createdAt,
57-
closedAt,
58-
tags,
59-
customFields,
60-
onhold,
61-
options: { offset, count, sort, fields },
62-
}),
63-
),
58+
await findRooms({
59+
agents,
60+
roomName,
61+
departmentId,
62+
open: open && open === 'true',
63+
createdAt,
64+
closedAt,
65+
tags,
66+
customFields,
67+
onhold,
68+
options: { offset, count, sort, fields },
69+
}),
6470
);
6571
},
6672
},

apps/meteor/app/livechat/server/lib/Livechat.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,12 @@ export const Livechat = {
392392

393393
const customFields = {};
394394

395-
if (!userId || hasPermission(userId, 'edit-livechat-room-customfields')) {
395+
if ((!userId || hasPermission(userId, 'edit-livechat-room-customfields')) && Object.keys(livechatData).length) {
396+
Livechat.logger.debug(`Saving custom fields for visitor ${_id}`);
396397
const fields = LivechatCustomField.findByScope('visitor');
397398
for await (const field of fields) {
398399
if (!livechatData.hasOwnProperty(field._id)) {
399-
return;
400+
continue;
400401
}
401402
const value = s.trim(livechatData[field._id]);
402403
if (value !== '' && field.regexp !== undefined && field.regexp !== '') {
@@ -408,6 +409,7 @@ export const Livechat = {
408409
customFields[field._id] = value;
409410
}
410411
updateData.livechatData = customFields;
412+
Livechat.logger.debug(`About to update ${Object.keys(customFields).length} custom fields for visitor ${_id}`);
411413
}
412414
const ret = await LivechatVisitors.saveGuestById(_id, updateData);
413415

@@ -584,7 +586,8 @@ export const Livechat = {
584586
const { livechatData = {} } = roomData;
585587
const customFields = {};
586588

587-
if (!userId || hasPermission(userId, 'edit-livechat-room-customfields')) {
589+
if ((!userId || hasPermission(userId, 'edit-livechat-room-customfields')) && Object.keys(livechatData).length) {
590+
Livechat.logger.debug(`Updating custom fields on room ${roomData._id}`);
588591
const fields = LivechatCustomField.findByScope('room');
589592
for await (const field of fields) {
590593
if (!livechatData.hasOwnProperty(field._id)) {
@@ -600,9 +603,11 @@ export const Livechat = {
600603
customFields[field._id] = value;
601604
}
602605
roomData.livechatData = customFields;
606+
Livechat.logger.debug(`About to update ${Object.keys(customFields).length} custom fields on room ${roomData._id}`);
603607
}
604608

605609
if (!LivechatRooms.saveRoomById(roomData)) {
610+
Livechat.logger.debug(`Failed to save room information on room ${roomData._id}`);
606611
return false;
607612
}
608613

@@ -611,7 +616,7 @@ export const Livechat = {
611616
});
612617
callbacks.runAsync('livechat.saveRoom', roomData);
613618

614-
if (!_.isEmpty(guestData.name)) {
619+
if (guestData?.name?.trim().length) {
615620
const { _id: rid } = roomData;
616621
const { name } = guestData;
617622
return (

apps/meteor/app/livechat/server/methods/saveInfo.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Meteor } from 'meteor/meteor';
22
import { Match, check } from 'meteor/check';
3+
import { isOmnichannelRoom } from '@rocket.chat/core-typings';
34

45
import { hasPermission } from '../../../authorization';
56
import { LivechatRooms } from '../../../models/server';
@@ -37,7 +38,7 @@ Meteor.methods({
3738
);
3839

3940
const room = LivechatRooms.findOneById(roomData._id);
40-
if (room == null || room.t !== 'l') {
41+
if (!room || !isOmnichannelRoom(room)) {
4142
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'livechat:saveInfo' });
4243
}
4344

@@ -49,7 +50,7 @@ Meteor.methods({
4950
delete guestData.phone;
5051
}
5152

52-
const ret = (await Livechat.saveGuest(guestData, userId)) && (await Livechat.saveRoomInfo(roomData, guestData, userId));
53+
await Promise.allSettled([Livechat.saveGuest(guestData), Livechat.saveRoomInfo(roomData)]);
5354

5455
const user = Meteor.users.findOne({ _id: userId }, { fields: { _id: 1, username: 1 } });
5556

@@ -60,6 +61,6 @@ Meteor.methods({
6061
});
6162
});
6263

63-
return ret;
64+
return true;
6465
},
6566
});

apps/meteor/client/sidebar/search/SearchList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const useSearchItems = (filterText: string): UseQueryResult<(ISubscription & IRo
140140
{
141141
staleTime: 60_000,
142142
keepPreviousData: true,
143+
placeholderData: localRooms,
143144
},
144145
);
145146
};

apps/meteor/client/views/omnichannel/currentChats/CurrentChatsRoute.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type useQueryType = (
3939
itemsPerPage: 25 | 50 | 100;
4040
current: number;
4141
},
42-
customFields: { [key: string]: string },
42+
customFields: { [key: string]: string } | undefined,
4343
[column, direction]: [string, 'asc' | 'desc'],
4444
) => LivechatRoomsProps | undefined;
4545

@@ -101,7 +101,7 @@ const useQuery: useQueryType = (
101101
}
102102

103103
if (customFields && Object.keys(customFields).length > 0) {
104-
const customFieldsQuery = Object.fromEntries(Object.entries(customFields).filter((item) => item[1] !== ''));
104+
const customFieldsQuery = Object.fromEntries(Object.entries(customFields).filter((item) => item[1] !== undefined && item[1] !== ''));
105105
if (Object.keys(customFieldsQuery).length > 0) {
106106
query.customFields = JSON.stringify(customFieldsQuery);
107107
}
@@ -112,7 +112,7 @@ const useQuery: useQueryType = (
112112

113113
const CurrentChatsRoute = (): ReactElement => {
114114
const { sortBy, sortDirection, setSort } = useSort<'fname' | 'departmentId' | 'servedBy' | 'ts' | 'lm' | 'open'>('fname');
115-
const [customFields, setCustomFields] = useState<{ [key: string]: string }>({});
115+
const [customFields, setCustomFields] = useState<{ [key: string]: string }>();
116116
const [params, setParams] = useState({
117117
guest: '',
118118
fname: '',
@@ -128,12 +128,6 @@ const CurrentChatsRoute = (): ReactElement => {
128128
const t = useTranslation();
129129
const id = useRouteParameter('id');
130130

131-
const onHeaderClick = useMutableCallback((id) => {
132-
if (sortBy === id) {
133-
setSort(id, sortDirection === 'asc' ? 'desc' : 'asc');
134-
}
135-
});
136-
137131
const debouncedParams = useDebouncedValue(params, 500);
138132
const debouncedCustomFields = useDebouncedValue(customFields, 500);
139133

@@ -224,7 +218,7 @@ const CurrentChatsRoute = (): ReactElement => {
224218
key='fname'
225219
direction={sortDirection}
226220
active={sortBy === 'fname'}
227-
onClick={onHeaderClick}
221+
onClick={setSort}
228222
sort='fname'
229223
data-qa='current-chats-header-name'
230224
>
@@ -234,7 +228,7 @@ const CurrentChatsRoute = (): ReactElement => {
234228
key='departmentId'
235229
direction={sortDirection}
236230
active={sortBy === 'departmentId'}
237-
onClick={onHeaderClick}
231+
onClick={setSort}
238232
sort='departmentId'
239233
data-qa='current-chats-header-department'
240234
>
@@ -244,7 +238,7 @@ const CurrentChatsRoute = (): ReactElement => {
244238
key='servedBy'
245239
direction={sortDirection}
246240
active={sortBy === 'servedBy'}
247-
onClick={onHeaderClick}
241+
onClick={setSort}
248242
sort='servedBy'
249243
data-qa='current-chats-header-servedBy'
250244
>
@@ -254,7 +248,7 @@ const CurrentChatsRoute = (): ReactElement => {
254248
key='ts'
255249
direction={sortDirection}
256250
active={sortBy === 'ts'}
257-
onClick={onHeaderClick}
251+
onClick={setSort}
258252
sort='ts'
259253
data-qa='current-chats-header-startedAt'
260254
>
@@ -264,7 +258,7 @@ const CurrentChatsRoute = (): ReactElement => {
264258
key='lm'
265259
direction={sortDirection}
266260
active={sortBy === 'lm'}
267-
onClick={onHeaderClick}
261+
onClick={setSort}
268262
sort='lm'
269263
data-qa='current-chats-header-lastMessage'
270264
>
@@ -274,7 +268,7 @@ const CurrentChatsRoute = (): ReactElement => {
274268
key='open'
275269
direction={sortDirection}
276270
active={sortBy === 'open'}
277-
onClick={onHeaderClick}
271+
onClick={setSort}
278272
sort='open'
279273
w='x100'
280274
data-qa='current-chats-header-status'

apps/meteor/client/views/omnichannel/currentChats/CustomFieldsVerticalBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Controller, useForm } from 'react-hook-form';
77
import VerticalBar from '../../../components/VerticalBar';
88

99
type CustomFieldsVerticalBarProps = {
10-
setCustomFields: Dispatch<SetStateAction<{ [key: string]: string }>>;
10+
setCustomFields: Dispatch<SetStateAction<{ [key: string]: string } | undefined>>;
1111
allCustomFields: OmnichannelCustomFieldEndpointPayload[];
1212
};
1313

apps/meteor/client/views/omnichannel/currentChats/FilterByText.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import RemoveAllClosed from './RemoveAllClosed';
1313

1414
type FilterByTextType = FC<{
1515
setFilter: Dispatch<SetStateAction<any>>;
16-
setCustomFields: Dispatch<SetStateAction<{ [key: string]: string }>>;
17-
customFields: { [key: string]: string };
16+
setCustomFields: Dispatch<SetStateAction<{ [key: string]: string } | undefined>>;
17+
customFields: { [key: string]: string } | undefined;
1818
hasCustomFields: boolean;
1919
reload?: () => void;
2020
}>;
@@ -55,7 +55,7 @@ const FilterByText: FilterByTextType = ({ setFilter, reload, customFields, setCu
5555
setFrom('');
5656
setTo('');
5757
setTags([]);
58-
setCustomFields({});
58+
setCustomFields(undefined);
5959
});
6060

6161
const forms = useFormsSubscription() as any;

apps/meteor/server/models/raw/LivechatRooms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ export class LivechatRoomsRaw extends BaseRaw {
10571057
if (tags) {
10581058
query.tags = { $in: tags };
10591059
}
1060-
if (customFields) {
1060+
if (customFields && Object.keys(customFields).length) {
10611061
query.$and = Object.keys(customFields).map((key) => ({
10621062
[`livechatData.${key}`]: new RegExp(customFields[key], 'i'),
10631063
}));

0 commit comments

Comments
 (0)