origin/apps/server/src/models/department/utils.ts

78 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-02-06 16:32:52 +08:00
import {
UserProfile,
db,
DeptSimpleTreeNode,
TreeDataNode,
} from '@nice/common';
2024-12-30 08:26:40 +08:00
/**
* DeptSimpleTreeNode结构
* @param department
* @returns DeptSimpleTreeNode对象
* :
* - id: 部门唯一标识
* - key: 部门唯一标识React中的key属性
* - value: 部门唯一标识
* - title: 部门名称
* - order: 部门排序值
* - pId: 父部门ID
* - isLeaf: 是否为叶子节点
* - hasStaff: 该部门是否包含员工
*/
export function mapToDeptSimpleTree(department: any): DeptSimpleTreeNode {
2025-02-06 16:32:52 +08:00
return {
id: department.id,
key: department.id,
value: department.id,
title: department.name,
order: department.order,
pId: department.parentId,
isLeaf: !Boolean(department.children?.length),
hasStaff: department?.deptStaffs?.length > 0,
};
2024-12-30 08:26:40 +08:00
}
/**
* ID列表获取相关员工信息
* @param ids ID列表
* @returns ID列表
* :
* - 使findManyID列表查询相关部门的员工信息
* - 使flatMap将查询结果扁平化ID
*/
export async function getStaffsByDeptIds(ids: string[]) {
2025-02-06 16:32:52 +08:00
const depts = await db.department.findMany({
where: { id: { in: ids } },
select: {
deptStaffs: {
select: { id: true },
},
},
});
return depts.flatMap((dept) => dept.deptStaffs);
2024-12-30 08:26:40 +08:00
}
/**
* ID列表
* @param params ID列表ID列表和员工信息
* @returns ID列表
* :
* - ID列表获取相关员工ID
* - ID与传入的员工ID列表合并使Set去重
* - ID
* - ID列表
*/
2025-02-06 16:32:52 +08:00
export async function extractUniqueStaffIds(params: {
deptIds?: string[];
staffIds?: string[];
staff?: UserProfile;
}): Promise<string[]> {
const { deptIds, staff, staffIds } = params;
const deptStaffs = await getStaffsByDeptIds(deptIds);
const result = new Set(deptStaffs.map((item) => item.id).concat(staffIds));
if (staff) {
result.delete(staff.id);
}
return Array.from(result);
}