darkwing/server/dtos/entities/
user_dto.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Data Transfer Objects (DTOs) for user-related operations.
//!
//! This module contains the DTOs used for serializing and deserializing user
//! data in API requests and responses. It includes:
//! - User role definitions and conversions
//! - Response DTOs for user information
//! - Authentication response structures
//!
//! The DTOs in this module help maintain a clear separation between the
//! database models and the API layer, ensuring consistent data representation
//! across the application.

use serde::{Deserialize, Serialize};

use crate::{database::user::User, server::error::Error};

/// Represents the role of a user in the system.
///
/// Users can have different levels of access and permissions based on their
/// role:
/// - `Admin`: Has full system access and management capabilities
/// - `Teamlead`: Can manage team members and team-specific resources
/// - `User`: Has basic access to system features
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum UserRole {
  /// Administrator role with full system access
  Admin,
  /// Team leader role with team management capabilities
  Teamlead,
  /// Standard user role with basic access (default)
  #[default]
  User,
}

impl TryFrom<String> for UserRole {
  type Error = Error;

  /// Attempts to convert a string into a UserRole.
  ///
  /// # Arguments
  ///
  /// * `value` - The string to convert
  ///
  /// # Returns
  ///
  /// * `Ok(UserRole)` - If the string matches a valid role
  /// * `Err(Error::BadRequest)` - If the string doesn't match any known role
  fn try_from(value: String) -> Result<Self, Self::Error> {
    match value.as_str() {
      "admin" => Ok(UserRole::Admin),
      "teamlead" => Ok(UserRole::Teamlead),
      "user" => Ok(UserRole::User),
      _ => Err(Error::BadRequest(format!("Invalid user role: {}", value))),
    }
  }
}

impl User {
  /// Converts a database User model into a DTO for API responses.
  ///
  /// # Returns
  ///
  /// A `ResponseUserDto` containing the user's information.
  ///
  /// # Note
  ///
  /// Currently uses unwrap_or for role conversion. This should be updated
  /// to properly handle conversion errors.
  pub fn into_dto(self) -> ResponseUserDto {
    ResponseUserDto {
      id: self.id,
      team_id: self.teamId,
      username: self.username,
      role: self.role.try_into().unwrap_or(UserRole::User),
      // TODO: change this fn to pass error to top
    }
  }
}

/// Data transfer object for user information in API responses.
#[derive(Clone, Serialize, Deserialize, Default, Debug)]
pub struct ResponseUserDto {
  /// Internal user identifier (not included in serialization)
  #[serde(skip_serializing, skip_deserializing)]
  pub id: u64,
  /// Identifier of the team the user belongs to
  pub team_id: i64,
  /// Username of the user
  pub username: String,
  /// Role of the user in the system
  pub role: UserRole,
}

/// Response structure for user authentication endpoints.
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct UserAuthenicationResponse {
  /// The authenticated user's information
  pub user: ResponseUserDto,
}