Lines
100 %
Functions
81.82 %
Branches
//! The duplicate-name notice of account create and rename: a warning, never
//! a refusal.
use crate::command::connection;
use sqlx::types::Uuid;
use supp_macro::command;
use super::{CmdError, CmdResult};
use crate::user::User;
/// The one text every client shows when an account shares its name with a
/// sibling.
#[must_use]
pub fn duplicate_name_notice(name: &str) -> String {
format!("Another account named '{name}' already exists under the same parent")
}
command! {
AccountNameNotice {
#[required]
user_id: Uuid,
account_id: Uuid,
} => {
let user = User { id: user_id };
let mut conn = connection(&user).await?;
let row = sqlx::query_file!("sql/select/accounts/name_twins.sql", account_id)
.fetch_optional(&mut *conn)
.await?;
Ok(row
.filter(|row| row.twins.unwrap_or_default() > 0)
.map(|row| CmdResult::String(duplicate_name_notice(&row.account_name))))
/// The notice for `account_id` after a create or rename, if it has a twin.
///
/// # Errors
/// The command's database error.
pub async fn account_name_notice(
) -> Result<Option<String>, CmdError> {
match AccountNameNotice::new()
.user_id(user_id)
.account_id(account_id)
.run()
.await?
{
Some(CmdResult::String(notice)) => Ok(Some(notice)),
_ => Ok(None),