Skip to content
Clone
main
56 lines 1.7 KB Rust
use sea_orm::entity::Set;
use sea_orm::entity::prelude::*;
use uuid::Uuid;
/// 邀请。链接邀请存 `token_hash`,明文只在创建响应里出现一次。
/// `max_uses = 0` 表示不限次数,否则兑换时在同一事务内做 uses 递增并用
/// `uses < max_uses` 作为条件,避免并发超发。
#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "space_invites")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(indexed)]
pub organization_id: Uuid,
pub method: super::SpaceJoinMethod,
#[sea_orm(unique, column_type = "String(StringLen::N(64))")]
pub token_hash: String,
#[sea_orm(indexed)]
pub role_id: Option<Uuid>,
#[sea_orm(indexed)]
pub target_user_id: Option<i64>,
#[sea_orm(column_type = "String(StringLen::N(255))")]
pub email: Option<String>,
#[sea_orm(column_type = "Text")]
pub extra_role_ids: Option<String>,
pub created_by: i64,
#[sea_orm(default_value = 0)]
pub max_uses: i32,
#[sea_orm(default_value = 0)]
pub uses: i32,
pub expires_at: Option<DateTimeUtc>,
pub revoked_at: Option<DateTimeUtc>,
#[sea_orm(default_expr = "Expr::current_timestamp()")]
pub created_at: DateTimeUtc,
#[sea_orm(belongs_to, from = "organization_id", to = "id", on_delete = "Cascade")]
pub organization: BelongsTo<super::spaces::Entity>,
}
#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {
async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, DbErr>
where
C: ConnectionTrait,
{
if insert && self.id.is_not_set() {
self.id = Set(Uuid::now_v7());
}
Ok(self)
}
}