Skip to main content

server/
split.rs

1//! Reading the splits of a transaction and their tags.
2
3use std::collections::HashMap;
4
5use finance::split::Split;
6use finance::tag::Tag;
7use sqlx::PgConnection;
8use sqlx::types::Uuid;
9
10use crate::db::DBError;
11use crate::error::ServerError;
12use crate::user::User;
13
14/// Every stored tag of each split by split id, ordered by name and value; a
15/// name with two values keeps both.
16///
17/// # Errors
18/// A query failing.
19pub(crate) async fn split_tags_of(
20    conn: &mut PgConnection,
21    splits: &[Split],
22) -> Result<HashMap<Uuid, Vec<Tag>>, sqlx::Error> {
23    let mut all = HashMap::with_capacity(splits.len());
24    for split in splits {
25        all.insert(split.id, tags_of_split(conn, split.id).await?);
26    }
27    Ok(all)
28}
29
30/// The tags of one split on a held connection, in the order of [`split_tags_of`].
31///
32/// # Errors
33/// The query failing.
34pub(crate) async fn tags_of_split(
35    conn: &mut PgConnection,
36    split_id: Uuid,
37) -> Result<Vec<Tag>, sqlx::Error> {
38    sqlx::query_file_as!(Tag, "sql/select/tags/by_split.sql", split_id)
39        .fetch_all(conn)
40        .await
41}
42
43/// Logging is left to the caller's boundary, which knows a missing row from a fault.
44fn db_error(err: sqlx::Error) -> ServerError {
45    ServerError::DB(DBError::Sqlx(err))
46}
47
48impl User {
49    /// Confirms the transaction exists.
50    ///
51    /// # Errors
52    /// `RowNotFound` when it does not, or a failing query.
53    pub async fn require_transaction(&self, tx_id: Uuid) -> Result<(), ServerError> {
54        let mut conn = self.get_connection().await?;
55        sqlx::query_file!("sql/select/transactions/by_id.sql", tx_id)
56            .fetch_one(&mut *conn)
57            .await
58            .map_err(db_error)?;
59        Ok(())
60    }
61
62    /// Confirms the split exists.
63    ///
64    /// # Errors
65    /// `RowNotFound` when it does not, or a failing query.
66    pub async fn require_split(&self, split_id: Uuid) -> Result<(), ServerError> {
67        let mut conn = self.get_connection().await?;
68        sqlx::query_file!("sql/select/splits/tx_id_by_id.sql", &split_id)
69            .fetch_one(&mut *conn)
70            .await
71            .map_err(db_error)?;
72        Ok(())
73    }
74
75    /// The splits of a transaction, without their tags.
76    ///
77    /// # Errors
78    /// The connection or the query failing.
79    pub async fn get_transaction_splits(&self, tx_id: Uuid) -> Result<Vec<Split>, ServerError> {
80        let mut conn = self.get_connection().await?;
81        sqlx::query_file_as!(Split, "sql/select/splits/by_transaction.sql", tx_id)
82            .fetch_all(&mut *conn)
83            .await
84            .map_err(db_error)
85    }
86
87    /// Every stored tag of each of `splits`, as `split_tags_of` reads them.
88    ///
89    /// # Errors
90    /// The connection or a query failing.
91    pub async fn split_tags_of(
92        &self,
93        splits: &[Split],
94    ) -> Result<HashMap<Uuid, Vec<Tag>>, ServerError> {
95        let mut conn = self.get_connection().await?;
96        split_tags_of(&mut conn, splits).await.map_err(db_error)
97    }
98}
99
100#[cfg(test)]
101#[path = "split_tests.rs"]
102mod tests;