Lines
98 %
Functions
11.69 %
Branches
100 %
//! `EditAccount`: rename, re-parent and replace the tags of an account in one
//! DB transaction. Lock order: the hierarchy lock (always, so the cycle check
//! and the write are serialized with every hierarchy writer), then the tag
//! keys, then the account rows. The revision is checked under the locks.
use crate::command::connection;
use finance::account::Account;
use finance::tag::Tag;
use sqlx::{PgConnection, types::Uuid};
use supp_macro::command;
use super::account_reparent::move_account;
use super::account_tag::not_found;
use super::attempt::{Attempt, found, missing};
use super::tag_replace::{args, identities, lock_keys, replace_tags, wanted_tags};
use super::{CmdError, CmdResult, FinanceEntity};
use crate::account_state::{STALE_ACCOUNT, read_account};
use crate::attempt::Tries;
use crate::hierarchy::lock_hierarchy;
use crate::tag::lock::begin;
use crate::tag::owned::Owner;
use crate::user::User;
command! {
EditAccount {
#[required]
user_id: Uuid,
account_id: Uuid,
name: String,
tags: Vec<Tag>,
revision: String,
#[optional]
parent: Uuid,
} => {
let wanted = account_wanted(&name, tags)?;
let user = User { id: user_id };
let mut conn = connection(&user).await?;
let mut tries = Tries::default();
while tries
.step(edit_once(&mut conn, user_id, account_id, &wanted, &revision, parent).await)?
.is_none()
{}
Ok(Some(CmdResult::Entity(FinanceEntity::Account(Account {
id: account_id,
parent,
}))))
}
/// The tags the account ends up with: the `name` tag and the given ones.
fn account_wanted(name: &str, tags: Vec<Tag>) -> Result<Vec<Tag>, CmdError> {
let name = Tag {
id: Uuid::new_v4(),
tag_name: "name".to_string(),
tag_value: name.to_string(),
description: None,
};
match name.is_blank() {
true => Err(args("The account name must not be blank")),
false => wanted_tags("An account", vec![name], tags),
async fn edit_once(
conn: &mut PgConnection,
wanted: &[Tag],
revision: &str,
parent: Option<Uuid>,
) -> Result<(), Attempt> {
let mut tx = begin(conn).await?;
lock_hierarchy(&mut tx, user_id).await?;
let before = found(read_account(&mut tx, account_id).await?, not_found)?;
lock_keys(&mut tx, &before.tags, wanted).await?;
Owner::Account
.lock_row(&mut tx, account_id)
.await
.map_err(|err| missing(err, not_found))?;
let stored = found(read_account(&mut tx, account_id).await?, not_found)?;
if identities(&stored.tags) != identities(&before.tags) {
return Err(Attempt::Retry);
if stored.revision() != revision {
return Err(CmdError::Args(STALE_ACCOUNT.to_string()).into());
if stored.parent != parent {
ensure_parent(&mut tx, parent).await?;
move_account(&mut tx, account_id, parent).await?;
replace_tags(&mut tx, Owner::Account, account_id, &stored.tags, wanted).await?;
tx.commit().await?;
Ok(())
/// Only a merge deletes accounts and it holds the hierarchy lock, so the
/// parent seen here stays until commit.
async fn ensure_parent(conn: &mut PgConnection, parent: Option<Uuid>) -> Result<(), Attempt> {
let Some(parent) = parent else {
return Ok(());
match read_account(conn, parent).await? {
Some(_) => Ok(()),
None => Err(CmdError::ParentGone(parent).into()),