-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from starknet-id/ayush/admin-dashboard-endpoints
feat: admin dashboard endpoints
- Loading branch information
Showing
61 changed files
with
3,309 additions
and
666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::models::{QuestDocument, QuestTaskDocument,JWTClaims}; | ||
use crate::{models::AppState, utils::get_error}; | ||
use axum::{ | ||
extract::State, | ||
http::StatusCode, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use axum_auto_routes::route; | ||
use mongodb::bson::{doc}; | ||
use mongodb::options::FindOneOptions; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
use std::sync::Arc; | ||
use crate::utils::verify_quest_auth; | ||
use axum::http::HeaderMap; | ||
use jsonwebtoken::{Validation,Algorithm,decode,DecodingKey}; | ||
|
||
|
||
pub_struct!(Deserialize; CreateCustom { | ||
quest_id: u32, | ||
name: String, | ||
desc: String, | ||
cta: String, | ||
href: String, | ||
}); | ||
|
||
#[route(post, "/admin/tasks/custom/create", crate::endpoints::admin::custom::create_custom)] | ||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
headers: HeaderMap, | ||
body: Json<CreateCustom>, | ||
) -> impl IntoResponse { | ||
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String; | ||
let collection = state.db.collection::<QuestTaskDocument>("tasks"); | ||
// Get the last id in increasing order | ||
let last_id_filter = doc! {}; | ||
let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); | ||
let last_doc = &collection.find_one(last_id_filter, options).await.unwrap(); | ||
|
||
let quests_collection = state.db.collection::<QuestDocument>("quests"); | ||
|
||
|
||
let res= verify_quest_auth(user, &quests_collection, &(body.quest_id as i32)).await; | ||
if !res { | ||
return get_error("Error creating task".to_string()); | ||
}; | ||
|
||
let mut next_id = 1; | ||
if let Some(doc) = last_doc { | ||
let last_id = doc.id; | ||
next_id = last_id + 1; | ||
} | ||
|
||
let new_document = QuestTaskDocument { | ||
name: body.name.clone(), | ||
desc: body.desc.clone(), | ||
verify_redirect: Some(body.href.clone()), | ||
href: body.href.clone(), | ||
quest_id : body.quest_id, | ||
id: next_id, | ||
cta: body.cta.clone(), | ||
verify_endpoint: "/quests/verify_custom".to_string(), | ||
verify_endpoint_type: "default".to_string(), | ||
task_type: Some("custom".to_string()), | ||
discord_guild_id: None, | ||
quiz_name: None, | ||
}; | ||
|
||
// insert document to boost collection | ||
return match collection | ||
.insert_one(new_document, | ||
None, | ||
) | ||
.await | ||
{ | ||
Ok(_) => ( | ||
StatusCode::OK, | ||
Json(json!({"message": "Task created successfully"})).into_response(), | ||
) | ||
.into_response(), | ||
Err(_e) => get_error("Error creating tasks".to_string()), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod create_custom; | ||
pub mod update_custom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::models::{QuestTaskDocument,JWTClaims}; | ||
use crate::{models::AppState, utils::get_error}; | ||
use axum::{ | ||
extract::State, | ||
http::StatusCode, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use axum_auto_routes::route; | ||
use mongodb::bson::{doc}; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
use std::sync::Arc; | ||
use crate::utils::verify_task_auth; | ||
use axum::http::HeaderMap; | ||
use jsonwebtoken::{Validation,Algorithm,decode,DecodingKey}; | ||
|
||
|
||
pub_struct!(Deserialize; CreateCustom { | ||
id: u32, | ||
name: Option<String>, | ||
desc: Option<String>, | ||
cta: Option<String>, | ||
verify_endpoint: Option<String>, | ||
verify_endpoint_type: Option<String>, | ||
verify_redirect: Option<String>, | ||
href: Option<String>, | ||
}); | ||
|
||
#[route(post, "/admin/tasks/custom/update", crate::endpoints::admin::custom::update_custom)] | ||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
headers: HeaderMap, | ||
body: Json<CreateCustom>, | ||
) -> impl IntoResponse { | ||
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String; | ||
let collection = state.db.collection::<QuestTaskDocument>("tasks"); | ||
|
||
let res= verify_task_auth(user,&collection,&(body.id as i32)).await; | ||
if !res{ | ||
return get_error("Error updating tasks".to_string()); | ||
} | ||
|
||
// filter to get existing quest | ||
let filter = doc! { | ||
"id": &body.id, | ||
}; | ||
|
||
let mut update_doc = doc! {}; | ||
|
||
if let Some(name) = &body.name { | ||
update_doc.insert("name", name); | ||
} | ||
if let Some(desc) = &body.desc { | ||
update_doc.insert("desc", desc); | ||
} | ||
if let Some(href) = &body.href { | ||
update_doc.insert("href", href); | ||
} | ||
if let Some(cta) = &body.cta { | ||
update_doc.insert("cta", cta); | ||
} | ||
if let Some(verify_redirect) = &body.verify_redirect { | ||
update_doc.insert("verify_redirect", verify_redirect); | ||
} | ||
if let Some(verify_endpoint) = &body.verify_endpoint { | ||
update_doc.insert("verify_endpoint", verify_endpoint); | ||
} | ||
if let Some(verify_endpoint_type) = &body.verify_endpoint_type { | ||
update_doc.insert("verify_endpoint_type", verify_endpoint_type); | ||
} | ||
|
||
// update quest query | ||
let update = doc! { | ||
"$set": update_doc | ||
}; | ||
|
||
|
||
// insert document to boost collection | ||
return match collection | ||
.find_one_and_update(filter, update, None) | ||
.await | ||
{ | ||
Ok(_) => ( | ||
StatusCode::OK, | ||
Json(json!({"message": "Task updated successfully"})).into_response(), | ||
) | ||
.into_response(), | ||
Err(_e) => get_error("Error updating tasks".to_string()), | ||
}; | ||
} |
Oops, something went wrong.