Skip to content

Commit

Permalink
Convert LivechatCustomField model to raw model
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Aug 2, 2022
1 parent 133aa10 commit 038f7bb
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 106 deletions.
18 changes: 8 additions & 10 deletions apps/meteor/app/livechat/server/api/v1/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import type { ILivechatVisitorDTO, IRoom } from '@rocket.chat/core-typings';
import { LivechatVisitors as VisitorsRaw } from '@rocket.chat/models';
import { LivechatVisitors as VisitorsRaw, LivechatCustomField } from '@rocket.chat/models';

import { LivechatRooms, LivechatCustomField } from '../../../../models/server';
import { LivechatRooms } from '../../../../models/server';
import { API } from '../../../../api/server';
import { findGuest, normalizeHttpHeaderData } from '../lib/livechat';
import { Livechat } from '../../lib/Livechat';
Expand Down Expand Up @@ -40,17 +40,15 @@ API.v1.addRoute('livechat/visitor', {
const visitorId = await Livechat.registerGuest(guest as any); // TODO: Rewrite Livechat to TS

let visitor = await VisitorsRaw.findOneById(visitorId, {});
// If it's updating an existing visitor, it must also update the roomInfo
const cursor = LivechatRooms.findOpenByVisitorToken(visitor?.token);
cursor.forEach((room: IRoom) => {
if (visitor) {
Livechat.saveRoomInfo(room, visitor);
}
});
if (visitor) {
// If it's updating an existing visitor, it must also update the roomInfo
const rooms = LivechatRooms.findOpenByVisitorToken(visitor?.token).fetch();
await Promise.all(rooms.map((room: IRoom) => Livechat.saveRoomInfo(room, visitor)));
}

if (customFields && customFields instanceof Array) {
customFields.forEach((field) => {
const customField = LivechatCustomField.findOneById(field.key);
const customField = Promise.await(LivechatCustomField.findOneById(field.key));
if (!customField) {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions apps/meteor/app/livechat/server/lib/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import s from 'underscore.string';
import { MatchKeysAndValues, OnlyFieldsOfType } from 'mongodb';
import { LivechatVisitors, Users, LivechatRooms } from '@rocket.chat/models';
import { LivechatVisitors, Users, LivechatRooms, LivechatCustomField } from '@rocket.chat/models';
import { ILivechatCustomField, ILivechatVisitor, IOmnichannelRoom } from '@rocket.chat/core-typings';

import { LivechatCustomField, Rooms, LivechatInquiry, Subscriptions } from '../../../models/server';
import { Rooms, LivechatInquiry, Subscriptions } from '../../../models/server';

type RegisterContactProps = {
_id?: string;
Expand Down Expand Up @@ -71,9 +71,9 @@ export const Contacts = {
}
}

const allowedCF: ILivechatCustomField['_id'][] = LivechatCustomField.find({ scope: 'visitor' }, { fields: { _id: 1 } }).map(
({ _id }: ILivechatCustomField) => _id,
);
const allowedCF = await LivechatCustomField.findByScope<Pick<ILivechatCustomField, '_id'>>('visitor', { projection: { _id: 1 } })
.map(({ _id }) => _id)
.toArray();

const livechatData = Object.keys(customFields)
.filter((key) => allowedCF.includes(key) && customFields[key] !== '' && customFields[key] !== undefined)
Expand Down
19 changes: 9 additions & 10 deletions apps/meteor/app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import _ from 'underscore';
import s from 'underscore.string';
import moment from 'moment-timezone';
import UAParser from 'ua-parser-js';
import { Users as UsersRaw, LivechatVisitors } from '@rocket.chat/models';
import { Users as UsersRaw, LivechatVisitors, LivechatCustomField } from '@rocket.chat/models';

import { QueueManager } from './QueueManager';
import { RoutingManager } from './RoutingManager';
Expand All @@ -26,7 +26,6 @@ import {
Rooms,
LivechatDepartmentAgents,
LivechatDepartment,
LivechatCustomField,
LivechatInquiry,
} from '../../../models/server';
import { Logger } from '../../../logger/server';
Expand Down Expand Up @@ -393,10 +392,10 @@ export const Livechat = {
}

const customFields = {};
const fields = LivechatCustomField.find({ scope: 'visitor' });

if (!userId || hasPermission(userId, 'edit-livechat-room-customfields')) {
fields.forEach((field) => {
const fields = LivechatCustomField.findByScope('visitor');
for await (const field of fields) {
if (!livechatData.hasOwnProperty(field._id)) {
return;
}
Expand All @@ -408,7 +407,7 @@ export const Livechat = {
}
}
customFields[field._id] = value;
});
}
updateData.livechatData = customFields;
}
const ret = await LivechatVisitors.saveGuestById(_id, updateData);
Expand Down Expand Up @@ -513,7 +512,7 @@ export const Livechat = {
check(overwrite, Boolean);
Livechat.logger.debug(`Setting custom fields data for visitor with token ${token}`);

const customField = LivechatCustomField.findOneById(key);
const customField = await LivechatCustomField.findOneById(key);
if (!customField) {
throw new Meteor.Error('invalid-custom-field');
}
Expand Down Expand Up @@ -579,14 +578,14 @@ export const Livechat = {
return rcSettings;
},

saveRoomInfo(roomData, guestData, userId) {
async saveRoomInfo(roomData, guestData, userId) {
Livechat.logger.debug(`Saving room information on room ${roomData._id}`);
const { livechatData = {} } = roomData;
const customFields = {};

if (!userId || hasPermission(userId, 'edit-livechat-room-customfields')) {
const fields = LivechatCustomField.find({ scope: 'room' });
fields.forEach((field) => {
const fields = LivechatCustomField.findByScope('room');
for await (const field of fields) {
if (!livechatData.hasOwnProperty(field._id)) {
return;
}
Expand All @@ -598,7 +597,7 @@ export const Livechat = {
}
}
customFields[field._id] = value;
});
}
roomData.livechatData = customFields;
}

Expand Down
7 changes: 3 additions & 4 deletions apps/meteor/app/livechat/server/methods/getCustomFields.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Meteor } from 'meteor/meteor';

import { LivechatCustomField } from '../../../models/server';
import { LivechatCustomField } from '@rocket.chat/models';

Meteor.methods({
'livechat:getCustomFields'() {
return LivechatCustomField.find().fetch();
async 'livechat:getCustomFields'() {
return LivechatCustomField.find().toArray();
},
});
6 changes: 2 additions & 4 deletions apps/meteor/app/livechat/server/methods/registerGuest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ Meteor.methods({
});

// If it's updating an existing visitor, it must also update the roomInfo
const cursor = LivechatRooms.findOpenByVisitorToken(token);
cursor.forEach((room) => {
Livechat.saveRoomInfo(room, visitor);
});
const rooms = LivechatRooms.findOpenByVisitorToken(token).fetch();
await Promise.all(rooms.map((room) => Livechat.saveRoomInfo(room, visitor)));

if (customFields && customFields instanceof Array) {
// TODO: refactor to use normal await
Expand Down
9 changes: 4 additions & 5 deletions apps/meteor/app/livechat/server/methods/removeCustomField.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { LivechatCustomField } from '@rocket.chat/models';

import { hasPermission } from '../../../authorization';
import { LivechatCustomField } from '../../../models/server';
import { hasPermission } from '../../../authorization/server';

Meteor.methods({
'livechat:removeCustomField'(_id) {
async 'livechat:removeCustomField'(_id) {
if (!Meteor.userId() || !hasPermission(Meteor.userId(), 'view-livechat-manager')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'livechat:removeCustomField',
Expand All @@ -14,8 +14,7 @@ Meteor.methods({

check(_id, String);

const customField = LivechatCustomField.findOneById(_id, { fields: { _id: 1 } });

const customField = await LivechatCustomField.findOneById(_id, { projection: { _id: 1 } });
if (!customField) {
throw new Meteor.Error('error-invalid-custom-field', 'Custom field not found', {
method: 'livechat:removeCustomField',
Expand Down
10 changes: 5 additions & 5 deletions apps/meteor/app/livechat/server/methods/saveCustomField.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { LivechatCustomField } from '@rocket.chat/models';

import { hasPermission } from '../../../authorization';
import { LivechatCustomField } from '../../../models/server';
import { hasPermission } from '../../../authorization/server';

Meteor.methods({
'livechat:saveCustomField'(_id, customFieldData) {
async 'livechat:saveCustomField'(_id, customFieldData) {
if (!Meteor.userId() || !hasPermission(Meteor.userId(), 'view-livechat-manager')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'livechat:saveCustomField',
Expand Down Expand Up @@ -36,7 +36,7 @@ Meteor.methods({
}

if (_id) {
const customField = LivechatCustomField.findOneById(_id);
const customField = await LivechatCustomField.findOneById(_id);
if (!customField) {
throw new Meteor.Error('error-invalid-custom-field', 'Custom Field Not found', {
method: 'livechat:saveCustomField',
Expand All @@ -45,7 +45,7 @@ Meteor.methods({
}

if (!_id) {
const customField = LivechatCustomField.findOneById(customFieldData.field);
const customField = await LivechatCustomField.findOneById(customFieldData.field);
if (customField) {
throw new Meteor.Error('error-custom-field-name-already-exists', 'Custom field name already exists', {
method: 'livechat:saveCustomField',
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/methods/saveInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Meteor.methods({
delete guestData.phone;
}

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

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

Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/app/livechat/server/methods/setCustomField.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Meteor } from 'meteor/meteor';
import { LivechatVisitors } from '@rocket.chat/models';
import { LivechatVisitors, LivechatCustomField } from '@rocket.chat/models';

import { LivechatRooms, LivechatCustomField } from '../../../models/server';
import { LivechatRooms } from '../../../models/server';

Meteor.methods({
async 'livechat:setCustomField'(token, key, value, overwrite = true) {
const customField = LivechatCustomField.findOneById(key);
const customField = await LivechatCustomField.findOneById(key);
if (customField) {
if (customField.scope === 'room') {
return LivechatRooms.updateDataByToken(token, key, value, overwrite);
Expand Down
2 changes: 0 additions & 2 deletions apps/meteor/app/models/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Settings from './models/Settings';
import Subscriptions from './models/Subscriptions';
import Users from './models/Users';
import Imports from './models/Imports';
import LivechatCustomField from './models/LivechatCustomField';
import LivechatDepartment from './models/LivechatDepartment';
import LivechatDepartmentAgents from './models/LivechatDepartmentAgents';
import LivechatRooms from './models/LivechatRooms';
Expand All @@ -29,7 +28,6 @@ export {
Subscriptions,
Users,
Imports,
LivechatCustomField,
LivechatDepartment,
LivechatDepartmentAgents,
LivechatRooms,
Expand Down
49 changes: 0 additions & 49 deletions apps/meteor/app/models/server/models/LivechatCustomField.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { getLivechatQueueInfo, getLivechatCustomFields } from '../lib/Helper';

callbacks.add(
'livechat.onLoadConfigApi',
// TODO callbacks cannot be async
async (options = {}) => {
const { room } = options;

const queueInfo = await getLivechatQueueInfo(room);
const customFields = getLivechatCustomFields();
const customFields = await getLivechatCustomFields();

return {
...(queueInfo && { queueInfo }),
Expand Down
15 changes: 10 additions & 5 deletions apps/meteor/ee/app/livechat-enterprise/server/lib/Helper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Meteor } from 'meteor/meteor';
import moment from 'moment';
import { Rooms as RoomRaw, LivechatRooms as LivechatRoomsRaw, LivechatDepartment as LivechatDepartmentRaw } from '@rocket.chat/models';
import {
Rooms as RoomRaw,
LivechatRooms as LivechatRoomsRaw,
LivechatDepartment as LivechatDepartmentRaw,
LivechatCustomField,
} from '@rocket.chat/models';

import { memoizeDebounce } from './debounceByParams';
import { Users, LivechatInquiry, LivechatRooms, Messages, LivechatCustomField } from '../../../../../app/models/server';
import { Users, LivechatInquiry, LivechatRooms, Messages } from '../../../../../app/models/server';
import { settings } from '../../../../../app/settings/server';
import { RoutingManager } from '../../../../../app/livechat/server/lib/RoutingManager';
import { dispatchAgentDelegated } from '../../../../../app/livechat/server/lib/Helper';
Expand Down Expand Up @@ -248,12 +253,12 @@ export const updatePriorityInquiries = (priority) => {
});
};

export const getLivechatCustomFields = () => {
const customFields = LivechatCustomField.find({
export const getLivechatCustomFields = async () => {
const customFields = await LivechatCustomField.find({
visibility: 'visible',
scope: 'visitor',
public: true,
}).fetch();
}).toArray();
return customFields.map(({ _id, label, regexp, required = false, type, defaultValue = null, options }) => ({
_id,
label,
Expand Down
Loading

0 comments on commit 038f7bb

Please sign in to comment.