Skip to content

Commit

Permalink
Regression: Fix Omnichannel not working after meteor update (#26194)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Jul 12, 2022
1 parent 0ae6ea7 commit 62d77d8
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ Meteor.startup(function () {
Imports.invalidateOperationsExceptId(idToKeep);

// Clean up all the raw import data, except for the last operation
runDrop(() => RawImports.model.rawCollection().remove({ import: { $ne: idToKeep } }));
runDrop(() => RawImports.model.rawCollection().deleteMany({ import: { $ne: idToKeep } }));
} else {
Imports.invalidateAllOperations();

// Clean up all the raw import data
runDrop(() => RawImports.model.rawCollection().remove({}));
runDrop(() => RawImports.model.rawCollection().deleteMany({}));
}
});
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/lib/QueueManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const QueueManager = {
);
logger.debug(`Generated inquiry for visitor ${guest._id} with id ${inquiry._id} [Not queued]`);

LivechatRooms.updateRoomCount();
await LivechatRooms.updateRoomCount();

await queueInquiry(room, inquiry, agent);
logger.debug(`Inquiry ${inquiry._id} queued`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';

import { Base } from './_Base';
Expand All @@ -14,9 +13,6 @@ export class LivechatDepartmentAgents extends Base {
this.tryEnsureIndex({ departmentEnabled: 1 });
this.tryEnsureIndex({ agentId: 1 });
this.tryEnsureIndex({ username: 1 });

const collectionObj = this.model.rawCollection();
this.findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);
}

