Skip to content

Commit

Permalink
Fix Crashing on NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
Nephelite committed Oct 24, 2024
1 parent 90da3a5 commit 8a3c86f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
18 changes: 16 additions & 2 deletions backend/services/assessmentResultService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,26 @@ export const getOrCreateAssessmentResults = async (
};

export const recalculateResult = async (resultId: string) => {
const result = await AssessmentResultModel.findById(resultId).populate('marks.submission').populate('averageScore');
const result: any = await AssessmentResultModel
.findById(resultId)
.populate('marks.submission.adjustedScore')
.populate('marks.submission.score')
.populate('averageScore');
if (!result) {
throw new NotFoundError('Result not found');
}

if (result.marks.length === 0) {
throw new BadRequestError('No marks to recalculate')
} else {
console.log('Result:', result);
result.marks.forEach((markEntry: any, index: any) => {
console.log(`Mark Entry ${index}:`, markEntry);
});
}

result.averageScore = result.marks.reduce((accumulator: number, markEntry: any) =>
accumulator + (markEntry.submission.adjustedScore ?? markEntry.submission.score), 0)
accumulator + markEntry.score, 0)
/ result.marks.length;
result.save();
}
Expand Down
4 changes: 2 additions & 2 deletions backend/services/submissionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export const createSubmission = async (
score: totalScore,
};
assessmentResult.marks.push(newMarkEntry);
assessmentResult.save();
await assessmentResult.save();

await recalculateResult(assessmentResult.id);
})
Expand Down Expand Up @@ -484,7 +484,7 @@ export const updateSubmission = async (
score: totalScore,
};
assessmentResult.marks.push(newMarkEntry);
assessmentResult.save();
await assessmentResult.save();

await recalculateResult(assessmentResult.id);
});
Expand Down
17 changes: 12 additions & 5 deletions multi-git-dashboard/src/components/cards/AssessmentResultCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,42 @@ interface StudentResult {

interface AssessmentResultCardProps {
studentResult: StudentResult;
maxScore?: number;
}

const AssessmentResultCard: React.FC<AssessmentResultCardProps> = ({
studentResult,
maxScore,
}) => {
const { student, team, result } = studentResult;
const maxScoreString = ` / ${maxScore}`;

return (
<Card withBorder shadow="sm" mb="sm">
<Group justify='space-between'>
<div>
<Group justify='space-between' flex='row'>
<Group>
<Text>{student.name}</Text>
<Text size="sm" c="dimmed">
Student ID: {student.identifier}
</Text>
{team && (
<Text size="sm" color="dimmed">
<Text size="sm" c="dimmed">
Team: {team.number}
</Text>
)}
</div>
</Group>
<Badge key={student._id} color="teal">
Average Score: {result?.averageScore.toPrecision(3)}{maxScoreString}
</Badge>
</Group>

{/* Display marks */}
<Group mt="md">
<Text>Submitted Scores:</Text>
{result && result.marks.length > 0 ? (
result.marks.map((markEntry, index) => (
<Badge key={index} color="teal">
Score: {markEntry.score}
Score: {markEntry.score.toPrecision(3)}{maxScoreString}
</Badge>
))
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface AssessmentInternalResultsProps {
teachingTeam: User[];
assignedTeams?: AssignedTeam[];
assignedUsers?: AssignedUser[];
maxScore?: number;
}

export interface StudentResult {
Expand All @@ -29,6 +30,7 @@ const AssessmentInternalResults: React.FC<AssessmentInternalResultsProps> = ({
teachingTeam,
assignedTeams,
assignedUsers,
maxScore,
}) => {
const [isResultFormOpen, setIsResultFormOpen] = useState<boolean>(false);
const [markerFilter, setMarkerFilter] = useState<string>('All');
Expand Down Expand Up @@ -205,6 +207,7 @@ const AssessmentInternalResults: React.FC<AssessmentInternalResultsProps> = ({
<AssessmentResultCard
key={sr.student._id}
studentResult={sr}
maxScore={maxScore}
/>
))}
</div>
Expand All @@ -215,6 +218,7 @@ const AssessmentInternalResults: React.FC<AssessmentInternalResultsProps> = ({
<AssessmentResultCard
key={sr.student._id}
studentResult={sr}
maxScore={maxScore}
/>
))
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ const InternalAssessmentDetail: React.FC = () => {
results={assessmentResults}
assignedTeams={assignedTeams}
assignedUsers={assignedUsers}
maxScore={assessment.maxMarks}
/>
</Tabs.Panel>
)}
Expand Down

0 comments on commit 8a3c86f

Please sign in to comment.