Skip to main content

server/command/
transaction.rs

1mod create;
2mod delete;
3mod read;
4mod update;
5
6pub use create::CreateTransaction;
7pub use delete::{DeleteTransaction, GetTransactionTag, SetTransactionTag};
8pub use read::{GetTransaction, GetTransactionDetail, ListTransactions};
9pub use update::{KeepStored, UpdateTransaction};
10
11use finance::error::FinanceError;
12
13use super::CmdError;
14
15/// The refusal for a split posted to an account that a concurrent merge or
16/// delete removed.
17pub const ACCOUNT_GONE: &str =
18    "An account in this transaction no longer exists (merged or deleted); pick it again";
19
20/// The refusal for a transaction that shares a price with another transaction.
21pub const CROSS_PRICE: &str = "This transaction shares a price with another transaction and cannot be changed or deleted; it is listed in the data findings";
22
23/// Refuses a transaction linked to a price whose other leg is in another
24/// transaction, before anything is written.
25pub(crate) async fn refuse_cross_price(
26    conn: &mut sqlx::PgConnection,
27    transaction_id: sqlx::types::Uuid,
28) -> Result<(), CmdError> {
29    let shared = sqlx::query_file!(
30        "sql/select/prices/cross_transaction.sql",
31        Some(transaction_id)
32    )
33    .fetch_all(conn)
34    .await?;
35    match shared.is_empty() {
36        true => Ok(()),
37        false => Err(CmdError::Refused(CROSS_PRICE.to_string())),
38    }
39}
40
41fn is_gone_account(err: &sqlx::Error) -> bool {
42    err.as_database_error()
43        .and_then(|db| db.constraint())
44        .is_some_and(|constraint| constraint == "splits_account_id_fkey")
45}
46
47/// `err`, or the [`ACCOUNT_GONE`] refusal for a split insert that lost its
48/// account.
49pub(crate) fn account_gone(err: CmdError) -> CmdError {
50    match err {
51        CmdError::DB(db) | CmdError::Finance(FinanceError::DB(db)) if is_gone_account(&db) => {
52            CmdError::Refused(ACCOUNT_GONE.to_string())
53        }
54        other => other,
55    }
56}
57
58#[cfg(test)]
59#[path = "transaction_tests/mod.rs"]
60mod command_tests;