1
//! `transaction-edit` submit: syntax, ownership, resolution and domain
2
//! stages, then the shared logical update path.
3

            
4
use forms::{FieldError, FormValues, Row, TRANSACTION_EDIT, address};
5
use uuid::Uuid;
6

            
7
use super::Failure;
8
use super::hidden::Hidden;
9
use super::transaction_rows::{error_at, joined, lower_rows, post_date};
10
use super::transaction_split_tags::split_tags;
11
use super::transaction_stored::is_owned;
12
use super::transaction_tags::required_tags;
13
use crate::lowering::{Header, update_lowered};
14
use crate::resolve::Directory;
15

            
16
const CTX: &str = "submit-form";
17

            
18
/// Validate `transaction-edit` values and replace the transaction's splits
19
/// and tags.
20
6653
pub(super) async fn edit_transaction(user_id: Uuid, values: &FormValues) -> Result<Uuid, Failure> {
21
6653
    let errors = values.check(&TRANSACTION_EDIT);
22
6653
    if !errors.is_empty() {
23
161
        return Err(errors.into());
24
6492
    }
25
6492
    let rows = values.rows("splits").unwrap_or_default();
26
6492
    let tags = joined(required_tags(values), split_tags(values, rows, true));
27
6492
    let (hidden, (tags, row_tags)) = joined(parse_hidden(rows), tags)?;
28
6156
    let post_date = post_date(values)?;
29
6156
    let transaction_id = owned_transaction(user_id, values.text("id").unwrap_or_default()).await?;
30
5993
    let directory = Directory::load(CTX, user_id).await?;
31
5993
    let lowered = lower_rows(&directory, rows, &hidden, &row_tags)?;
32
5828
    let header = Header {
33
5828
        note: values.text("note").map(blank_to_empty),
34
5828
        post_date: Some(post_date),
35
5828
        tags: Some(tags),
36
5828
    };
37
5828
    let revision = values.text("revision").map(str::trim);
38
5828
    update_lowered(CTX, user_id, transaction_id, header, lowered, revision).await
39
6653
}
40

            
41
/// A whitespace-only note clears like an empty one.
42
5823
fn blank_to_empty(note: &str) -> String {
43
5823
    match note.trim().is_empty() {
44
15
        true => String::new(),
45
5808
        false => note.to_string(),
46
    }
47
5823
}
48

            
49
/// Parse every row's hidden data, collecting the errors of all of them.
50
6492
fn parse_hidden(rows: &[Row]) -> Result<Vec<Option<Hidden>>, Vec<FieldError>> {
51
6492
    let mut hidden = Vec::with_capacity(rows.len());
52
6492
    let mut errors = Vec::new();
53
6492
    for (index, row) in rows.iter().enumerate() {
54
10193
        match row
55
10193
            .get("hidden")
56
9061
            .map(|text| text.trim())
57
9061
            .filter(|text| !text.is_empty())
58
        {
59
1132
            None => hidden.push(None),
60
9061
            Some(text) => match Hidden::parse(text) {
61
8730
                Ok(parsed) => hidden.push(Some(parsed)),
62
331
                Err(message) => errors.push(error_at(address::row("splits", index), message)),
63
            },
64
        }
65
    }
66
6492
    match errors.is_empty() {
67
6161
        true => Ok(hidden),
68
331
        false => Err(errors),
69
    }
70
6492
}
71

            
72
/// The id of a transaction that is the user's, else a field error at `id`.
73
/// Staleness is checked first by the update itself, under the transaction's
74
/// row lock, then the same refusals as at load.
75
6156
async fn owned_transaction(user_id: Uuid, text: &str) -> Result<Uuid, Failure> {
76
163
    let unknown = || error_at("id".to_string(), "unknown transaction");
77
6156
    let id = Uuid::parse_str(text.trim()).map_err(|_| unknown())?;
78
6155
    match is_owned(user_id, id).await? {
79
5993
        true => Ok(id),
80
162
        false => Err(unknown().into()),
81
    }
82
6156
}