Made accounts and their transaction accessible to account members only.
This commit is contained in:
@@ -44,7 +44,7 @@ async fn list_account_members(
|
||||
};
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_exists(&mut tx, account_id).await?;
|
||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
||||
let members = repository::account_members::list(&mut *tx, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
@@ -63,7 +63,7 @@ async fn get_account_member(
|
||||
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Json<AccountMemberResponse>, AppError> {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_exists(&mut tx, account_id).await?;
|
||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
||||
let member = repository::account_members::get_by_id(&mut *tx, account_id, member_id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
@@ -83,7 +83,7 @@ async fn upsert_account_member(
|
||||
let member = member.into_domain(member_id, account_id)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_exists(&mut tx, account_id).await?;
|
||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
||||
let upserted = repository::account_members::upsert(&mut *tx, member).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
@@ -104,7 +104,21 @@ async fn delete_account_member(
|
||||
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_exists(&mut tx, account_id).await?;
|
||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
||||
let members = repository::account_members::list(
|
||||
&mut *tx,
|
||||
repository::account_members::Query {
|
||||
account_id,
|
||||
limit: Some(2),
|
||||
offset: None,
|
||||
},
|
||||
)
|
||||
.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, account_id, member_id).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
@@ -114,11 +128,12 @@ async fn delete_account_member(
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn ensure_account_exists(
|
||||
async fn ensure_account_ownership(
|
||||
connection: &mut sqlx::PgConnection,
|
||||
user_id: Uuid,
|
||||
account_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
repository::accounts::get_by_id(&mut *connection, account_id)
|
||||
repository::accounts::get_by_id(&mut *connection, user_id, account_id)
|
||||
.await?
|
||||
.map(|_| ())
|
||||
.ok_or(AppError::NotFound)
|
||||
|
||||
@@ -7,7 +7,7 @@ use super::{
|
||||
dtos::account::{AccountRequest, AccountResponse},
|
||||
extractors::{Auth, Json, Path, Query},
|
||||
};
|
||||
use crate::{error::AppError, repository};
|
||||
use crate::{domain::AccountMember, error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
axum::Router::new()
|
||||
@@ -34,27 +34,18 @@ async fn list_accounts(
|
||||
State(pool): State<PgPool>,
|
||||
Query(query): Query<QueryParams>,
|
||||
) -> Result<Json<Vec<AccountResponse>>, AppError> {
|
||||
let mut tx = pool
|
||||
.begin()
|
||||
.await
|
||||
.map_err(repository::RepositoryError::from)?;
|
||||
|
||||
let query = repository::accounts::Query {
|
||||
name: query.name,
|
||||
limit: Some(query.limit.unwrap_or(20)),
|
||||
offset: query.offset,
|
||||
};
|
||||
|
||||
let accounts = repository::accounts::list(&mut *tx, query)
|
||||
let accounts = repository::accounts::list(&pool, auth.user_id(), query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(AccountResponse::from)
|
||||
.collect();
|
||||
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(repository::RepositoryError::from)?;
|
||||
|
||||
Ok(Json(accounts))
|
||||
}
|
||||
|
||||
@@ -65,7 +56,7 @@ async fn get_account(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<AccountResponse>, AppError> {
|
||||
let account = repository::accounts::get_by_id(&pool, id)
|
||||
let account = repository::accounts::get_by_id(&pool, auth.user_id(), id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
@@ -80,9 +71,31 @@ async fn upsert_account(
|
||||
Path(id): Path<Uuid>,
|
||||
Json(account): Json<AccountRequest>,
|
||||
) -> Result<(StatusCode, Json<AccountResponse>), AppError> {
|
||||
let user_id = auth.user_id();
|
||||
let account = account.into_domain(id)?;
|
||||
|
||||
let upserted = repository::accounts::upsert(&pool, account).await?;
|
||||
let mut tx = pool
|
||||
.begin()
|
||||
.await
|
||||
.map_err(repository::RepositoryError::from)?;
|
||||
|
||||
let upserted = repository::accounts::upsert(&mut *tx, user_id, account)
|
||||
.await?
|
||||
.ok_or(AppError::AccountIdAlreadyExists)?;
|
||||
|
||||
if upserted.previous.is_none() {
|
||||
let member = AccountMember {
|
||||
id: Uuid::new_v4(),
|
||||
account_id: id,
|
||||
user_id,
|
||||
};
|
||||
repository::account_members::upsert(&mut *tx, member).await?;
|
||||
}
|
||||
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(repository::RepositoryError::from)?;
|
||||
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
@@ -99,7 +112,7 @@ async fn delete_account(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
repository::accounts::delete_by_id(&pool, id)
|
||||
repository::accounts::delete_by_id(&pool, auth.user_id(), id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
|
||||
@@ -25,12 +25,30 @@ impl IntoResponse for AppError {
|
||||
impl ClientError for AppError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
Self::AccountIdAlreadyExists => {
|
||||
(StatusCode::CONFLICT, "Account ID already exists".into())
|
||||
}
|
||||
Self::AccountIdNotFound => (
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"Account ID cannot be found".into(),
|
||||
),
|
||||
Self::TransactionIdAlreadyExists => {
|
||||
(StatusCode::CONFLICT, "Transaction ID already exists".into())
|
||||
}
|
||||
Self::ScheduledTransactionIdAlreadyExists => (
|
||||
StatusCode::CONFLICT,
|
||||
"Scheduled transaction ID already exists".into(),
|
||||
),
|
||||
Self::AppIdAlreadyExists => (StatusCode::CONFLICT, "App ID already exists".into()),
|
||||
Self::InvalidClientCredentials => (
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"Invalid client credentials".into(),
|
||||
),
|
||||
Self::InvalidRefreshToken => (StatusCode::UNAUTHORIZED, "Invalid refresh token".into()),
|
||||
Self::LastAccountMemberRemoval => (
|
||||
StatusCode::CONFLICT,
|
||||
"Cannot remove the last member of an account".into(),
|
||||
),
|
||||
Self::NotFound => (StatusCode::NOT_FOUND, "Not found".into()),
|
||||
Self::Domain(error) => error.client_response(),
|
||||
Self::Extractor(error) => error.client_response(),
|
||||
|
||||
@@ -49,8 +49,9 @@ async fn list_scheduled_transactions(
|
||||
};
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let transactions = repository::recurring_transactions::list(&mut *tx, query).await?;
|
||||
let accounts = get_accounts_for_transactions(&mut tx, &transactions).await?;
|
||||
let transactions =
|
||||
repository::recurring_transactions::list(&mut *tx, auth.user_id(), query).await?;
|
||||
let accounts = get_accounts_for_transactions(&mut tx, auth.user_id(), &transactions).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let responses = transactions
|
||||
@@ -69,11 +70,12 @@ async fn get_scheduled_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ScheduledTransactionResponse>, AppError> {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let transaction = repository::recurring_transactions::get_by_id(&mut *tx, id)
|
||||
let transaction = repository::recurring_transactions::get_by_id(&mut *tx, auth.user_id(), id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&transaction)).await?;
|
||||
get_accounts_for_transactions(&mut tx, auth.user_id(), std::slice::from_ref(&transaction))
|
||||
.await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
Ok(Json(create_response(transaction, &accounts, Utc::now())?))
|
||||
@@ -91,9 +93,13 @@ async fn upsert_scheduled_transaction(
|
||||
let transaction = transaction.into_domain(id)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let upserted = repository::recurring_transactions::upsert(&mut *tx, transaction).await?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&upserted.current)).await?;
|
||||
let account = accounts::get_by_id(&mut *tx, auth.user_id(), transaction.account_id)
|
||||
.await?
|
||||
.ok_or(AppError::AccountIdNotFound)?;
|
||||
let upserted =
|
||||
repository::recurring_transactions::upsert(&mut *tx, auth.user_id(), transaction)
|
||||
.await?
|
||||
.ok_or(AppError::ScheduledTransactionIdAlreadyExists)?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let status = if upserted.previous.is_some() {
|
||||
@@ -102,6 +108,8 @@ async fn upsert_scheduled_transaction(
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
let accounts = HashMap::from([(account.id, account)]);
|
||||
|
||||
Ok((
|
||||
status,
|
||||
Json(create_response(upserted.current, &accounts, Utc::now())?),
|
||||
@@ -115,7 +123,7 @@ async fn delete_scheduled_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
repository::recurring_transactions::delete_by_id(&pool, id)
|
||||
repository::recurring_transactions::delete_by_id(&pool, auth.user_id(), id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
@@ -124,6 +132,7 @@ async fn delete_scheduled_transaction(
|
||||
|
||||
async fn get_accounts_for_transactions(
|
||||
connection: &mut sqlx::PgConnection,
|
||||
user_id: Uuid,
|
||||
transactions: &[domain::RecurringTransaction],
|
||||
) -> Result<HashMap<Uuid, domain::Account>, AppError> {
|
||||
let account_ids = transactions
|
||||
@@ -133,7 +142,7 @@ async fn get_accounts_for_transactions(
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(accounts::get_by_ids(connection, &account_ids).await?)
|
||||
Ok(accounts::get_by_ids(connection, user_id, &account_ids).await?)
|
||||
}
|
||||
|
||||
fn create_response(
|
||||
|
||||
@@ -45,8 +45,8 @@ async fn list_transactions(
|
||||
};
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let transactions = repository::transactions::list(&mut *tx, query).await?;
|
||||
let accounts = get_accounts_for_transactions(&mut tx, &transactions).await?;
|
||||
let transactions = repository::transactions::list(&mut *tx, auth.user_id(), query).await?;
|
||||
let accounts = get_accounts_for_transactions(&mut tx, auth.user_id(), &transactions).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let responses = transactions
|
||||
@@ -65,11 +65,12 @@ async fn get_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<TransactionResponse>, AppError> {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let transaction = repository::transactions::get_by_id(&mut *tx, id)
|
||||
let transaction = repository::transactions::get_by_id(&mut *tx, auth.user_id(), id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&transaction)).await?;
|
||||
get_accounts_for_transactions(&mut tx, auth.user_id(), std::slice::from_ref(&transaction))
|
||||
.await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
Ok(Json(create_response(transaction, &accounts)?))
|
||||
@@ -86,9 +87,12 @@ async fn upsert_transaction(
|
||||
let transaction = transaction.into_domain(id)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let upserted = repository::transactions::upsert(&mut *tx, transaction).await?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&upserted.current)).await?;
|
||||
let account = repository::accounts::get_by_id(&mut *tx, auth.user_id(), transaction.account_id)
|
||||
.await?
|
||||
.ok_or(AppError::AccountIdNotFound)?;
|
||||
let upserted = repository::transactions::upsert(&mut *tx, auth.user_id(), transaction)
|
||||
.await?
|
||||
.ok_or(AppError::TransactionIdAlreadyExists)?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let status = if upserted.previous.is_some() {
|
||||
@@ -97,6 +101,8 @@ async fn upsert_transaction(
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
let accounts = HashMap::from([(account.id, account)]);
|
||||
|
||||
Ok((status, Json(create_response(upserted.current, &accounts)?)))
|
||||
}
|
||||
|
||||
@@ -107,7 +113,7 @@ async fn delete_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
repository::transactions::delete_by_id(&pool, id)
|
||||
repository::transactions::delete_by_id(&pool, auth.user_id(), id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
@@ -116,6 +122,7 @@ async fn delete_transaction(
|
||||
|
||||
async fn get_accounts_for_transactions(
|
||||
connection: &mut sqlx::PgConnection,
|
||||
user_id: Uuid,
|
||||
transactions: &[domain::Transaction],
|
||||
) -> Result<HashMap<Uuid, domain::Account>, AppError> {
|
||||
let account_ids = transactions
|
||||
@@ -125,7 +132,7 @@ async fn get_accounts_for_transactions(
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(repository::accounts::get_by_ids(connection, &account_ids).await?)
|
||||
Ok(repository::accounts::get_by_ids(connection, user_id, &account_ids).await?)
|
||||
}
|
||||
|
||||
fn create_response(
|
||||
|
||||
Reference in New Issue
Block a user