server/command/
account_edit.rs1use crate::command::connection;
7use finance::account::Account;
8use finance::tag::Tag;
9use sqlx::{PgConnection, types::Uuid};
10use supp_macro::command;
11
12use super::account_reparent::move_account;
13use super::account_tag::not_found;
14use super::attempt::{Attempt, found, missing};
15use super::tag_replace::{args, identities, lock_keys, replace_tags, wanted_tags};
16use super::{CmdError, CmdResult, FinanceEntity};
17use crate::account_state::{STALE_ACCOUNT, read_account};
18use crate::attempt::Tries;
19use crate::hierarchy::lock_hierarchy;
20use crate::tag::lock::begin;
21use crate::tag::owned::Owner;
22use crate::user::User;
23
24command! {
25 EditAccount {
26 #[required]
27 user_id: Uuid,
28 #[required]
29 account_id: Uuid,
30 #[required]
31 name: String,
32 #[required]
33 tags: Vec<Tag>,
34 #[required]
35 revision: String,
36 #[optional]
37 parent: Uuid,
38 } => {
39 let wanted = account_wanted(&name, tags)?;
40 let user = User { id: user_id };
41 let mut conn = connection(&user).await?;
42 let mut tries = Tries::default();
43 while tries
44 .step(edit_once(&mut conn, user_id, account_id, &wanted, &revision, parent).await)?
45 .is_none()
46 {}
47 Ok(Some(CmdResult::Entity(FinanceEntity::Account(Account {
48 id: account_id,
49 parent,
50 }))))
51 }
52}
53
54fn account_wanted(name: &str, tags: Vec<Tag>) -> Result<Vec<Tag>, CmdError> {
56 let name = Tag {
57 id: Uuid::new_v4(),
58 tag_name: "name".to_string(),
59 tag_value: name.to_string(),
60 description: None,
61 };
62 match name.is_blank() {
63 true => Err(args("The account name must not be blank")),
64 false => wanted_tags("An account", vec![name], tags),
65 }
66}
67
68async fn edit_once(
69 conn: &mut PgConnection,
70 user_id: Uuid,
71 account_id: Uuid,
72 wanted: &[Tag],
73 revision: &str,
74 parent: Option<Uuid>,
75) -> Result<(), Attempt> {
76 let mut tx = begin(conn).await?;
77 lock_hierarchy(&mut tx, user_id).await?;
78 let before = found(read_account(&mut tx, account_id).await?, not_found)?;
79 lock_keys(&mut tx, &before.tags, wanted).await?;
80 Owner::Account
81 .lock_row(&mut tx, account_id)
82 .await
83 .map_err(|err| missing(err, not_found))?;
84 let stored = found(read_account(&mut tx, account_id).await?, not_found)?;
85 if identities(&stored.tags) != identities(&before.tags) {
86 return Err(Attempt::Retry);
87 }
88 if stored.revision() != revision {
89 return Err(CmdError::Args(STALE_ACCOUNT.to_string()).into());
90 }
91 if stored.parent != parent {
92 ensure_parent(&mut tx, parent).await?;
93 move_account(&mut tx, account_id, parent).await?;
94 }
95 replace_tags(&mut tx, Owner::Account, account_id, &stored.tags, wanted).await?;
96 tx.commit().await?;
97 Ok(())
98}
99
100async fn ensure_parent(conn: &mut PgConnection, parent: Option<Uuid>) -> Result<(), Attempt> {
103 let Some(parent) = parent else {
104 return Ok(());
105 };
106 match read_account(conn, parent).await? {
107 Some(_) => Ok(()),
108 None => Err(CmdError::ParentGone(parent).into()),
109 }
110}