forked from lfglabs-dev/api.starknet.quest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_balance.rs
88 lines (80 loc) · 2.72 KB
/
create_balance.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::models::{QuestDocument, QuestTaskDocument};
use crate::utils::verify_quest_auth;
use crate::{models::AppState, utils::get_error};
use crate::middleware::auth::auth_middleware;
use axum::{
extract::{Extension, 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 starknet::core::types::FieldElement;
use std::str::FromStr;
use std::sync::Arc;
pub_struct!(Deserialize; CreateBalance {
quest_id: i64,
name: String,
desc: String,
contracts: String,
href: String,
cta: String,
});
#[route(post, "/admin/tasks/balance/create", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Extension(sub): Extension<String>,
Json(body): Json<CreateBalance>,
) -> impl IntoResponse {
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(sub, &quests_collection, &(body.quest_id as i64)).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;
}
// Build a vector of FieldElement from the comma separated contracts string
let parsed_contracts: Vec<FieldElement> = body
.contracts
.split(",")
.map(|x| FieldElement::from_str(&x).unwrap())
.collect();
let new_document = QuestTaskDocument {
name: body.name.clone(),
desc: body.desc.clone(),
verify_redirect: None,
href: body.href.clone(),
total_amount: None,
quest_id: body.quest_id,
id: next_id,
cta: body.cta.clone(),
verify_endpoint: "quests/verify_balance".to_string(),
verify_endpoint_type: "default".to_string(),
task_type: Some("balance".to_string()),
discord_guild_id: None,
quiz_name: None,
contracts: Some(parsed_contracts),
api_url: None,
regex: 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()),
};
}