1
mod create;
2
mod delete;
3
mod read;
4
mod update;
5

            
6
pub use create::CreateTransaction;
7
pub use delete::{DeleteTransaction, GetTransactionTag, SetTransactionTag};
8
pub use read::{GetTransaction, GetTransactionDetail, ListTransactions};
9
pub use update::{KeepStored, UpdateTransaction};
10

            
11
use finance::error::FinanceError;
12

            
13
use super::CmdError;
14

            
15
/// The refusal for a split posted to an account that a concurrent merge or
16
/// delete removed.
17
pub 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.
21
pub 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.
25
24498
pub(crate) async fn refuse_cross_price(
26
24498
    conn: &mut sqlx::PgConnection,
27
24498
    transaction_id: sqlx::types::Uuid,
28
24498
) -> Result<(), CmdError> {
29
7210
    let shared = sqlx::query_file!(
30
7210
        "sql/select/prices/cross_transaction.sql",
31
7210
        Some(transaction_id)
32
    )
33
7210
    .fetch_all(conn)
34
7210
    .await?;
35
7210
    match shared.is_empty() {
36
7205
        true => Ok(()),
37
5
        false => Err(CmdError::Refused(CROSS_PRICE.to_string())),
38
    }
39
7210
}
40

            
41
366
fn is_gone_account(err: &sqlx::Error) -> bool {
42
366
    err.as_database_error()
43
366
        .and_then(|db| db.constraint())
44
365
        .is_some_and(|constraint| constraint == "splits_account_id_fkey")
45
366
}
46

            
47
/// `err`, or the [`ACCOUNT_GONE`] refusal for a split insert that lost its
48
/// account.
49
366
pub(crate) fn account_gone(err: CmdError) -> CmdError {
50
366
    match err {
51
184
        CmdError::DB(db) | CmdError::Finance(FinanceError::DB(db)) if is_gone_account(&db) => {
52
365
            CmdError::Refused(ACCOUNT_GONE.to_string())
53
        }
54
1
        other => other,
55
    }
56
366
}
57

            
58
#[cfg(test)]
59
#[path = "transaction_tests/mod.rs"]
60
mod command_tests;