Files
imphnen-backend-service/imphnen-iam/src/v1/users/users_dto.rs
T

173 lines
4.3 KiB
Rust
Raw Normal View History

2025-05-18 03:12:20 +07:00
use crate::{RolesDetailItemDto, RolesDetailQueryDto};
use lazy_static::lazy_static;
use regex::Regex;
2025-02-26 15:45:05 +07:00
use serde::{Deserialize, Serialize};
2025-03-26 13:06:34 +07:00
use surrealdb::sql::Thing;
2025-02-26 15:45:05 +07:00
use utoipa::ToSchema;
use validator::Validate;
2025-02-26 15:45:05 +07:00
lazy_static! {
static ref PASSWORD_REGEX: Regex = Regex::new(r"^[A-Za-z\d@$!%*?&]{8,}$").unwrap();
}
2025-05-18 03:12:20 +07:00
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct UsersActiveInactiveRequestDto {
pub is_active: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct UsersSetNewPasswordRequestDto {
pub password: String,
pub old_password: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate)]
2025-03-20 22:41:20 +07:00
pub struct UsersCreateRequestDto {
#[validate(
length(min = 1, message = "Email cannot be empty"),
email(message = "Email not valid")
)]
2025-03-20 22:41:20 +07:00
pub email: String,
2025-05-17 02:08:59 +07:00
#[validate(length(
min = 8,
message = "Password must have at least 8 characters"
))]
#[validate(regex(
path = "PASSWORD_REGEX",
message = "Password must include uppercase, lowercase, number, and special character"
))]
2025-03-20 22:41:20 +07:00
pub password: String,
2025-05-17 02:08:59 +07:00
#[validate(length(min = 2, message = "Fullname at least have 2 character"))]
pub fullname: String,
2025-05-17 02:08:59 +07:00
#[validate(length(
min = 10,
message = "Phone number at least have 10 character"
))]
pub phone_number: String,
pub is_active: bool,
2025-03-23 20:42:38 +07:00
pub role_id: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate)]
pub struct UsersUpdateRequestDto {
#[validate(
length(min = 1, message = "Email cannot be empty"),
email(message = "Email not valid")
)]
pub email: String,
#[validate(length(
min = 8,
message = "Password must have at least 8 characters"
))]
#[validate(length(min = 2, message = "Fullname at least have 2 character"))]
pub fullname: String,
#[validate(length(
min = 10,
message = "Phone number at least have 10 character"
))]
pub phone_number: String,
pub is_active: bool,
2025-03-30 02:46:16 +07:00
#[validate(length(min = 1, message = "Gender is required"))]
pub gender: Option<String>,
2025-03-30 02:46:16 +07:00
#[validate(length(min = 1, message = "Birthdate is required"))]
pub birthdate: Option<String>,
2025-03-30 02:46:16 +07:00
#[validate(length(min = 1, message = "Avatar is required"))]
pub avatar: Option<String>,
2025-03-23 20:42:38 +07:00
pub role_id: String,
2025-03-20 22:41:20 +07:00
}
2025-03-30 02:46:16 +07:00
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct UsersDetailItemDto {
pub id: String,
2025-05-18 03:12:20 +07:00
pub role: RolesDetailItemDto,
2025-03-26 13:06:34 +07:00
pub fullname: String,
pub email: String,
pub avatar: Option<String>,
pub phone_number: String,
pub is_active: bool,
pub gender: Option<String>,
pub birthdate: Option<String>,
2025-05-17 02:08:59 +07:00
pub created_at: String,
pub updated_at: String,
2025-03-26 13:06:34 +07:00
}
2025-05-18 03:12:20 +07:00
impl UsersDetailItemDto {
pub fn from(dto: UsersDetailQueryDto) -> Self {
Self {
id: dto.id.id.to_raw(),
role: RolesDetailItemDto::from(&dto.role),
fullname: dto.fullname,
email: dto.email,
avatar: dto.avatar,
phone_number: dto.phone_number,
is_active: dto.is_active,
gender: dto.gender,
birthdate: dto.birthdate,
created_at: dto.created_at,
updated_at: dto.updated_at,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
2025-03-30 02:46:16 +07:00
pub struct UsersListItemDto {
pub id: String,
pub role: String,
pub fullname: String,
pub email: String,
pub avatar: Option<String>,
pub phone_number: String,
pub is_active: bool,
2025-03-30 16:08:58 +07:00
pub created_at: String,
pub updated_at: String,
2025-02-26 15:45:05 +07:00
}
2025-03-30 02:46:16 +07:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2025-05-17 02:08:59 +07:00
pub struct UsersListQueryDto {
2025-03-30 02:46:16 +07:00
pub id: Thing,
2025-05-17 02:08:59 +07:00
pub role: RolesDetailQueryDto,
2025-03-30 02:46:16 +07:00
pub fullname: String,
pub email: String,
pub avatar: Option<String>,
pub phone_number: String,
pub is_active: bool,
2025-03-30 16:08:58 +07:00
pub created_at: String,
pub updated_at: String,
2025-03-30 02:46:16 +07:00
}
2025-05-17 02:08:59 +07:00
impl UsersListQueryDto {
2025-05-18 03:12:20 +07:00
pub fn from(&self, role: String) -> UsersListItemDto {
2025-05-17 02:08:59 +07:00
UsersListItemDto {
id: self.id.id.to_raw(),
role,
fullname: self.fullname.clone(),
email: self.email.clone(),
avatar: self.avatar.clone(),
phone_number: self.phone_number.clone(),
is_active: self.is_active,
created_at: self.created_at.clone(),
updated_at: self.updated_at.clone(),
}
}
}
2025-03-30 02:46:16 +07:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2025-05-17 02:08:59 +07:00
pub struct UsersDetailQueryDto {
2025-03-30 02:46:16 +07:00
pub id: Thing,
pub fullname: String,
pub email: String,
pub avatar: Option<String>,
pub phone_number: String,
pub is_active: bool,
pub is_deleted: bool,
pub gender: Option<String>,
pub birthdate: Option<String>,
pub password: String,
2025-05-17 02:08:59 +07:00
pub role: RolesDetailQueryDto,
2025-03-30 02:46:16 +07:00
pub created_at: String,
pub updated_at: String,
}