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
+55 -21
View File
@@ -11,20 +11,25 @@ pub struct Query {
pub offset: Option<i64>,
}
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<AccountRow>, DatabaseError>
pub async fn list<'e, E>(
executor: E,
user_id: Uuid,
query: Query,
) -> Result<Vec<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
r#"
SELECT id, name
FROM accounts
WHERE ($1::text IS NULL OR name ILIKE '%' || $1 || '%')
SELECT id AS "id!", name AS "name!"
FROM accessible_accounts($1)
WHERE ($2::text IS NULL OR name ILIKE '%' || $2 || '%')
ORDER BY name DESC, id DESC
LIMIT $2
OFFSET $3
LIMIT $3
OFFSET $4
"#,
user_id,
query.name,
query.limit,
query.offset,
@@ -34,17 +39,22 @@ where
.map_err(DatabaseError::from)
}
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<AccountRow>, DatabaseError>
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
r#"
SELECT id, name
FROM accounts
WHERE id = $1
SELECT id AS "id!", name AS "name!"
FROM accessible_accounts($1)
WHERE id = $2
"#,
user_id,
id,
)
.fetch_optional(executor)
@@ -52,17 +62,22 @@ where
.map_err(DatabaseError::from)
}
pub async fn get_by_ids<'e, E>(executor: E, ids: &[Uuid]) -> Result<Vec<AccountRow>, DatabaseError>
pub async fn get_by_ids<'e, E>(
executor: E,
user_id: Uuid,
ids: &[Uuid],
) -> Result<Vec<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
r#"
SELECT id, name
FROM accounts
WHERE id = ANY($1)
SELECT id AS "id!", name AS "name!"
FROM accessible_accounts($1)
WHERE id = ANY($2)
"#,
user_id,
ids,
)
.fetch_all(executor)
@@ -72,30 +87,39 @@ where
pub async fn upsert<'e, E>(
executor: E,
user_id: Uuid,
account: AccountRow,
) -> Result<Upsert<AccountRow>, DatabaseError>
) -> Result<Option<Upsert<AccountRow>>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query!(
r#"
INSERT INTO accounts (id, name)
VALUES ($1, $2)
VALUES ($2, $3)
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name
WHERE accounts.id IN (SELECT id FROM accessible_accounts($1))
RETURNING WITH (OLD AS old_row, NEW AS new_row)
old_row.id AS "old_id?",
COALESCE(old_row.name, new_row.name) AS "old_name!",
new_row.id AS "id!",
new_row.name AS "name!"
"#,
user_id,
account.id,
account.name,
)
.fetch_one(executor)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
// Nothing is written when the account is not accessible, or when the id belongs to a row in an
// account that is not accessible.
let Some(row) = row else {
return Ok(None);
};
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
// coalesced with the new row's values in case there was no previous row. This enables static
// typing on the previous row instead of having to check nullability for each old field in code.
@@ -104,22 +128,32 @@ where
name: row.old_name,
});
Ok(Upsert {
Ok(Some(Upsert {
previous,
current: AccountRow {
id: row.id,
name: row.name,
},
})
}))
}
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<AccountRow>, DatabaseError>
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<AccountRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
AccountRow,
"DELETE FROM accounts WHERE id = $1 RETURNING id, name",
r#"
DELETE FROM accounts
WHERE id IN (SELECT id FROM accessible_accounts($1))
AND id = $2
RETURNING id, name
"#,
user_id,
id,
)
.fetch_optional(executor)