@@ -80,9 +80,21 @@ export class CohortsController implements CohortsControllerType {
8080 } ;
8181 }
8282
83+ // Calculate average of all test scores, taking only the highest score for each test type
8384 private calculateAverageTestScore ( tests : Test [ ] ) : number | null {
85+ // Group by test type
86+ const testTypes = Object . groupBy ( tests , ( test ) => test . type ) ;
87+
88+ // Select highest test for each type
89+ const scores = Object . values ( testTypes )
90+ . map ( ( testsWithTheSameType ) => {
91+ return this . getTestWithMaxScore ( testsWithTheSameType ) ;
92+ } )
93+ . filter ( ( test ) => test !== null )
94+ . map ( ( test ) => test . score )
95+ . filter ( ( score ) => score !== null ) ;
96+
8497 // Filter out tests without scores
85- const scores = tests . map ( ( test ) => test . score ) . filter ( ( score ) => score !== undefined ) ;
8698 if ( scores . length === 0 ) {
8799 return null ;
88100 }
@@ -101,4 +113,15 @@ export class CohortsController implements CohortsControllerType {
101113 // Primary sort by status, secondary sort by display name
102114 return statusIndexA - statusIndexB || a . displayName . localeCompare ( b . displayName ) ;
103115 }
116+
117+ private getTestWithMaxScore ( tests : Test [ ] ) : Test | null {
118+ const sortedByScore = tests
119+ . filter ( ( test : Test ) => test . score !== null )
120+ . sort ( ( a , b ) => ( b . score ?? 0 ) - ( a . score ?? 0 ) ) ;
121+
122+ if ( sortedByScore . length === 0 ) {
123+ return null ;
124+ }
125+ return sortedByScore [ 0 ] ;
126+ }
104127}
0 commit comments