use super::{account::Account, email::Email, guest_role::GuestRole};
use chrono::{DateTime, Local};
use mycelium_base::{
dtos::{Children, Parent},
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 GuestUser {
pub id: Option<Uuid>,
pub email: Email,
pub guest_role: Parent<GuestRole, Uuid>,
pub created: DateTime<Local>,
pub updated: Option<DateTime<Local>>,
pub accounts: Option<Children<Account, Uuid>>,
pub was_verified: bool,
}
impl GuestUser {
pub fn guest_role_id(&self) -> Result<Uuid, MappedErrors> {
match self.guest_role.to_owned() {
Parent::Id(id) => Ok(id),
Parent::Record(record) => match record.id {
Some(id) => Ok(id),
None => dto_err("Guest role id is required").as_error(),
},
}
}
pub fn build_role_url(&self, base_url: String) -> Result<String, ()> {
match self.guest_role.to_owned() {
Parent::Id(id) => Ok(format!("{}/{}", base_url, id)),
Parent::Record(record) => match record.id {
Some(id) => Ok(format!("{}/{}", base_url, id)),
None => Err(()),
},
}
}
pub fn new_unverified(
email: Email,
guest_role: Parent<GuestRole, Uuid>,
accounts: Option<Children<Account, Uuid>>,
) -> Self {
Self {
id: None,
email,
guest_role,
created: Local::now(),
updated: None,
accounts,
was_verified: false,
}
}
pub fn new_existing(
id: Uuid,
email: Email,
guest_role: Parent<GuestRole, Uuid>,
created: DateTime<Local>,
updated: Option<DateTime<Local>>,
accounts: Option<Children<Account, Uuid>>,
was_verified: bool,
) -> Self {
Self {
id: Some(id),
email,
guest_role,
created,
updated,
accounts,
was_verified,
}
}
}