1
use crate::command::connection;
2
use finance::tag::normalize;
3
use sqlx::types::Uuid;
4
use supp_macro::command;
5

            
6
use super::attempt::settled;
7
use super::{CmdError, CmdResult};
8
use crate::attempt::Tries;
9
use crate::{tag::owned::Owner, user::User};
10

            
11
2
pub(super) fn not_found() -> CmdError {
12
2
    CmdError::NotFound("Account not found".to_string())
13
2
}
14

            
15
command! {
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
560
}