main
use utoipa::ToSchema;
use sea_orm::entity::Set;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(
Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize, ToSchema,
)]
#[serde(rename_all = "snake_case")]
#[sea_orm(
rs_type = "String",
db_type = "String(StringLen::N(32))",
rename_all = "snake_case"
)]
pub enum UserEventType {
Register,
Login,
Logout,
PasswordChange,
ProfileUpdate,
TwoFactorEnabled,
TwoFactorDisabled,
FailedLoginAttempt,
AccountLocked,
AccountUnlocked,
EmailChange,
EmailVerified,
RoleChanged,
PasswordResetRequested,
PublicKeyAdded,
PublicKeyRemoved,
AccessKeyAdded,
AccessKeyRemoved,
Follow,
Unfollow,
Block,
Unblock,
}
#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "user_events")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(indexed)]
pub user_id: i64,
#[sea_orm(indexed)]
pub actor_user_id: Option<i64>,
pub event_type: UserEventType,
#[sea_orm(indexed, default_expr = "Expr::current_timestamp()")]
pub occurred_at: DateTimeUtc,
#[sea_orm(column_type = "String(StringLen::N(45))")]
pub ip_address: Option<String>,
#[sea_orm(column_type = "Text")]
pub user_agent: Option<String>,
#[sea_orm(column_type = "JsonBinary")]
pub details: Option<Json>,
#[sea_orm(column_type = "String(StringLen::N(64))")]
pub trace_id: Option<String>,
#[sea_orm(default_value = 1)]
pub version: i32,
#[sea_orm(belongs_to, from = "user_id", to = "id", skip_fk)]
pub user: BelongsTo<super::users::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)
}
}