import { isValidEvenementFamille } from "./EvenementFamille"; import { Famille, isExResistant, isResistant } from "./Famille"; export function checkDataConsistency(families: Famille[]): ConsistencyReport { const reports = families.map((family) => { return checkFamilyDataConsistency(family); }); return { errors: reports.flatMap((r) => r.errors), warnings: reports.flatMap((r) => r.warnings), }; } export type ConsistencyReport = { warnings: ConsistencyIssue[]; errors: ConsistencyIssue[]; }; export type ConsistencyIssue = { issueType: string; familyId: string; }; function checkFamilyDataConsistency(family: Famille): ConsistencyReport { const consistencyErrors: ConsistencyIssue[] = []; const consistencyWarnings: ConsistencyIssue[] = []; if (family.Statut === "Résistant.e") { if (family.Integration === null) { consistencyErrors.push({ familyId: family.Titre, issueType: "Résistant.e without startResistant", }); } if (family.Sortie !== null) { consistencyErrors.push({ familyId: family.Titre, issueType: "Résistant.e with endResistant!!", }); } } else if (family.Statut === "Ex résistant·e·s") { if (family.Integration === null) { consistencyErrors.push({ familyId: family.Titre, issueType: "Ex résistant.e.s without startResistant", }); } if (family.Sortie === null) { consistencyErrors.push({ familyId: family.Titre, issueType: "Ex résistant.e.s without endResistant", }); } if (family.Integration! > family.Sortie!) { consistencyErrors.push({ familyId: family.Titre, issueType: "startResistsant > endResistant ", }); } } if ( (isResistant(family) || isExResistant(family)) && family.Integration !== null ) { const miseEnDemeureBeforeInteg = family.Evenements.find( (e) => e.Type === "Mise en demeure de scolarisation" && (e.Date === null || e.Date < family.Integration!) ) !== undefined; if ( miseEnDemeureBeforeInteg && family.ContexteEntree !== "Après mise en demeure" ) { consistencyWarnings.push({ familyId: family.Titre, issueType: `ContextEntree incorrect`, }); } } consistencyErrors.push( ...family.Evenements.filter((e) => !isValidEvenementFamille(e.Type)).map( (e) => ({ familyId: family.Titre, issueType: `Event ${e.notionId} has unknown event Type "${e.Type}"`, }) ) ); consistencyWarnings.push( ...family.Evenements.filter((e) => e.Type !== null && e.Date === null).map( (e) => ({ familyId: family.Titre, issueType: `Event ${e.notionId} with non null Type "${e.Type}" but null Date`, }) ) ); return { errors: consistencyErrors, warnings: consistencyWarnings, }; }