1
//! A stored transaction as the edit form can represent it.
2

            
3
use std::collections::HashMap;
4

            
5
use finance::split::Split;
6
use finance::tag::Tag;
7
use server::command::CmdError;
8
use server::logical::{form_refusal, is_note, pick_note, stored_revision};
9
use server::user::User;
10
use uuid::Uuid;
11

            
12
use super::Failure;
13
use crate::host_failure::cmd_failure;
14
use crate::stored::raise_rows;
15
use crate::stored::{Detail, TaggedRow};
16

            
17
pub(super) struct Stored {
18
    pub(super) detail: Detail,
19
    /// Transaction tags except the note, sorted by name.
20
    pub(super) tags: Vec<Tag>,
21
    pub(super) rows: Vec<TaggedRow>,
22
    /// The server's digest of the raw stored state; a changed digest means the
23
    /// loaded form is stale.
24
    pub(super) revision: String,
25
}
26

            
27
fn refusal(err: CmdError) -> Failure {
28
    Failure::Refused(match err {
29
        CmdError::Args(reason) => reason,
30
        other => other.to_string(),
31
    })
32
}
33

            
34
/// Every non-note tag of the transaction, sorted by name and value, with the
35
/// note.
36
10815
fn other_tags(mut tags: Vec<Tag>) -> (Vec<Tag>, Option<String>) {
37
10815
    sort(&mut tags);
38
10815
    let note = pick_note(tags.iter()).map(|tag| tag.normalized().1.to_string());
39
34341
    tags.retain(|tag| !is_note(tag));
40
10815
    (tags, note)
41
10815
}
42

            
43
41763
fn sort(tags: &mut [Tag]) {
44
41763
    tags.sort_by(|a, b| (&a.tag_name, &a.tag_value).cmp(&(&b.tag_name, &b.tag_value)));
45
41763
}
46

            
47
/// Every tag of each split, sorted, a split without tags included.
48
10815
fn split_tags(splits: &[Split], mut all: HashMap<Uuid, Vec<Tag>>) -> HashMap<Uuid, Vec<Tag>> {
49
10815
    for split in splits {
50
30948
        sort(all.entry(split.id).or_default());
51
    }
52
10815
    all
53
10815
}
54

            
55
/// Whether the transaction is the user's, without the form's refusals.
56
6155
pub(super) async fn is_owned(user_id: Uuid, transaction_id: Uuid) -> Result<bool, Failure> {
57
6155
    let snapshot = User { id: user_id }
58
6155
        .transaction_snapshot(transaction_id)
59
6155
        .await
60
        .map_err(|err| cmd_failure("load-form", err.into()))?;
61
6155
    Ok(snapshot.is_some())
62
6155
}
63

            
64
/// The user's transaction raised to rows, `None` when it is not theirs. A
65
/// transaction the form would corrupt is a refusal, never a form.
66
11791
pub(super) async fn load_stored(
67
11791
    user_id: Uuid,
68
11791
    transaction_id: Uuid,
69
11791
) -> Result<Option<Stored>, Failure> {
70
11791
    let Some(snapshot) = User { id: user_id }
71
11791
        .transaction_snapshot(transaction_id)
72
11791
        .await
73
2
        .map_err(|err| cmd_failure("load-form", err.into()))?
74
    else {
75
323
        return Ok(None);
76
    };
77
11466
    form_refusal(
78
11466
        &snapshot.tags,
79
11466
        &snapshot.splits,
80
11466
        &snapshot.split_tags,
81
11466
        &snapshot.prices,
82
11466
    )
83
11466
    .map_err(Failure::Refused)?;
84
10815
    let revision = stored_revision(
85
10815
        snapshot.transaction.post_date,
86
10815
        snapshot.tags.clone(),
87
10815
        &snapshot.splits,
88
10815
        &snapshot.split_tags,
89
10815
        &snapshot.prices,
90
10815
    );
91
10815
    let (tags, note) = other_tags(snapshot.tags);
92
10815
    let splits = snapshot.splits;
93
10815
    let split_tags = split_tags(&splits, snapshot.split_tags);
94
10815
    let detail = Detail {
95
10815
        transaction: snapshot.transaction,
96
10815
        note,
97
10815
        prices: snapshot.prices,
98
10815
    };
99
10815
    let rows = raise_rows(&splits, &split_tags, &detail.prices).map_err(refusal)?;
100
10815
    Ok(Some(Stored {
101
10815
        detail,
102
10815
        tags,
103
10815
        rows,
104
10815
        revision,
105
10815
    }))
106
11791
}