this repo has no description
1use crate::state::AppState;
2use axum::{
3 Json,
4 extract::{Query, State},
5 http::StatusCode,
6 response::{IntoResponse, Response},
7};
8use serde::{Deserialize, Serialize};
9use serde_json::json;
10use tracing::error;
11
12#[derive(Deserialize)]
13pub struct GetAccountInfoParams {
14 pub did: String,
15}
16
17#[derive(Serialize)]
18#[serde(rename_all = "camelCase")]
19pub struct AccountInfo {
20 pub did: String,
21 pub handle: String,
22 pub email: Option<String>,
23 pub indexed_at: String,
24 pub invite_note: Option<String>,
25 pub invites_disabled: bool,
26 pub email_confirmed_at: Option<String>,
27 pub deactivated_at: Option<String>,
28}
29
30#[derive(Serialize)]
31#[serde(rename_all = "camelCase")]
32pub struct GetAccountInfosOutput {
33 pub infos: Vec<AccountInfo>,
34}
35
36pub async fn get_account_info(
37 State(state): State<AppState>,
38 headers: axum::http::HeaderMap,
39 Query(params): Query<GetAccountInfoParams>,
40) -> Response {
41 let auth_header = headers.get("Authorization");
42 if auth_header.is_none() {
43 return (
44 StatusCode::UNAUTHORIZED,
45 Json(json!({"error": "AuthenticationRequired"})),
46 )
47 .into_response();
48 }
49 let did = params.did.trim();
50 if did.is_empty() {
51 return (
52 StatusCode::BAD_REQUEST,
53 Json(json!({"error": "InvalidRequest", "message": "did is required"})),
54 )
55 .into_response();
56 }
57 let result = sqlx::query!(
58 r#"
59 SELECT did, handle, email, created_at
60 FROM users
61 WHERE did = $1
62 "#,
63 did
64 )
65 .fetch_optional(&state.db)
66 .await;
67 match result {
68 Ok(Some(row)) => (
69 StatusCode::OK,
70 Json(AccountInfo {
71 did: row.did,
72 handle: row.handle,
73 email: row.email,
74 indexed_at: row.created_at.to_rfc3339(),
75 invite_note: None,
76 invites_disabled: false,
77 email_confirmed_at: None,
78 deactivated_at: None,
79 }),
80 )
81 .into_response(),
82 Ok(None) => (
83 StatusCode::NOT_FOUND,
84 Json(json!({"error": "AccountNotFound", "message": "Account not found"})),
85 )
86 .into_response(),
87 Err(e) => {
88 error!("DB error in get_account_info: {:?}", e);
89 (
90 StatusCode::INTERNAL_SERVER_ERROR,
91 Json(json!({"error": "InternalError"})),
92 )
93 .into_response()
94 }
95 }
96}
97
98#[derive(Deserialize)]
99pub struct GetAccountInfosParams {
100 pub dids: String,
101}
102
103pub async fn get_account_infos(
104 State(state): State<AppState>,
105 headers: axum::http::HeaderMap,
106 Query(params): Query<GetAccountInfosParams>,
107) -> Response {
108 let auth_header = headers.get("Authorization");
109 if auth_header.is_none() {
110 return (
111 StatusCode::UNAUTHORIZED,
112 Json(json!({"error": "AuthenticationRequired"})),
113 )
114 .into_response();
115 }
116 let dids: Vec<&str> = params.dids.split(',').map(|s| s.trim()).collect();
117 if dids.is_empty() {
118 return (
119 StatusCode::BAD_REQUEST,
120 Json(json!({"error": "InvalidRequest", "message": "dids is required"})),
121 )
122 .into_response();
123 }
124 let mut infos = Vec::new();
125 for did in dids {
126 if did.is_empty() {
127 continue;
128 }
129 let result = sqlx::query!(
130 r#"
131 SELECT did, handle, email, created_at
132 FROM users
133 WHERE did = $1
134 "#,
135 did
136 )
137 .fetch_optional(&state.db)
138 .await;
139 if let Ok(Some(row)) = result {
140 infos.push(AccountInfo {
141 did: row.did,
142 handle: row.handle,
143 email: row.email,
144 indexed_at: row.created_at.to_rfc3339(),
145 invite_note: None,
146 invites_disabled: false,
147 email_confirmed_at: None,
148 deactivated_at: None,
149 });
150 }
151 }
152 (StatusCode::OK, Json(GetAccountInfosOutput { infos })).into_response()
153}