diff --git a/moneymgr_backend/src/controllers/movement_controller.rs b/moneymgr_backend/src/controllers/movement_controller.rs index d051046..324e9aa 100644 --- a/moneymgr_backend/src/controllers/movement_controller.rs +++ b/moneymgr_backend/src/controllers/movement_controller.rs @@ -8,7 +8,7 @@ use actix_web::{HttpResponse, web}; /// Create a new movement pub async fn create(auth: AuthExtractor, req: web::Json) -> HttpResult { - if let Some(err) = req.check_error(auth.user_id()).await? { + if let Some(err) = req.check_error(auth.user_id(), None).await? { return Ok(HttpResponse::BadRequest().json(err)); } @@ -34,7 +34,10 @@ pub async fn update( movement: MovementInPath, req: web::Json, ) -> HttpResult { - if let Some(err) = req.check_error(auth.user_id()).await? { + if let Some(err) = req + .check_error(auth.user_id(), Some(movement.movement().id())) + .await? + { return Ok(HttpResponse::BadRequest().json(err)); } diff --git a/moneymgr_backend/src/services/movements_service.rs b/moneymgr_backend/src/services/movements_service.rs index e0caa25..44036fc 100644 --- a/moneymgr_backend/src/services/movements_service.rs +++ b/moneymgr_backend/src/services/movements_service.rs @@ -21,7 +21,11 @@ pub struct UpdateMovementQuery { } impl UpdateMovementQuery { - pub async fn check_error(&self, user_id: UserID) -> anyhow::Result> { + pub async fn check_error( + &self, + user_id: UserID, + ref_movement: Option, + ) -> anyhow::Result> { let constraints = ServerConstraints::default(); // Check movement label @@ -30,7 +34,9 @@ impl UpdateMovementQuery { } // Check the account - let account = accounts_service::get_by_id(self.account_id).await?; + let Ok(account) = accounts_service::get_by_id(self.account_id).await else { + return Ok(Some("The specified account does not exists!")); + }; if account.user_id() != user_id { return Ok(Some("The user does not own the account!")); } @@ -45,13 +51,15 @@ impl UpdateMovementQuery { } // Check for conflict with other movements - if get_by_account_label_amount_time(self.account_id, &self.label, self.amount, self.time) - .await - .is_ok() + if let Ok(movement) = + get_by_account_label_amount_time(self.account_id, &self.label, self.amount, self.time) + .await { - return Ok(Some( - "A movement taken at the same time with the same label and the same amount already exists!", - )); + if Some(movement.id()) != ref_movement { + return Ok(Some( + "A movement taken at the same time with the same label and the same amount already exists!", + )); + } } Ok(None)