From d7c147fde85498b5f2bee600424d1bfc6f6c2f26 Mon Sep 17 00:00:00 2001 From: Gabriel Chang <77312579+GabrielCWT@users.noreply.github.com> Date: Mon, 14 Oct 2024 22:30:27 +0800 Subject: [PATCH] fix: Fix empty submisisons creation for team assessments (#1202) * feat: Implement unique index for team-assessment in submissions * fix: Fix creation of empty submission for teams * chore: Negate the if statement * chore: Format * Let Elixir do its magic * fix: Duplicate team submissions being created --------- Co-authored-by: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> --- lib/cadet/assessments/submission.ex | 1 + lib/cadet/jobs/autograder/grading_job.ex | 52 +++++++++++++------ ...600_create_teams_submission_constraint.exs | 8 +++ 3 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 priv/repo/migrations/20241014200600_create_teams_submission_constraint.exs diff --git a/lib/cadet/assessments/submission.ex b/lib/cadet/assessments/submission.ex index 596f33c13..658f79f1f 100644 --- a/lib/cadet/assessments/submission.ex +++ b/lib/cadet/assessments/submission.ex @@ -43,6 +43,7 @@ defmodule Cadet.Assessments.Submission do |> validate_xor_relationship |> validate_required(@required_fields) |> foreign_key_constraint(:student_id) + |> foreign_key_constraint(:team_id) |> foreign_key_constraint(:assessment_id) |> foreign_key_constraint(:unsubmitted_by_id) end diff --git a/lib/cadet/jobs/autograder/grading_job.ex b/lib/cadet/jobs/autograder/grading_job.ex index 618b4c885..e15f33480 100644 --- a/lib/cadet/jobs/autograder/grading_job.ex +++ b/lib/cadet/jobs/autograder/grading_job.ex @@ -112,36 +112,54 @@ defmodule Cadet.Autograder.GradingJob do |> where([_, tm], tm.student_id == ^student_id) |> Repo.one() - if !team do - # Student is not in any team - # Create new team just for the student - team = - %Team{} - |> Team.changeset(%{ - assessment_id: assessment.id + team = + if team do + team + else + # Student is not in any team + # Create new team just for the student + team = + %Team{} + |> Team.changeset(%{ + assessment_id: assessment.id + }) + |> Repo.insert!() + + %TeamMember{} + |> TeamMember.changeset(%{ + team_id: team.id, + student_id: student_id }) |> Repo.insert!() - %TeamMember{} - |> TeamMember.changeset(%{ - team_id: team.id, - student_id: student_id - }) - |> Repo.insert!() - end + team + end + find_or_create_team_submission(team.id, assessment) + else + # Individual assessment %Submission{} |> Submission.changeset(%{ - team_id: team.id, + student_id: student_id, assessment: assessment, status: :submitted }) |> Repo.insert!() + end + end + + defp find_or_create_team_submission(team_id, assessment) when is_ecto_id(team_id) do + submission = + Submission + |> where(team_id: ^team_id, assessment_id: ^assessment.id) + |> Repo.one() + + if submission do + submission else - # Individual assessment %Submission{} |> Submission.changeset(%{ - student_id: student_id, + team_id: team_id, assessment: assessment, status: :submitted }) diff --git a/priv/repo/migrations/20241014200600_create_teams_submission_constraint.exs b/priv/repo/migrations/20241014200600_create_teams_submission_constraint.exs new file mode 100644 index 000000000..d92c2d89a --- /dev/null +++ b/priv/repo/migrations/20241014200600_create_teams_submission_constraint.exs @@ -0,0 +1,8 @@ +defmodule Cadet.Repo.Migrations.CreateTeamsSubmissionConstraint do + use Ecto.Migration + + def change do + create(index(:submissions, :team_id)) + create(unique_index(:submissions, [:assessment_id, :team_id])) + end +end