Perform last member removal check on the database.
This commit is contained in:
@@ -9,11 +9,7 @@ use super::{
|
||||
dtos::account_member::{AccountMemberRequest, AccountMemberResponse},
|
||||
extractors::{Auth, Json, Path, Query},
|
||||
};
|
||||
use crate::{
|
||||
domain,
|
||||
error::AppError,
|
||||
repository::{self, RepositoryError},
|
||||
};
|
||||
use crate::{domain, error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
@@ -124,32 +120,10 @@ async fn delete_account_member(
|
||||
) -> Result<StatusCode, AppError> {
|
||||
let user_id = auth.user_id();
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
|
||||
repository::accounts::get_by_id_locking(&mut *tx, user_id, account_id)
|
||||
repository::account_members::delete_by_id(&pool, user_id, account_id, member_id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
let members_query = repository::account_members::Query {
|
||||
limit: Some(2),
|
||||
offset: None,
|
||||
};
|
||||
|
||||
let members =
|
||||
repository::account_members::list(&mut *tx, user_id, account_id, members_query).await?;
|
||||
if let [only] = members.as_slice()
|
||||
&& only.id == member_id
|
||||
{
|
||||
return Err(AppError::LastAccountMemberRemoval);
|
||||
}
|
||||
|
||||
let deleted =
|
||||
repository::account_members::delete_by_id(&mut *tx, user_id, account_id, member_id).await?;
|
||||
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
deleted.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
|
||||
@@ -44,10 +44,6 @@ impl ClientError for AppError {
|
||||
Self::TransactionIdAlreadyExists => {
|
||||
(StatusCode::CONFLICT, "Transaction ID already exists".into())
|
||||
}
|
||||
Self::LastAccountMemberRemoval => (
|
||||
StatusCode::CONFLICT,
|
||||
"Cannot remove the last member of an account".into(),
|
||||
),
|
||||
Self::InvalidClientCredentials => (
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"Invalid client credentials".into(),
|
||||
@@ -157,6 +153,10 @@ impl ClientError for RepositoryError {
|
||||
StatusCode::CONFLICT,
|
||||
"Still referenced by one or more transactions".into(),
|
||||
),
|
||||
(Kind::LastRemaining, Relation::AccountMember) => (
|
||||
StatusCode::CONFLICT,
|
||||
"Cannot remove the last member of an account".into(),
|
||||
),
|
||||
_ => internal_error(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user