metis-desk/modules/companies/src/api/application/process-review.builder.ts

71 lines
2.2 KiB
TypeScript
Raw Normal View History

import type { ProcessReferenceInput } from "../domain/process-values";
2026-10-10 10:01:58 +00:00
export type ProcessReviewSources = {
assets: readonly { id: string; name: string; personId: string | null; available: boolean }[];
profiles: readonly { id: string; personId: string; name: string; active: boolean }[];
configurations: readonly {
id: string;
personId: string;
kind: ProcessReferenceInput["kind"];
label: string;
state: string;
sourceKind?: ProcessReferenceInput["sourceKind"];
}[];
relationships: readonly {
id: string;
personId: string;
kind: ProcessReferenceInput["kind"];
label: string;
endDate: string | null;
configuration: string | null;
}[];
authorizations: readonly { id: string; personId: string; active: boolean }[];
};
2026-10-10 10:01:58 +00:00
export function buildProcessReview(
personId: string,
sources: ProcessReviewSources,
today: string
): ProcessReferenceInput[] {
const closed = (state: string | null) => state === "removed" || state === "never_configured";
2026-10-10 10:01:58 +00:00
const result: ProcessReferenceInput[] = sources.assets
.filter((a) => a.personId === personId && a.available)
.map((a) => ({ kind: "asset", id: a.id, label: a.name }));
2026-10-10 10:01:58 +00:00
for (const p of sources.profiles) {
if (p.personId === personId && p.active) {
result.push({ kind: "profile", id: p.id, label: p.name });
}
}
for (const c of sources.configurations) {
if (c.personId === personId && !closed(c.state)) {
result.push({
kind: c.kind,
id: c.id,
label: c.label,
...(c.sourceKind ? { sourceKind: c.sourceKind } : {}),
});
}
}
const relevantRelationships = sources.relationships
.filter((r) => r.personId === personId)
.filter(
(r) =>
!r.endDate || r.endDate >= today || Boolean(r.configuration && !closed(r.configuration))
);
2026-10-10 10:01:58 +00:00
result.push(...relevantRelationships.map((r) => ({ kind: r.kind, id: r.id, label: r.label })));
for (const a of sources.authorizations) {
if (a.personId === personId && a.active) {
result.push({ kind: "telework", id: a.id, label: personId });
}
}
2026-10-10 10:01:58 +00:00
return [
...new Map(
result.map((ref) => [`${ref.kind}:${ref.id}`, { ...ref, label: ref.label.slice(0, 500) }])
).values(),
];
}