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