findByDepartmentId(departmentId) {
Expand Down Expand Up @@ -96,7 +92,7 @@ export class LivechatDepartmentAgents extends Base {

const collectionObj = this.model.rawCollection();

const agent = Promise.await(collectionObj.findAndModify(query, sort, update));
const agent = Promise.await(collectionObj.findOneAndUpdate(query, update, { sort, returnNewDocument: 'after' }));
if (agent && agent.value) {
return {
agentId: agent.value.agentId,
Expand Down Expand Up @@ -188,7 +184,7 @@ export class LivechatDepartmentAgents extends Base {
},
};

const bot = this.findAndModify(query, sort, update);
const bot = this.model.rawCollection().findOneAndUpdate(query, update, { sort, returnNewDocument: 'after' });
if (bot && bot.value) {
return {
agentId: bot.value.agentId,
Expand Down
14 changes: 4 additions & 10 deletions apps/meteor/app/models/server/models/LivechatRooms.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import s from 'underscore.string';
import _ from 'underscore';
import { Settings } from '@rocket.chat/models';

import { Base } from './_Base';
import Rooms from './Rooms';
import Settings from './Settings';

export class LivechatRooms extends Base {
constructor(...args) {
Expand Down Expand Up @@ -35,11 +35,6 @@ export class LivechatRooms extends Base {
);
}

findLivechat(filter = {}, offset = 0, limit = 20) {
const query = Object.assign(filter, { t: 'l' });
return this.find(query, { sort: { ts: -1 }, offset, limit });
}

findOneByIdOrName(_idOrName, options) {
const query = {
t: 'l',
Expand Down Expand Up @@ -258,7 +253,7 @@ export class LivechatRooms extends Base {
return this.findOne(query, options);
}

updateRoomCount = function () {
updateRoomCount = async function () {
const query = {
_id: 'Livechat_Room_Count',
};
Expand All @@ -269,9 +264,8 @@ export class LivechatRooms extends Base {
},
};

const livechatCount = Settings.findAndModify(query, null, update);

return livechatCount.value.value;
const livechatCount = await Settings.findOneAndUpdate(query, update, { returnDocument: 'after' });
return livechatCount.value;
};

findOpenByVisitorToken(visitorToken, options) {
Expand Down
5 changes: 0 additions & 5 deletions apps/meteor/app/models/server/models/Settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Meteor } from 'meteor/meteor';

import { Base } from './_Base';

export class Settings extends Base {
Expand All @@ -8,9 +6,6 @@ export class Settings extends Base {

this.tryEnsureIndex({ blocked: 1 }, { sparse: 1 });
this.tryEnsureIndex({ hidden: 1 }, { sparse: 1 });

const collectionObj = this.model.rawCollection();
this.findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);
}

// FIND
Expand Down
7 changes: 2 additions & 5 deletions apps/meteor/app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ export class Users extends Base {
this.tryEnsureIndex({ language: 1 }, { sparse: true });
this.tryEnsureIndex({ 'active': 1, 'services.email2fa.enabled': 1 }, { sparse: true }); // used by statistics
this.tryEnsureIndex({ 'active': 1, 'services.totp.enabled': 1 }, { sparse: true }); // used by statistics

const collectionObj = this.model.rawCollection();
this.findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);
}

getLoginTokensByUserId(userId) {
Expand Down Expand Up @@ -228,7 +225,7 @@ export class Users extends Base {
},
};

const user = this.findAndModify(query, sort, update);
const user = this.model.rawCollection().findOneAndUpdate(query, update, { sort, returnDocument: 'after' });
if (user && user.value) {
return {
agentId: user.value._id,
Expand Down Expand Up @@ -262,7 +259,7 @@ export class Users extends Base {
},
};

const user = this.findAndModify(query, sort, update);
const user = this.model.rawCollection().findOneAndUpdate(query, update, { sort, returnDocument: 'after' });
if (user && user.value) {
return {
agentId: user.value._id,
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/ee/app/livechat-enterprise/server/lib/Helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meteor } from 'meteor/meteor';
import moment from 'moment';
import { Rooms as RoomRaw } from '@rocket.chat/models';
import { Rooms as RoomRaw, LivechatRooms as LivechatRoomsRaw } from '@rocket.chat/models';

import { memoizeDebounce } from './debounceByParams';
import { LivechatDepartment, Users, LivechatInquiry, LivechatRooms, Messages, LivechatCustomField } from '../../../../../app/models/server';
Expand Down Expand Up @@ -166,7 +166,7 @@ export const updatePredictedVisitorAbandonment = () => {
if (!settings.get('Livechat_abandoned_rooms_action') || settings.get('Livechat_abandoned_rooms_action') === 'none') {
LivechatRooms.unsetPredictedVisitorAbandonment();
} else {
LivechatRooms.findLivechat({ open: true }).forEach((room) => setPredictedVisitorAbandonmentTime(room));
LivechatRoomsRaw.findPaginated({ t: 'l', open: true }).cursor.forEach((room) => setPredictedVisitorAbandonmentTime(room));
}
};

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/methods/browseChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ async function findUsers({ text, sort, pagination, workspace, viewFullOtherUserI
const options = {
...pagination,
sort,
fields: {
projection: {
username: 1,
name: 1,
nickname: 1,
Expand Down
82 changes: 82 additions & 0 deletions apps/meteor/server/models/raw/LivechatRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,88 @@ export class LivechatRoomsRaw extends BaseRaw {
return this.col.aggregate(params);
}

findRoomsWithCriteria({
agents,
roomName,
departmentId,
open,
served,
createdAt,
closedAt,
tags,
customFields,
visitorId,
roomIds,
onhold,
options = {},
}) {
const query = {
t: 'l',
};
if (agents) {
query.$or = [{ 'servedBy._id': { $in: agents } }, { 'servedBy.username': { $in: agents } }];
}
if (roomName) {
query.fname = new RegExp(roomName, 'i');
}
if (departmentId && departmentId !== 'undefined') {
query.departmentId = departmentId;
}
if (open !== undefined) {
query.open = { $exists: open };
query.onHold = { $ne: true };
}
if (served !== undefined) {
query.servedBy = { $exists: served };
}
if (visitorId && visitorId !== 'undefined') {
query['v._id'] = visitorId;
}
if (createdAt) {
query.ts = {};
if (createdAt.start) {
query.ts.$gte = new Date(createdAt.start);
}
if (createdAt.end) {
query.ts.$lte = new Date(createdAt.end);
}
}
if (closedAt) {
query.closedAt = {};
if (closedAt.start) {
query.closedAt.$gte = new Date(closedAt.start);
}
if (closedAt.end) {
query.closedAt.$lte = new Date(closedAt.end);
}
}
if (tags) {
query.tags = { $in: tags };
}
if (customFields) {
query.$and = Object.keys(customFields).map((key) => ({
[`livechatData.${key}`]: new RegExp(customFields[key], 'i'),
}));
}

if (roomIds) {
query._id = { $in: roomIds };
}

if (onhold) {
query.onHold = {
$exists: true,
$eq: onhold,
};
}

return this.findPaginated(query, {
sort: options.sort || { name: 1 },
skip: options.offset,
limit: options.count,
});
}

getOnHoldConversationsBetweenDate(from, to, departmentId) {
const query = {
onHold: {
Expand Down
8 changes: 2 additions & 6 deletions apps/meteor/server/models/raw/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,18 +407,14 @@ export class UsersRaw extends BaseRaw {
}

async setLastRoutingTime(userId) {
const result = await this.col.findAndModify(
const result = await this.findOneAndUpdate(
{ _id: userId },
{
sort: {
_id: 1,
},
},
{
$set: {
lastRoutingTime: new Date(),
},
},
{ returnDocument: 'after' },
);
return result.value;
}
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/services/banner/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class BannerService extends ServiceClassInternal implements IBannerServic

const { _id, ...banner } = result;

Banners.update({ _id }, { ...banner, ...doc, active: true }); // reenable the banner
Banners.updateOne({ _id }, { $set: { ...banner, ...doc, active: true } }); // reenable the banner

api.broadcast('banner.enabled', bannerId);
return true;
Expand Down

0 comments on commit 62d77d8

Please sign in to comment.