@@ -56,69 +56,70 @@ export class WardEventD2Repository implements WardEventRepository {
5656 orgUnit : countryOU . orgUnitId ,
5757 ouMode : "SELECTED" ,
5858 } )
59- )
60- . flatMap ( ( { instances } ) => {
61- const events = _c ( instances )
62- . compactMap ( instance => {
63- const getDataValue = ( dataElementId : string ) =>
64- instance . dataValues . find ( dv => dv . dataElement === dataElementId ) ?. value ;
65- const rootSurveyName = getDataValue ( PREVALENCE_SURVEY_NAME_DATAELEMENT_ID ) ;
66- const startDate = getDataValue ( PREVALENCE_START_DATE_DATAELEMENT_ID ) ;
67-
68- if ( ! rootSurveyName || ! startDate ) {
69- console . warn (
70- `Missing root survey name or start date for survey with id ${ instance . event } `
71- ) ;
72- return undefined ;
73- }
74-
75- return {
76- rootSurveyId : instance . event ,
77- rootSurveyName : rootSurveyName ,
78- startDate : new Date ( startDate ) ,
79- } ;
80- } )
81- . value ( ) ;
82-
83- return Future . parallel (
84- events . map ( countryEvent =>
85- this . getFacilityEvents ( facility . orgUnitId , countryEvent )
86- ) ,
87- { concurrency : 5 }
88- ) ;
89- } )
90- . flatMap ( facilityEvents =>
91- Future . success (
92- facilityEvents . filter ( facilityEvent => facilityEvent . events . length > 0 )
93- )
94- ) ;
59+ ) . flatMap ( ( { instances } ) => {
60+ const events = _c ( instances )
61+ . compactMap ( instance => {
62+ const getDataValue = ( dataElementId : string ) =>
63+ instance . dataValues . find ( dv => dv . dataElement === dataElementId ) ?. value ;
64+ const rootSurveyName = getDataValue ( PREVALENCE_SURVEY_NAME_DATAELEMENT_ID ) ;
65+ const startDate = getDataValue ( PREVALENCE_START_DATE_DATAELEMENT_ID ) ;
66+
67+ if ( ! rootSurveyName || ! startDate ) {
68+ console . warn (
69+ `Missing root survey name or start date for survey with id ${ instance . event } `
70+ ) ;
71+ return undefined ;
72+ }
73+
74+ return {
75+ rootSurveyId : instance . event ,
76+ rootSurveyName : rootSurveyName ,
77+ startDate : new Date ( startDate ) ,
78+ } ;
79+ } )
80+ . value ( ) ;
81+
82+ return this . getFacilityEvents ( facility . orgUnitId , events ) ;
83+ } ) ;
9584 }
9685
9786 private getFacilityEvents (
9887 facilityId : Id ,
99- countryEvent : { rootSurveyId : string ; rootSurveyName : string ; startDate : Date }
88+ events : { rootSurveyId : string ; rootSurveyName : string ; startDate : Date } [ ]
10089 ) {
90+ const rootSurveyIds = events . map ( e => e . rootSurveyId ) . join ( ";" ) ;
10191 return apiToFuture (
10292 this . api . tracker . trackedEntities . get ( {
10393 fields : trackedEntityFields ,
10494 program : PREVALENCE_FACILITY_LEVEL_FORM_ID ,
10595 orgUnit : facilityId ,
10696 ouMode : "DESCENDANTS" ,
107- filter : `${ SURVEY_ID_FACILITY_LEVEL_DATAELEMENT_ID } :eq :${ countryEvent . rootSurveyId } ` ,
97+ filter : `${ SURVEY_ID_FACILITY_LEVEL_DATAELEMENT_ID } :in :${ rootSurveyIds } ` ,
10898 } )
10999 ) . flatMap ( ( { instances } ) =>
110- Future . success ( {
111- ...countryEvent ,
112- events : instances
113- . flatMap ( instance =>
114- instance . enrollments . flatMap ( enrollment => enrollment . events )
115- )
116- . filter (
117- event =>
118- event . programStage === WARD_DATA_PROGRAM_STAGE_ID &&
119- event . dataValues . length > 0
120- ) ,
121- } )
100+ Future . success (
101+ events
102+ . map ( event => ( {
103+ ...event ,
104+ events : instances . flatMap ( instance => {
105+ const matchesRootSurveyId =
106+ instance . attributes . find (
107+ attr =>
108+ attr . attribute === SURVEY_ID_FACILITY_LEVEL_DATAELEMENT_ID
109+ ) ?. value === event . rootSurveyId ;
110+ if ( ! matchesRootSurveyId ) return [ ] ;
111+
112+ return instance . enrollments
113+ . flatMap ( enrollment => enrollment . events )
114+ . filter (
115+ event =>
116+ event . programStage === WARD_DATA_PROGRAM_STAGE_ID &&
117+ event . dataValues . length > 0
118+ ) ;
119+ } ) ,
120+ } ) )
121+ . filter ( facilityEvent => facilityEvent . events . length > 0 )
122+ )
122123 ) ;
123124 }
124125
0 commit comments