2024-06-05 21:35:55 +02:00
|
|
|
import { isValidEvenementFamille } from "./EvenementFamille";
|
2024-07-31 14:41:22 +02:00
|
|
|
import { Famille, isExResistant, isResistant } from "./Famille";
|
2024-06-02 14:34:11 +02:00
|
|
|
|
2024-07-31 14:41:22 +02:00
|
|
|
export function checkDataConsistency(families: Famille[]): ConsistencyReport {
|
|
|
|
|
const reports = families.map((family) => {
|
2024-06-02 14:34:11 +02:00
|
|
|
return checkFamilyDataConsistency(family);
|
|
|
|
|
});
|
2024-07-31 14:41:22 +02:00
|
|
|
return {
|
|
|
|
|
errors: reports.flatMap((r) => r.errors),
|
|
|
|
|
warnings: reports.flatMap((r) => r.warnings),
|
|
|
|
|
};
|
2024-06-02 14:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-31 14:41:22 +02:00
|
|
|
export type ConsistencyReport = {
|
|
|
|
|
warnings: ConsistencyIssue[];
|
|
|
|
|
errors: ConsistencyIssue[];
|
|
|
|
|
};
|
2024-06-02 14:34:11 +02:00
|
|
|
export type ConsistencyIssue = {
|
|
|
|
|
issueType: string;
|
|
|
|
|
familyId: string;
|
|
|
|
|
};
|
2024-07-31 14:41:22 +02:00
|
|
|
function checkFamilyDataConsistency(family: Famille): ConsistencyReport {
|
|
|
|
|
const consistencyErrors: ConsistencyIssue[] = [];
|
|
|
|
|
const consistencyWarnings: ConsistencyIssue[] = [];
|
2024-06-02 14:34:11 +02:00
|
|
|
|
2024-06-02 21:00:42 +02:00
|
|
|
if (family.Statut === "Résistant.e") {
|
|
|
|
|
if (family.Integration === null) {
|
2024-07-31 14:41:22 +02:00
|
|
|
consistencyErrors.push({
|
2024-06-02 21:00:42 +02:00
|
|
|
familyId: family.Titre,
|
2024-06-02 14:34:11 +02:00
|
|
|
issueType: "Résistant.e without startResistant",
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-06-02 21:00:42 +02:00
|
|
|
if (family.Sortie !== null) {
|
2024-07-31 14:41:22 +02:00
|
|
|
consistencyErrors.push({
|
2024-06-02 21:00:42 +02:00
|
|
|
familyId: family.Titre,
|
2024-06-02 14:34:11 +02:00
|
|
|
issueType: "Résistant.e with endResistant!!",
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-06-02 21:00:42 +02:00
|
|
|
} else if (family.Statut === "Ex résistant·e·s") {
|
|
|
|
|
if (family.Integration === null) {
|
2024-07-31 14:41:22 +02:00
|
|
|
consistencyErrors.push({
|
2024-06-02 21:00:42 +02:00
|
|
|
familyId: family.Titre,
|
2024-06-02 14:34:11 +02:00
|
|
|
issueType: "Ex résistant.e.s without startResistant",
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-06-02 21:00:42 +02:00
|
|
|
if (family.Sortie === null) {
|
2024-07-31 14:41:22 +02:00
|
|
|
consistencyErrors.push({
|
2024-06-02 21:00:42 +02:00
|
|
|
familyId: family.Titre,
|
2024-06-02 14:34:11 +02:00
|
|
|
issueType: "Ex résistant.e.s without endResistant",
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-06-04 21:55:38 +02:00
|
|
|
if (family.Integration! > family.Sortie!) {
|
2024-07-31 14:41:22 +02:00
|
|
|
consistencyErrors.push({
|
2024-06-02 21:00:42 +02:00
|
|
|
familyId: family.Titre,
|
2024-06-02 14:34:11 +02:00
|
|
|
issueType: "startResistsant > endResistant ",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-31 14:41:22 +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"
|
|
|
|
|
) {
|
|
|
|
|
consistencyWarnings.push({
|
|
|
|
|
familyId: family.Titre,
|
|
|
|
|
issueType: `ContextEntree incorrect`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
consistencyErrors.push(
|
2024-06-05 21:35:55 +02:00
|
|
|
...family.Evenements.filter((e) => !isValidEvenementFamille(e.Type)).map(
|
|
|
|
|
(e) => ({
|
|
|
|
|
familyId: family.Titre,
|
2024-06-06 21:48:10 +02:00
|
|
|
issueType: `Event ${e.notionId} has unknown event Type "${e.Type}"`,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
2024-07-31 14:41:22 +02:00
|
|
|
consistencyWarnings.push(
|
2024-06-06 21:48:10 +02:00
|
|
|
...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`,
|
2024-06-05 21:35:55 +02:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
2024-07-31 14:41:22 +02:00
|
|
|
return {
|
|
|
|
|
errors: consistencyErrors,
|
|
|
|
|
warnings: consistencyWarnings,
|
|
|
|
|
};
|
2024-06-02 14:34:11 +02:00
|
|
|
}
|