Skip to content

Commit

Permalink
[FIX] Business Units endpoints not filtering by Unit type (#26713)
Browse files Browse the repository at this point in the history
Co-authored-by: murtaza98 <murtaza.patrawala@rocket.chat>
  • Loading branch information
2 people authored and sampaiodiego committed Aug 29, 2022
1 parent eefaf91 commit dd4c2f7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export const validators = [
{
...(departmentIds && departmentIds.length > 0 && { department: { $in: departmentIds } }),
},
{
department: { $exists: false }, // No department == public queue
},
],
};

Expand Down
10 changes: 6 additions & 4 deletions apps/meteor/ee/app/license/server/getStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ export async function getStatistics(): Promise<ENTERPRISE_STATISTICS> {

// Number of business units
statsPms.push(
LivechatUnit.col.count().then((count) => {
statistics.businessUnits = count;
return true;
}),
LivechatUnit.find({ type: 'u' })
.count()
.then((count) => {
statistics.businessUnits = count;
return true;
}),
);

await Promise.all(statsPms).catch(log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export async function findUnits({

const query = { ...(text && { $or: [{ name: filter }] }) };

// TODO need to enfore type IOmnichannelBusinessUnit on LivechatUnit model, so don't need to use generic everywhere
const { cursor, totalCount } = LivechatUnit.findPaginated<IOmnichannelBusinessUnit>(query, {
const { cursor, totalCount } = LivechatUnit.findPaginatedUnits(query, {
sort: sort || { name: 1 },
skip: offset,
limit: count,
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/ee/app/models/server/raw/LivechatRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const applyRestrictions = (method) =>
overwriteClassOnLicense('livechat-enterprise', LivechatRoomsRaw, {
find: applyRestrictions('find'),
update: applyRestrictions('update'),
findPaginated: applyRestrictions('findPaginated'),
remove: applyRestrictions('remove'),
updateDepartmentAncestorsById(originalFn, _id, departmentAncestors) {
const query = {
Expand Down
14 changes: 12 additions & 2 deletions apps/meteor/ee/server/models/raw/LivechatUnit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import type { ILivechatUnitModel } from '@rocket.chat/model-typings';
import type { IOmnichannelBusinessUnit } from '@rocket.chat/core-typings';
import type { FindPaginated, ILivechatUnitModel } from '@rocket.chat/model-typings';
import type { FindOptions, Filter, FindCursor } from 'mongodb';

import { LivechatDepartmentRaw } from '../../../../server/models/raw/LivechatDepartment';

export class LivechatUnitRaw extends LivechatDepartmentRaw implements ILivechatUnitModel {}
export class LivechatUnitRaw extends LivechatDepartmentRaw implements ILivechatUnitModel {
findPaginatedUnits(
query: Filter<IOmnichannelBusinessUnit>,
options?: FindOptions<IOmnichannelBusinessUnit>,
): FindPaginated<FindCursor<IOmnichannelBusinessUnit>> {
// @ts-expect-error - This extend from LivechatDepartment messes up with the types
return super.findPaginated({ ...query, type: 'u' }, options);
}
}
8 changes: 8 additions & 0 deletions packages/model-typings/src/models/ILivechatUnitModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import type { IOmnichannelBusinessUnit } from '@rocket.chat/core-typings';
import type { FindOptions, Filter, FindCursor } from 'mongodb';

import type { FindPaginated } from './IBaseModel';
import type { ILivechatDepartmentModel } from './ILivechatDepartmentModel';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ILivechatUnitModel extends ILivechatDepartmentModel {
//
findPaginatedUnits(
query: Filter<IOmnichannelBusinessUnit>,
options?: FindOptions<IOmnichannelBusinessUnit>,
): FindPaginated<FindCursor<IOmnichannelBusinessUnit>>;
}

0 comments on commit dd4c2f7

Please sign in to comment.