Skip to main content

quorum_public/models/
user.rs

1use serde::{Deserialize, Serialize};
2use surrealdb_types::{Datetime, RecordId, SurrealValue};
3
4#[derive(Debug, Clone, Serialize, Deserialize, SurrealValue)]
5pub struct User {
6    pub id: RecordId,
7    pub username: String,
8    pub email: Option<String>,
9    pub password_hash: Option<String>,
10    pub created_at: Datetime,
11    pub last_login: Option<Datetime>,
12    pub is_banned: bool,
13    pub is_deleted: bool,
14    pub email_backup_codes: Option<Vec<String>>,
15}
16
17#[derive(Debug, Deserialize)]
18pub struct SignupRequest {
19    pub username: String,
20    pub email: Option<String>,
21    pub password: String,
22}
23
24#[derive(Debug, Deserialize)]
25pub struct LoginRequest {
26    pub username_or_email: String,
27    pub password: String,
28}
29
30#[derive(Debug, Deserialize)]
31pub struct DeleteAccountRequest {
32    pub username_or_email: String,
33    pub password: String,
34    pub user_id: String,
35}
36
37#[derive(Debug, Deserialize)]
38pub struct GetUserDataRequest {
39    pub user_id: String,
40    pub username_or_email: String,
41    pub password: String,
42    pub fields: Vec<String>,
43}
44
45#[derive(Debug, Serialize)]
46pub struct UserDataResponse {
47    pub success: bool,
48    pub data: Option<serde_json::Map<String, serde_json::Value>>,
49    pub message: String,
50}
51
52#[derive(Debug, Serialize)]
53pub struct UserResponse {
54    pub id: String,
55    pub username: String,
56    pub email: Option<String>,
57    pub created_at: Datetime,
58}
59
60#[derive(Debug, Serialize)]
61pub struct TokenResponse {
62    pub access_token: String,
63    pub refresh_token: String,
64    pub expires_in: i64,
65}
66
67#[derive(Debug, Serialize)]
68pub struct AuthTokenResponse {
69    pub success: bool,
70    pub user: Option<UserResponse>,
71    pub tokens: Option<TokenResponse>,
72    pub message: String,
73}
74
75#[derive(Debug, Deserialize)]
76pub struct UpdateUserProfileRequest {
77    pub user_id: String,
78    pub email: Option<String>,
79    pub username: String,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize, SurrealValue)]
83pub struct EmailBackupCode {
84    pub plain: Option<String>,
85    pub hash: String,
86}
87
88impl User {
89    pub fn to_response(&self) -> UserResponse {
90        UserResponse {
91            id: match &self.id.key {
92                surrealdb_types::RecordIdKey::String(s) => s.to_string(),
93                surrealdb_types::RecordIdKey::Number(n) => n.to_string(),
94                _ => format!("{:?}", self.id.key),
95            },
96            username: self.username.clone(),
97            email: self.email.clone(),
98            created_at: self.created_at,
99        }
100    }
101}