Files
upstream-mirror 30810eecd7
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
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

226 lines
6.3 KiB
TypeScript

import { faker } from '@faker-js/faker';
import type { Credentials } from '@rocket.chat/api-client';
import type { ILivechatDepartment, IUser, LivechatDepartmentDTO } from '@rocket.chat/core-typings';
import { Random } from '@rocket.chat/random';
import { expect } from 'chai';
import { api, credentials, request } from '../api-data';
import { makeAgentAvailable } from './rooms';
import { createAgentAndReLogin, createAnAwayAgent, createAnOfflineAgent } from './users';
import type { WithRequiredProperty } from './utils';
const NewDepartmentData = ((): Partial<ILivechatDepartment> => ({
enabled: true,
name: `new department ${Random.id()}`,
description: 'created from api',
showOnRegistration: true,
email: faker.internet.email(),
showOnOfflineForm: true,
}))();
export const createDepartment = async (
departmentData?: Partial<ILivechatDepartment>,
agents?: { agentId: string; count?: string; order?: string }[],
): Promise<ILivechatDepartment> => {
const department = { ...NewDepartmentData, ...departmentData };
const response = await request
.post(api('livechat/department'))
.set(credentials)
.send({
department,
...(agents && { agents }),
})
.expect(200);
return response.body.department;
};
const updateDepartment = async (departmentId: string, departmentData: Partial<LivechatDepartmentDTO>): Promise<ILivechatDepartment> => {
const response = await request
.put(api(`livechat/department/${departmentId}`))
.set(credentials)
.send({
department: departmentData,
})
.expect(200);
return response.body.department;
};
export type OnlineAgent = {
user: WithRequiredProperty<IUser, 'username'>;
credentials: Credentials;
};
export const createDepartmentWithAnOnlineAgent = async (): Promise<{ department: ILivechatDepartment; agent: OnlineAgent }> => {
const { user, credentials: agentCredentials } = await createAgentAndReLogin();
const department = await createDepartment();
await addOrRemoveAgentFromDepartment(department._id, { agentId: user._id, username: user.username }, true);
await makeAgentAvailable(agentCredentials);
return {
department,
agent: {
credentials: agentCredentials,
user,
},
};
};
export const createDepartmentWith2OnlineAgents = async (): Promise<{
department: ILivechatDepartment;
agent1: OnlineAgent;
agent2: OnlineAgent;
}> => {
const [agent1, agent2] = await Promise.all([createAgentAndReLogin(), createAgentAndReLogin()]);
const department = await createDepartment(
{
enabled: true,
name: Random.id(),
showOnRegistration: true,
email: `${Random.id()}@example.com`,
showOnOfflineForm: true,
},
[{ agentId: agent1.user._id }, { agentId: agent2.user._id }],
);
await Promise.all([makeAgentAvailable(agent1.credentials), makeAgentAvailable(agent2.credentials)]);
return {
department,
agent1,
agent2,
};
};
export const createDepartmentWithAgent = async (agent: OnlineAgent): Promise<{ department: ILivechatDepartment; agent: OnlineAgent }> => {
const { user, credentials } = agent;
const department = await createDepartment();
await addOrRemoveAgentFromDepartment(department._id, { agentId: user._id, username: user.username }, true);
return {
department,
agent: {
credentials,
user,
},
};
};
export const addOrRemoveAgentFromDepartment = async (
departmentId: string,
agent: { agentId: string; username: string; count?: number; order?: number },
add: boolean,
) => {
const response = await request
.post(api(`livechat/department/${departmentId}/agents`))
.set(credentials)
.send({
...(add ? { upsert: [agent], remove: [] } : { remove: [agent], upsert: [] }),
});
if (response.status !== 200) {
throw new Error(`Failed to add or remove agent from department. Status code: ${response.status}\n${response.body}`);
}
};
export const createDepartmentWithAnOfflineAgent = async ({
allowReceiveForwardOffline = false,
fallbackForwardDepartment,
departmentsAllowedToForward,
}: {
allowReceiveForwardOffline?: boolean;
fallbackForwardDepartment?: string;
departmentsAllowedToForward?: string[];
}): Promise<{
department: ILivechatDepartment;
agent: {
credentials: Credentials;
user: WithRequiredProperty<IUser, 'username'>;
};
}> => {
const { user, credentials } = await createAnOfflineAgent();
const department = (await createDepartment({
allowReceiveForwardOffline,
fallbackForwardDepartment,
departmentsAllowedToForward,
})) as ILivechatDepartment;
await addOrRemoveAgentFromDepartment(department._id, { agentId: user._id, username: user.username }, true);
return {
department,
agent: {
credentials,
user,
},
};
};
export const createDepartmentWithAnAwayAgent = async ({
allowReceiveForwardOffline = false,
fallbackForwardDepartment,
departmentsAllowedToForward,
}: {
allowReceiveForwardOffline?: boolean;
fallbackForwardDepartment?: string;
departmentsAllowedToForward?: string[];
}): Promise<{
department: ILivechatDepartment;
agent: {
credentials: Credentials;
user: WithRequiredProperty<IUser, 'username'>;
};
}> => {
const { user, credentials } = await createAnAwayAgent();
const department = (await createDepartment({
allowReceiveForwardOffline,
fallbackForwardDepartment,
departmentsAllowedToForward,
})) as ILivechatDepartment;
await addOrRemoveAgentFromDepartment(department._id, { agentId: user._id, username: user.username }, true);
return {
department,
agent: {
credentials,
user,
},
};
};
export const archiveDepartment = async (departmentId: string): Promise<void> => {
await request
.post(api(`livechat/department/${departmentId}/archive`))
.set(credentials)
.expect(200);
};
export const disableDepartment = async (department: Omit<ILivechatDepartment, '_updatedAt'>): Promise<void> => {
department.enabled = false;
delete department._updatedAt;
const updatedDepartment = await updateDepartment(department._id, department);
expect(updatedDepartment.enabled).to.be.false;
};
export const deleteDepartment = async (departmentId: string): Promise<void> => {
await request
.delete(api(`livechat/department/${departmentId}`))
.set(credentials)
.expect(200);
};
export const getDepartmentById = async (departmentId: string): Promise<ILivechatDepartment> => {
const response = await request
.get(api(`livechat/department/${departmentId}`))
.set(credentials)
.expect(200);
return response.body.department;
};