backend/service/
users.rs

1//! Service layer for user data.
2
3use reqwest::StatusCode;
4use uuid::Uuid;
5
6use crate::{
7    config::data::{SharedHttpClient, SharedKeycloakApi, SharedPool},
8    error::ServiceError,
9    keycloak_api::dtos::UserDto,
10    model::{
11        dto::{PageParameters, UserSearchParameters, UsersDto},
12        entity::Users,
13    },
14};
15
16/// Create a user data entry for a new user.
17///
18/// # Errors
19/// If the connection to the database could not be established.
20pub async fn create(
21    user_id: Uuid,
22    user_data: UsersDto,
23    pool: &SharedPool,
24) -> Result<UsersDto, ServiceError> {
25    let mut conn = pool.get().await?;
26    let result = Users::create(user_data, user_id, &mut conn).await?;
27    Ok(result)
28}
29
30/// Get a user by its id.
31///
32/// # Errors
33/// * If the connection to the Keycloak API could not be established.
34pub async fn find_by_id(
35    user_id: Uuid,
36    keycloak_api: &SharedKeycloakApi,
37    http_client: &SharedHttpClient,
38) -> Result<UserDto, ServiceError> {
39    let user = keycloak_api
40        .get_user_by_id(http_client, user_id)
41        .await
42        .map_err(|e| {
43            log::error!("Error getting user data from Keycloak API: {e}");
44            ServiceError::new(
45                StatusCode::INTERNAL_SERVER_ERROR,
46                "Error getting user data from Keycloak API",
47            )
48        })?;
49
50    Ok(user)
51}
52
53/// Get users by their ids.
54///
55/// # Errors
56/// * If the connection to the Keycloak API could not be established.
57pub async fn find_by_ids(
58    user_ids: Vec<Uuid>,
59    keycloak_api: &SharedKeycloakApi,
60    http_client: &SharedHttpClient,
61) -> Result<Vec<UserDto>, ServiceError> {
62    let users = keycloak_api
63        .get_users_by_ids(http_client, user_ids)
64        .await
65        .map_err(|e| {
66            log::error!("Error getting user data from Keycloak API: {e}");
67            ServiceError::new(
68                StatusCode::INTERNAL_SERVER_ERROR,
69                "Error getting user data from Keycloak API",
70            )
71        })?;
72
73    Ok(users)
74}
75
76/// Search for users by their username.
77///
78/// # Errors
79/// * If the connection to the Keycloak API could not be established.
80pub async fn search_by_username(
81    search_params: &UserSearchParameters,
82    pagination: &PageParameters,
83    user_id: Uuid,
84    keycloak_api: &SharedKeycloakApi,
85    http_client: &SharedHttpClient,
86) -> Result<Vec<UserDto>, ServiceError> {
87    let users = keycloak_api
88        .search_users_by_username(search_params, pagination, http_client)
89        .await
90        .map_err(|e| {
91            log::error!("Error getting user data from Keycloak API: {e}");
92            ServiceError::new(
93                StatusCode::INTERNAL_SERVER_ERROR,
94                format!("Error getting user data from Keycloak API: {e}").as_str(),
95            )
96        })?;
97
98    // Filter out the user making the request
99    let filtered_users = users
100        .iter()
101        .filter(|user| user.id != user_id)
102        .cloned()
103        .collect::<Vec<_>>();
104
105    Ok(filtered_users)
106}