Made accounts and their transaction accessible to account members only.

This commit is contained in:
2026-10-11 13:28:16 +02:00
parent 21f0c17af9
commit 3faea141dd
34 changed files with 462 additions and 295 deletions
+30 -33
View File
@@ -17,7 +17,11 @@ pub struct Query {
pub offset: Option<u64>,
}
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<Account>, RepositoryError>
pub async fn list<'e, E>(
executor: E,
user_id: Uuid,
query: Query,
) -> Result<Vec<Account>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
@@ -33,7 +37,7 @@ where
.transpose()?,
};
let accounts = db::list(executor, query)
let accounts = db::list(executor, user_id, query)
.await?
.into_iter()
.map(decode)
@@ -42,17 +46,25 @@ where
Ok(accounts)
}
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<Account>, RepositoryError>
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<Account>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let account = db::get_by_id(executor, id).await?.map(decode).transpose()?;
let account = db::get_by_id(executor, user_id, id)
.await?
.map(decode)
.transpose()?;
Ok(account)
}
pub async fn get_by_ids<'e, E>(
executor: E,
user_id: Uuid,
ids: &[Uuid],
) -> Result<HashMap<Uuid, Account>, RepositoryError>
where
@@ -62,7 +74,7 @@ where
return Ok(HashMap::new());
}
let accounts = db::get_by_ids(executor, ids)
let accounts = db::get_by_ids(executor, user_id, ids)
.await?
.into_iter()
.map(decode)
@@ -78,24 +90,30 @@ where
pub async fn upsert<'e, E>(
executor: E,
user_id: Uuid,
account: Account,
) -> Result<Upsert<Account>, RepositoryError>
) -> Result<Option<Upsert<Account>>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let account = db::upsert(executor, encode(account))
let upserted = db::upsert(executor, user_id, encode(account))
.await?
.try_map(decode)?
.into();
.map(|upserted| upserted.try_map(decode))
.transpose()?
.map(Into::into);
Ok(account)
Ok(upserted)
}
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<Account>, RepositoryError>
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<Account>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let account = db::delete_by_id(executor, id)
let account = db::delete_by_id(executor, user_id, id)
.await
.map_err(classify_delete_error)?
.map(decode)
@@ -106,14 +124,6 @@ where
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::AccountMemberAccount,
..
} => RepositoryError::ReferenceViolation {
relation: ReferenceRelation::AccountMember,
kind: ReferenceViolationKind::StillReferenced,
source: error,
},
DatabaseError::ForeignKeyViolation {
constraint: Constraint::TransactionAccount,
..
@@ -161,19 +171,6 @@ mod tests {
}
}
#[test]
fn classifies_account_member_account_delete_foreign_key_constraint() {
let error = classify_delete_error(foreign_key_violation(Constraint::AccountMemberAccount));
assert!(matches!(
error,
RepositoryError::ReferenceViolation {
relation: ReferenceRelation::AccountMember,
kind: ReferenceViolationKind::StillReferenced,
..
}
));
}
#[test]
fn classifies_account_delete_foreign_key_constraint() {
let error = classify_delete_error(foreign_key_violation(