Визуализатор: справочник ответственных, каноническая длительность, поддержка обеих схем
Модель читает khbuild_responsible (колонки responsible, responsible2/3 = ключ «роль - Имя»); если справочника нет (архив khnata) - прежние responsible_role/responsible_name и khnata_roles. Длительность: 5d - календарные дни, 5wd - рабочие, суффикс ? игнорируется при расчёте и выводится в Ганте и ARIS. Тесты на обе схемы и откат на прежний справочник. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,19 +9,27 @@ export interface ProcessRow {
|
||||
stage: string | null;
|
||||
actionName: string | null;
|
||||
description: string | null;
|
||||
/** ключи справочника ответственных (колонки responsible, responsible2, responsible3), вида «роль - Имя» */
|
||||
responsible: string[];
|
||||
/** роль первого ответственного (для дорожек ARIS); для архива khnata - колонка responsible_role */
|
||||
role: string | null;
|
||||
/** имена ответственных через «; »; для архива khnata - колонка responsible_name */
|
||||
responsibleName: string | null;
|
||||
systemAsIs: string | null;
|
||||
systemToBe: string | null;
|
||||
dataIn: string[];
|
||||
dataOut: string[];
|
||||
dependsOn: string[];
|
||||
/** каноническая длительность: `5d` (календарные дни), `5wd` (рабочие дни), суффикс `?` - оценка (при расчёте игнорируется, но выводится) */
|
||||
duration: string | null;
|
||||
dateConstraint: string | null;
|
||||
questions: string | null;
|
||||
/** признак вехи: колонка is_milestone; если колонки нет или она пустая - вехами считаются чек-поинты CP-* */
|
||||
isMilestone: boolean;
|
||||
}
|
||||
/** Запись справочника ответственных: ключ `id` = «роль - Имя». */
|
||||
export interface ResponsibleRow { id: string; role: string | null; person: string | null; comment: string | null }
|
||||
/** Роль (производная от справочника ответственных): имена всех ответственных этой роли. */
|
||||
export interface RoleRow { id: string; person: string | null; comment: string | null }
|
||||
export interface SystemRow { id: string; name: string | null; comment: string | null }
|
||||
export interface DataRow { id: string; description: string | null; role: string | null; comment: string | null }
|
||||
@@ -29,6 +37,7 @@ export interface DataRow { id: string; description: string | null; role: string
|
||||
export interface ProcessModel {
|
||||
set: ProcessSet;
|
||||
rows: ProcessRow[];
|
||||
responsible: ResponsibleRow[];
|
||||
roles: RoleRow[];
|
||||
systems: SystemRow[];
|
||||
data: DataRow[];
|
||||
@@ -44,7 +53,8 @@ export function mapRow(r: Raw): ProcessRow {
|
||||
stage: str(r.stage),
|
||||
actionName: str(r.action_name),
|
||||
description: str(r.action_description),
|
||||
role: str(r.responsible_role),
|
||||
responsible: list(r.responsible, r.responsible2, r.responsible3),
|
||||
role: str(r.responsible_role), // архив khnata; для актуальной схемы заполняется в buildModel
|
||||
responsibleName: str(r.responsible_name),
|
||||
systemAsIs: str(r.system_as_is),
|
||||
systemToBe: str(r.system_to_be),
|
||||
@@ -58,16 +68,84 @@ export function mapRow(r: Raw): ProcessRow {
|
||||
};
|
||||
}
|
||||
|
||||
/** Читает «звезду» из БД. Имена таблиц берутся только из белого списка PROCESS_SETS. Отсутствующие колонки (в архиве khnata их меньше) дают null. */
|
||||
export interface RawStar {
|
||||
process: Raw[];
|
||||
/** справочник ответственных (khbuild_responsible) либо прежний справочник ролей (khnata_roles) */
|
||||
refs: Raw[];
|
||||
/** true, если refs - прежний справочник ролей (id, role_person, comment) */
|
||||
legacyRoles: boolean;
|
||||
systems: Raw[];
|
||||
data: Raw[];
|
||||
}
|
||||
|
||||
/** Собирает модель из сырых строк. Чистая функция: проверяется тестами без БД. */
|
||||
export function buildModel(set: ProcessSet, raw: RawStar): ProcessModel {
|
||||
const responsible: ResponsibleRow[] = raw.refs.map((r) =>
|
||||
raw.legacyRoles
|
||||
? { id: String(r.id), role: String(r.id), person: str(r.role_person), comment: str(r.comment) }
|
||||
: { id: String(r.id), role: str(r.role), person: str(r.person), comment: str(r.comment) },
|
||||
);
|
||||
const byId = new Map(responsible.map((r) => [r.id, r]));
|
||||
|
||||
const rows = raw.process.map(mapRow).map((row) => {
|
||||
const refs = row.responsible.map((id) => byId.get(id));
|
||||
if (!refs.length) return row;
|
||||
return { ...row, role: refs[0]?.role ?? row.role, responsibleName: refs.map((r, i) => r?.person ?? row.responsible[i]).join("; ") };
|
||||
});
|
||||
|
||||
// Роли: в новой схеме производные (группировка справочника по роли), в архиве - сам справочник
|
||||
const roles: RoleRow[] = [];
|
||||
for (const r of responsible) {
|
||||
const key = r.role ?? r.id;
|
||||
const found = roles.find((x) => x.id === key);
|
||||
const person = r.person ?? null;
|
||||
if (!found) roles.push({ id: key, person, comment: r.comment });
|
||||
else {
|
||||
if (person && !(found.person ?? "").split(", ").includes(person)) found.person = found.person ? `${found.person}, ${person}` : person;
|
||||
found.comment ??= r.comment;
|
||||
}
|
||||
}
|
||||
|
||||
const data: DataRow[] = raw.data.map((r) => {
|
||||
const ref = str(r.responsible) ? byId.get(String(r.responsible)) : undefined;
|
||||
return { id: String(r.id), description: str(r.data_description), role: ref?.role ?? str(r.responsible_role), comment: str(r.comment) };
|
||||
});
|
||||
|
||||
return {
|
||||
set,
|
||||
rows,
|
||||
responsible,
|
||||
roles,
|
||||
systems: raw.systems.map((r) => ({ id: String(r.id), name: str(r.system_name), comment: str(r.comment) })),
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
/** Ответственные строки для подсказок: ключи «роль - Имя»; для архива (колонки роль и имя) - в прежнем виде. */
|
||||
export function describeResponsible(row: ProcessRow, model: ProcessModel): string {
|
||||
if (row.responsible.length) return row.responsible.join("; ");
|
||||
const roleLabel = row.role ? model.roles.find((r) => r.id === row.role)?.person ?? row.role : null;
|
||||
return [roleLabel, row.responsibleName].filter(Boolean).join(" / ") || "не указан";
|
||||
}
|
||||
|
||||
const PG_UNDEFINED_TABLE = "42P01";
|
||||
|
||||
/**
|
||||
* Читает «звезду» из БД. Имена таблиц берутся только из белого списка PROCESS_SETS.
|
||||
* Справочник ответственных: `<набор>_responsible`; если такой таблицы нет (архив khnata), читается прежний `<набор>_roles`.
|
||||
*/
|
||||
export async function loadModel(db: Queryable, set: ProcessSet): Promise<ProcessModel> {
|
||||
if (!PROCESS_SETS.includes(set)) throw new Error(`неизвестный набор: ${set}`);
|
||||
const q = async (table: string): Promise<Raw[]> => (await db.query(`select * from ${set}_${table} order by id`)).rows as Raw[];
|
||||
const [process, roles, systems, data] = await Promise.all([q("process"), q("roles"), q("systems"), q("data_objects")]);
|
||||
return {
|
||||
set,
|
||||
rows: process.map(mapRow),
|
||||
roles: roles.map((r) => ({ id: String(r.id), person: str(r.role_person), comment: str(r.comment) })),
|
||||
systems: systems.map((r) => ({ id: String(r.id), name: str(r.system_name), comment: str(r.comment) })),
|
||||
data: data.map((r) => ({ id: String(r.id), description: str(r.data_description), role: str(r.responsible_role), comment: str(r.comment) })),
|
||||
};
|
||||
const [process, systems, data] = await Promise.all([q("process"), q("systems"), q("data_objects")]);
|
||||
let refs: Raw[];
|
||||
let legacyRoles = false;
|
||||
try {
|
||||
refs = await q("responsible");
|
||||
} catch (e) {
|
||||
if ((e as { code?: string }).code !== PG_UNDEFINED_TABLE) throw e;
|
||||
refs = await q("roles");
|
||||
legacyRoles = true;
|
||||
}
|
||||
return buildModel(set, { process, refs, legacyRoles, systems, data });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user