From cf2a3d1da002b8f99eb060fa652912b77c9dd5ee Mon Sep 17 00:00:00 2001 From: Ell1ott <85990359+Ell1ott@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:32:59 +0200 Subject: [PATCH] Refactor assignment deployment logic in assignments.js --- routes/assignments.js | 60 ++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/routes/assignments.js b/routes/assignments.js index 6bee5ef..01f6e81 100644 --- a/routes/assignments.js +++ b/routes/assignments.js @@ -132,40 +132,46 @@ router.post("/:id/deploy", async function (req, res, next) { // Create assignment answers for all students in assigned groups const assigned_groups = deployed_assignment.asigned_groups_ids; - const students = await prisma.user_group.findMany({ - where: { - groupId: { - in: assigned_groups, + const students_ids = await prisma.user_group + .findMany({ + where: { + groupId: { + in: assigned_groups, + }, }, - }, - }); - - students = [ - ...students, - ...(await prisma.user_group.findMany({ - where: { user_id: { in: deployed_assignment.asigned_users_ids } }, - })), + }) + .map((g) => g.userId); + + students_ids = [ + ...students_ids, + ...( + await prisma.user.findMany({ + where: { user_id: { in: deployed_assignment.asigned_users_ids } }, + }) + ).map((u) => u.id), ]; // Filter out duplicates - const unique_students = students.filter( + const unique_students_ids = students.filter( (v, i, a) => a.findIndex((t) => t.user_id === v.user_id) === i ); - const assignment_answers_promises = unique_students.map(async (student) => { - try { - const assignment_answer = await prisma.assignment_answer.create({ - data: { - assignment_id: assignment_id, - student_id: student.userId, - status: "NOT_STARTED", - }, - }); - return assignment_answer; - } catch (error) { - console.error("Error creating assignment answer: ", error); - return null; + const assignment_answers_promises = unique_students_ids.map( + async (student_id) => { + try { + const assignment_answer = await prisma.assignment_answer.create({ + data: { + assignment_id: assignment_id, + student_id: student_id, + status: "NOT_STARTED", + }, + }); + return assignment_answer; + } catch (error) { + console.error("Error creating assignment answer: ", error); + return null; + } } - }); + ); const assignment_answers = await Promise.all(assignment_answers_promises);