app_settings.rs 73 lignes · 2104 octets
use chrono::{DateTime, Utc};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// AppSettings entity model
///
/// Represents global application settings stored as key-value pairs.
/// Settings can be updated by administrators with traceability (updated_by).
#[derive(Clone, Debug, Serialize, Deserialize, DeriveEntityModel)]
#[sea_orm(table_name = "app_settings")]
pub struct Model {
    /// Unique identifier for the setting
    #[sea_orm(primary_key)]
    pub id: Uuid,
    /// Setting key (unique, max 100 characters)
    #[sea_orm(unique)]
    pub key: String,
    /// Setting value (text, can be JSON for complex values)
    pub value: String,
    /// Optional description of the setting
    pub description: Option<String>,
    /// User ID who last updated this setting (nullable, SET NULL on delete)
    pub updated_by: Option<Uuid>,
    /// Last update timestamp
    pub updated_at: DateTime<Utc>,
}

/// SeaORM relations for the AppSettings entity.
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    /// The user who last updated this setting.
    #[sea_orm(
        belongs_to = "super::user::Entity",
        from = "Column::UpdatedBy",
        to = "super::user::Column::Id"
    )]
    UpdatedByUser,
}

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

impl ActiveModelBehavior for ActiveModel {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_app_settings_model() {
        let id = Uuid::new_v4();
        let updated_by = Some(Uuid::new_v4());
        let now = Utc::now();

        let setting = Model {
            id,
            key: "app_name".to_string(),
            value: "RustWarden".to_string(),
            description: Some("Application name".to_string()),
            updated_by,
            updated_at: now,
        };

        assert_eq!(setting.id, id);
        assert_eq!(setting.key, "app_name");
        assert_eq!(setting.value, "RustWarden");
        assert_eq!(setting.updated_by, updated_by);
    }
}