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
50 let did = params.did.trim();
51 if did.is_empty() {
52 return (
53 StatusCode::BAD_REQUEST,
54 Json(json!({"error": "InvalidRequest", "message": "did is required"})),
55 )
56 .into_response();
57 }
58
59 let result = sqlx::query!(
60 r#"
61 SELECT did, handle, email, created_at
62 FROM users
63 WHERE did = $1
64 "#,
65 did
66 )
67 .fetch_optional(&state.db)
68 .await;
69
70 match result {
71 Ok(Some(row)) => {
72 (
73 StatusCode::OK,
74 Json(AccountInfo {
75 did: row.did,
76 handle: row.handle,
77 email: row.email,
78 indexed_at: row.created_at.to_rfc3339(),
79 invite_note: None,
80 invites_disabled: false,
81 email_confirmed_at: None,
82 deactivated_at: None,
83 }),
84 )
85 .into_response()
86 }
87 Ok(None) => (
88 StatusCode::NOT_FOUND,
89 Json(json!({"error": "AccountNotFound", "message": "Account not found"})),
90 )
91 .into_response(),
92 Err(e) => {
93 error!("DB error in get_account_info: {:?}", e);
94 (
95 StatusCode::INTERNAL_SERVER_ERROR,
96 Json(json!({"error": "InternalError"})),
97 )
98 .into_response()
99 }
100 }
101}
102
103#[derive(Deserialize)]
104pub struct GetAccountInfosParams {
105 pub dids: String,
106}
107
108pub async fn get_account_infos(
109 State(state): State<AppState>,
110 headers: axum::http::HeaderMap,
111 Query(params): Query<GetAccountInfosParams>,
112) -> Response {
113 let auth_header = headers.get("Authorization");
114 if auth_header.is_none() {
115 return (
116 StatusCode::UNAUTHORIZED,
117 Json(json!({"error": "AuthenticationRequired"})),
118 )
119 .into_response();
120 }
121
122 let dids: Vec<&str> = params.dids.split(',').map(|s| s.trim()).collect();
123 if dids.is_empty() {
124 return (
125 StatusCode::BAD_REQUEST,
126 Json(json!({"error": "InvalidRequest", "message": "dids is required"})),
127 )
128 .into_response();
129 }
130
131 let mut infos = Vec::new();
132
133 for did in dids {
134 if did.is_empty() {
135 continue;
136 }
137
138 let result = sqlx::query!(
139 r#"
140 SELECT did, handle, email, created_at
141 FROM users
142 WHERE did = $1
143 "#,
144 did
145 )
146 .fetch_optional(&state.db)
147 .await;
148
149 if let Ok(Some(row)) = result {
150 infos.push(AccountInfo {
151 did: row.did,
152 handle: row.handle,
153 email: row.email,
154 indexed_at: row.created_at.to_rfc3339(),
155 invite_note: None,
156 invites_disabled: false,
157 email_confirmed_at: None,
158 deactivated_at: None,
159 });
160 }
161 }
162
163 (StatusCode::OK, Json(GetAccountInfosOutput { infos })).into_response()
164}