use serde::{Deserialize, Serialize};
pub const DEFAULT_TENANT: &str = "default";
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TenantId(pub String);
impl TenantId {
pub fn default_tenant() -> Self {
Self(DEFAULT_TENANT.to_string())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for TenantId {
fn default() -> Self {
Self::default_tenant()
}
}
impl std::fmt::Display for TenantId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for TenantId {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl From<String> for TenantId {
fn from(s: String) -> Self {
Self(s)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tenant {
pub id: TenantId,
pub name: String,
pub status: TenantStatus,
#[serde(skip_serializing_if = "Option::is_none")]
pub policy: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limits: Option<TenantLimits>,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TenantStatus {
Active,
Suspended,
Archived,
}
impl TenantStatus {
pub fn as_str(&self) -> &'static str {
match self {
Self::Active => "active",
Self::Suspended => "suspended",
Self::Archived => "archived",
}
}
pub fn parse(s: &str) -> Self {
match s {
"suspended" => Self::Suspended,
"archived" => Self::Archived,
_ => Self::Active,
}
}
}
impl Tenant {
pub fn policy_set(&self) -> Option<jamjet_ir::workflow::PolicySetIr> {
self.policy
.as_ref()
.and_then(|v| serde_json::from_value(v.clone()).ok())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TenantLimits {
#[serde(skip_serializing_if = "Option::is_none")]
pub max_concurrent_executions: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_workflows: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens_per_month: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_cost_per_month_usd: Option<f64>,
}