/ backend / src / api / frontend / me.rs
me.rs
 1  use rocket::State;
 2  use serde::Serialize;
 3  
 4  use crate::{
 5      api::{auth::session_guard::Session, common::response::ResponseStatus},
 6      db::DB,
 7      models::authority::AuthorityLevel,
 8      services::user_service::{self, UserError},
 9  };
10  
11  #[derive(Serialize, Clone)]
12  #[serde(rename_all = "camelCase")]
13  pub struct MeResponse {
14      first_name: String,
15      last_name: String,
16      email: Option<String>,
17      authority: AuthorityLevel,
18  }
19  
20  #[get("/me")]
21  pub async fn get_me(
22      db_pool: &State<sqlx::Pool<DB>>,
23      session: Session,
24  ) -> ResponseStatus<MeResponse> {
25      let resp = match user_service::get_me(session.account_id, db_pool).await {
26          Ok((acc, login_details)) => MeResponse {
27              first_name: acc.first_name,
28              last_name: acc.last_name,
29              email: match login_details {
30                  Some(l) => Some(l.email),
31                  None => None,
32              },
33              authority: acc.authority,
34          },
35          Err(UserError::AccountNotFound) => {
36              error!("Unable to find the account in the session!");
37              // Here we should probably clear the session and require re-authorization, but for now return an error
38              return ResponseStatus::internal_err();
39          }
40          Err(err) => {
41              error!("An error occured whilst retrieving me, err: {:?}", err);
42              return ResponseStatus::internal_err();
43          }
44      };
45  
46      ResponseStatus::ok(resp)
47  }