Skip to content

Commit 9cbd038

Browse files
committed
Improved average calculation
1 parent 7ee0c25 commit 9cbd038

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

server/src/controllers/CohortsController.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

server/src/models/Test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface Test {
44
readonly id: string;
55
date: Date;
66
type: TestType;
7-
score?: number;
7+
score: number | null;
88
result: TestResult;
99
comments?: string;
1010
}

server/src/repositories/TraineesRepository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class MongooseTraineesRepository implements TraineesRepository {
7171
'educationInfo.learningStatus',
7272
'educationInfo.strikes.id',
7373
'educationInfo.tests.score',
74+
'educationInfo.tests.type',
7475
'educationInfo.currentCohort',
7576
'employmentInfo.jobPath',
7677
])

0 commit comments

Comments
 (0)