statistiques/src/data/checkDataConsistency.ts

100 lines
3 KiB
TypeScript

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 sans date d'Intégration",
});
}
if (family.Sortie !== null) {
consistencyErrors.push({
familyId: family.Titre,
issueType: "Résistant.e avec Date de Sortie",
});
}
} else if (family.Statut === "Ex résistant·e·s") {
if (family.Integration === null) {
consistencyErrors.push({
familyId: family.Titre,
issueType: "Ex résistant.e.s sans date Intégration",
});
}
if (family.Sortie === null) {
consistencyErrors.push({
familyId: family.Titre,
issueType: "Ex résistant.e.s sans date Sortie",
});
}
if (family.Integration! > family.Sortie!) {
consistencyErrors.push({
familyId: family.Titre,
issueType: "Date Intégration > date Sortie ",
});
}
}
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" &&
family.ContexteEntree !== "Après poursuite procureur"
) {
consistencyWarnings.push({
familyId: family.Titre,
issueType: `Valeur de ContextEntree incorrecte: Le Context d'Entree est "${family.ContexteEntree}" alors que la date de mise en demeure avant date d'intégration`,
});
}
}
consistencyWarnings.push(
...family.Evenements.filter((e) => !isValidEvenementFamille(e.Type)).map(
(e) => ({
familyId: family.Titre,
issueType: `Evenement ${e.notionId} a un Type non géré: "${e.Type}"`,
})
)
);
consistencyWarnings.push(
...family.Evenements.filter((e) => e.Type !== null && e.Date === null).map(
(e) => ({
familyId: family.Titre,
issueType: `Evenement ${e.notionId} avec Type "${e.Type}" n'a pas de Date`,
})
)
);
return {
errors: consistencyErrors,
warnings: consistencyWarnings,
};
}