1
//! A stored transaction's splits with their complete tag lists.
2

            
3
use std::collections::HashMap;
4

            
5
use finance::price::Price;
6
use finance::split::Split;
7
use finance::tag::Tag;
8
use finance::transaction::Transaction;
9
use server::logical::{is_note, pick_note};
10
use server::user::User;
11
use uuid::Uuid;
12

            
13
use super::{Entity, Failure};
14
use crate::host_failure::cmd_failure;
15
use crate::resolve::Directory;
16
use 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)]
21
pub 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)]
30
pub struct StoredSplit {
31
    pub split: Split,
32
    pub tags: Vec<Tag>,
33
}
34

            
35
960
fn labels(directory: &Directory, source: DirectorySource) -> HashMap<Uuid, String> {
36
960
    directory
37
960
        .options(source)
38
960
        .into_iter()
39
1760
        .map(|option| (option.id, option.label))
40
        .collect()
41
960
}
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)]
46
pub 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

            
54
12507
fn sorted(mut tags: Vec<Tag>) -> Vec<Tag> {
55
12507
    tags.sort_by(|a, b| (&a.tag_name, &a.tag_value).cmp(&(&b.tag_name, &b.tag_value)));
56
12507
    tags
57
12507
}
58

            
59
/// The user's transaction as one snapshot, without any labels; `Missing`
60
/// when it does not exist.
61
4330
pub async fn detail(user_id: Uuid, transaction_id: Uuid) -> Result<TransactionDetail, Failure> {
62
4330
    let snapshot = User { id: user_id }
63
4330
        .transaction_snapshot(transaction_id)
64
4330
        .await
65
        .map_err(|err| cmd_failure("transaction-detail", err.into()))?
66
4330
        .ok_or(Failure::Missing(Entity::Transaction))?;
67
3849
    let note = pick_note(snapshot.tags.iter()).map(|tag| tag.normalized().1.to_string());
68
3849
    let mut split_tags = snapshot.split_tags;
69
3849
    let tx_tags = sorted(
70
3849
        snapshot
71
3849
            .tags
72
3849
            .into_iter()
73
4489
            .filter(|tag| !is_note(tag))
74
            .collect(),
75
    );
76
3849
    let splits = snapshot
77
3849
        .splits
78
3849
        .into_iter()
79
        .map(|split| StoredSplit {
80
8658
            tags: sorted(split_tags.remove(&split.id).unwrap_or_default()),
81
8658
            split,
82
8658
        })
83
        .collect();
84
3849
    Ok(TransactionDetail {
85
3849
        transaction: snapshot.transaction,
86
3849
        note,
87
3849
        prices: snapshot.prices,
88
3849
        tags: tx_tags,
89
3849
        splits,
90
3849
    })
91
4330
}
92

            
93
/// The splits of the transaction with account and commodity labels, for the
94
/// web row fragment.
95
800
pub async fn labelled_splits(
96
800
    user_id: Uuid,
97
800
    transaction_id: Uuid,
98
800
) -> Result<Vec<SplitDetail>, Failure> {
99
800
    let stored = detail(user_id, transaction_id).await?;
100
480
    let directory = Directory::load("transaction-detail", user_id).await?;
101
480
    let accounts = labels(&directory, DirectorySource::Accounts);
102
480
    let commodities = labels(&directory, DirectorySource::Commodities);
103
480
    Ok(stored
104
480
        .splits
105
480
        .into_iter()
106
        .map(|stored| SplitDetail {
107
960
            account: accounts
108
960
                .get(&stored.split.account_id)
109
960
                .cloned()
110
                .unwrap_or_else(|| format!("Account {}", stored.split.account_id)),
111
960
            commodity: commodities
112
960
                .get(&stored.split.commodity_id)
113
960
                .cloned()
114
960
                .unwrap_or_default(),
115
960
            tags: stored.tags,
116
960
            split: stored.split,
117
960
        })
118
        .collect())
119
800
}