Files
leadboard/contractor-store.js
走狗andClaude Opus 5.5 f2d6fa5dc6
CI / build (push) Successful in 2m2s
feat: persist regular contractors on the board
Contractors are not in HR. Store them in COST_MODEL_DATA_DIR
(contractors.json, never in Git) behind a PUT /api/cost-model/contractors
endpoint with server-side normalization, and save automatically from
the board whenever the contractor list changes. Production no longer
shows the synthetic seed contractors.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
2026-10-21 09:05:29 +00:00

52 lines
1.9 KiB
JavaScript

// Regular contractors are not in HR. They are maintained by hand on the board and
// persisted next to the HR overrides (COST_MODEL_DATA_DIR), never in Git.
export const CONTRACTOR_ROLE = 'O - 外包';
export const MAX_CONTRACTORS = 500;
function text(value, max = 80) {
return typeof value === 'string' ? value.trim().slice(0, max) : '';
}
function month(value, minimum, maximum) {
const number = Number(value);
return Number.isInteger(number) && number >= minimum && number <= maximum ? number : '';
}
export function normalizeContractor(input = {}, index = 0) {
const name = text(input.name);
if (!name) return null;
const dayrate = Number(input.dayrate);
const workRate = Number(input.workRateUsed);
const row = /^[a-zA-Z0-9_-]{1,40}$/.test(String(input.row ?? '')) ? String(input.row) : `contractor-${index + 1}`;
return {
row,
name,
title: text(input.title),
company: text(input.company) || '外包',
dept: text(input.dept),
group: text(input.group),
role: CONTRACTOR_ROLE,
isManager: false,
salary: '',
dayrate: Number.isFinite(dayrate) && dayrate >= 0 ? String(Math.round(dayrate)) : '0',
h1Start: month(input.h1Start, 1, 6),
h1End: month(input.h1End, 1, 6),
h2Start: month(input.h2Start, 7, 12),
h2End: month(input.h2End, 7, 12),
workRateUsed: Number.isFinite(workRate) && workRate > 0 && workRate <= 10 ? workRate : 1.5,
source: 'contractor-store',
};
}
export function normalizeContractors(list) {
if (!Array.isArray(list)) throw new TypeError('contractors must be an array');
if (list.length > MAX_CONTRACTORS) throw new RangeError('too many contractors');
const seen = new Set();
return list.map((item, index) => normalizeContractor(item, index)).filter(Boolean).map((item, index) => {
// Rows must be unique so the board can address each record.
if (seen.has(item.row)) item.row = `contractor-${index + 1}-${seen.size}`;
seen.add(item.row);
return item;
});
}