Skip to content

Commit

Permalink
Changes to process ended exam enrollments
Browse files Browse the repository at this point in the history
  • Loading branch information
Maija Y committed Jul 11, 2024
1 parent cbeb17f commit bfd4b83
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
27 changes: 26 additions & 1 deletion services/headless-lms/models/src/exams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ WHERE exam_id = $1
Ok(())
}

pub async fn update_exam_ended(
pub async fn update_exam_ended_at(
conn: &mut PgConnection,
exam_id: Uuid,
user_id: Uuid,
Expand All @@ -519,6 +519,31 @@ WHERE exam_id = $1
Ok(())
}

pub async fn update_exam_ended_at_for_users_with_exam_id(
conn: &mut PgConnection,
exam_id: Uuid,
user_ids: &[Uuid],
ended_at: DateTime<Utc>,
) -> ModelResult<()> {
sqlx::query!(
"
UPDATE exam_enrollments
SET ended_at = $3
WHERE user_id IN (
SELECT UNNEST($1::uuid [])
)
AND exam_id = $2
AND deleted_at IS NULL
",
user_ids,
exam_id,
ended_at
)
.execute(conn)
.await?;
Ok(())
}

pub async fn update_show_exercise_answers(
conn: &mut PgConnection,
exam_id: Uuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub async fn fetch_exam_for_user(
&& Utc::now() > enrollment.started_at + Duration::minutes(exam.time_minutes.into())
{
// exam is still open but the student's time has expired
exams::update_exam_ended(&mut conn, *exam_id, user.id, Utc::now()).await?;
exams::update_exam_ended_at(&mut conn, *exam_id, user.id, Utc::now()).await?;
let token: domain::authorization::AuthorizationToken =
authorize(&mut conn, Act::View, Some(user.id), Res::Exam(*exam_id)).await?;
return token.authorized_ok(web::Json(ExamData {
Expand Down Expand Up @@ -454,7 +454,7 @@ pub async fn end_exam_time(
let mut conn = pool.acquire().await?;

let ended_at = Utc::now();
models::exams::update_exam_ended(&mut conn, *exam_id, user.id, ended_at).await?;
models::exams::update_exam_ended_at(&mut conn, *exam_id, user.id, ended_at).await?;

let token = authorize(&mut conn, Act::View, Some(user.id), Res::Exam(*exam_id)).await?;
token.authorized_ok(web::Json(()))
Expand Down
47 changes: 32 additions & 15 deletions services/headless-lms/server/src/programs/ended_exams_processor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{collections::HashSet, env};
use std::{
collections::{HashMap, HashSet},
env,
};

use crate::setup_tracing;
use chrono::{Duration, Utc};
Expand Down Expand Up @@ -79,26 +82,40 @@ async fn process_ended_exam_enrollments(conn: &mut PgConnection) -> anyhow::Resu
models::exams::get_ongoing_exam_enrollments(&mut tx).await?;
let exams = models::exams::get_exams(&mut tx).await?;

let mut needs_ended_at_date: HashMap<Uuid, Vec<Uuid>> = HashMap::new();
for enrollment in ongoing_exam_enrollments {
let exam = exams.get(&enrollment.exam_id).ok_or_else(|| {
ModelError::new(ModelErrorType::Generic, "Exam not found".into(), None)
})?;

//Check if users exams should have ended and add the ending time to exam_enrollmets
//Check if users exams should have ended
if Utc::now() > enrollment.started_at + Duration::minutes(exam.time_minutes.into()) {
match models::exams::update_exam_ended(&mut tx, exam.id, enrollment.user_id, Utc::now())
.await
{
Ok(_) => success += 1,
Err(err) => {
failed += 1;
tracing::error!(
"Failed to end users {} exam {}: {:#?}",
enrollment.user_id,
exam.id,
err
);
}
needs_ended_at_date
.entry(exam.id)
.or_default()
.push(enrollment.user_id);
}
}

for entry in needs_ended_at_date.into_iter() {
let exam_id = entry.0;
let user_ids = entry.1;
match models::exams::update_exam_ended_at_for_users_with_exam_id(
&mut tx,
exam_id,
&user_ids,
Utc::now(),
)
.await
{
Ok(_) => success += user_ids.len(),
Err(err) => {
failed += user_ids.len();
tracing::error!(
"Failed to end exam enrolments for exam {}: {:#?}",
exam_id,
err
);
}
}
}
Expand Down

0 comments on commit bfd4b83

Please sign in to comment.