feat: error when starting BAL if user already has an ongoing one
All checks were successful
/ test (push) Successful in 15m24s

This commit is contained in:
Ninjdai 2025-08-11 17:26:08 +02:00
parent c667608fbb
commit ffa19712bd
2 changed files with 9 additions and 0 deletions

View file

@ -25,6 +25,11 @@ pub enum BalState {
Ended
}
pub async fn get_user_ongoing_bal<C>(db_conn: &C, user_id: u32) -> Option<Model>
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")]

View file

@ -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);