use crate::domain::dtos::{email::Email, user::User};
use mycelium_base::utils::errors::{dto_err, MappedErrors};
use serde::{Deserialize, Serialize};
use utoipa::{ToResponse, ToSchema};
use uuid::Uuid;
#[derive(
Clone, Debug, Deserialize, Serialize, ToSchema, Eq, PartialEq, ToResponse,
)]
#[serde(rename_all = "camelCase")]
pub struct Owner {
pub id: Uuid,
pub email: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub first_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub username: Option<String>,
pub is_principal: bool,
}
impl Owner {
pub fn from_user(user: User) -> Result<Self, MappedErrors> {
let user_id = match user.id {
Some(id) => id,
None => {
return dto_err("User ID should not be empty".to_string())
.as_error()
}
};
Ok(Self {
id: user_id,
email: user.email.email(),
first_name: user.to_owned().first_name,
last_name: user.to_owned().last_name,
username: Some(user.to_owned().username),
is_principal: user.is_principal(),
})
}
pub fn redacted_email(&self) -> String {
Email::redact_email(&self.email)
}
}