From 9fb5f894c72932e77dafa6d0f00df7a8abdfa84c Mon Sep 17 00:00:00 2001 From: Henrik Nygren Date: Thu, 5 Sep 2024 13:49:19 +0300 Subject: [PATCH] Fix peer review course instance problem (#1307) --- .../src/library/peer_or_self_reviewing.rs | 5 ++ .../controllers/course_material/exercises.rs | 55 ++++++++++++++----- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/services/headless-lms/models/src/library/peer_or_self_reviewing.rs b/services/headless-lms/models/src/library/peer_or_self_reviewing.rs index 5c08c0d7cd5a..dd8693e48a0b 100644 --- a/services/headless-lms/models/src/library/peer_or_self_reviewing.rs +++ b/services/headless-lms/models/src/library/peer_or_self_reviewing.rs @@ -521,6 +521,11 @@ async fn try_to_select_peer_review_candidate_from_queue( .await .optional()?; if let Some(ess) = ess { + // Peer reviewing only works if there is a course_id and a course instance id in it. + if ess.course_id.is_none() || ess.course_instance_id.is_none() { + warn!(exercise_slide_submission_id = %ess_id, "Selected exercise slide submission that doesn't have a course_id or course_instance_id. Skipping it."); + continue; + }; if ess.deleted_at.is_none() { return Ok(Some(ess)); } diff --git a/services/headless-lms/server/src/controllers/course_material/exercises.rs b/services/headless-lms/server/src/controllers/course_material/exercises.rs index 85a63b9b0457..9a2ef9bd9b00 100644 --- a/services/headless-lms/server/src/controllers/course_material/exercises.rs +++ b/services/headless-lms/server/src/controllers/course_material/exercises.rs @@ -317,20 +317,47 @@ async fn submit_peer_or_self_review( payload.exercise_slide_submission_id, ) .await?; - let receiver_user_exercise_state = user_exercise_states::get_users_current_by_exercise( - &mut conn, - exercise_slide_submission.user_id, - &exercise, - ) - .await?; - models::library::peer_or_self_reviewing::create_peer_or_self_review_submission_for_user( - &mut conn, - &exercise, - giver_user_exercise_state, - receiver_user_exercise_state, - payload.0, - ) - .await?; + + if let Some(receiver_course_instance_id) = exercise_slide_submission.course_instance_id { + let receiver_user_exercise_state = user_exercise_states::get_user_exercise_state_if_exists( + &mut conn, + exercise_slide_submission.user_id, + exercise.id, + user_exercise_states::CourseInstanceOrExamId::Instance(receiver_course_instance_id), + ) + .await?; + if let Some(receiver_user_exercise_state) = receiver_user_exercise_state { + models::library::peer_or_self_reviewing::create_peer_or_self_review_submission_for_user( + &mut conn, + &exercise, + giver_user_exercise_state, + receiver_user_exercise_state, + payload.0, + ) + .await?; + } else { + warn!( + "No user exercise state found for receiver's exercise slide submission id: {}", + exercise_slide_submission.id + ); + return Err(ControllerError::new( + ControllerErrorType::BadRequest, + "No user exercise state found for receiver's exercise slide submission." + .to_string(), + None, + )); + } + } else { + warn!( + "No course instance id found for receiver's exercise slide submission id: {}", + exercise_slide_submission.id + ); + return Err(ControllerError::new( + ControllerErrorType::BadRequest, + "No course instance id found for receiver's exercise slide submission.".to_string(), + None, + )); + } let token = skip_authorize(); token.authorized_ok(web::Json(true)) }