fix: fetch users from all sub-departments in syncAll and fetchOrgTreeWithUsers

This commit is contained in:
2026-07-10 10:06:35 +08:00
parent 3992f6c33f
commit 3ccb878092

View File

@@ -260,10 +260,22 @@ export class DingTalkService {
const t0 = Date.now(); const t0 = Date.now();
const token = await this.getAccessToken(); const token = await this.getAccessToken();
// 递归收集所有部门 ID
const collectDeptIds = async (deptId: number): Promise<number[]> => {
const ids: number[] = [deptId];
const subs = await this.getSubDepts(token, deptId);
for (const sd of subs) {
ids.push(...(await collectDeptIds(sd.dept_id)));
}
return ids;
};
const allDeptIds = await collectDeptIds(rootDeptId);
let userCount = 0; let userCount = 0;
const seenUserIds = new Set<string>(); const seenUserIds = new Set<string>();
const dingUsers = await this.getDeptUsers(token, rootDeptId); for (const did of allDeptIds) {
const dingUsers = await this.getDeptUsers(token, did);
for (const du of dingUsers) { for (const du of dingUsers) {
if (seenUserIds.has(du.userid)) continue; if (seenUserIds.has(du.userid)) continue;
seenUserIds.add(du.userid); seenUserIds.add(du.userid);
@@ -271,11 +283,12 @@ export class DingTalkService {
await this.syncOneUser(du); await this.syncOneUser(du);
userCount++; userCount++;
} }
}
this.logger.log( this.logger.log(
`钉钉同步完成: ${userCount} 个用户, API 请求 ${this.apiRequestCount} 次, 耗时 ${Date.now() - t0}ms`, `钉钉同步完成: ${userCount} 个用户, ${allDeptIds.length} 个部门, API 请求 ${this.apiRequestCount} 次, 耗时 ${Date.now() - t0}ms`,
); );
return { deptCount: 0, userCount }; return { deptCount: allDeptIds.length, userCount };
} }
// ═══════════════════════════════════════════ // ═══════════════════════════════════════════
@@ -328,29 +341,39 @@ export class DingTalkService {
const rootInfo = await this.getDeptInfo(token, rootDeptId); const rootInfo = await this.getDeptInfo(token, rootDeptId);
if (!rootInfo) return []; if (!rootInfo) return [];
// 从根部门拉全部用户(翻页),按 dept_id_list 分配到各部门 // 1. 先建部门树(复用 buildDeptNode
const allUsers = await this.getDeptUsers(token, rootDeptId); const deptTree = await this.buildDeptNode(token, rootDeptId, rootInfo.name, rootInfo.parent_id);
// 2. 收集所有部门 ID
const allDeptIds: number[] = [];
const collectIds = (node: OrgDeptNode) => {
allDeptIds.push(node.id);
for (const c of node.children) collectIds(c);
};
collectIds(deptTree);
// 3. 从每个部门拉用户,按 dept_id_list 分配
const usersByDept = new Map<number, Array<{ userid: string; name: string; mobile: string; deptIds: number[] }>>(); const usersByDept = new Map<number, Array<{ userid: string; name: string; mobile: string; deptIds: number[] }>>();
for (const u of allUsers) { for (const did of allDeptIds) {
for (const did of u.dept_id_list) { const deptUsers = await this.getDeptUsers(token, did);
if (!usersByDept.has(did)) usersByDept.set(did, []); for (const u of deptUsers) {
usersByDept.get(did)!.push({ userid: u.userid, name: u.name, mobile: u.mobile, deptIds: u.dept_id_list }); for (const udid of u.dept_id_list) {
if (!usersByDept.has(udid)) usersByDept.set(udid, []);
usersByDept.get(udid)!.push({ userid: u.userid, name: u.name, mobile: u.mobile, deptIds: u.dept_id_list });
}
} }
} }
const buildWithUsers = async (deptId: number, name: string, parentId: number): Promise<OrgDeptNodeWithUsers> => { // 4. 递归挂用户到树节点
const subDepts = await this.getSubDepts(token, deptId); const attachUsers = (node: OrgDeptNode): OrgDeptNodeWithUsers => ({
const children = await Promise.all( id: node.id,
subDepts.map(sd => buildWithUsers(sd.dept_id, sd.name, sd.parent_id)), name: node.name,
); parentId: node.parentId,
return { children: node.children.map(attachUsers),
id: deptId, name, parentId, children, users: usersByDept.get(node.id) ?? [],
users: usersByDept.get(deptId) ?? [], });
};
};
const node = await buildWithUsers(rootDeptId, rootInfo.name, rootInfo.parent_id); return [attachUsers(deptTree)];
return [node];
} }
// ═══════════════════════════════════════════ // ═══════════════════════════════════════════