Skip to content

Commit

Permalink
Chore: Migrate from meteor model to raw (#25756)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Sampaio <[email protected]>
  • Loading branch information
KevLehman and sampaiodiego authored Jun 24, 2022
1 parent 4fbe782 commit 24ffc28
Show file tree
Hide file tree
Showing 52 changed files with 526 additions and 511 deletions.
24 changes: 17 additions & 7 deletions apps/meteor/app/apps/server/bridges/livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { IUser } from '@rocket.chat/apps-engine/definition/users';
import { IMessage } from '@rocket.chat/apps-engine/definition/messages';
import { IExtraRoomParams } from '@rocket.chat/apps-engine/definition/accessors/ILivechatCreator';
import { OmnichannelSourceType } from '@rocket.chat/core-typings';
import { LivechatVisitors } from '@rocket.chat/models';

import { getRoom } from '../../../livechat/server/api/lib/livechat';
import { Livechat } from '../../../livechat/server/lib/Livechat';
import { Users, LivechatDepartment, LivechatVisitors, LivechatRooms } from '../../../models/server';
import { Users, LivechatDepartment, LivechatRooms } from '../../../models/server';
import { AppServerOrchestrator } from '../orchestrator';

export class AppLivechatBridge extends LivechatBridge {
Expand Down Expand Up @@ -216,9 +217,9 @@ export class AppLivechatBridge extends LivechatBridge {
console.warn('The method AppLivechatBridge.findVisitors is deprecated. Please consider using its alternatives');
}

return LivechatVisitors.find(query)
.fetch()
.map((visitor: IVisitor) => this.orch.getConverters()?.get('visitors').convertVisitor(visitor));
return (await LivechatVisitors.find(query).toArray()).map(
(visitor) => visitor && this.orch.getConverters()?.get('visitors').convertVisitor(visitor),
);
}

protected async findVisitorById(id: string, appId: string): Promise<IVisitor | undefined> {
Expand All @@ -230,19 +231,28 @@ export class AppLivechatBridge extends LivechatBridge {
protected async findVisitorByEmail(email: string, appId: string): Promise<IVisitor | undefined> {
this.orch.debugLog(`The App ${appId} is looking for livechat visitors.`);

return this.orch.getConverters()?.get('visitors').convertVisitor(LivechatVisitors.findOneGuestByEmailAddress(email));
return this.orch
.getConverters()
?.get('visitors')
.convertVisitor(await LivechatVisitors.findOneGuestByEmailAddress(email));
}

protected async findVisitorByToken(token: string, appId: string): Promise<IVisitor | undefined> {
this.orch.debugLog(`The App ${appId} is looking for livechat visitors.`);

return this.orch.getConverters()?.get('visitors').convertVisitor(LivechatVisitors.getVisitorByToken(token, {}));
return this.orch
.getConverters()
?.get('visitors')
.convertVisitor(await LivechatVisitors.getVisitorByToken(token, {}));
}

protected async findVisitorByPhoneNumber(phoneNumber: string, appId: string): Promise<IVisitor | undefined> {
this.orch.debugLog(`The App ${appId} is looking for livechat visitors.`);

return this.orch.getConverters()?.get('visitors').convertVisitor(LivechatVisitors.findOneVisitorByPhone(phoneNumber));
return this.orch
.getConverters()
?.get('visitors')
.convertVisitor(await LivechatVisitors.findOneVisitorByPhone(phoneNumber));
}

protected async findDepartmentByIdOrName(value: string, appId: string): Promise<IDepartment | undefined> {
Expand Down
5 changes: 3 additions & 2 deletions apps/meteor/app/apps/server/converters/rooms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RoomType } from '@rocket.chat/apps-engine/definition/rooms';
import { LivechatVisitors } from '@rocket.chat/models';

import { Rooms, Users, LivechatVisitors, LivechatDepartment } from '../../../models/server';
import { Rooms, Users, LivechatDepartment } from '../../../models/server';
import { transformMappedData } from '../../lib/misc/transformMappedData';

export class AppRoomsConverter {
Expand Down Expand Up @@ -36,7 +37,7 @@ export class AppRoomsConverter {

let v;
if (room.visitor) {
const visitor = LivechatVisitors.findOneById(room.visitor.id);
const visitor = Promise.await(LivechatVisitors.findOneById(room.visitor.id));
v = {
_id: visitor._id,
username: visitor.username,
Expand Down
8 changes: 5 additions & 3 deletions apps/meteor/app/apps/server/converters/visitors.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import LivechatVisitors from '../../../models/server/models/LivechatVisitors';
import { LivechatVisitors } from '@rocket.chat/models';

import { transformMappedData } from '../../lib/misc/transformMappedData';

// TODO: check if functions from this converter can be async
export class AppVisitorsConverter {
constructor(orch) {
this.orch = orch;
}

convertById(id) {
const visitor = LivechatVisitors.findOneById(id);
const visitor = Promise.await(LivechatVisitors.findOneById(id));

return this.convertVisitor(visitor);
}

convertByToken(token) {
const visitor = LivechatVisitors.getVisitorByToken(token);
const visitor = Promise.await(LivechatVisitors.getVisitorByToken(token));

return this.convertVisitor(visitor);
}
Expand Down
11 changes: 6 additions & 5 deletions apps/meteor/app/livechat/imports/server/rest/facebook.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import crypto from 'crypto';

import { Random } from 'meteor/random';
import { LivechatVisitors } from '@rocket.chat/models';

import { API } from '../../../../api/server';
import { LivechatRooms, LivechatVisitors } from '../../../../models/server';
import { LivechatRooms } from '../../../../models/server';
import { settings } from '../../../../settings/server';
import { Livechat } from '../../../server/lib/Livechat';

Expand All @@ -21,7 +22,7 @@ import { Livechat } from '../../../server/lib/Livechat';
* @apiParam {String} [attachments] Facebook message attachments
*/
API.v1.addRoute('livechat/facebook', {
post() {
async post() {
if (!this.bodyParams.text && !this.bodyParams.attachments) {
return {
success: false,
Expand Down Expand Up @@ -63,7 +64,7 @@ API.v1.addRoute('livechat/facebook', {
},
},
};
let visitor = LivechatVisitors.getVisitorByToken(this.bodyParams.token);
let visitor = await LivechatVisitors.getVisitorByToken(this.bodyParams.token);
if (visitor) {
const rooms = LivechatRooms.findOpenByVisitorToken(visitor.token).fetch();
if (rooms && rooms.length > 0) {
Expand All @@ -76,12 +77,12 @@ API.v1.addRoute('livechat/facebook', {
sendMessage.message.rid = Random.id();
sendMessage.message.token = this.bodyParams.token;

const userId = Livechat.registerGuest({
const userId = await Livechat.registerGuest({
token: sendMessage.message.token,
name: `${this.bodyParams.first_name} ${this.bodyParams.last_name}`,
});

visitor = LivechatVisitors.findOneById(userId);
visitor = await LivechatVisitors.findOneById(userId);
}

sendMessage.message.msg = this.bodyParams.text;
Expand Down
9 changes: 5 additions & 4 deletions apps/meteor/app/livechat/imports/server/rest/sms.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { OmnichannelSourceType } from '@rocket.chat/core-typings';
import { LivechatVisitors } from '@rocket.chat/models';

import { FileUpload } from '../../../../file-upload/server';
import { LivechatRooms, LivechatVisitors, LivechatDepartment } from '../../../../models/server';
import { LivechatRooms, LivechatDepartment } from '../../../../models/server';
import { API } from '../../../../api/server';
import { fetch } from '../../../../../server/lib/http/fetch';
import { SMS } from '../../../../sms';
Expand Down Expand Up @@ -34,7 +35,7 @@ const defineDepartment = (idOrName) => {
return department && department._id;
};

const defineVisitor = (smsNumber, targetDepartment) => {
const defineVisitor = async (smsNumber, targetDepartment) => {
const visitor = LivechatVisitors.findOneVisitorByPhone(smsNumber);
let data = {
token: (visitor && visitor.token) || Random.id(),
Expand All @@ -53,7 +54,7 @@ const defineVisitor = (smsNumber, targetDepartment) => {
data.department = targetDepartment;
}

const id = Livechat.registerGuest(data);
const id = await Livechat.registerGuest(data);
return LivechatVisitors.findOneById(id);
};

Expand All @@ -79,7 +80,7 @@ API.v1.addRoute('livechat/sms-incoming/:service', {
targetDepartment = defineDepartment(SMS.department);
}

const visitor = defineVisitor(sms.from, targetDepartment);
const visitor = await defineVisitor(sms.from, targetDepartment);
const { token } = visitor;
const room = LivechatRooms.findOneOpenByVisitorTokenAndDepartmentId(token, targetDepartment);
const roomExists = !!room;
Expand Down
19 changes: 9 additions & 10 deletions apps/meteor/app/livechat/imports/server/rest/upload.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Meteor } from 'meteor/meteor';
import filesize from 'filesize';
import { LivechatVisitors } from '@rocket.chat/models';

import { settings } from '../../../../settings/server';
import { Settings, LivechatRooms, LivechatVisitors } from '../../../../models/server';
import { Settings, LivechatRooms } from '../../../../models/server';
import { fileUploadIsValidContentType } from '../../../../utils/server';
import { FileUpload } from '../../../../file-upload';
import { API } from '../../../../api/server';
Expand All @@ -19,13 +20,13 @@ settings.watch('FileUpload_MaxFileSize', function (value) {
});

API.v1.addRoute('livechat/upload/:rid', {
post() {
async post() {
if (!this.request.headers['x-visitor-token']) {
return API.v1.unauthorized();
}

const visitorToken = this.request.headers['x-visitor-token'];
const visitor = LivechatVisitors.getVisitorByToken(visitorToken);
const visitor = await LivechatVisitors.getVisitorByToken(visitorToken);

if (!visitor) {
return API.v1.unauthorized();
Expand All @@ -36,13 +37,11 @@ API.v1.addRoute('livechat/upload/:rid', {
return API.v1.unauthorized();
}

const [file, fields] = Promise.await(
getUploadFormData(
{
request: this.request,
},
{ field: 'file' },
),
const [file, fields] = await getUploadFormData(
{
request: this.request,
},
{ field: 'file' },
);

if (!fileUploadIsValidContentType(file.mimetype)) {
Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/app/livechat/server/api/lib/livechat.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { EmojiCustom, LivechatTrigger } from '@rocket.chat/models';
import { EmojiCustom, LivechatTrigger, LivechatVisitors } from '@rocket.chat/models';

import { LivechatRooms, LivechatVisitors, LivechatDepartment } from '../../../../models/server';
import { LivechatRooms, LivechatDepartment } from '../../../../models/server';
import { Livechat } from '../../lib/Livechat';
import { callbacks } from '../../../../../lib/callbacks';
import { normalizeAgent } from '../../lib/Helper';
Expand Down Expand Up @@ -40,7 +40,7 @@ export function findDepartments(businessUnit) {

export function findGuest(token) {
return LivechatVisitors.getVisitorByToken(token, {
fields: {
projection: {
name: 1,
username: 1,
token: 1,
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/livechat/server/api/v1/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { findRoom, findGuest, findAgent, findOpenRoom } from '../lib/livechat';
import { Livechat } from '../../lib/Livechat';

API.v1.addRoute('livechat/agent.info/:rid/:token', {
get() {
async get() {
try {
check(this.urlParams, {
rid: String,
token: String,
});

const visitor = findGuest(this.urlParams.token);
const visitor = await findGuest(this.urlParams.token);
if (!visitor) {
throw new Meteor.Error('invalid-token');
}
Expand Down
14 changes: 7 additions & 7 deletions apps/meteor/app/livechat/server/api/v1/contact.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { LivechatVisitors } from '@rocket.chat/models';

import { API } from '../../../../api/server';
import { Contacts } from '../../lib/Contacts';
import { LivechatVisitors } from '../../../../models/server';

API.v1.addRoute(
'omnichannel/contact',
{ authRequired: true },
{
post() {
async post() {
try {
check(this.bodyParams, {
_id: Match.Maybe(String),
Expand All @@ -23,19 +23,19 @@ API.v1.addRoute(
}),
});

const contact = Contacts.registerContact(this.bodyParams);
const contact = await Contacts.registerContact(this.bodyParams);

return API.v1.success({ contact });
} catch (e) {
return API.v1.failure(e);
}
},
get() {
async get() {
check(this.queryParams, {
contactId: String,
});

const contact = Promise.await(LivechatVisitors.findOneById(this.queryParams.contactId));
const contact = await LivechatVisitors.findOneById(this.queryParams.contactId);

return API.v1.success({ contact });
},
Expand All @@ -46,7 +46,7 @@ API.v1.addRoute(
'omnichannel/contact.search',
{ authRequired: true },
{
get() {
async get() {
try {
check(this.queryParams, {
email: Match.Maybe(String),
Expand All @@ -67,7 +67,7 @@ API.v1.addRoute(
},
);

const contact = Promise.await(LivechatVisitors.findOne(query));
const contact = await LivechatVisitors.findOne(query);
return API.v1.success({ contact });
} catch (e) {
return API.v1.failure(e);
Expand Down
26 changes: 14 additions & 12 deletions apps/meteor/app/livechat/server/api/v1/customField.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Livechat } from '../../lib/Livechat';
import { findLivechatCustomFields, findCustomFieldById } from '../lib/customFields';

API.v1.addRoute('livechat/custom.field', {
post() {
async post() {
try {
check(this.bodyParams, {
token: String,
Expand All @@ -18,12 +18,12 @@ API.v1.addRoute('livechat/custom.field', {

const { token, key, value, overwrite } = this.bodyParams;

const guest = findGuest(token);
const guest = await findGuest(token);
if (!guest) {
throw new Meteor.Error('invalid-token');
}

if (!Livechat.setCustomFields({ token, key, value, overwrite })) {
if (!(await Livechat.setCustomFields({ token, key, value, overwrite }))) {
return API.v1.failure();
}

Expand All @@ -35,7 +35,7 @@ API.v1.addRoute('livechat/custom.field', {
});

API.v1.addRoute('livechat/custom.fields', {
post() {
async post() {
check(this.bodyParams, {
token: String,
customFields: [
Expand All @@ -48,19 +48,21 @@ API.v1.addRoute('livechat/custom.fields', {
});

const { token } = this.bodyParams;
const guest = findGuest(token);
const guest = await findGuest(token);
if (!guest) {
throw new Meteor.Error('invalid-token');
}

const fields = this.bodyParams.customFields.map((customField) => {
const data = Object.assign({ token }, customField);
if (!Livechat.setCustomFields(data)) {
return API.v1.failure();
}
const fields = await Promise.all(
this.bodyParams.customFields.map(async (customField) => {
const data = Object.assign({ token }, customField);
if (!(await Livechat.setCustomFields(data))) {
return API.v1.failure();
}

return { Key: customField.key, value: customField.value, overwrite: customField.overwrite };
});
return { Key: customField.key, value: customField.value, overwrite: customField.overwrite };
}),
);

return API.v1.success({ fields });
},
Expand Down
Loading

0 comments on commit 24ffc28

Please sign in to comment.