mod meta;
mod status;
pub use meta::TenantMetaKey;
pub use status::TenantStatus;
use super::{account::Account, profile::Owner, tag::Tag};
use chrono::{DateTime, Local};
use mycelium_base::{
dtos::{Children, Parent},
utils::errors::{dto_err, MappedErrors},
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use utoipa::ToSchema;
use uuid::Uuid;
pub type TenantMeta = HashMap<TenantMetaKey, String>;
#[derive(Clone, Debug, Deserialize, Serialize, ToSchema, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Tenant {
pub id: Option<Uuid>,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub owners: Children<Owner, Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub manager: Option<Parent<Account, Uuid>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<Tag>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub meta: Option<HashMap<TenantMetaKey, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<Vec<TenantStatus>>,
pub created: DateTime<Local>,
#[serde(skip_serializing_if = "Option::is_none")]
pub updated: Option<DateTime<Local>>,
}
impl Tenant {
pub fn new_with_owners(
name: String,
description: Option<String>,
owners: Children<Owner, Uuid>,
) -> Self {
Self {
id: None,
name,
description,
owners,
manager: None,
tags: None,
meta: None,
status: None,
created: Local::now(),
updated: None,
}
}
pub fn tenant_string_or_error(&self) -> Result<String, MappedErrors> {
if let Some(id) = self.id {
Ok(format!("tid/{}", id))
} else {
dto_err("Unable to format owner name").as_error()
}
}
}