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-09-06 12:35:57 +02:00
issueType : "Résistant.e sans date d'Intégration" ,
2024-06-02 14:34:11 +02:00
} ) ;
}
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-09-06 12:35:57 +02:00
issueType : "Résistant.e avec Date de Sortie" ,
2024-06-02 14:34:11 +02:00
} ) ;
}
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-09-06 12:35:57 +02:00
issueType : "Ex résistant.e.s sans date Intégration" ,
2024-06-02 14:34:11 +02:00
} ) ;
}
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-09-06 12:35:57 +02:00
issueType : "Ex résistant.e.s sans date Sortie" ,
2024-06-02 14:34:11 +02:00
} ) ;
}
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-09-06 12:35:57 +02:00
issueType : "Date Intégration > date Sortie " ,
2024-06-02 14:34:11 +02:00
} ) ;
}
}
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 &&
2024-09-06 12:35:57 +02:00
family . ContexteEntree !== "Après mise en demeure" &&
family . ContexteEntree !== "Après poursuite procureur"
2024-07-31 14:41:22 +02:00
) {
consistencyWarnings . push ( {
familyId : family.Titre ,
2024-09-06 12:35:57 +02:00
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 ` ,
2024-07-31 14:41:22 +02:00
} ) ;
}
}
2024-09-06 12:35:57 +02:00
consistencyWarnings . push (
2024-06-05 21:35:55 +02:00
. . . family . Evenements . filter ( ( e ) = > ! isValidEvenementFamille ( e . Type ) ) . map (
( e ) = > ( {
familyId : family.Titre ,
2024-09-06 12:35:57 +02:00
issueType : ` Evenement ${ e . notionId } a un Type non géré: " ${ e . Type } " ` ,
2024-06-06 21:48:10 +02:00
} )
)
) ;
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 ,
2024-09-06 12:35:57 +02:00
issueType : ` Evenement ${ e . notionId } avec Type " ${ e . Type } " n'a pas de 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
}