Files
2026-11-03 02:47:18 +00:00

44 lines
1.7 KiB
JavaScript

// HR 的主管掛在組織單位上(集團/公司/部/組),一個人可能同時是多個單位的主管,
// 也可能是「次要」主管。兩者都算管理職。
export function supervisorIndex(organizations) {
const index = new Map();
for (const unit of Array.isArray(organizations) ? organizations : []) {
for (const supervisor of Array.isArray(unit?.supervisors) ? unit.supervisors : []) {
const number = supervisor?.EmployeeNumber;
if (!number) continue;
const entry = index.get(String(number)) || [];
entry.push({
deptCode: unit.deptCode,
deptName: unit.deptName,
levelName: unit.levelName,
secondary: supervisor.HierachyRelationshipName === '次要',
});
index.set(String(number), entry);
}
}
return index;
}
// Classify only units explicitly marked as a department or group in HR's tree.
export function splitOrganization(employee, organizations) {
const byCode = new Map(organizations.map(unit => [unit.deptCode, unit]));
let unit = byCode.get(employee.departmentCode);
const seen = new Set();
let departmentName = null;
let groupName = null;
let corporationName = null;
while (unit && !seen.has(unit.deptCode)) {
seen.add(unit.deptCode);
const level = String(unit.levelName || '').trim();
if (!groupName && ['組', '組別'].includes(level)) groupName = unit.deptName;
if (!departmentName && ['部', '部門'].includes(level)) departmentName = unit.deptName;
if (!corporationName && typeof unit.corporationName === 'string' && unit.corporationName.trim()) {
corporationName = unit.corporationName.trim();
}
unit = byCode.get(unit.upperDeptCode);
}
return { departmentName, groupName, corporationName };
}