From 26a370aa31ee13eb91bf4c1f3831f7c052d44a37 Mon Sep 17 00:00:00 2001 From: Rogier Reerink Date: Sun, 27 Sep 2026 11:04:47 +0200 Subject: [PATCH] Added source and destination accounts. --- ...9b03ecc2c407701f1952f4b4e40fc9d6e12d1.json | 84 ---------------- ...40647a1965dca8419e6aa9d33a5dfa9e5426.json} | 24 +++-- ...34f22568eb9288e8d56522bdfbbc2aab43fb4.json | 98 +++++++++++++++++++ ...ad13ec035b8d409b880d6cff2ef9e2615fa4.json} | 24 +++-- ...sql => 20260918000000_create_accounts.sql} | 0 ...20000000_create_recurring_transactions.sql | 2 + server/src/database.rs | 20 ++++ server/src/database/accounts.rs | 21 ++-- .../database/dtos/recurring_transaction.rs | 2 + server/src/database/recurring_transactions.rs | 37 +++++-- server/src/database/transactions.rs | 24 +++-- server/src/domain/recurring_transaction.rs | 2 + server/src/error.rs | 5 +- server/src/repository.rs | 7 +- .../src/repository/recurring_transactions.rs | 15 ++- .../src/routes/dtos/scheduled_transaction.rs | 10 ++ 16 files changed, 253 insertions(+), 122 deletions(-) delete mode 100644 server/.sqlx/query-0670f762b989960690b3975bf3f9b03ecc2c407701f1952f4b4e40fc9d6e12d1.json rename server/.sqlx/{query-b90fc3934e144910e05cddcfb11e66a670b842a4636960ac59f18d741cd9cc28.json => query-a67033cd73e269ec203817b402ef40647a1965dca8419e6aa9d33a5dfa9e5426.json} (67%) create mode 100644 server/.sqlx/query-cd3573646e7cfe523b7dc97092134f22568eb9288e8d56522bdfbbc2aab43fb4.json rename server/.sqlx/{query-1a4dc56d068d3d5b531ca43e8e0bc5ba6789ffce3f896334780965afd1cc214f.json => query-fae9d6766e5bce18e7917df6a0d6ad13ec035b8d409b880d6cff2ef9e2615fa4.json} (68%) rename server/migrations/{20260927000000_create_accounts.sql => 20260918000000_create_accounts.sql} (100%) diff --git a/server/.sqlx/query-0670f762b989960690b3975bf3f9b03ecc2c407701f1952f4b4e40fc9d6e12d1.json b/server/.sqlx/query-0670f762b989960690b3975bf3f9b03ecc2c407701f1952f4b4e40fc9d6e12d1.json deleted file mode 100644 index 464cb3b..0000000 --- a/server/.sqlx/query-0670f762b989960690b3975bf3f9b03ecc2c407701f1952f4b4e40fc9d6e12d1.json +++ /dev/null @@ -1,84 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n INSERT INTO recurring_transactions (\n id,\n name,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n amount_cents = EXCLUDED.amount_cents,\n direction = EXCLUDED.direction,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n recurrence_every = EXCLUDED.recurrence_every,\n recurrence_unit = EXCLUDED.recurrence_unit,\n weekend_adjustment = EXCLUDED.weekend_adjustment\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n new_row.id AS \"id!\",\n new_row.name AS \"name!\",\n new_row.amount_cents AS \"amount_cents!\",\n new_row.direction AS \"direction!\",\n new_row.starts_at AS \"starts_at!\",\n new_row.ends_at AS \"ends_at?\",\n new_row.recurrence_every AS \"recurrence_every!\",\n new_row.recurrence_unit AS \"recurrence_unit!\",\n new_row.weekend_adjustment AS \"weekend_adjustment?\"\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "old_id?", - "type_info": "Uuid" - }, - { - "ordinal": 1, - "name": "id!", - "type_info": "Uuid" - }, - { - "ordinal": 2, - "name": "name!", - "type_info": "Text" - }, - { - "ordinal": 3, - "name": "amount_cents!", - "type_info": "Int8" - }, - { - "ordinal": 4, - "name": "direction!", - "type_info": "Text" - }, - { - "ordinal": 5, - "name": "starts_at!", - "type_info": "Timestamptz" - }, - { - "ordinal": 6, - "name": "ends_at?", - "type_info": "Timestamptz" - }, - { - "ordinal": 7, - "name": "recurrence_every!", - "type_info": "Int4" - }, - { - "ordinal": 8, - "name": "recurrence_unit!", - "type_info": "Text" - }, - { - "ordinal": 9, - "name": "weekend_adjustment?", - "type_info": "Text" - } - ], - "parameters": { - "Left": [ - "Uuid", - "Text", - "Int8", - "Text", - "Timestamptz", - "Timestamptz", - "Int4", - "Text", - "Text" - ] - }, - "nullable": [ - false, - false, - false, - false, - false, - false, - true, - false, - false, - true - ] - }, - "hash": "0670f762b989960690b3975bf3f9b03ecc2c407701f1952f4b4e40fc9d6e12d1" -} diff --git a/server/.sqlx/query-b90fc3934e144910e05cddcfb11e66a670b842a4636960ac59f18d741cd9cc28.json b/server/.sqlx/query-a67033cd73e269ec203817b402ef40647a1965dca8419e6aa9d33a5dfa9e5426.json similarity index 67% rename from server/.sqlx/query-b90fc3934e144910e05cddcfb11e66a670b842a4636960ac59f18d741cd9cc28.json rename to server/.sqlx/query-a67033cd73e269ec203817b402ef40647a1965dca8419e6aa9d33a5dfa9e5426.json index 7041a1b..b7b04a9 100644 --- a/server/.sqlx/query-b90fc3934e144910e05cddcfb11e66a670b842a4636960ac59f18d741cd9cc28.json +++ b/server/.sqlx/query-a67033cd73e269ec203817b402ef40647a1965dca8419e6aa9d33a5dfa9e5426.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n FROM recurring_transactions\n ORDER BY starts_at DESC, id DESC\n LIMIT $1 OFFSET $2\n ", + "query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n source_account_id,\n destination_account_id,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n FROM recurring_transactions\n ORDER BY starts_at DESC, id DESC\n LIMIT $1 OFFSET $2\n ", "describe": { "columns": [ { @@ -25,26 +25,36 @@ }, { "ordinal": 4, + "name": "source_account_id", + "type_info": "Uuid" + }, + { + "ordinal": 5, + "name": "destination_account_id", + "type_info": "Uuid" + }, + { + "ordinal": 6, "name": "starts_at", "type_info": "Timestamptz" }, { - "ordinal": 5, + "ordinal": 7, "name": "ends_at", "type_info": "Timestamptz" }, { - "ordinal": 6, + "ordinal": 8, "name": "recurrence_every", "type_info": "Int4" }, { - "ordinal": 7, + "ordinal": 9, "name": "recurrence_unit", "type_info": "Text" }, { - "ordinal": 8, + "ordinal": 10, "name": "weekend_adjustment", "type_info": "Text" } @@ -60,6 +70,8 @@ false, false, false, + true, + true, false, true, false, @@ -67,5 +79,5 @@ true ] }, - "hash": "b90fc3934e144910e05cddcfb11e66a670b842a4636960ac59f18d741cd9cc28" + "hash": "a67033cd73e269ec203817b402ef40647a1965dca8419e6aa9d33a5dfa9e5426" } diff --git a/server/.sqlx/query-cd3573646e7cfe523b7dc97092134f22568eb9288e8d56522bdfbbc2aab43fb4.json b/server/.sqlx/query-cd3573646e7cfe523b7dc97092134f22568eb9288e8d56522bdfbbc2aab43fb4.json new file mode 100644 index 0000000..f0dc97d --- /dev/null +++ b/server/.sqlx/query-cd3573646e7cfe523b7dc97092134f22568eb9288e8d56522bdfbbc2aab43fb4.json @@ -0,0 +1,98 @@ +{ + "db_name": "PostgreSQL", + "query": "\n INSERT INTO recurring_transactions (\n id,\n name,\n amount_cents,\n direction,\n source_account_id,\n destination_account_id,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n amount_cents = EXCLUDED.amount_cents,\n direction = EXCLUDED.direction,\n source_account_id = EXCLUDED.source_account_id,\n destination_account_id = EXCLUDED.destination_account_id,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n recurrence_every = EXCLUDED.recurrence_every,\n recurrence_unit = EXCLUDED.recurrence_unit,\n weekend_adjustment = EXCLUDED.weekend_adjustment\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n new_row.id AS \"id!\",\n new_row.name AS \"name!\",\n new_row.amount_cents AS \"amount_cents!\",\n new_row.direction AS \"direction!\",\n new_row.source_account_id AS \"source_account_id?\",\n new_row.destination_account_id AS \"destination_account_id?\",\n new_row.starts_at AS \"starts_at!\",\n new_row.ends_at AS \"ends_at?\",\n new_row.recurrence_every AS \"recurrence_every!\",\n new_row.recurrence_unit AS \"recurrence_unit!\",\n new_row.weekend_adjustment AS \"weekend_adjustment?\"\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "old_id?", + "type_info": "Uuid" + }, + { + "ordinal": 1, + "name": "id!", + "type_info": "Uuid" + }, + { + "ordinal": 2, + "name": "name!", + "type_info": "Text" + }, + { + "ordinal": 3, + "name": "amount_cents!", + "type_info": "Int8" + }, + { + "ordinal": 4, + "name": "direction!", + "type_info": "Text" + }, + { + "ordinal": 5, + "name": "source_account_id?", + "type_info": "Uuid" + }, + { + "ordinal": 6, + "name": "destination_account_id?", + "type_info": "Uuid" + }, + { + "ordinal": 7, + "name": "starts_at!", + "type_info": "Timestamptz" + }, + { + "ordinal": 8, + "name": "ends_at?", + "type_info": "Timestamptz" + }, + { + "ordinal": 9, + "name": "recurrence_every!", + "type_info": "Int4" + }, + { + "ordinal": 10, + "name": "recurrence_unit!", + "type_info": "Text" + }, + { + "ordinal": 11, + "name": "weekend_adjustment?", + "type_info": "Text" + } + ], + "parameters": { + "Left": [ + "Uuid", + "Text", + "Int8", + "Text", + "Uuid", + "Uuid", + "Timestamptz", + "Timestamptz", + "Int4", + "Text", + "Text" + ] + }, + "nullable": [ + false, + false, + false, + false, + false, + true, + true, + false, + true, + false, + false, + true + ] + }, + "hash": "cd3573646e7cfe523b7dc97092134f22568eb9288e8d56522bdfbbc2aab43fb4" +} diff --git a/server/.sqlx/query-1a4dc56d068d3d5b531ca43e8e0bc5ba6789ffce3f896334780965afd1cc214f.json b/server/.sqlx/query-fae9d6766e5bce18e7917df6a0d6ad13ec035b8d409b880d6cff2ef9e2615fa4.json similarity index 68% rename from server/.sqlx/query-1a4dc56d068d3d5b531ca43e8e0bc5ba6789ffce3f896334780965afd1cc214f.json rename to server/.sqlx/query-fae9d6766e5bce18e7917df6a0d6ad13ec035b8d409b880d6cff2ef9e2615fa4.json index 6b0d357..5f5bfce 100644 --- a/server/.sqlx/query-1a4dc56d068d3d5b531ca43e8e0bc5ba6789ffce3f896334780965afd1cc214f.json +++ b/server/.sqlx/query-fae9d6766e5bce18e7917df6a0d6ad13ec035b8d409b880d6cff2ef9e2615fa4.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n FROM recurring_transactions\n WHERE id = $1\n ", + "query": "\n SELECT\n id,\n name,\n amount_cents,\n direction,\n source_account_id,\n destination_account_id,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n FROM recurring_transactions\n WHERE id = $1\n ", "describe": { "columns": [ { @@ -25,26 +25,36 @@ }, { "ordinal": 4, + "name": "source_account_id", + "type_info": "Uuid" + }, + { + "ordinal": 5, + "name": "destination_account_id", + "type_info": "Uuid" + }, + { + "ordinal": 6, "name": "starts_at", "type_info": "Timestamptz" }, { - "ordinal": 5, + "ordinal": 7, "name": "ends_at", "type_info": "Timestamptz" }, { - "ordinal": 6, + "ordinal": 8, "name": "recurrence_every", "type_info": "Int4" }, { - "ordinal": 7, + "ordinal": 9, "name": "recurrence_unit", "type_info": "Text" }, { - "ordinal": 8, + "ordinal": 10, "name": "weekend_adjustment", "type_info": "Text" } @@ -59,6 +69,8 @@ false, false, false, + true, + true, false, true, false, @@ -66,5 +78,5 @@ true ] }, - "hash": "1a4dc56d068d3d5b531ca43e8e0bc5ba6789ffce3f896334780965afd1cc214f" + "hash": "fae9d6766e5bce18e7917df6a0d6ad13ec035b8d409b880d6cff2ef9e2615fa4" } diff --git a/server/migrations/20260927000000_create_accounts.sql b/server/migrations/20260918000000_create_accounts.sql similarity index 100% rename from server/migrations/20260927000000_create_accounts.sql rename to server/migrations/20260918000000_create_accounts.sql diff --git a/server/migrations/20260920000000_create_recurring_transactions.sql b/server/migrations/20260920000000_create_recurring_transactions.sql index d687c92..d1a1db0 100644 --- a/server/migrations/20260920000000_create_recurring_transactions.sql +++ b/server/migrations/20260920000000_create_recurring_transactions.sql @@ -3,6 +3,8 @@ CREATE TABLE recurring_transactions ( name TEXT NOT NULL, amount_cents BIGINT NOT NULL CHECK (amount_cents >= 0), direction TEXT NOT NULL CHECK (direction IN ('in', 'out')), + source_account_id UUID REFERENCES accounts (id), + destination_account_id UUID REFERENCES accounts (id), starts_at TIMESTAMPTZ NOT NULL, ends_at TIMESTAMPTZ, recurrence_every INTEGER NOT NULL CHECK (recurrence_every > 0), diff --git a/server/src/database.rs b/server/src/database.rs index 1d56ad7..0be2fc7 100644 --- a/server/src/database.rs +++ b/server/src/database.rs @@ -2,3 +2,23 @@ pub mod accounts; pub mod dtos; pub mod recurring_transactions; pub mod transactions; + +#[derive(Debug, thiserror::Error)] +pub enum DatabaseError { + #[error("Foreign key violation")] + ForeignKeyViolation, + + #[error("Internal error: {0}")] + Internal(#[source] sqlx::Error), +} + +impl From for DatabaseError { + fn from(err: sqlx::Error) -> Self { + match err { + sqlx::Error::Database(db_err) if db_err.code().as_deref() == Some("23503") => { + DatabaseError::ForeignKeyViolation + } + _ => DatabaseError::Internal(err), + } + } +} diff --git a/server/src/database/accounts.rs b/server/src/database/accounts.rs index 4341451..d63a782 100644 --- a/server/src/database/accounts.rs +++ b/server/src/database/accounts.rs @@ -1,6 +1,9 @@ use uuid::Uuid; -use super::dtos::{AccountRow, Delete, Upsert}; +use super::{ + DatabaseError, + dtos::{AccountRow, Delete, Upsert}, +}; pub struct Query { pub name: Option, @@ -18,7 +21,7 @@ impl Default for Query { } } -pub async fn query<'e, E>(executor: E, query: Query) -> Result, sqlx::Error> +pub async fn query<'e, E>(executor: E, query: Query) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -38,9 +41,10 @@ where ) .fetch_all(executor) .await + .map_err(DatabaseError::from) } -pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result, sqlx::Error> +pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -55,12 +59,13 @@ where ) .fetch_optional(executor) .await + .map_err(DatabaseError::from) } pub async fn upsert<'e, E>( executor: E, account: AccountRow, -) -> Result, sqlx::Error> +) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -79,7 +84,8 @@ where account.name, ) .fetch_one(executor) - .await?; + .await + .map_err(DatabaseError::from)?; let account = AccountRow { id: row.id, @@ -93,13 +99,14 @@ where } } -pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result +pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { let deleted = sqlx::query!("DELETE FROM accounts WHERE id = $1 RETURNING id", id) .fetch_optional(executor) - .await?; + .await + .map_err(DatabaseError::from)?; Ok(if deleted.is_some() { Delete::Deleted diff --git a/server/src/database/dtos/recurring_transaction.rs b/server/src/database/dtos/recurring_transaction.rs index ad0aeb0..9068612 100644 --- a/server/src/database/dtos/recurring_transaction.rs +++ b/server/src/database/dtos/recurring_transaction.rs @@ -6,6 +6,8 @@ pub struct RecurringTransactionRow { pub name: String, pub amount_cents: i64, pub direction: String, + pub source_account_id: Option, + pub destination_account_id: Option, pub starts_at: DateTime, pub ends_at: Option>, pub recurrence_every: i32, diff --git a/server/src/database/recurring_transactions.rs b/server/src/database/recurring_transactions.rs index c9a333e..fdf03e0 100644 --- a/server/src/database/recurring_transactions.rs +++ b/server/src/database/recurring_transactions.rs @@ -1,6 +1,9 @@ use uuid::Uuid; -use super::dtos::{Delete, RecurringTransactionRow, Upsert}; +use super::{ + DatabaseError, + dtos::{Delete, RecurringTransactionRow, Upsert}, +}; pub struct Query { pub limit: Option, @@ -19,7 +22,7 @@ impl Default for Query { pub async fn query<'e, E>( executor: E, query: Query, -) -> Result, sqlx::Error> +) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -31,6 +34,8 @@ where name, amount_cents, direction, + source_account_id, + destination_account_id, starts_at, ends_at, recurrence_every, @@ -45,12 +50,13 @@ where ) .fetch_all(executor) .await + .map_err(DatabaseError::from) } pub async fn get_by_id<'e, E>( executor: E, id: Uuid, -) -> Result, sqlx::Error> +) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -62,6 +68,8 @@ where name, amount_cents, direction, + source_account_id, + destination_account_id, starts_at, ends_at, recurrence_every, @@ -74,12 +82,13 @@ where ) .fetch_optional(executor) .await + .map_err(DatabaseError::from) } pub async fn upsert<'e, E>( executor: E, transaction: RecurringTransactionRow, -) -> Result, sqlx::Error> +) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -90,17 +99,21 @@ where name, amount_cents, direction, + source_account_id, + destination_account_id, starts_at, ends_at, recurrence_every, recurrence_unit, weekend_adjustment ) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, amount_cents = EXCLUDED.amount_cents, direction = EXCLUDED.direction, + source_account_id = EXCLUDED.source_account_id, + destination_account_id = EXCLUDED.destination_account_id, starts_at = EXCLUDED.starts_at, ends_at = EXCLUDED.ends_at, recurrence_every = EXCLUDED.recurrence_every, @@ -112,6 +125,8 @@ where new_row.name AS "name!", new_row.amount_cents AS "amount_cents!", new_row.direction AS "direction!", + new_row.source_account_id AS "source_account_id?", + new_row.destination_account_id AS "destination_account_id?", new_row.starts_at AS "starts_at!", new_row.ends_at AS "ends_at?", new_row.recurrence_every AS "recurrence_every!", @@ -122,6 +137,8 @@ where transaction.name, transaction.amount_cents, transaction.direction, + transaction.source_account_id, + transaction.destination_account_id, transaction.starts_at, transaction.ends_at, transaction.recurrence_every, @@ -129,13 +146,16 @@ where transaction.weekend_adjustment, ) .fetch_one(executor) - .await?; + .await + .map_err(DatabaseError::from)?; let transaction = RecurringTransactionRow { id: row.id, name: row.name, amount_cents: row.amount_cents, direction: row.direction, + source_account_id: row.source_account_id, + destination_account_id: row.destination_account_id, starts_at: row.starts_at, ends_at: row.ends_at, recurrence_every: row.recurrence_every, @@ -150,7 +170,7 @@ where } } -pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result +pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -159,7 +179,8 @@ where id, ) .fetch_optional(executor) - .await?; + .await + .map_err(DatabaseError::from)?; Ok(if deleted.is_some() { Delete::Deleted diff --git a/server/src/database/transactions.rs b/server/src/database/transactions.rs index b20ec0a..9876882 100644 --- a/server/src/database/transactions.rs +++ b/server/src/database/transactions.rs @@ -1,6 +1,9 @@ use uuid::Uuid; -use super::dtos::{Delete, TransactionRow, Upsert}; +use super::{ + DatabaseError, + dtos::{Delete, TransactionRow, Upsert}, +}; pub struct Query { pub limit: Option, @@ -16,7 +19,7 @@ impl Default for Query { } } -pub async fn query<'e, E>(executor: E, query: Query) -> Result, sqlx::Error> +pub async fn query<'e, E>(executor: E, query: Query) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -34,9 +37,13 @@ where ) .fetch_all(executor) .await + .map_err(DatabaseError::from) } -pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result, sqlx::Error> +pub async fn get_by_id<'e, E>( + executor: E, + id: Uuid, +) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -51,12 +58,13 @@ where ) .fetch_optional(executor) .await + .map_err(DatabaseError::from) } pub async fn upsert<'e, E>( executor: E, transaction: TransactionRow, -) -> Result, sqlx::Error> +) -> Result, DatabaseError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { @@ -84,7 +92,8 @@ where transaction.date, ) .fetch_one(executor) - .await?; + .await + .map_err(DatabaseError::from)?; let transaction = TransactionRow { id: row.id, @@ -101,13 +110,14 @@ where } } -pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result +pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { let deleted = sqlx::query!("DELETE FROM transactions WHERE id = $1 RETURNING id", id) .fetch_optional(executor) - .await?; + .await + .map_err(DatabaseError::from)?; Ok(if deleted.is_some() { Delete::Deleted diff --git a/server/src/domain/recurring_transaction.rs b/server/src/domain/recurring_transaction.rs index 2cd4c72..6c735e5 100644 --- a/server/src/domain/recurring_transaction.rs +++ b/server/src/domain/recurring_transaction.rs @@ -8,5 +8,7 @@ pub struct RecurringTransaction { pub name: String, pub amount: Cents, pub direction: Direction, + pub source_account_id: Option, + pub destination_account_id: Option, pub schedule: Schedule, } diff --git a/server/src/error.rs b/server/src/error.rs index 288eae9..fa3b76b 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -3,7 +3,7 @@ use axum::{ response::{IntoResponse, Response}, }; -use crate::{domain::DomainError, repository::RepositoryError}; +use crate::{database::DatabaseError, domain::DomainError, repository::RepositoryError}; #[derive(Debug, thiserror::Error)] pub enum AppError { @@ -32,6 +32,9 @@ impl IntoResponse for AppError { Self::Domain(error) => { return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string()); } + Self::Repository(RepositoryError::Database(DatabaseError::ForeignKeyViolation)) => { + return error_response(StatusCode::UNPROCESSABLE_ENTITY, "Reference violation"); + } Self::Repository(RepositoryError::Database(error)) => { tracing::error!(%error, "Database query failed"); } diff --git a/server/src/repository.rs b/server/src/repository.rs index ba57d77..7a6e914 100644 --- a/server/src/repository.rs +++ b/server/src/repository.rs @@ -1,4 +1,7 @@ -use crate::domain::{CentsError, ScheduleError}; +use crate::{ + database::DatabaseError, + domain::{CentsError, ScheduleError}, +}; pub mod accounts; pub mod dtos; @@ -9,7 +12,7 @@ pub mod transactions; #[derive(Debug, thiserror::Error)] pub enum RepositoryError { #[error(transparent)] - Database(#[from] sqlx::Error), + Database(#[from] DatabaseError), #[error(transparent)] Decode(#[from] DecodeError), diff --git a/server/src/repository/recurring_transactions.rs b/server/src/repository/recurring_transactions.rs index 79cd72a..8e7e626 100644 --- a/server/src/repository/recurring_transactions.rs +++ b/server/src/repository/recurring_transactions.rs @@ -109,6 +109,8 @@ fn decode(row: RecurringTransactionRow) -> Result RecurringTransactionRow { name: transaction.name, amount_cents: transaction.amount.into(), direction: encode_direction(transaction.direction).to_owned(), + source_account_id: transaction.source_account_id, + destination_account_id: transaction.destination_account_id, starts_at: transaction.schedule.start(), ends_at: transaction.schedule.end(), recurrence_every: recurrence.every.into(), @@ -146,6 +150,8 @@ mod tests { name: "Rent".to_owned(), amount_cents: 120_000, direction: "out".to_owned(), + source_account_id: Some(Uuid::new_v4()), + destination_account_id: Some(Uuid::new_v4()), starts_at: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(), ends_at: None, recurrence_every: 1, @@ -157,7 +163,12 @@ mod tests { #[test] fn decodes_valid_rows() { let row = row(); - let (id, start) = (row.id, row.starts_at); + let (id, start, source_account_id, destination_account_id) = ( + row.id, + row.starts_at, + row.source_account_id, + row.destination_account_id, + ); let transaction = decode(row).unwrap(); let recurrence = transaction.schedule.recurrence(); @@ -165,6 +176,8 @@ mod tests { assert_eq!(transaction.name, "Rent"); assert_eq!(transaction.amount.get(), 120_000); assert_eq!(transaction.direction, Direction::Out); + assert_eq!(transaction.source_account_id, source_account_id); + assert_eq!(transaction.destination_account_id, destination_account_id); assert_eq!(transaction.schedule.start(), start); assert_eq!(transaction.schedule.end(), None); assert_eq!(recurrence.every.get(), 1); diff --git a/server/src/routes/dtos/scheduled_transaction.rs b/server/src/routes/dtos/scheduled_transaction.rs index 9cd8908..715668f 100644 --- a/server/src/routes/dtos/scheduled_transaction.rs +++ b/server/src/routes/dtos/scheduled_transaction.rs @@ -30,6 +30,8 @@ pub struct RecurringTransactionRequest { pub name: String, pub amount_cents: u64, pub direction: Direction, + pub source_account_id: Option, + pub destination_account_id: Option, pub schedule: ScheduleRequest, } @@ -48,6 +50,8 @@ impl RecurringTransactionRequest { name: self.name, amount: domain::Cents::try_from(self.amount_cents)?, direction: self.direction.into(), + source_account_id: self.source_account_id, + destination_account_id: self.destination_account_id, schedule: domain::Schedule::new( self.schedule.start, self.schedule.end, @@ -64,6 +68,8 @@ pub struct RecurringTransactionResponse { pub name: String, pub amount_cents: u64, pub direction: Direction, + pub source_account_id: Option, + pub destination_account_id: Option, pub schedule: ScheduleResponse, } @@ -74,6 +80,8 @@ impl RecurringTransactionResponse { name: transaction.name, amount_cents: transaction.amount.get(), direction: transaction.direction.into(), + source_account_id: transaction.source_account_id, + destination_account_id: transaction.destination_account_id, schedule: ScheduleResponse::new(transaction.schedule, next), } } @@ -199,6 +207,8 @@ mod tests { name: String::new(), amount_cents, direction: Direction::Out, + source_account_id: None, + destination_account_id: None, schedule: ScheduleRequest { start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(), end: None,