Perform last member removal check on the database.

This commit is contained in:
2026-10-12 18:05:10 +02:00
parent 6729dd060b
commit 2b37fd6ebc
11 changed files with 102 additions and 123 deletions
+2 -28
View File
@@ -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)
}