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

            
4
use forms::{ACCOUNT_EDIT, FieldError, FormValues};
5
use server::account_state::STALE_ACCOUNT;
6
use server::command::account_edit::EditAccount;
7
use server::command::account_reparent::CYCLE_REFUSED;
8
use server::command::{CmdError, CmdResult, FinanceEntity};
9
use server::user::User;
10
use uuid::Uuid;
11

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

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

            
20
/// Validate `account-edit` values and apply the rename, the re-parent and the
21
/// tag replacement.
22
1469
pub(super) async fn edit_account(user_id: Uuid, values: &FormValues) -> Result<Uuid, Failure> {
23
1469
    let errors = values.check(&ACCOUNT_EDIT);
24
1469
    if !errors.is_empty() {
25
162
        return Err(errors.into());
26
1307
    }
27
1307
    let name = values.text("name").unwrap_or_default().trim();
28
1307
    let named = single_line("name", name).map_err(|error| vec![error]);
29
1307
    let carried = joined(named, parent_present(values).map_err(|error| vec![error]));
30
1307
    let (_, tags) = joined(
31
1307
        carried.map(|_| ()),
32
1307
        edited_tags(values, &["name"], "an account"),
33
167
    )?;
34
1140
    let account_id = owned_account(user_id, values.text("id").unwrap_or_default()).await?;
35
1139
    let parent = parent_of(user_id, values).await?;
36
1137
    let mut command = EditAccount::new()
37
1137
        .user_id(user_id)
38
1137
        .account_id(account_id)
39
1137
        .name(name.to_string())
40
1137
        .tags(tags)
41
1137
        .revision(
42
1137
            values
43
1137
                .text("revision")
44
1137
                .unwrap_or_default()
45
1137
                .trim()
46
1137
                .to_string(),
47
1137
        );
48
1137
    if let Some(parent) = parent {
49
176
        command = command.parent(parent);
50
961
    }
51
1137
    match command.run().await {
52
973
        Ok(Some(CmdResult::Entity(FinanceEntity::Account(account)))) => Ok(account.id),
53
        Ok(other) => Err(Failure::Internal(wasmtime::Error::msg(format!(
54
            "{CTX}: unexpected edit result {other:?}"
55
        )))),
56
164
        Err(err) => Err(edit_failure(err)),
57
    }
58
1469
}
59

            
60
/// A client that drops `parent` is refused; an empty one moves to the root.
61
1307
fn parent_present(values: &FormValues) -> Result<(), FieldError> {
62
1307
    values.text("parent").map(drop).ok_or_else(|| {
63
1
        error_at(
64
1
            "parent".to_string(),
65
1
            "is missing; send an empty value for the root",
66
1
        )
67
1
    })
68
1307
}
69

            
70
/// The stale revision and the cycle are field errors; the rest as elsewhere.
71
164
fn edit_failure(err: CmdError) -> Failure {
72
164
    match err {
73
162
        CmdError::Args(message) if message == STALE_ACCOUNT => {
74
162
            error_at("revision".to_string(), message).into()
75
        }
76
2
        CmdError::Refused(message) if message == CYCLE_REFUSED => {
77
2
            error_at("parent".to_string(), message).into()
78
        }
79
        gone @ CmdError::ParentGone(_) => error_at("parent".to_string(), gone.to_string()).into(),
80
        other => Failure::from_command(CTX, other),
81
    }
82
164
}
83

            
84
/// The id of an account that is the user's, else a field error at `id`.
85
1140
async fn owned_account(user_id: Uuid, text: &str) -> Result<Uuid, Failure> {
86
1
    let unknown = || error_at("id".to_string(), "unknown account");
87
1140
    let id = Uuid::parse_str(text.trim()).map_err(|_| unknown())?;
88
1140
    let stored = User { id: user_id }
89
1140
        .account_snapshot(id)
90
1140
        .await
91
        .map_err(|err| cmd_failure(CTX, err.into()))?;
92
1140
    stored.map(|_| id).ok_or_else(|| unknown().into())
93
1140
}