Made accounts and their transaction accessible to account members only.
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -22,6 +22,7 @@ pub struct Query {
|
||||
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<RecurringTransaction>, RepositoryError>
|
||||
where
|
||||
@@ -38,7 +39,7 @@ where
|
||||
.transpose()?,
|
||||
};
|
||||
|
||||
let transactions = db::list(executor, query)
|
||||
let transactions = db::list(executor, user_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
@@ -49,30 +50,36 @@ where
|
||||
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<RecurringTransaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let transaction = db::get_by_id(executor, id).await?.map(decode).transpose()?;
|
||||
let transaction = db::get_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
transaction: RecurringTransaction,
|
||||
) -> Result<Upsert<RecurringTransaction>, RepositoryError>
|
||||
) -> Result<Option<Upsert<RecurringTransaction>>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let transaction = db::upsert(executor, encode(transaction))
|
||||
let upserted = db::upsert(executor, user_id, encode(transaction))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?
|
||||
.into();
|
||||
.map(|upserted| upserted.try_map(decode))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
|
||||
Ok(transaction)
|
||||
Ok(upserted)
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
@@ -96,12 +103,13 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<RecurringTransaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let transaction = db::delete_by_id(executor, id)
|
||||
let transaction = db::delete_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
@@ -15,7 +15,11 @@ pub struct Query {
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<Transaction>, RepositoryError>
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<Transaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -30,7 +34,7 @@ where
|
||||
.transpose()?,
|
||||
};
|
||||
|
||||
let transactions = db::list(executor, query)
|
||||
let transactions = db::list(executor, user_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
@@ -39,29 +43,38 @@ where
|
||||
Ok(transactions)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<Transaction>, RepositoryError>
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<Transaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let transaction = db::get_by_id(executor, id).await?.map(decode).transpose()?;
|
||||
let transaction = db::get_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
transaction: Transaction,
|
||||
) -> Result<Upsert<Transaction>, RepositoryError>
|
||||
) -> Result<Option<Upsert<Transaction>>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let transaction = db::upsert(executor, encode(transaction))
|
||||
let upserted = db::upsert(executor, user_id, encode(transaction))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?
|
||||
.into();
|
||||
.map(|upserted| upserted.try_map(decode))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
|
||||
Ok(transaction)
|
||||
Ok(upserted)
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
@@ -80,12 +93,13 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<Transaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let transaction = db::delete_by_id(executor, id)
|
||||
let transaction = db::delete_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Reference in New Issue
Block a user