Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions client/src/components/cohorts/CohortAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const CohortAccordion = ({ cohortInfo }: CohortAccordionProps) => {
<TableCell sx={headerStyle} width={50}>
Work Permit
</TableCell>
<TableCell sx={headerStyle} width={100}>
Avg Score
</TableCell>
<TableCell sx={headerStyle} width={50}>
Strikes
</TableCell>
Expand Down Expand Up @@ -83,6 +86,9 @@ export const CohortAccordion = ({ cohortInfo }: CohortAccordionProps) => {
</TableCell>
<TableCell>{trainee.location}</TableCell>
<TableCell>{convertToString(trainee.hasWorkPermit)}</TableCell>
<TableCell sx={{ color: getScoreColor(trainee.averageTestScore) }}>
{trainee.averageTestScore !== null ? trainee.averageTestScore.toFixed(1) : '-'}
</TableCell>
<TableCell>{trainee.strikes}</TableCell>
<TableCell sx={{ whiteSpace: 'nowrap' }} onClick={(e) => e.stopPropagation()}>
<div>
Expand Down Expand Up @@ -128,3 +134,16 @@ const convertToString = (value: boolean | null | undefined) => {
}
return value ? 'Yes' : 'No';
};

const getScoreColor = (score: number | null) => {
if (score === null) {
return 'inherit';
}
if (score < 7) {
return 'orange';
}
if (score >= 8.5) {
return 'green';
}
return 'inherit';
};
1 change: 1 addition & 0 deletions client/src/models/Cohorts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface TraineeSummary {
LearningStatus: LearningStatus;
JobPath: string;
strikes: number;
averageTestScore: number | null;
}
8 changes: 5 additions & 3 deletions server/src/controllers/CohortsController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import { TraineesRepository } from '../repositories';
import { LearningStatus, Trainee } from '../models';
import { LearningStatus, calculateAverageTestScore, Trainee } from '../models';

interface Cohort {
cohort: number | null;
Expand All @@ -21,6 +21,7 @@ interface TraineeSummary {
LearningStatus: string;
JobPath: string;
strikes: number;
averageTestScore: number | null;
}

export interface CohortsControllerType {
Expand All @@ -47,14 +48,14 @@ export class CohortsController implements CohortsControllerType {

// Sort trainees in each group
Object.values(cohortDictionary).forEach((trainees) => {
trainees?.sort(this.compareTraineeInCohort);
trainees?.sort(this.compareTraineeInCohort.bind(this));
});
// Convert dictionary to array of cohorts
const result: Cohort[] = Object.entries(cohortDictionary).map(([cohortNumber, trainees]) => {
const cohortNumberInt = Number.parseInt(cohortNumber);
return {
cohort: isNaN(cohortNumberInt) ? null : cohortNumberInt,
trainees: (trainees ?? []).map(this.getTraineeSummary),
trainees: (trainees ?? []).map(this.getTraineeSummary.bind(this)),
};
});
res.status(200).json(result);
Expand All @@ -75,6 +76,7 @@ export class CohortsController implements CohortsControllerType {
LearningStatus: trainee.educationInfo.learningStatus,
JobPath: trainee.employmentInfo.jobPath,
strikes: trainee.educationInfo.strikes.length,
averageTestScore: calculateAverageTestScore(trainee.educationInfo.tests),
};
}

Expand Down
32 changes: 31 additions & 1 deletion server/src/models/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Test {
readonly id: string;
date: Date;
type: TestType;
score?: number;
score: number | null;
result: TestResult;
comments?: string;
}
Expand All @@ -20,3 +20,33 @@ export const validateTest = (test: Test): void => {
throw new Error(`Unknown test type [${Object.values(TestType)}]`);
}
};

// Calculate average of all test scores, taking only the highest score for each test type
export const calculateAverageTestScore = (tests: Test[]): number | null => {
// Group by test type
const testsByType = Object.groupBy(tests, (test) => test.type);

// Select highest test for each type
const scores = Object.values(testsByType)
.map((testGroup) => {
return getTestWithMaxScore(testGroup)?.score;
})
.filter((score) => score !== null && score !== undefined);

// No scores, no average
if (scores.length === 0) {
return null;
}

// Calculate average
const sum = scores.reduce((acc, score) => acc + score, 0);
return sum / scores.length;
};

// Helper function to get the best test (highest score) from a list of tests
const getTestWithMaxScore = (tests: Test[]): Test | null => {
const sortedByScore = tests
.filter((test: Test) => Number.isFinite(test.score))
.sort((a, b) => (b.score ?? 0) - (a.score ?? 0));
return sortedByScore[0] ?? null;
};
2 changes: 2 additions & 0 deletions server/src/repositories/TraineesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export class MongooseTraineesRepository implements TraineesRepository {
'personalInfo.hasWorkPermit',
'educationInfo.learningStatus',
'educationInfo.strikes.id',
'educationInfo.tests.score',
'educationInfo.tests.type',
'educationInfo.currentCohort',
'employmentInfo.jobPath',
])
Expand Down