statistiques/src/data/checkDataConsistency.ts

101 lines
3 KiB
TypeScript
Raw Normal View History

2024-06-05 21:35:55 +02:00
import { isValidEvenementFamille } from "./EvenementFamille";
import { Famille, isExResistant, isResistant } from "./Famille";
2024-06-02 14:34:11 +02:00
export function checkDataConsistency(families: Famille[]): ConsistencyReport {
const reports = families.map((family) => {
2024-06-02 14:34:11 +02:00
return checkFamilyDataConsistency(family);
});
return {
errors: reports.flatMap((r) => r.errors),
warnings: reports.flatMap((r) => r.warnings),
};
2024-06-02 14:34:11 +02:00
}
export type ConsistencyReport = {
warnings: ConsistencyIssue[];
errors: ConsistencyIssue[];
};
2024-06-02 14:34:11 +02:00
export type ConsistencyIssue = {
issueType: string;
familyId: string;
};
function checkFamilyDataConsistency(family: Famille): ConsistencyReport {
const consistencyErrors: ConsistencyIssue[] = [];
const consistencyWarnings: ConsistencyIssue[] = [];
2024-06-02 14:34:11 +02:00
if (family.Statut === "Résistant.e") {
if (family.Integration === null) {
consistencyErrors.push({
familyId: family.Titre,
issueType: "Résistant.e sans date d'Intégration",
2024-06-02 14:34:11 +02:00
});
}
if (family.Sortie !== null) {
consistencyErrors.push({
familyId: family.Titre,
issueType: "Résistant.e avec Date de Sortie",
2024-06-02 14:34:11 +02:00
});
}
} 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",
2024-06-02 14:34:11 +02:00
});
}
if (family.Sortie === null) {
consistencyErrors.push({
familyId: family.Titre,
issueType: "Ex résistant.e.s sans date Sortie",
2024-06-02 14:34:11 +02:00
});
}
if (family.Integration! > family.Sortie!) {
consistencyErrors.push({
familyId: family.Titre,
issueType: "Date Intégration > date Sortie ",
2024-06-02 14:34:11 +02:00
});
}
}
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(
2024-06-05 21:35:55 +02:00
...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`,
2024-06-05 21:35:55 +02:00
})
)
);
return {
errors: consistencyErrors,
warnings: consistencyWarnings,
};
2024-06-02 14:34:11 +02:00
}