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)
+29 -8
View File
@@ -12,6 +12,7 @@ pub struct Query {
pub async fn list<'e, E>(
executor: E,
user_id: Uuid,
query: Query,
) -> Result<Vec<RecurringTransactionRow>, DatabaseError>
where
@@ -32,9 +33,11 @@ where
recurrence_unit,
weekend_adjustment
FROM recurring_transactions
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
ORDER BY starts_at DESC, id DESC
LIMIT $1 OFFSET $2
LIMIT $2 OFFSET $3
"#,
user_id,
query.limit,
query.offset,
)
@@ -45,6 +48,7 @@ where
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<RecurringTransactionRow>, DatabaseError>
where
@@ -65,8 +69,10 @@ where
recurrence_unit,
weekend_adjustment
FROM recurring_transactions
WHERE id = $1
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
AND id = $2
"#,
user_id,
id,
)
.fetch_optional(executor)
@@ -76,8 +82,9 @@ where
pub async fn upsert<'e, E>(
executor: E,
user_id: Uuid,
transaction: RecurringTransactionRow,
) -> Result<Upsert<RecurringTransactionRow>, DatabaseError>
) -> Result<Option<Upsert<RecurringTransactionRow>>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
@@ -95,7 +102,9 @@ where
recurrence_unit,
weekend_adjustment
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
SELECT $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
FROM accessible_accounts($1) AS accounts
WHERE accounts.id = $4
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name,
account_id = EXCLUDED.account_id,
@@ -106,6 +115,7 @@ where
recurrence_every = EXCLUDED.recurrence_every,
recurrence_unit = EXCLUDED.recurrence_unit,
weekend_adjustment = EXCLUDED.weekend_adjustment
WHERE recurring_transactions.account_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!",
@@ -128,6 +138,7 @@ where
new_row.recurrence_unit AS "recurrence_unit!",
new_row.weekend_adjustment AS "weekend_adjustment?"
"#,
user_id,
transaction.id,
transaction.name,
transaction.account_id,
@@ -139,13 +150,20 @@ where
transaction.recurrence_unit,
transaction.weekend_adjustment,
)
.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.
// `ends_at` and `weekend_adjustment` are genuinely nullable and stay optional.
let previous = row.old_id.map(|id| RecurringTransactionRow {
id,
name: row.old_name,
@@ -159,7 +177,7 @@ where
weekend_adjustment: row.old_weekend_adjustment,
});
Ok(Upsert {
Ok(Some(Upsert {
previous,
current: RecurringTransactionRow {
id: row.id,
@@ -173,11 +191,12 @@ where
recurrence_unit: row.recurrence_unit,
weekend_adjustment: row.weekend_adjustment,
},
})
}))
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<RecurringTransactionRow>, DatabaseError>
where
@@ -187,7 +206,8 @@ where
RecurringTransactionRow,
r#"
DELETE FROM recurring_transactions
WHERE id = $1
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
AND id = $2
RETURNING
id,
name,
@@ -200,6 +220,7 @@ where
recurrence_unit,
weekend_adjustment
"#,
user_id,
id,
)
.fetch_optional(executor)
+51 -28
View File
@@ -10,7 +10,11 @@ pub struct Query {
pub offset: Option<i64>,
}
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<TransactionRow>, DatabaseError>
pub async fn list<'e, E>(
executor: E,
user_id: Uuid,
query: Query,
) -> Result<Vec<TransactionRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
@@ -19,10 +23,12 @@ where
r#"
SELECT id, account_id, amount_cents, direction, date, description
FROM transactions
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
ORDER BY date DESC, id DESC
LIMIT $1
OFFSET $2
LIMIT $2
OFFSET $3
"#,
user_id,
query.limit,
query.offset,
)
@@ -33,6 +39,7 @@ where
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<TransactionRow>, DatabaseError>
where
@@ -43,8 +50,10 @@ where
r#"
SELECT id, account_id, amount_cents, direction, date, description
FROM transactions
WHERE id = $1
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
AND id = $2
"#,
user_id,
id,
)
.fetch_optional(executor)
@@ -54,35 +63,40 @@ where
pub async fn upsert<'e, E>(
executor: E,
user_id: Uuid,
transaction: TransactionRow,
) -> Result<Upsert<TransactionRow>, DatabaseError>
) -> Result<Option<Upsert<TransactionRow>>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query!(
r#"
INSERT INTO transactions (id, account_id, amount_cents, direction, date, description)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (id) DO UPDATE SET
SELECT $2, $3, $4, $5, $6, $7
FROM accessible_accounts($1) AS accounts
WHERE accounts.id = $3
ON CONFLICT (id) DO UPDATE SET
account_id = EXCLUDED.account_id,
amount_cents = EXCLUDED.amount_cents,
direction = EXCLUDED.direction,
date = EXCLUDED.date,
description = EXCLUDED.description
RETURNING WITH (OLD AS old_row, NEW AS new_row)
old_row.id AS "old_id?",
COALESCE(old_row.account_id, new_row.account_id) AS "old_account_id!",
COALESCE(old_row.amount_cents, new_row.amount_cents) AS "old_amount_cents!",
COALESCE(old_row.direction, new_row.direction) AS "old_direction!",
COALESCE(old_row.date, new_row.date) AS "old_date!",
COALESCE(old_row.description, new_row.description) AS "old_description!",
new_row.id AS "id!",
amount_cents = EXCLUDED.amount_cents,
direction = EXCLUDED.direction,
date = EXCLUDED.date,
description = EXCLUDED.description
WHERE transactions.account_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.account_id, new_row.account_id) AS "old_account_id!",
COALESCE(old_row.amount_cents, new_row.amount_cents) AS "old_amount_cents!",
COALESCE(old_row.direction, new_row.direction) AS "old_direction!",
COALESCE(old_row.date, new_row.date) AS "old_date!",
COALESCE(old_row.description, new_row.description) AS "old_description!",
new_row.id AS "id!",
new_row.account_id AS "account_id!",
new_row.amount_cents AS "amount_cents!",
new_row.direction AS "direction!",
new_row.date AS "date!",
new_row.description AS "description!"
"#,
new_row.amount_cents AS "amount_cents!",
new_row.direction AS "direction!",
new_row.date AS "date!",
new_row.description AS "description!"
"#,
user_id,
transaction.id,
transaction.account_id,
transaction.amount_cents,
@@ -90,10 +104,16 @@ where
transaction.date,
transaction.description,
)
.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.
@@ -106,7 +126,7 @@ where
description: row.old_description,
});
Ok(Upsert {
Ok(Some(Upsert {
previous,
current: TransactionRow {
id: row.id,
@@ -116,11 +136,12 @@ where
date: row.date,
description: row.description,
},
})
}))
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<TransactionRow>, DatabaseError>
where
@@ -130,9 +151,11 @@ where
TransactionRow,
r#"
DELETE FROM transactions
WHERE id = $1
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
AND id = $2
RETURNING id, account_id, amount_cents, direction, date, description
"#,
user_id,
id,
)
.fetch_optional(executor)