1
//! `commodity-edit` submit: syntax, ownership, then the one `EditCommodity`
2
//! command under the commodity's locks.
3

            
4
use forms::{COMMODITY_EDIT, FormValues};
5
use server::command::commodity_edit::EditCommodity;
6
use server::command::commodity_tag::IDENTITY_TAGS;
7
use server::command::{CmdError, CmdResult, FinanceEntity};
8
use server::commodity_state::STALE_COMMODITY;
9
use server::user::User;
10
use uuid::Uuid;
11

            
12
use super::Failure;
13
use super::edit_tags::edited_tags;
14
use super::transaction_rows::{error_at, joined, single_line};
15
use crate::host_failure::cmd_failure;
16

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

            
19
/// Validate `commodity-edit` values and apply the symbol, the name and the
20
/// tag replacement.
21
1140
pub(super) async fn edit_commodity(user_id: Uuid, values: &FormValues) -> Result<Uuid, Failure> {
22
1140
    let errors = values.check(&COMMODITY_EDIT);
23
1140
    if !errors.is_empty() {
24
324
        return Err(errors.into());
25
816
    }
26
4862
    let text = |key| values.text(key).unwrap_or_default().trim();
27
816
    let lines: Vec<_> = IDENTITY_TAGS
28
816
        .into_iter()
29
1632
        .filter_map(|key| single_line(key, text(key)).err())
30
        .collect();
31
816
    let named = if lines.is_empty() { Ok(()) } else { Err(lines) };
32
816
    let (_, tags) = joined(named, edited_tags(values, &IDENTITY_TAGS, "a commodity"))?;
33
809
    let commodity_id = owned_commodity(user_id, text("id")).await?;
34
807
    let edited = EditCommodity::new()
35
807
        .user_id(user_id)
36
807
        .commodity_id(commodity_id)
37
807
        .symbol(text("symbol").to_string())
38
807
        .name(text("name").to_string())
39
807
        .tags(tags)
40
807
        .revision(text("revision").to_string())
41
807
        .run()
42
807
        .await;
43
807
    match edited {
44
645
        Ok(Some(CmdResult::Entity(FinanceEntity::Commodity(commodity)))) => Ok(commodity.id),
45
        Ok(other) => Err(Failure::Internal(wasmtime::Error::msg(format!(
46
            "{CTX}: unexpected edit result {other:?}"
47
        )))),
48
162
        Err(err) => Err(edit_failure(err)),
49
    }
50
1140
}
51

            
52
/// The stale revision is a field error; the rest as elsewhere.
53
162
fn edit_failure(err: CmdError) -> Failure {
54
162
    match err {
55
162
        CmdError::Args(message) if message == STALE_COMMODITY => {
56
162
            error_at("revision".to_string(), message).into()
57
        }
58
        other => Failure::from_command(CTX, other),
59
    }
60
162
}
61

            
62
/// The id of a commodity that is the user's, else a field error at `id`.
63
809
async fn owned_commodity(user_id: Uuid, text: &str) -> Result<Uuid, Failure> {
64
2
    let unknown = || error_at("id".to_string(), "unknown commodity");
65
809
    let id = Uuid::parse_str(text).map_err(|_| unknown())?;
66
808
    let stored = User { id: user_id }
67
808
        .commodity_snapshot(id)
68
808
        .await
69
        .map_err(|err| cmd_failure(CTX, err.into()))?;
70
808
    stored.map(|_| id).ok_or_else(|| unknown().into())
71
809
}