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