Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions apps/meteor/app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,12 @@ export const Livechat = {

const customFields = {};

if (!userId || hasPermission(userId, 'edit-livechat-room-customfields')) {
if ((!userId || hasPermission(userId, 'edit-livechat-room-customfields')) && Object.keys(livechatData).length) {
Livechat.logger.debug(`Saving custom fields for visitor ${_id}`);
const fields = LivechatCustomField.findByScope('visitor');
for await (const field of fields) {
if (!livechatData.hasOwnProperty(field._id)) {
return;
continue;
}
const value = s.trim(livechatData[field._id]);
if (value !== '' && field.regexp !== undefined && field.regexp !== '') {
Expand All @@ -408,6 +409,7 @@ export const Livechat = {
customFields[field._id] = value;
}
updateData.livechatData = customFields;
Livechat.logger.debug(`About to update ${Object.keys(customFields).length} custom fields for visitor ${_id}`);
}
const ret = await LivechatVisitors.saveGuestById(_id, updateData);

Expand Down Expand Up @@ -584,7 +586,8 @@ export const Livechat = {
const { livechatData = {} } = roomData;
const customFields = {};

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

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

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

if (!_.isEmpty(guestData.name)) {
if (guestData?.name?.trim().length) {
const { _id: rid } = roomData;
const { name } = guestData;
return (
Expand Down
7 changes: 4 additions & 3 deletions apps/meteor/app/livechat/server/methods/saveInfo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { isOmnichannelRoom } from '@rocket.chat/core-typings';

import { hasPermission } from '../../../authorization';
import { LivechatRooms } from '../../../models/server';
Expand Down Expand Up @@ -37,7 +38,7 @@ Meteor.methods({
);

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

Expand All @@ -49,7 +50,7 @@ Meteor.methods({
delete guestData.phone;
}

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

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

Expand All @@ -60,6 +61,6 @@ Meteor.methods({
});
});

return ret;
return true;
},
});