1
//! `get-transaction-logical`: a stored transaction as the from→to plist that
2
//! `create-transaction-logical` / `update-transaction-logical` accept.
3

            
4
use finance::tag::Tag;
5
use server::command::CmdError;
6
use server::logical::{RaisedRow, RowTags, pick_note};
7
use server::user::User;
8
use uuid::Uuid;
9

            
10
use super::parse::parse_transaction_id_arg;
11
use super::render::quote_string;
12

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

            
16
const CTX: &str = "get-transaction-logical";
17

            
18
1626
fn render_tag(tag: &Tag) -> String {
19
1626
    let description = tag
20
1626
        .description
21
1626
        .as_deref()
22
7
        .map(|d| format!(" :description {}", quote_string(d)))
23
        .unwrap_or_default();
24
1626
    format!(
25
1626
        "(:name {} :value {}{description})",
26
1626
        quote_string(&tag.tag_name),
27
1626
        quote_string(&tag.tag_value)
28
    )
29
1626
}
30

            
31
/// `((:name "n" :value "v" [:description "d"]) …)`; `()` when empty.
32
11078
pub(in crate::natives) fn render_tags(tags: &[Tag]) -> String {
33
11078
    let items: Vec<String> = tags.iter().map(render_tag).collect();
34
11078
    format!("({})", items.join(" "))
35
11078
}
36

            
37
36
fn render_row_tags(key: &str, tags: &[Tag]) -> String {
38
36
    match tags {
39
22
        [] => String::new(),
40
14
        _ => format!(" :{key} {}", render_tags(tags)),
41
    }
42
36
}
43

            
44
12
fn render_row(row: &RaisedRow, tags: &RowTags) -> String {
45
12
    let to_amount = row
46
12
        .to_amount
47
3
        .map(|v| format!(" :to-amount {}", amount::wire_rational(v)))
48
        .unwrap_or_default();
49
12
    let tags = [
50
12
        ("tags", &tags.shared),
51
12
        ("from-tags", &tags.from),
52
12
        ("to-tags", &tags.to),
53
12
    ]
54
36
    .map(|(key, tags)| render_row_tags(key, tags))
55
    .concat();
56
12
    format!(
57
12
        "(:from \"{}\" :to \"{}\" :from-commodity \"{}\" :to-commodity \"{}\" :value {}{to_amount}{tags})",
58
12
        row.from_account,
59
12
        row.to_account,
60
12
        row.from_commodity,
61
12
        row.to_commodity,
62
12
        amount::wire_rational(row.value),
63
    )
64
12
}
65

            
66
12
fn render_logical(detail: &Detail, rows: &[TaggedRow]) -> String {
67
12
    let rendered: Vec<String> = rows
68
12
        .iter()
69
12
        .map(|(row, tags)| render_row(row, tags))
70
        .collect();
71
12
    let note = detail
72
12
        .note
73
12
        .as_deref()
74
7
        .map(|n| format!(" :note {}", quote_string(n)))
75
        .unwrap_or_default();
76
12
    format!(
77
12
        "(:transaction-id \"{}\" :date \"{}\"{note} :splits ({}))",
78
12
        detail.transaction.id,
79
12
        detail.transaction.post_date.to_rfc3339(),
80
12
        rendered.join(" ")
81
    )
82
12
}
83

            
84
16
pub(super) async fn run_get_transaction_logical(
85
16
    user_id: Uuid,
86
16
    id_arg: Option<String>,
87
16
) -> wasmtime::Result<String> {
88
16
    let transaction_id = parse_transaction_id_arg(id_arg)?;
89
16
    let snapshot = User { id: user_id }
90
16
        .transaction_snapshot(transaction_id)
91
16
        .await
92
        .map_err(|err| cmd_failure(CTX, err.into()))?
93
1
        .ok_or_else(|| cmd_failure(CTX, CmdError::NotFound("Transaction not found".to_string())))?;
94
15
    let note = pick_note(snapshot.tags.iter()).map(|tag| tag.tag_value.clone());
95
15
    let detail = Detail {
96
15
        transaction: snapshot.transaction,
97
15
        note,
98
15
        prices: snapshot.prices,
99
15
    };
100
15
    let rows = raise_rows(&snapshot.splits, &snapshot.split_tags, &detail.prices)
101
3
        .map_err(|err| cmd_failure(CTX, err))?;
102
12
    Ok(render_logical(&detail, &rows))
103
16
}