Made accounts and their transaction accessible to account members only.
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user