RocketChat/Rocket.Chat

View on GitHub
apps/meteor/ee/app/livechat-enterprise/server/lib/query.helper.js

Summary

Maintainability
A
0 mins
Test Coverage
import { getUnitsFromUser } from './units';

// TODO: We need to add a new index in the departmentAncestors field

export const addQueryRestrictionsToRoomsModel = async (originalQuery = {}) => {
    const query = { ...originalQuery };

    const units = await getUnitsFromUser();
    if (!Array.isArray(units)) {
        return query;
    }

    const expressions = query.$and || [];
    const condition = {
        $or: [{ departmentAncestors: { $in: units } }, { departmentId: { $in: units } }],
    };
    query.$and = [condition, ...expressions];
    return query;
};