Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
198 lines
5.5 KiB
TypeScript
198 lines
5.5 KiB
TypeScript
import type { ILivechatDepartment, ILivechatDepartmentAgents } from '@rocket.chat/core-typings';
|
|
import { LivechatDepartment, LivechatDepartmentAgents } from '@rocket.chat/models';
|
|
import { applyDepartmentRestrictions } from '@rocket.chat/omni-core';
|
|
import type { PaginatedResult } from '@rocket.chat/rest-typings';
|
|
import { escapeRegExp } from '@rocket.chat/tools';
|
|
import type { Document, Filter, FilterOperators, FindOptions } from 'mongodb';
|
|
|
|
import { hasPermissionAsync } from '../../../../lib/authorization/hasPermission';
|
|
|
|
type Pagination<T extends Document> = { pagination: { offset: number; count: number; sort: FindOptions<T>['sort'] } };
|
|
type FindDepartmentParams = {
|
|
userId: string;
|
|
onlyMyDepartments?: boolean;
|
|
text?: string;
|
|
enabled?: boolean;
|
|
excludeDepartmentId?: string;
|
|
showArchived?: boolean;
|
|
} & Pagination<ILivechatDepartment>;
|
|
type FindDepartmentByIdParams = {
|
|
userId: string;
|
|
departmentId: string;
|
|
includeAgents?: boolean;
|
|
onlyMyDepartments?: boolean;
|
|
};
|
|
type FindDepartmentToAutocompleteParams = {
|
|
uid: string;
|
|
selector: {
|
|
exceptions: string[];
|
|
conditions: Filter<ILivechatDepartment>;
|
|
term: string;
|
|
};
|
|
onlyMyDepartments?: boolean;
|
|
showArchived?: boolean;
|
|
};
|
|
type FindDepartmentAgentsParams = {
|
|
userId: string;
|
|
departmentId: string;
|
|
} & Pagination<ILivechatDepartmentAgents>;
|
|
|
|
export async function findDepartments({
|
|
userId,
|
|
onlyMyDepartments = false,
|
|
text,
|
|
enabled,
|
|
excludeDepartmentId,
|
|
showArchived = false,
|
|
pagination: { offset, count, sort },
|
|
}: FindDepartmentParams): Promise<PaginatedResult<{ departments: ILivechatDepartment[] }>> {
|
|
let query: FilterOperators<ILivechatDepartment> = {
|
|
$or: [{ type: { $eq: 'd' } }, { type: { $exists: false } }],
|
|
...(!showArchived && { archived: { $ne: !showArchived } }),
|
|
...(enabled && { enabled: Boolean(enabled) }),
|
|
...(text && { name: new RegExp(escapeRegExp(text), 'i') }),
|
|
...(excludeDepartmentId && { _id: { $ne: excludeDepartmentId } }),
|
|
};
|
|
|
|
if (onlyMyDepartments) {
|
|
query = await applyDepartmentRestrictions(query, userId);
|
|
}
|
|
|
|
const { cursor, totalCount } = LivechatDepartment.findPaginated(query, {
|
|
sort: sort || { name: 1 },
|
|
skip: offset,
|
|
limit: count,
|
|
});
|
|
|
|
const [departments, total] = await Promise.all([cursor.toArray(), totalCount]);
|
|
|
|
return {
|
|
departments,
|
|
count: departments.length,
|
|
offset,
|
|
total,
|
|
};
|
|
}
|
|
|
|
export async function findArchivedDepartments({
|
|
userId,
|
|
onlyMyDepartments = false,
|
|
text,
|
|
excludeDepartmentId,
|
|
pagination: { offset, count, sort },
|
|
}: FindDepartmentParams): Promise<PaginatedResult<{ departments: ILivechatDepartment[] }>> {
|
|
let query: FilterOperators<ILivechatDepartment> = {
|
|
$or: [{ type: { $eq: 'd' } }, { type: { $exists: false } }],
|
|
archived: { $eq: true },
|
|
...(text && { name: new RegExp(escapeRegExp(text), 'i') }),
|
|
...(excludeDepartmentId && { _id: { $ne: excludeDepartmentId } }),
|
|
};
|
|
|
|
if (onlyMyDepartments) {
|
|
query = await applyDepartmentRestrictions(query, userId);
|
|
}
|
|
|
|
const { cursor, totalCount } = LivechatDepartment.findPaginated(query, {
|
|
sort: sort || { name: 1 },
|
|
skip: offset,
|
|
limit: count,
|
|
});
|
|
|
|
const [departments, total] = await Promise.all([cursor.toArray(), totalCount]);
|
|
|
|
return {
|
|
departments,
|
|
count: departments.length,
|
|
offset,
|
|
total,
|
|
};
|
|
}
|
|
|
|
export async function findDepartmentById({
|
|
userId,
|
|
departmentId,
|
|
includeAgents = true,
|
|
onlyMyDepartments = false,
|
|
}: FindDepartmentByIdParams): Promise<{
|
|
department: ILivechatDepartment | null;
|
|
agents?: ILivechatDepartmentAgents[];
|
|
}> {
|
|
const canViewLivechatDepartments = includeAgents && (await hasPermissionAsync(userId, 'view-livechat-departments'));
|
|
|
|
let query: FilterOperators<ILivechatDepartment> = { _id: departmentId };
|
|
|
|
if (onlyMyDepartments) {
|
|
query = await applyDepartmentRestrictions(query, userId);
|
|
}
|
|
|
|
const result = {
|
|
department: await LivechatDepartment.findOne(query),
|
|
...(includeAgents &&
|
|
canViewLivechatDepartments && {
|
|
agents: await LivechatDepartmentAgents.findByDepartmentId(departmentId).toArray(),
|
|
}),
|
|
};
|
|
|
|
return result;
|
|
}
|
|
|
|
export async function findDepartmentsToAutocomplete({
|
|
uid,
|
|
selector,
|
|
onlyMyDepartments = false,
|
|
showArchived = false,
|
|
}: FindDepartmentToAutocompleteParams): Promise<{ items: Pick<ILivechatDepartment, '_id' | 'name'>[] }> {
|
|
const { exceptions = [] } = selector;
|
|
let { conditions = {} } = selector;
|
|
|
|
if (onlyMyDepartments) {
|
|
conditions = await applyDepartmentRestrictions(conditions, uid);
|
|
}
|
|
|
|
const conditionsWithArchived = { archived: { $ne: !showArchived }, ...conditions };
|
|
|
|
const items = await LivechatDepartment.findByNameRegexWithExceptionsAndConditions(selector.term, exceptions, conditionsWithArchived, {
|
|
projection: {
|
|
_id: 1,
|
|
name: 1,
|
|
},
|
|
sort: {
|
|
name: 1,
|
|
},
|
|
}).toArray();
|
|
return {
|
|
items,
|
|
};
|
|
}
|
|
|
|
export async function findDepartmentAgents({
|
|
departmentId,
|
|
pagination: { offset, count, sort },
|
|
}: FindDepartmentAgentsParams): Promise<PaginatedResult<{ agents: ILivechatDepartmentAgents[] }>> {
|
|
const { cursor, totalCount } = LivechatDepartmentAgents.findAgentsByDepartmentId<ILivechatDepartmentAgents>(departmentId, {
|
|
sort: sort || { username: 1 },
|
|
skip: offset,
|
|
limit: count,
|
|
});
|
|
|
|
const [agents, total] = await Promise.all([cursor.toArray(), totalCount]);
|
|
|
|
return {
|
|
agents,
|
|
count: agents.length,
|
|
offset,
|
|
total,
|
|
};
|
|
}
|
|
|
|
export async function findDepartmentsBetweenIds({
|
|
ids,
|
|
fields,
|
|
}: {
|
|
ids: string[];
|
|
fields: Record<string, unknown>;
|
|
}): Promise<{ departments: ILivechatDepartment[] }> {
|
|
const departments = await LivechatDepartment.findInIds(ids, { projection: fields }).toArray();
|
|
return { departments };
|
|
}
|