Files
leadboard/english-names.js
2026-11-17 04:09:22 +00:00

53 lines
2.5 KiB
JavaScript

// 英文名對照(English 介面顯示「中文+英文」用)。
// 存在 COST_MODEL_DATA_DIR/english-names.json(不進 Git)。HR 之後補齊英文名時,
// 這份對照表只留給 HR 沒有的人(例如常態型外包)與指定的寫法(例如 Woody Yeh)。
// - byEmployee:工號 → 英文名(HR 在職人員)
// - byName:中文姓名 → 英文名(常態型外包等不在 HR 的人)
const MAX = 3000;
const clean = (value) => (typeof value === 'string' ? value.replace(/\s+/g, ' ').trim().slice(0, 80) : '');
export function normalizeEnglishNames(input = {}) {
if (!input || typeof input !== 'object' || Array.isArray(input)) throw new TypeError('names must be an object');
const section = (value, keyOk) => {
if (value === undefined) return {};
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('names section must be an object');
const output = {};
for (const [rawKey, rawValue] of Object.entries(value).slice(0, MAX)) {
const key = clean(rawKey);
const english = clean(rawValue);
if (keyOk(key) && /[A-Za-z]/.test(english)) output[key] = english;
}
return output;
};
return {
byEmployee: section(input.byEmployee, key => /^\d{1,12}$/.test(key)),
byName: section(input.byName, key => key.length <= 20 && /[㐀-鿿]/.test(key)),
};
}
// 目前名單上的人 → { 中文姓名: 英文名 }。工號對照優先,其次姓名對照;離職的人不在名單上就不會出現。
export function englishNameMap(people, store) {
const byEmployee = store?.byEmployee || {};
const byName = store?.byName || {};
const names = {};
for (const person of people || []) {
const name = clean(person?.name);
if (!name) continue;
const english = byEmployee[String(person.employeeNumber || '').trim()] || byName[name];
if (english) names[name] = english;
}
return names;
}
// 只回傳這個人看得到的人:最高管理者、全公司檢視看全部;部主管看自己部門;組主管看自己的組。
export function visiblePeople(people, permission) {
if (!permission || permission.denied) return [];
if (permission.admin || permission.company) return people || [];
const depts = new Set(permission.depts || []);
const groups = new Set(permission.groups || []);
return (people || []).filter(p => {
const dept = String(p?.dept || '').trim();
return depts.has(dept) || groups.has(`${dept}|${p?.group || '未分組'}`);
});
}