1.6 KiB · text History 7a00c02
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

/// What a member may do in an area. Ordered: each role can do everything the
/// ones before it can.
///
/// * `Voter` — see the area, rank, start/join/vote on lunches, log visits.
/// * `Editor` — also add, edit, archive and refresh restaurants.
/// * `Admin` — also rename the area and manage members and invitations.
#[derive(
    Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, EnumIter, DeriveActiveEnum, Serialize, Deserialize,
)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(16))")]
#[serde(rename_all = "lowercase")]
pub enum Role {
    #[sea_orm(string_value = "voter")]
    Voter,
    #[sea_orm(string_value = "editor")]
    Editor,
    #[sea_orm(string_value = "admin")]
    Admin,
}

impl Role {
    pub const ALL: [Role; 3] = [Role::Voter, Role::Editor, Role::Admin];

    pub fn as_str(self) -> &'static str {
        match self {
            Role::Voter => "voter",
            Role::Editor => "editor",
            Role::Admin => "admin",
        }
    }

    pub fn parse(s: &str) -> Option<Role> {
        Role::ALL.into_iter().find(|r| r.as_str() == s)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "area_members")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub area_id: Uuid,
    #[sea_orm(primary_key, auto_increment = false)]
    pub user_id: Uuid,
    pub role: Role,
    pub created_at: DateTimeWithTimeZone,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}