From ffa19712bd2589ab13fe31c9b7dba995bfe2a145 Mon Sep 17 00:00:00 2001 From: Ninjdai Date: Mon, 11 Aug 2025 17:26:08 +0200 Subject: [PATCH] feat: error when starting BAL if user already has an ongoing one --- src/entities/bal.rs | 5 +++++ src/routes/bal.rs | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/entities/bal.rs b/src/entities/bal.rs index b5f2080..47bbcfd 100644 --- a/src/entities/bal.rs +++ b/src/entities/bal.rs @@ -25,6 +25,11 @@ pub enum BalState { Ended } +pub async fn get_user_ongoing_bal(db_conn: &C, user_id: u32) -> Option +where C: ConnectionTrait { + Entity::find().filter(Column::UserId.eq(user_id)).filter(Column::State.eq(BalState::Ongoing)).one(db_conn).await.unwrap_or(None) +} + #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { #[sea_orm(has_many = "super::book_instance::Entity")] diff --git a/src/routes/bal.rs b/src/routes/bal.rs index 585aa06..a4d4f77 100644 --- a/src/routes/bal.rs +++ b/src/routes/bal.rs @@ -160,6 +160,7 @@ pub async fn update_bal( responses( (status = OK, body = bal::Model, description = "Successfully started bal"), (status = CONFLICT, description = "The specified BAL was not in a Pending state, cannot start it"), + (status = CONFLICT, description = "Cannot have multiple ongoing BALs at the same time !"), (status = NOT_FOUND, description = "No bal with specified id was found"), (status = FORBIDDEN, description = "You don't own the specified bal"), ), @@ -179,6 +180,9 @@ pub async fn start_bal( if bal.state != BalState::Pending { return (StatusCode::CONFLICT, Json(None)); } + if bal::get_user_ongoing_bal(state.db_conn.as_ref(), claims.user_id).await.is_some() { + return (StatusCode::CONFLICT, Json(None)); + } let mut bal: bal::ActiveModel = bal.into(); bal.state = Set(BalState::Ongoing);