Skip to content
Merged
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
91 changes: 28 additions & 63 deletions src/scores/scores.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,64 +230,35 @@ export class ScoresService {
}

async getUserScores(id: string): Promise<GetUsersScoresResponseDto> {
const cohorts = (
const [cohorts, attendances, gdScores, exerciseScores] =
await Promise.all([
this.cohortRepository.find({
where: {
users: { id: id },
hasExercises: true,
weeks: {
attendances: {
user: { id: id },
},
groupDiscussionScores: {
user: { id: id },
},
exerciseScores: {
user: { id: id },
},
},
},
relations: {
weeks: {
attendances: {
user: true,
},
groupDiscussionScores: {
user: true,
},
exerciseScores: {
user: true,
},
},
},
where: { users: { id: id } },
relations: { weeks: true },
}),
this.cohortRepository.find({
where: {
users: { id: id },
hasExercises: false,
weeks: {
attendances: {
user: { id: id },
},
groupDiscussionScores: {
user: { id: id },
},
},
},
relations: {
weeks: {
attendances: {
user: true,
},
groupDiscussionScores: {
user: true,
},
},
},
this.attendanceRepository.find({
where: { user: { id: id } },
relations: { cohortWeek: true },
}),
this.groupDiscussionScoreRepository.find({
where: { user: { id: id } },
relations: { cohortWeek: true },
}),
this.exerciseScoreRepository.find({
where: { user: { id: id } },
relations: { cohortWeek: true },
}),
])
).flat();
]);

const attendanceByWeekId = new Map(
attendances.map((a) => [a.cohortWeek.id, a]),
);
const gdScoreByWeekId = new Map(
gdScores.map((s) => [s.cohortWeek.id, s]),
);
const exerciseScoreByWeekId = new Map(
exerciseScores.map((s) => [s.cohortWeek.id, s]),
);

const cohortScore: GetCohortScoresResponseDto[] = [];
let totalScore = 0;
Expand All @@ -299,17 +270,11 @@ export class ScoresService {
let cohortMaxTotalScore = 0;

for (const week of cohort.weeks) {
const attendance = week.attendances?.find(
(a) => a.user.id === id,
);
const attendance = attendanceByWeekId.get(week.id);
const groupDiscussionScore =
week.groupDiscussionScores?.find(
(score) => score.user.id === id,
) ?? null;
gdScoreByWeekId.get(week.id) ?? null;
const exerciseScore =
week.exerciseScores?.find(
(score) => score.user.id === id,
) ?? null;
exerciseScoreByWeekId.get(week.id) ?? null;

if (attendance) {
const weeklyScore = WeeklyScore.fromScores(
Expand Down
Loading