diff --git a/src/helpers/dashboard.adviser.helper.ts b/src/helpers/dashboard.adviser.helper.ts index a2da210..c16c5c5 100644 --- a/src/helpers/dashboard.adviser.helper.ts +++ b/src/helpers/dashboard.adviser.helper.ts @@ -1,11 +1,11 @@ -import { DeadlineType } from "@prisma/client"; +import { DeadlineType, Prisma } from "@prisma/client"; import { SkylabError } from "../errors/SkylabError"; import { findUniqueAdviserWithProjectData } from "../models/advisers.db"; import { findManyDeadlines } from "../models/deadline.db"; -import { findManyRelationsWithFromToProjectData } from "../models/relations.db"; import { findFirstNonDraftSubmission, findFirstSubmission, + findManySubmissions, } from "../models/submissions.db"; import { HttpStatusCode } from "../utils/HTTP_Status_Codes"; @@ -114,10 +114,6 @@ export async function getProjectSubmissionsViaAdviserId(adviserId: number) { const projectIds = adviser.projects.map(({ id }) => id); - const relations = await findManyRelationsWithFromToProjectData({ - where: { fromProjectId: { in: projectIds } }, - }); - const cohortDeadlines = await findManyDeadlines({ where: { cohortYear: adviser.cohortYear }, }); @@ -144,23 +140,21 @@ export async function getProjectSubmissionsViaAdviserId(adviserId: number) { submissions: [], }; } - const evaluationSubmissions = relations.map(async (relation) => { - const submission = await findFirstNonDraftSubmission({ - where: { - deadlineId: deadline.id, - fromProjectId: relation.fromProjectId, - toProjectId: relation.toProjectId, - }, - }); - return { - fromProject: relation.fromProject, - toProject: relation.toProject, - ...submission, - }; - }); + // Query submissions directly — survives EvaluationRelation deletion. + const evaluationSubmissions = (await findManySubmissions({ + where: { + deadlineId: deadline.id, + fromProjectId: { in: projectIds }, + toProjectId: { not: null }, + isDraft: false, + }, + include: { fromProject: true, toProject: true }, + })) as Prisma.SubmissionGetPayload<{ + include: { fromProject: true; toProject: true }; + }>[]; return { deadline: deadline, - submissions: await Promise.all(evaluationSubmissions), + submissions: evaluationSubmissions, }; } else if (deadline.type == "Feedback") { const feedbackSubmissions = adviser.projects.map(async (project) => { diff --git a/src/helpers/dashboard.student.helper.ts b/src/helpers/dashboard.student.helper.ts index 6da9ee0..2888505 100644 --- a/src/helpers/dashboard.student.helper.ts +++ b/src/helpers/dashboard.student.helper.ts @@ -1,10 +1,7 @@ -import { DeadlineType, Submission } from "@prisma/client"; +import { DeadlineType, Prisma, Submission } from "@prisma/client"; import { SkylabError } from "../errors/SkylabError"; import { findManyDeadlines, findManyEvaluations } from "../models/deadline.db"; -import { - findManyRelationsWithFromProjectData, - findManyRelationsWithFromToProjectData, -} from "../models/relations.db"; +import { findManyRelationsWithFromToProjectData } from "../models/relations.db"; import { findUniqueStudentWithProjectWithAdviserData, findUniqueStudentWithProjectWithAdviserUserData, @@ -12,6 +9,7 @@ import { import { findFirstSubmission, findFirstNonDraftSubmission, + findManySubmissions, } from "../models/submissions.db"; import { findUniqueUser } from "../models/users.db"; import { HttpStatusCode } from "../utils/HTTP_Status_Codes"; @@ -190,17 +188,11 @@ export async function getPeerEvaluationFeedbackByStudentID(studentId: number) { pCohortFeedbacks, ]); - const relations = await findManyRelationsWithFromProjectData({ - where: { - toProjectId: projectId, - }, - }); - const pProjectSubmissions = [...cohortEvaluations, ...cohortFeedbacks].map( async (deadline) => { - type PeerFeedback = Partial & { - fromProject: typeof relations[0]["fromProject"]; - }; + type PeerFeedback = Prisma.SubmissionGetPayload<{ + include: { fromProject: true }; + }>; type AdviserFeedback = Partial & { fromUser: typeof adviserUser; }; @@ -209,22 +201,16 @@ export async function getPeerEvaluationFeedbackByStudentID(studentId: number) { let peerSubmissions: PeerFeedback[] = []; if (deadline.evaluatorType !== "Adviser") { - const pPeerSubmissions = relations.map(async (relation) => { - const submission = await findFirstNonDraftSubmission({ - where: { - deadlineId: deadline.id, - fromProjectId: relation.fromProjectId, - toProjectId: projectId, - }, - }); - - const peerFeedback: PeerFeedback = { - ...(submission || {}), - fromProject: relation.fromProject, - }; - return peerFeedback; - }); - peerSubmissions = await Promise.all(pPeerSubmissions); + // Query submissions directly — survives EvaluationRelation deletion. + peerSubmissions = (await findManySubmissions({ + where: { + deadlineId: deadline.id, + toProjectId: projectId, + fromProjectId: { not: null }, + isDraft: false, + }, + include: { fromProject: true }, + })) as PeerFeedback[]; } if (deadline.type !== "Evaluation") { diff --git a/src/helpers/users.helper.ts b/src/helpers/users.helper.ts index 30c85d7..93b5e71 100644 --- a/src/helpers/users.helper.ts +++ b/src/helpers/users.helper.ts @@ -38,7 +38,11 @@ export function formatUserWithRoleData(user: UserWithRoleData) { ...userInfoWithoutPassword, student: student ? student[0] ?? {} : {}, mentor: mentor ? mentor[0] ?? {} : {}, - adviser: adviser ? adviser[0] ?? {} : undefined, + adviser: adviser + ? Array.isArray(adviser) + ? [...adviser].sort((a, b) => b.cohortYear - a.cohortYear)[0] ?? {} + : adviser + : undefined, administrator: administrator ? administrator[0] ?? {} : undefined, }; }