refactor: Replace make_thing with make_thing_from_enum for consistency and clarity across multiple modules
This commit is contained in:
@@ -84,7 +84,7 @@ impl<'a> EventsRepository<'a> {
|
||||
pub async fn query_create_event(&self, data: EventsSchema) -> Result<String> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
let query_str = format!("CREATE {} CONTENT ...", ResourceEnum::Events.to_string());
|
||||
let query_str = format!("CREATE {} CONTENT ...", ResourceEnum::Events);
|
||||
info!(query = %query_str, "Executing SurrealDB query");
|
||||
let record: Option<EventsSchema> = db
|
||||
.create(ResourceEnum::Events.to_string())
|
||||
|
||||
@@ -35,7 +35,7 @@ impl MentorsService {
|
||||
let role_repo = RolesRepository::new(state);
|
||||
let auth_repo = AuthRepository::new(state);
|
||||
|
||||
let user_email = dto.email.clone();
|
||||
let user_email = &dto.email;
|
||||
let mut _user_to_update: Option<UsersSchema> = None;
|
||||
|
||||
let existing_user_result =
|
||||
@@ -56,7 +56,7 @@ impl MentorsService {
|
||||
);
|
||||
}
|
||||
|
||||
let mut user_schema = UsersSchema::from(user_detail_query_dto.clone());
|
||||
let mut user_schema = UsersSchema::from(user_detail_query_dto);
|
||||
|
||||
user_schema.fullname = dto.fullname.clone();
|
||||
user_schema.phone_number = dto.phone_number.clone();
|
||||
@@ -99,7 +99,7 @@ impl MentorsService {
|
||||
}
|
||||
};
|
||||
user_schema.role =
|
||||
imphnen_utils::make_thing(&ResourceEnum::Roles.to_string(), &mentor_role.id);
|
||||
imphnen_utils::make_thing_from_enum(ResourceEnum::Roles, &mentor_role.id);
|
||||
user_schema.is_active = false;
|
||||
|
||||
if let Err(_err) = user_repo.query_update_user(user_schema.clone()).await {
|
||||
@@ -139,14 +139,14 @@ impl MentorsService {
|
||||
};
|
||||
|
||||
let new_user_schema = UsersSchema {
|
||||
id: imphnen_utils::make_thing(
|
||||
&ResourceEnum::Users.to_string(),
|
||||
id: imphnen_utils::make_thing_from_enum(
|
||||
ResourceEnum::Users,
|
||||
&Uuid::new_v4().to_string(),
|
||||
),
|
||||
email: dto.email.clone(),
|
||||
fullname: dto.fullname.clone(),
|
||||
email: dto.email,
|
||||
fullname: dto.fullname,
|
||||
password: hashed_password,
|
||||
phone_number: dto.phone_number.clone(),
|
||||
phone_number: dto.phone_number,
|
||||
// Store personal data from identity_and_verification in user
|
||||
legal_name: Some(dto.identity_and_verification.legal_name.clone()),
|
||||
gender: dto.identity_and_verification.gender.clone(),
|
||||
@@ -161,8 +161,8 @@ impl MentorsService {
|
||||
portfolio_url: dto.professional_profile.portfolio_url.clone(),
|
||||
created_at: imphnen_utils::get_iso_date(),
|
||||
updated_at: imphnen_utils::get_iso_date(),
|
||||
role: imphnen_utils::make_thing(
|
||||
&ResourceEnum::Roles.to_string(),
|
||||
role: imphnen_utils::make_thing_from_enum(
|
||||
ResourceEnum::Roles,
|
||||
&mentor_role.id,
|
||||
),
|
||||
is_active: false,
|
||||
|
||||
@@ -34,10 +34,10 @@ impl GachaItemDto {
|
||||
pub fn from(dto: GachaItemSchema) -> Self {
|
||||
Self {
|
||||
id: dto.id.id.to_raw(),
|
||||
name: dto.name.clone(),
|
||||
name: dto.name,
|
||||
is_deleted: dto.is_deleted,
|
||||
created_at: dto.created_at.clone(),
|
||||
updated_at: dto.updated_at.clone(),
|
||||
created_at: dto.created_at,
|
||||
updated_at: dto.updated_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ impl<'a> GachaItemRepository<'a> {
|
||||
let now = Instant::now();
|
||||
let surreal_query = format!(
|
||||
"SELECT * FROM {} WHERE is_deleted = false AND name LIKE ?",
|
||||
ResourceEnum::GachaItems.to_string()
|
||||
ResourceEnum::GachaItems
|
||||
);
|
||||
info!(query = %surreal_query, "Executing SurrealDB query");
|
||||
let raw_result: ResponseListSuccessDto<Vec<GachaItemSchema>> =
|
||||
@@ -63,7 +63,7 @@ impl<'a> GachaItemRepository<'a> {
|
||||
pub async fn query_gacha_item_by_id(&self, id: String) -> Result<GachaItemSchema> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
let surreal_query = format!("SELECT * FROM {} WHERE id = '{}'", ResourceEnum::GachaItems.to_string(), id);
|
||||
let surreal_query = format!("SELECT * FROM {} WHERE id = '{}'", ResourceEnum::GachaItems, id);
|
||||
info!(query = %surreal_query, "Executing SurrealDB query");
|
||||
let result: Option<GachaItemSchema> = db
|
||||
.select((ResourceEnum::GachaItems.to_string(), id.clone()))
|
||||
@@ -87,7 +87,7 @@ impl<'a> GachaItemRepository<'a> {
|
||||
) -> Result<String> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
let surreal_query = format!("CREATE {} CONTENT ...", ResourceEnum::GachaItems.to_string());
|
||||
let surreal_query = format!("CREATE {} CONTENT ...", ResourceEnum::GachaItems);
|
||||
info!(query = %surreal_query, "Executing SurrealDB query");
|
||||
let record: Option<GachaItemSchema> = db
|
||||
.create(ResourceEnum::GachaItems.to_string())
|
||||
|
||||
@@ -162,7 +162,6 @@ impl AuthServiceTrait for AuthService {
|
||||
payload: AuthLoginRequestDto,
|
||||
state: &AppState,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let payload = payload;
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
if let Err((status, message)) = validate_request(&payload) {
|
||||
@@ -264,7 +263,6 @@ impl AuthServiceTrait for AuthService {
|
||||
payload: AuthRegisterRequestDto,
|
||||
state: &AppState,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let payload = payload;
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
if let Err((status, message)) = validate_request(&payload) {
|
||||
@@ -367,7 +365,6 @@ impl AuthServiceTrait for AuthService {
|
||||
payload: AuthResendOtpRequestDto,
|
||||
state: &AppState,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let payload = payload;
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
if let Err((status, message)) = validate_request(&payload) {
|
||||
@@ -408,7 +405,6 @@ impl AuthServiceTrait for AuthService {
|
||||
payload: AuthRefreshTokenRequestDto,
|
||||
state: &AppState,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let payload = payload;
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
if let Err((status, message)) = validate_request(&payload) {
|
||||
@@ -462,7 +458,6 @@ impl AuthServiceTrait for AuthService {
|
||||
payload: AuthResendOtpRequestDto,
|
||||
state: &AppState,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let payload = payload;
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
if let Err((status, message)) = validate_request(&payload) {
|
||||
@@ -500,7 +495,6 @@ impl AuthServiceTrait for AuthService {
|
||||
payload: AuthVerifyEmailRequestDto,
|
||||
state: &AppState,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let payload = payload;
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
if let Err((status, message)) = validate_request(&payload) {
|
||||
@@ -556,7 +550,6 @@ impl AuthServiceTrait for AuthService {
|
||||
payload: AuthNewPasswordRequestDto,
|
||||
state: &AppState,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let payload = payload;
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
if let Err((status, message)) = validate_request(&payload) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::{ResourceEnum, make_thing};
|
||||
use crate::ResourceEnum;
|
||||
use imphnen_utils::make_thing_from_enum;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::{Uuid, sql::Thing};
|
||||
|
||||
@@ -15,9 +16,9 @@ pub struct PermissionsSchema {
|
||||
|
||||
impl Default for PermissionsSchema {
|
||||
fn default() -> Self {
|
||||
PermissionsSchema {
|
||||
id: make_thing(
|
||||
&ResourceEnum::Permissions.to_string(),
|
||||
Self {
|
||||
id: make_thing_from_enum(
|
||||
ResourceEnum::Permissions,
|
||||
&Uuid::new_v4().to_string(),
|
||||
),
|
||||
name: String::new(),
|
||||
|
||||
@@ -2,8 +2,8 @@ use super::{
|
||||
RolesDetailItemDto, RolesDetailQueryDto, RolesListItemDto, RolesRequestCreateDto,
|
||||
RolesRequestUpdateDto,
|
||||
};
|
||||
use crate::{ResourceEnum, make_thing};
|
||||
use imphnen_utils::get_iso_date;
|
||||
use crate::ResourceEnum;
|
||||
use imphnen_utils::{get_iso_date, make_thing_from_enum};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use surrealdb::{Uuid, sql::Thing};
|
||||
@@ -21,12 +21,12 @@ pub struct RolesSchema {
|
||||
impl Default for RolesSchema {
|
||||
fn default() -> Self {
|
||||
RolesSchema {
|
||||
id: make_thing(
|
||||
&ResourceEnum::Roles.to_string(),
|
||||
id: make_thing_from_enum(
|
||||
ResourceEnum::Roles,
|
||||
&Uuid::new_v4().to_string(),
|
||||
),
|
||||
permissions: vec![make_thing(
|
||||
&ResourceEnum::Permissions.to_string(),
|
||||
permissions: vec![make_thing_from_enum(
|
||||
ResourceEnum::Permissions,
|
||||
&Uuid::new_v4().to_string(),
|
||||
)],
|
||||
name: String::new(),
|
||||
@@ -46,7 +46,7 @@ impl RolesSchema {
|
||||
.permissions
|
||||
.into_iter()
|
||||
.map(|perm| {
|
||||
make_thing(&ResourceEnum::Permissions.to_string(), &perm.id.id.to_raw())
|
||||
make_thing_from_enum(ResourceEnum::Permissions, &perm.id.id.to_raw())
|
||||
})
|
||||
.collect(),
|
||||
is_deleted: dto.is_deleted,
|
||||
@@ -59,11 +59,11 @@ impl RolesSchema {
|
||||
let permissions: Vec<Thing> = dto
|
||||
.permissions
|
||||
.into_iter()
|
||||
.map(|id| make_thing(&ResourceEnum::Permissions.to_string(), &id))
|
||||
.map(|id| make_thing_from_enum(ResourceEnum::Permissions, &id))
|
||||
.collect();
|
||||
Self {
|
||||
id: make_thing(
|
||||
&ResourceEnum::Roles.to_string(),
|
||||
id: make_thing_from_enum(
|
||||
ResourceEnum::Roles,
|
||||
&Uuid::new_v4().to_string(),
|
||||
),
|
||||
name: dto.name,
|
||||
@@ -84,7 +84,7 @@ impl RolesSchema {
|
||||
match (dto.permissions, dto.overwrite.unwrap_or(false)) {
|
||||
(Some(new_ids), true) => new_ids
|
||||
.iter()
|
||||
.map(|id| make_thing(&ResourceEnum::Permissions.to_string(), id))
|
||||
.map(|id| make_thing_from_enum(ResourceEnum::Permissions, id))
|
||||
.collect(),
|
||||
(Some(new_ids), false) => {
|
||||
let mut all_ids: HashSet<String> =
|
||||
@@ -94,17 +94,17 @@ impl RolesSchema {
|
||||
}
|
||||
all_ids
|
||||
.into_iter()
|
||||
.map(|id| make_thing(&ResourceEnum::Permissions.to_string(), &id))
|
||||
.map(|id| make_thing_from_enum(ResourceEnum::Permissions, &id))
|
||||
.collect()
|
||||
}
|
||||
},
|
||||
(None, _) => existing
|
||||
.permissions
|
||||
.iter()
|
||||
.map(|p| make_thing(&ResourceEnum::Permissions.to_string(), &p.id))
|
||||
.map(|p| make_thing_from_enum(ResourceEnum::Permissions, &p.id))
|
||||
.collect(),
|
||||
};
|
||||
Self {
|
||||
id: make_thing(&ResourceEnum::Roles.to_string(), &id),
|
||||
id: make_thing_from_enum(ResourceEnum::Roles, &id),
|
||||
name,
|
||||
permissions,
|
||||
is_deleted: existing.is_deleted,
|
||||
|
||||
@@ -181,15 +181,15 @@ pub struct UsersDetailItemDto {
|
||||
}
|
||||
|
||||
impl UsersDetailItemDto {
|
||||
pub fn from(dto: &UsersDetailQueryDto) -> Self { // Reverted to taking a reference
|
||||
pub fn from(dto: &UsersDetailQueryDto) -> Self {
|
||||
Self {
|
||||
id: dto.id.id.to_raw().clone(),
|
||||
id: dto.id.id.to_raw(),
|
||||
role: RolesDetailItemDto::from(&dto.role),
|
||||
fullname: dto.fullname.clone(),
|
||||
legal_name: dto.legal_name.clone(),
|
||||
email: dto.email.clone(),
|
||||
avatar: dto.avatar.clone(),
|
||||
phone_number: dto.phone_number.clone(), // Corrected from dto.phone.clone()
|
||||
phone_number: dto.phone_number.clone(),
|
||||
phone_for_verification: dto.phone_for_verification.clone(),
|
||||
is_active: dto.is_active,
|
||||
gender: dto.gender.clone(),
|
||||
@@ -276,14 +276,14 @@ impl UsersListQueryDto {
|
||||
pub fn from(self) -> UsersListItemDto {
|
||||
UsersListItemDto {
|
||||
id: self.id.id.to_raw(),
|
||||
role: self.role.name.clone(),
|
||||
fullname: self.fullname.clone(),
|
||||
email: self.email.clone(),
|
||||
avatar: self.avatar.clone(),
|
||||
phone_number: self.phone_number.clone(),
|
||||
role: self.role.name,
|
||||
fullname: self.fullname,
|
||||
email: self.email,
|
||||
avatar: self.avatar,
|
||||
phone_number: self.phone_number,
|
||||
is_active: self.is_active,
|
||||
created_at: self.created_at.clone(),
|
||||
updated_at: self.updated_at.clone(),
|
||||
created_at: self.created_at,
|
||||
updated_at: self.updated_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -323,39 +323,8 @@ pub struct UsersDetailQueryDto {
|
||||
}
|
||||
|
||||
impl UsersDetailQueryDto {
|
||||
pub fn from(&self) -> Self {
|
||||
Self {
|
||||
id: self.id.clone(),
|
||||
role: self.role.clone(),
|
||||
fullname: self.fullname.clone(),
|
||||
legal_name: self.legal_name.clone(),
|
||||
email: self.email.clone(),
|
||||
avatar: self.avatar.clone(),
|
||||
phone_number: self.phone_number.clone(),
|
||||
phone_for_verification: self.phone_for_verification.clone(),
|
||||
is_active: self.is_active,
|
||||
mentor_id: self.mentor_id.clone(),
|
||||
gender: self.gender.clone(),
|
||||
domicile: self.domicile.clone(),
|
||||
bio: self.bio.clone(),
|
||||
last_education: self.last_education.clone(),
|
||||
linkedin_url: self.linkedin_url.clone(),
|
||||
github_url: self.github_url.clone(),
|
||||
cv_url: self.cv_url.clone(),
|
||||
portfolio_url: self.portfolio_url.clone(),
|
||||
website_url: self.website_url.clone(),
|
||||
twitter_url: self.twitter_url.clone(),
|
||||
location: self.location.clone(),
|
||||
skills: self.skills.clone(),
|
||||
experience: self.experience.clone(),
|
||||
education: self.education.clone(),
|
||||
career_status: self.career_status.clone(),
|
||||
is_deleted: self.is_deleted,
|
||||
password: self.password.clone(),
|
||||
birthdate: self.birthdate.clone(),
|
||||
created_at: self.created_at.clone(),
|
||||
updated_at: self.updated_at.clone(),
|
||||
}
|
||||
pub fn from(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::{
|
||||
};
|
||||
use surrealdb::sql::Thing;
|
||||
use anyhow::{Result, bail};
|
||||
use imphnen_utils::{DetailQueryBuilder, QueryListBuilder};
|
||||
use imphnen_utils::{DetailQueryBuilder, QueryListBuilder, make_thing_from_enum};
|
||||
use serde_json;
|
||||
use std::time::Instant;
|
||||
use surrealdb::{Surreal, engine::remote::ws::Client};
|
||||
@@ -105,7 +105,7 @@ impl<'a> UsersRepository<'a> {
|
||||
if user.role.updated_at.is_none() || user.role.is_deleted {
|
||||
bail!("User not found");
|
||||
}
|
||||
Ok(UsersDetailQueryDto::from(&user))
|
||||
Ok(UsersDetailQueryDto::from(user))
|
||||
}
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ impl<'a> UsersRepository<'a> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
let builder = DetailQueryBuilder::new(ResourceEnum::Users.to_string())
|
||||
.with_id(&id.id.to_raw())
|
||||
.with_id(id.id.to_raw())
|
||||
.with_select_fields(vec!["*"])
|
||||
.with_fetch("role")
|
||||
.with_fetch("role.permissions");
|
||||
@@ -139,7 +139,7 @@ impl<'a> UsersRepository<'a> {
|
||||
if user.role.is_deleted {
|
||||
bail!("User's role has been deleted");
|
||||
}
|
||||
Ok(UsersDetailQueryDto::from(&user))
|
||||
Ok(UsersDetailQueryDto::from(user))
|
||||
}
|
||||
|
||||
|
||||
@@ -203,7 +203,7 @@ impl<'a> UsersRepository<'a> {
|
||||
pub async fn query_delete_user(&self, id: String) -> Result<String> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
let user = self.query_user_by_id(&make_thing(&ResourceEnum::Users.to_string(), &id)).await?;
|
||||
let user = self.query_user_by_id(&make_thing_from_enum(ResourceEnum::Users, &id)).await?;
|
||||
if user.is_deleted {
|
||||
bail!("User not found");
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::{UsersCreateRequestDto, UsersDetailQueryDto, UsersUpdateRequestDto, ExperienceDto, EducationDto};
|
||||
use imphnen_libs::{ResourceEnum, hash_password};
|
||||
use imphnen_utils::extract_id;
|
||||
use imphnen_utils::{get_iso_date, make_thing};
|
||||
use imphnen_utils::{get_iso_date, make_thing_from_enum};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::{Uuid, sql::Thing};
|
||||
|
||||
@@ -62,8 +62,8 @@ pub struct UsersSchema {
|
||||
impl Default for UsersSchema {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: make_thing(
|
||||
&ResourceEnum::Users.to_string(),
|
||||
id: make_thing_from_enum(
|
||||
ResourceEnum::Users,
|
||||
&Uuid::new_v4().to_string(),
|
||||
),
|
||||
fullname: String::new(),
|
||||
@@ -92,8 +92,8 @@ impl Default for UsersSchema {
|
||||
experience: None,
|
||||
education: None,
|
||||
career_status: None,
|
||||
role: make_thing(
|
||||
&ResourceEnum::Roles.to_string(),
|
||||
role: make_thing_from_enum(
|
||||
ResourceEnum::Roles,
|
||||
"5713cb37-dc02-4e87-8048-d7a41d352059",
|
||||
),
|
||||
created_at: get_iso_date(),
|
||||
@@ -134,13 +134,13 @@ impl UsersSchema {
|
||||
password: dto.password,
|
||||
created_at: dto.created_at,
|
||||
updated_at: dto.updated_at,
|
||||
role: make_thing(&ResourceEnum::Roles.to_string(), &extract_id(&dto.role.id)),
|
||||
role: make_thing_from_enum(ResourceEnum::Roles, &extract_id(&dto.role.id)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(_user: UsersUpdateRequestDto, id: String) -> Self {
|
||||
Self {
|
||||
id: make_thing(&ResourceEnum::Users.to_string(), &id),
|
||||
id: make_thing_from_enum(ResourceEnum::Users, &id),
|
||||
updated_at: get_iso_date(),
|
||||
// Set defaults for required fields - these should be overridden by actual data from DB
|
||||
..Default::default()
|
||||
@@ -159,7 +159,7 @@ impl UsersSchema {
|
||||
schema.email = email;
|
||||
}
|
||||
if let Some(password) = user.password {
|
||||
schema.password = hash_password(&password).unwrap_or_else(|_| password);
|
||||
schema.password = hash_password(&password).unwrap_or(password);
|
||||
}
|
||||
if let Some(phone_number) = user.phone_number {
|
||||
schema.phone_number = phone_number;
|
||||
@@ -168,7 +168,7 @@ impl UsersSchema {
|
||||
schema.is_active = is_active;
|
||||
}
|
||||
if let Some(role_id) = user.role_id {
|
||||
schema.role = make_thing(&ResourceEnum::Roles.to_string(), &role_id);
|
||||
schema.role = make_thing_from_enum(ResourceEnum::Roles, &role_id);
|
||||
}
|
||||
|
||||
// Optional fields - only update if provided
|
||||
@@ -236,8 +236,8 @@ impl UsersSchema {
|
||||
pub fn create(user: UsersCreateRequestDto) -> Self {
|
||||
let password = hash_password(&user.password).unwrap();
|
||||
Self {
|
||||
id: make_thing(
|
||||
&ResourceEnum::Users.to_string(),
|
||||
id: make_thing_from_enum(
|
||||
ResourceEnum::Users,
|
||||
&Uuid::new_v4().to_string(),
|
||||
),
|
||||
fullname: user.fullname,
|
||||
@@ -266,7 +266,7 @@ impl UsersSchema {
|
||||
career_status: None,
|
||||
avatar: user.avatar,
|
||||
is_deleted: false,
|
||||
role: make_thing(&ResourceEnum::Roles.to_string(), &user.role_id),
|
||||
role: make_thing_from_enum(ResourceEnum::Roles, &user.role_id),
|
||||
created_at: get_iso_date(),
|
||||
updated_at: get_iso_date(),
|
||||
}
|
||||
@@ -281,10 +281,7 @@ impl UsersSchema {
|
||||
}
|
||||
|
||||
pub fn update_mentor_id(mut self, mentor_id: Option<String>) -> Self {
|
||||
self.mentor_id = match mentor_id {
|
||||
Some(id) => Some(make_thing(&ResourceEnum::Users.to_string(), &id)),
|
||||
None => None, // Set to None if no mentor_id provided
|
||||
};
|
||||
self.mentor_id = mentor_id.map(|id| make_thing_from_enum(ResourceEnum::Users, &id));
|
||||
self.updated_at = get_iso_date();
|
||||
self
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::{
|
||||
};
|
||||
use axum::{http::StatusCode, response::Response, extract::Multipart};
|
||||
use imphnen_libs::{ResourceEnum, hash_password, verify_password, MinioConfig, FileType, decode_base64_file, extract_content_type_from_data_url, create_minio_service_from_config};
|
||||
use imphnen_utils::make_thing;
|
||||
use imphnen_utils::make_thing_from_enum;
|
||||
use uuid::Uuid;
|
||||
use std::pin::Pin;
|
||||
use std::future::Future;
|
||||
@@ -84,7 +84,7 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
return common_response(StatusCode::BAD_REQUEST, "Invalid User ID format");
|
||||
}
|
||||
let repo = UsersRepository::new(&state);
|
||||
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &id);
|
||||
let thing_id = make_thing_from_enum(ResourceEnum::Users, &id);
|
||||
match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
|
||||
data: UserDto::from(&user), // Corrected to use UserDto::from by reference
|
||||
@@ -100,7 +100,7 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
let repo = UsersRepository::new(&state);
|
||||
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &claims.user_id);
|
||||
let thing_id = make_thing_from_enum(ResourceEnum::Users, &claims.user_id);
|
||||
match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
|
||||
data: UserDto::from(&user),
|
||||
@@ -116,7 +116,6 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
new_user: UsersCreateRequestDto,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let state = state.to_owned();
|
||||
let new_user = new_user;
|
||||
Box::pin(async move {
|
||||
if let Err((status, message)) = validate_request(&new_user) {
|
||||
return common_response(status, &message);
|
||||
@@ -145,7 +144,6 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let state = state.to_owned();
|
||||
let id = id.to_owned();
|
||||
let user = user;
|
||||
Box::pin(async move {
|
||||
if Uuid::parse_str(&id).is_err() {
|
||||
return common_response(StatusCode::BAD_REQUEST, "Invalid User ID format");
|
||||
@@ -156,7 +154,7 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
}
|
||||
|
||||
// Get current user data first
|
||||
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &id);
|
||||
let thing_id = make_thing_from_enum(ResourceEnum::Users, &id);
|
||||
let current_user = match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) => user,
|
||||
Err(_) => return common_response(StatusCode::NOT_FOUND, "User not found"),
|
||||
@@ -177,11 +175,10 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let claims = claims.to_owned();
|
||||
let state = state.to_owned();
|
||||
let user_update_dto = user_update_dto;
|
||||
Box::pin(async move {
|
||||
let repo = UsersRepository::new(&state);
|
||||
|
||||
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &claims.user_id);
|
||||
let thing_id = make_thing_from_enum(ResourceEnum::Users, &claims.user_id);
|
||||
let user_data = match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) => user,
|
||||
Err(_) => return common_response(StatusCode::NOT_FOUND, "User not found"),
|
||||
@@ -206,13 +203,12 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let state = state.to_owned();
|
||||
let id = id.to_owned();
|
||||
let payload = payload;
|
||||
Box::pin(async move {
|
||||
if Uuid::parse_str(&id).is_err() {
|
||||
return common_response(StatusCode::BAD_REQUEST, "Invalid User ID format");
|
||||
}
|
||||
let repo = UsersRepository::new(&state);
|
||||
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &id);
|
||||
let thing_id = make_thing_from_enum(ResourceEnum::Users, &id);
|
||||
match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) if !user.is_deleted => {
|
||||
let patch = UsersSchema {
|
||||
@@ -238,7 +234,6 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
||||
let state = state.to_owned();
|
||||
let email = email.to_owned();
|
||||
let payload = payload;
|
||||
Box::pin(async move {
|
||||
let repo = UsersRepository::new(&state);
|
||||
let user = match repo.query_user_by_email(email.clone()).await {
|
||||
@@ -287,7 +282,7 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
let mentor_id = mentor_id.to_owned();
|
||||
Box::pin(async move {
|
||||
let repo = UsersRepository::new(&state);
|
||||
let thing_id = make_thing(&ResourceEnum::Mentors.to_string(), &mentor_id);
|
||||
let thing_id = make_thing_from_enum(ResourceEnum::Mentors, &mentor_id);
|
||||
match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
|
||||
data: UserDto::from(&user), // Corrected to use UserDto::from by reference
|
||||
@@ -306,7 +301,7 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
return common_response(StatusCode::BAD_REQUEST, "Invalid User ID format");
|
||||
}
|
||||
let repo = UsersRepository::new(&state);
|
||||
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &id);
|
||||
let thing_id = make_thing_from_enum(ResourceEnum::Users, &id);
|
||||
if repo.query_user_by_id(&thing_id).await.is_err() {
|
||||
return common_response(StatusCode::BAD_REQUEST, "User not found");
|
||||
}
|
||||
@@ -332,7 +327,6 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
}
|
||||
|
||||
fn create_user_by_dto(&self, new_user: CreateUserDto, state: &AppState) -> Pin<Box<dyn Future<Output = Result<UserDto>> + Send>> {
|
||||
let new_user = new_user;
|
||||
let state = state.to_owned();
|
||||
Box::pin(async move {
|
||||
let repo = UsersRepository::new(&state);
|
||||
@@ -344,7 +338,7 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
phone_number: new_user.phone_number,
|
||||
is_active: new_user.is_active,
|
||||
avatar: new_user.avatar,
|
||||
role: make_thing(&ResourceEnum::Roles.to_string(), &new_user.role_id),
|
||||
role: make_thing_from_enum(ResourceEnum::Roles, &new_user.role_id),
|
||||
..Default::default()
|
||||
};
|
||||
match repo.query_create_user(user_schema).await {
|
||||
@@ -422,7 +416,7 @@ pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
|
||||
// Get actual user data from database using user_id (which is a UUID)
|
||||
let repo = UsersRepository::new(&state);
|
||||
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &user_id);
|
||||
let thing_id = make_thing_from_enum(ResourceEnum::Users, &user_id);
|
||||
let user_data = match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) => {
|
||||
info!("Found user in DB. User ID: {}, Email: {}", user.id.id.to_raw(), user.email);
|
||||
|
||||
@@ -60,7 +60,7 @@ pub async fn auth_middleware(
|
||||
match repo.get_user_by_id_internal(&thing_id, &state).await {
|
||||
Ok(user) => {
|
||||
// Optionally: insert into mem for future requests
|
||||
let _: Result<Vec<imphnen_iam::v1::users::users_dto::UsersDetailQueryDto>, _> = mem_db.update(&thing_id.id.to_raw()).content(user.clone()).await;
|
||||
let _: Result<Option<UsersDetailQueryDto>, _> = mem_db.update(("users", &user_id)).content(user.clone()).await;
|
||||
user
|
||||
},
|
||||
Err(_) => return Ok(common_response(StatusCode::UNAUTHORIZED, "User not found")),
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
use surrealdb::sql::Thing;
|
||||
use std::fmt::Display;
|
||||
|
||||
pub fn make_thing(table: &str, id: &str) -> Thing {
|
||||
Thing::from((table, id))
|
||||
}
|
||||
|
||||
pub fn make_thing_from_enum<T: Display>(table: T, id: &str) -> Thing {
|
||||
Thing::from((table.to_string().as_str(), id))
|
||||
}
|
||||
|
||||
pub fn make_thing_str(table: &str, id: &str) -> String {
|
||||
format!("{table}:⟨{id}⟩")
|
||||
}
|
||||
|
||||
@@ -122,9 +122,9 @@ impl ListQueryBuilder {
|
||||
};
|
||||
|
||||
let select_clause = if self.select_fields.is_empty() {
|
||||
"*".to_string()
|
||||
"*"
|
||||
} else {
|
||||
self.select_fields.join(", ")
|
||||
&self.select_fields.join(", ")
|
||||
};
|
||||
|
||||
format!(
|
||||
@@ -196,7 +196,7 @@ impl DetailQueryBuilder {
|
||||
);
|
||||
}
|
||||
self.thing = Some(thing.to_string());
|
||||
self.resource = thing.tb.clone();
|
||||
self.resource = thing.tb.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
@@ -240,48 +240,42 @@ impl DetailQueryBuilder {
|
||||
|
||||
pub fn build(&self) -> String {
|
||||
let select_clause = if self.select_fields.is_empty() {
|
||||
"*".to_string()
|
||||
"*"
|
||||
} else {
|
||||
self.select_fields.join(", ")
|
||||
&self.select_fields.join(", ")
|
||||
};
|
||||
|
||||
let fetch_clause = if self.fetch_fields.is_empty() {
|
||||
String::new()
|
||||
""
|
||||
} else {
|
||||
format!("FETCH {}", self.fetch_fields.join(", ")) // Fixed: Changed self.fetch to self.fetch_fields
|
||||
&format!("FETCH {}", self.fetch_fields.join(", "))
|
||||
};
|
||||
|
||||
// Determine the base FROM clause
|
||||
let mut from_clause_base = if let Some(thing) = &self.thing {
|
||||
thing.to_string()
|
||||
let from_clause_base = if let Some(thing) = &self.thing {
|
||||
thing.as_str()
|
||||
} else if let Some(id_val) = &self.id {
|
||||
format!("{}:⟨{}⟩", self.resource, id_val)
|
||||
&format!("{}:⟨{}⟩", self.resource, id_val)
|
||||
} else {
|
||||
self.resource.clone() // Start with resource name for WHERE queries
|
||||
&self.resource
|
||||
};
|
||||
|
||||
// Add WHERE clause based on accumulated conditions
|
||||
if !self.conditions.is_empty() {
|
||||
// This logic needs to be careful: if `from_clause_base` already contains `WHERE` (e.g. from `id` lookup),
|
||||
// then `conditions` should append with `AND`. But for `DetailQueryBuilder`, only one `WHERE` style is expected.
|
||||
// The panic conditions in `with_id`, `with_thing`, `with_where` should prevent logical conflicts.
|
||||
from_clause_base = format!(
|
||||
"{} WHERE {}",
|
||||
from_clause_base,
|
||||
self.conditions.join(" AND ")
|
||||
);
|
||||
}
|
||||
let final_from_clause = if !self.conditions.is_empty() {
|
||||
format!("{} WHERE {}", from_clause_base, self.conditions.join(" AND "))
|
||||
} else {
|
||||
from_clause_base.to_string()
|
||||
};
|
||||
|
||||
format!("SELECT {select_clause} FROM {from_clause_base} {fetch_clause}")
|
||||
format!("SELECT {select_clause} FROM {final_from_clause} {fetch_clause}")
|
||||
}
|
||||
|
||||
// Modified apply_bindings to clone both key and value
|
||||
pub fn apply_bindings<'q>(
|
||||
&self,
|
||||
mut query: Query<'q, any::Any>,
|
||||
) -> Query<'q, any::Any> {
|
||||
for (key, val) in &self.bindings {
|
||||
query = query.bind((key.clone(), val.clone())); // Clone both key and value
|
||||
query = query.bind((key.clone(), val.clone()));
|
||||
}
|
||||
query
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user