Lines
100 %
Functions
92.86 %
Branches
mod create;
mod delete;
mod read;
mod update;
pub use create::CreateTransaction;
pub use delete::{DeleteTransaction, GetTransactionTag, SetTransactionTag};
pub use read::{GetTransaction, GetTransactionDetail, ListTransactions};
pub use update::{KeepStored, UpdateTransaction};
use finance::error::FinanceError;
use super::CmdError;
/// The refusal for a split posted to an account that a concurrent merge or
/// delete removed.
pub const ACCOUNT_GONE: &str =
"An account in this transaction no longer exists (merged or deleted); pick it again";
/// The refusal for a transaction that shares a price with another transaction.
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";
/// Refuses a transaction linked to a price whose other leg is in another
/// transaction, before anything is written.
pub(crate) async fn refuse_cross_price(
conn: &mut sqlx::PgConnection,
transaction_id: sqlx::types::Uuid,
) -> Result<(), CmdError> {
let shared = sqlx::query_file!(
"sql/select/prices/cross_transaction.sql",
Some(transaction_id)
)
.fetch_all(conn)
.await?;
match shared.is_empty() {
true => Ok(()),
false => Err(CmdError::Refused(CROSS_PRICE.to_string())),
}
fn is_gone_account(err: &sqlx::Error) -> bool {
err.as_database_error()
.and_then(|db| db.constraint())
.is_some_and(|constraint| constraint == "splits_account_id_fkey")
/// `err`, or the [`ACCOUNT_GONE`] refusal for a split insert that lost its
/// account.
pub(crate) fn account_gone(err: CmdError) -> CmdError {
match err {
CmdError::DB(db) | CmdError::Finance(FinanceError::DB(db)) if is_gone_account(&db) => {
CmdError::Refused(ACCOUNT_GONE.to_string())
other => other,
#[cfg(test)]
#[path = "transaction_tests/mod.rs"]
mod command_tests;