From edd4c4558c95a67cbc3ee2129815a2c41849f079 Mon Sep 17 00:00:00 2001 From: Anmol Sharma Date: Wed, 8 Apr 2026 21:36:19 +0530 Subject: [PATCH] fix: return correct user scores for weeks with only GD and no exercise MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getUserScores used two queries split by cohort.hasExercises, with exerciseScores in the WHERE clause acting as an INNER JOIN — weeks that had GD but no exercise score were silently dropped from results. Rewrite to fetch cohorts/weeks separately from user score entities, index scores by week ID, and join in application code. This avoids the INNER JOIN filtering and fetches only the target user's data. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/scores/scores.service.ts | 91 +++++++++++------------------------- 1 file changed, 28 insertions(+), 63 deletions(-) diff --git a/src/scores/scores.service.ts b/src/scores/scores.service.ts index 2a5c077..b561d7f 100644 --- a/src/scores/scores.service.ts +++ b/src/scores/scores.service.ts @@ -230,64 +230,35 @@ export class ScoresService { } async getUserScores(id: string): Promise { - 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; @@ -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(