Lines
90.32 %
Functions
65.38 %
Branches
100 %
//! `account-edit` submit: syntax, ownership, resolution, then the one
//! `EditAccount` command under the account's locks.
use forms::{ACCOUNT_EDIT, FieldError, FormValues};
use server::account_state::STALE_ACCOUNT;
use server::command::account_edit::EditAccount;
use server::command::account_reparent::CYCLE_REFUSED;
use server::command::{CmdError, CmdResult, FinanceEntity};
use server::user::User;
use uuid::Uuid;
use super::Failure;
use super::account_create::parent_of;
use super::edit_tags::edited_tags;
use super::transaction_rows::{error_at, joined, single_line};
use crate::host_failure::cmd_failure;
const CTX: &str = "submit-form";
/// Validate `account-edit` values and apply the rename, the re-parent and the
/// tag replacement.
pub(super) async fn edit_account(user_id: Uuid, values: &FormValues) -> Result<Uuid, Failure> {
let errors = values.check(&ACCOUNT_EDIT);
if !errors.is_empty() {
return Err(errors.into());
}
let name = values.text("name").unwrap_or_default().trim();
let named = single_line("name", name).map_err(|error| vec![error]);
let carried = joined(named, parent_present(values).map_err(|error| vec![error]));
let (_, tags) = joined(
carried.map(|_| ()),
edited_tags(values, &["name"], "an account"),
)?;
let account_id = owned_account(user_id, values.text("id").unwrap_or_default()).await?;
let parent = parent_of(user_id, values).await?;
let mut command = EditAccount::new()
.user_id(user_id)
.account_id(account_id)
.name(name.to_string())
.tags(tags)
.revision(
values
.text("revision")
.unwrap_or_default()
.trim()
.to_string(),
);
if let Some(parent) = parent {
command = command.parent(parent);
match command.run().await {
Ok(Some(CmdResult::Entity(FinanceEntity::Account(account)))) => Ok(account.id),
Ok(other) => Err(Failure::Internal(wasmtime::Error::msg(format!(
"{CTX}: unexpected edit result {other:?}"
)))),
Err(err) => Err(edit_failure(err)),
/// A client that drops `parent` is refused; an empty one moves to the root.
fn parent_present(values: &FormValues) -> Result<(), FieldError> {
values.text("parent").map(drop).ok_or_else(|| {
error_at(
"parent".to_string(),
"is missing; send an empty value for the root",
)
})
/// The stale revision and the cycle are field errors; the rest as elsewhere.
fn edit_failure(err: CmdError) -> Failure {
match err {
CmdError::Args(message) if message == STALE_ACCOUNT => {
error_at("revision".to_string(), message).into()
CmdError::Refused(message) if message == CYCLE_REFUSED => {
error_at("parent".to_string(), message).into()
gone @ CmdError::ParentGone(_) => error_at("parent".to_string(), gone.to_string()).into(),
other => Failure::from_command(CTX, other),
/// The id of an account that is the user's, else a field error at `id`.
async fn owned_account(user_id: Uuid, text: &str) -> Result<Uuid, Failure> {
let unknown = || error_at("id".to_string(), "unknown account");
let id = Uuid::parse_str(text.trim()).map_err(|_| unknown())?;
let stored = User { id: user_id }
.account_snapshot(id)
.await
.map_err(|err| cmd_failure(CTX, err.into()))?;
stored.map(|_| id).ok_or_else(|| unknown().into())