feat: filter org-tree-with-users to deepest 2 levels
This commit is contained in:
@@ -514,21 +514,37 @@ export class DingTalkService {
|
||||
throw new ServiceUnavailableException('钉钉未配置');
|
||||
}
|
||||
const token = await this.getAccessToken();
|
||||
const deptIds = await this.getAllDeptIds(token, rootDeptId);
|
||||
|
||||
// Compute global depth map (always from rootDeptId=1)
|
||||
const depthMap = await this.getDeptDepthMap(token);
|
||||
const maxDepth = Math.max(...depthMap.values());
|
||||
|
||||
// Get subtree IDs for the user's selected root
|
||||
const subtreeIds = await this.getAllDeptIds(token, rootDeptId);
|
||||
|
||||
// Filter: only keep departments at the deepest 2 levels of the global tree
|
||||
const filteredIds = subtreeIds.filter((id) => {
|
||||
const d = depthMap.get(id) ?? -1;
|
||||
return d === maxDepth || d === maxDepth - 1;
|
||||
});
|
||||
|
||||
if (filteredIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 拉每个部门详情
|
||||
const nodes: DingOrgTreeNodeWithUsers[] = [];
|
||||
// Before dedup: collect deptIds per user
|
||||
const userDeptMap = new Map<string, number[]>();
|
||||
|
||||
for (let i = 0; i < deptIds.length; i++) {
|
||||
for (let i = 0; i < filteredIds.length; i++) {
|
||||
if (i > 0) await this.delay(i);
|
||||
const detail = await this.getDeptDetail(token, deptIds[i]);
|
||||
const detail = await this.getDeptDetail(token, filteredIds[i]);
|
||||
if (!detail) continue;
|
||||
|
||||
// 拉该部门下的用户
|
||||
const dingUsers = await this.getDeptUsers(token, deptIds[i]);
|
||||
this.logger.log(`[dingtalk] dept ${deptIds[i]} (${detail.name}): ${dingUsers.length} users`);
|
||||
const dingUsers = await this.getDeptUsers(token, filteredIds[i]);
|
||||
this.logger.log(`[dingtalk] dept ${filteredIds[i]} (${detail.name}): ${dingUsers.length} users`);
|
||||
|
||||
nodes.push({
|
||||
id: detail.dept_id,
|
||||
@@ -567,13 +583,13 @@ export class DingTalkService {
|
||||
}));
|
||||
}
|
||||
|
||||
// 组装成树
|
||||
// 组装成树(父节点可能已被过滤,缺失的父节点 → 节点提升为根)
|
||||
const map = new Map<number, DingOrgTreeNodeWithUsers>();
|
||||
nodes.forEach((n) => map.set(n.id, n));
|
||||
const roots: DingOrgTreeNodeWithUsers[] = [];
|
||||
for (const node of nodes) {
|
||||
const parent = map.get(node.parentId);
|
||||
if (parent && node.id !== rootDeptId) {
|
||||
if (parent) {
|
||||
parent.children.push(node);
|
||||
} else {
|
||||
roots.push(node);
|
||||
|
||||
Reference in New Issue
Block a user