Skip to content
Clone
ef0b83a
38 lines 1.2 KB Rust
use sea_orm::entity::Set;
use sea_orm::entity::prelude::*;
use uuid::Uuid;
/// 兑换记录。同一次邀请对同一个用户只记一次,用于"已在组织内时幂等成功"的判定,
/// 也留下"谁通过哪个邀请进来的"的审计线索。
#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "space_invite_redemptions")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(unique_key = "invite_user")]
pub invite_id: Uuid,
#[sea_orm(indexed, unique_key = "invite_user")]
pub user_id: i64,
#[sea_orm(indexed)]
pub organization_id: Uuid,
#[sea_orm(default_expr = "Expr::current_timestamp()")]
pub redeemed_at: DateTimeUtc,
#[sea_orm(belongs_to, from = "invite_id", to = "id", on_delete = "Cascade")]
pub invite: BelongsTo<super::space_invites::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)
}
}