team.rs 45 lignes · 1384 octets
use chrono::{DateTime, Utc};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// A named group of users. Backs [`TeamService`](crate::services::team_service::TeamService)
/// and is an accepted principal for [`ResourceService`](crate::services::resource_service::ResourceService)
/// sharing.
#[derive(Clone, Debug, Serialize, Deserialize, DeriveEntityModel)]
#[sea_orm(table_name = "teams")]
pub struct Model {
    /// Primary key.
    #[sea_orm(primary_key)]
    pub id: Uuid,
    /// URL-safe handle (`@backend`, `design-ops`, …). Unique across the tenant.
    #[sea_orm(unique)]
    pub slug: String,
    /// Human-readable label.
    pub name: String,
    /// User who created the team and retains admin rights by default.
    pub owner_id: Uuid,
    /// Timestamp when the team was created.
    pub created_at: DateTime<Utc>,
}

/// SeaORM relations for the Team entity.
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    /// The user who owns (created) this team.
    #[sea_orm(
        belongs_to = "super::user::Entity",
        from = "Column::OwnerId",
        to = "super::user::Column::Id",
        on_delete = "Cascade"
    )]
    Owner,
}

impl Related<super::user::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Owner.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}