Skip to main content

rpc/forms/
detail.rs

1//! A stored transaction's splits with their complete tag lists.
2
3use std::collections::HashMap;
4
5use finance::price::Price;
6use finance::split::Split;
7use finance::tag::Tag;
8use finance::transaction::Transaction;
9use server::logical::{is_note, pick_note};
10use server::user::User;
11use uuid::Uuid;
12
13use super::{Entity, Failure};
14use crate::host_failure::cmd_failure;
15use crate::resolve::Directory;
16use crate::resolve::options::DirectorySource;
17
18/// One stored split with its account path, commodity symbol and all its
19/// tags, sorted by name then value.
20#[derive(Debug)]
21pub struct SplitDetail {
22    pub split: Split,
23    pub account: String,
24    pub commodity: String,
25    pub tags: Vec<Tag>,
26}
27
28/// One stored split with all its tags, sorted by name then value.
29#[derive(Debug)]
30pub struct StoredSplit {
31    pub split: Split,
32    pub tags: Vec<Tag>,
33}
34
35fn labels(directory: &Directory, source: DirectorySource) -> HashMap<Uuid, String> {
36    directory
37        .options(source)
38        .into_iter()
39        .map(|option| (option.id, option.label))
40        .collect()
41}
42
43/// A stored transaction as one complete read: header, prices, its own tags
44/// (the `note` tag excluded, it is `note`) and the splits with their tags.
45#[derive(Debug)]
46pub struct TransactionDetail {
47    pub transaction: Transaction,
48    pub note: Option<String>,
49    pub prices: Vec<Price>,
50    pub tags: Vec<Tag>,
51    pub splits: Vec<StoredSplit>,
52}
53
54fn sorted(mut tags: Vec<Tag>) -> Vec<Tag> {
55    tags.sort_by(|a, b| (&a.tag_name, &a.tag_value).cmp(&(&b.tag_name, &b.tag_value)));
56    tags
57}
58
59/// The user's transaction as one snapshot, without any labels; `Missing`
60/// when it does not exist.
61pub async fn detail(user_id: Uuid, transaction_id: Uuid) -> Result<TransactionDetail, Failure> {
62    let snapshot = User { id: user_id }
63        .transaction_snapshot(transaction_id)
64        .await
65        .map_err(|err| cmd_failure("transaction-detail", err.into()))?
66        .ok_or(Failure::Missing(Entity::Transaction))?;
67    let note = pick_note(snapshot.tags.iter()).map(|tag| tag.normalized().1.to_string());
68    let mut split_tags = snapshot.split_tags;
69    let tx_tags = sorted(
70        snapshot
71            .tags
72            .into_iter()
73            .filter(|tag| !is_note(tag))
74            .collect(),
75    );
76    let splits = snapshot
77        .splits
78        .into_iter()
79        .map(|split| StoredSplit {
80            tags: sorted(split_tags.remove(&split.id).unwrap_or_default()),
81            split,
82        })
83        .collect();
84    Ok(TransactionDetail {
85        transaction: snapshot.transaction,
86        note,
87        prices: snapshot.prices,
88        tags: tx_tags,
89        splits,
90    })
91}
92
93/// The splits of the transaction with account and commodity labels, for the
94/// web row fragment.
95pub async fn labelled_splits(
96    user_id: Uuid,
97    transaction_id: Uuid,
98) -> Result<Vec<SplitDetail>, Failure> {
99    let stored = detail(user_id, transaction_id).await?;
100    let directory = Directory::load("transaction-detail", user_id).await?;
101    let accounts = labels(&directory, DirectorySource::Accounts);
102    let commodities = labels(&directory, DirectorySource::Commodities);
103    Ok(stored
104        .splits
105        .into_iter()
106        .map(|stored| SplitDetail {
107            account: accounts
108                .get(&stored.split.account_id)
109                .cloned()
110                .unwrap_or_else(|| format!("Account {}", stored.split.account_id)),
111            commodity: commodities
112                .get(&stored.split.commodity_id)
113                .cloned()
114                .unwrap_or_default(),
115            tags: stored.tags,
116            split: stored.split,
117        })
118        .collect())
119}