|
| 1 | +use crate::models::{QuestDocument, QuestTaskDocument}; |
| 2 | +use crate::utils::verify_quest_auth; |
| 3 | +use crate::{models::AppState, utils::get_error}; |
| 4 | +use crate::middleware::auth::auth_middleware; |
| 5 | +use axum::{ |
| 6 | + extract::{Extension, State}, |
| 7 | + http::StatusCode, |
| 8 | + response::{IntoResponse, Json} |
| 9 | +}; |
| 10 | +use axum_auto_routes::route; |
| 11 | +use mongodb::bson::doc; |
| 12 | +use mongodb::options::FindOneOptions; |
| 13 | +use serde::Deserialize; |
| 14 | +use serde_json::json; |
| 15 | +use std::sync::Arc; |
| 16 | + |
| 17 | +pub_struct!(Deserialize; CreateCustomAPI { |
| 18 | + quest_id: i64, |
| 19 | + name: String, |
| 20 | + desc: String, |
| 21 | + href: String, |
| 22 | + cta: String, |
| 23 | + api_url: String, |
| 24 | + regex: String, |
| 25 | +}); |
| 26 | + |
| 27 | +#[route(post, "/admin/tasks/custom_api/create", auth_middleware)] |
| 28 | +pub async fn handler( |
| 29 | + State(state): State<Arc<AppState>>, |
| 30 | + Extension(sub): Extension<String>, |
| 31 | + Json(body): Json<CreateCustomAPI>, |
| 32 | +) -> impl IntoResponse { |
| 33 | + let collection = state.db.collection::<QuestTaskDocument>("tasks"); |
| 34 | + // Get the last id in increasing order |
| 35 | + let last_id_filter = doc! {}; |
| 36 | + let options = FindOneOptions::builder().sort(doc! {"id": -1}).build(); |
| 37 | + let last_doc = &collection.find_one(last_id_filter, options).await.unwrap(); |
| 38 | + |
| 39 | + let quests_collection = state.db.collection::<QuestDocument>("quests"); |
| 40 | + |
| 41 | + let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await; |
| 42 | + if !res { |
| 43 | + return get_error("Error creating task".to_string()); |
| 44 | + }; |
| 45 | + |
| 46 | + let mut next_id = 1; |
| 47 | + if let Some(doc) = last_doc { |
| 48 | + let last_id = doc.id; |
| 49 | + next_id = last_id + 1; |
| 50 | + } |
| 51 | + |
| 52 | + let new_document = QuestTaskDocument { |
| 53 | + name: body.name.clone(), |
| 54 | + desc: body.desc.clone(), |
| 55 | + verify_redirect: None, |
| 56 | + href: body.href.clone(), |
| 57 | + total_amount: None, |
| 58 | + quest_id: body.quest_id, |
| 59 | + id: next_id, |
| 60 | + cta: body.cta.clone(), |
| 61 | + verify_endpoint: "quests/verify_custom_api".to_string(), |
| 62 | + verify_endpoint_type: "default".to_string(), |
| 63 | + calls: None, |
| 64 | + task_type: Some("custom_api".to_string()), |
| 65 | + discord_guild_id: None, |
| 66 | + quiz_name: None, |
| 67 | + contracts: None, |
| 68 | + api_url: Some(body.api_url.clone()), |
| 69 | + regex: Some(body.regex.clone()), |
| 70 | + }; |
| 71 | + |
| 72 | + // insert document to boost collection |
| 73 | + return match collection.insert_one(new_document, None).await { |
| 74 | + Ok(_) => ( |
| 75 | + StatusCode::OK, |
| 76 | + Json(json!({"message": "Task created successfully"})).into_response(), |
| 77 | + ) |
| 78 | + .into_response(), |
| 79 | + Err(_e) => get_error("Error creating tasks".to_string()), |
| 80 | + }; |
| 81 | +} |
0 commit comments