Skip to main content

server/command/
account_tag.rs

1use crate::command::connection;
2use finance::tag::normalize;
3use sqlx::types::Uuid;
4use supp_macro::command;
5
6use super::attempt::settled;
7use super::{CmdError, CmdResult};
8use crate::attempt::Tries;
9use crate::{tag::owned::Owner, user::User};
10
11pub(super) fn not_found() -> CmdError {
12    CmdError::NotFound("Account not found".to_string())
13}
14
15command! {
16    ClearAccountTag {
17        #[required]
18        user_id: Uuid,
19        #[required]
20        account_id: Uuid,
21        #[required]
22        tag_name: String,
23    } => {
24        let (tag_name, _) = normalize(&tag_name, "");
25        if tag_name.is_empty() {
26            return Err(CmdError::Args("Tag name must not be blank".to_string()));
27        }
28        if tag_name == "name" {
29            return Err(CmdError::Args("The name tag cannot be cleared".to_string()));
30        }
31        let user = User { id: user_id };
32        let mut conn = connection(&user).await?;
33        let mut tries = Tries::default();
34        while tries
35            .step(settled(Owner::Account.clear(&mut conn, account_id, tag_name).await, not_found))?
36            .is_none()
37        {}
38        Ok(Some(CmdResult::String("ok".to_string())))
39    }
40}