Skip to content

Commit

Permalink
pull from testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaelkingsdev committed Nov 25, 2024
2 parents 1a00286 + 87befe5 commit 7ce6eb0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
89 changes: 89 additions & 0 deletions src/endpoints/admin/quest_boost/get_quest_users.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use crate::middleware::auth::auth_middleware;
use crate::utils::verify_quest_auth;
use crate::{
models::{AppState, CompletedTaskDocument, QuestDocument, QuestTaskDocument},
utils::get_error,
};
use axum::{
extract::{Extension, Query, State},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use futures::TryStreamExt;
use mongodb::bson::doc;
use serde::Deserialize;
use serde_json::json;
use std::collections::HashSet;
use std::sync::Arc;

pub_struct!(Deserialize; GetQuestUsersParams {
quest_id: i64,
});

#[route(get, "/admin/quests/get_quest_users", auth_middleware)]
pub async fn get_quest_users_handler(
State(state): State<Arc<AppState>>,
Extension(_sub): Extension<String>,
Query(params): Query<GetQuestUsersParams>,
) -> impl IntoResponse {
let tasks_collection = state.db.collection::<QuestTaskDocument>("tasks");
let completed_tasks_collection = state
.db
.collection::<CompletedTaskDocument>("completed_tasks");
let quests_collection = state.db.collection::<QuestDocument>("quests");

let res = verify_quest_auth(_sub, &quests_collection, &(params.quest_id)).await;
if !res {
return get_error("Error getting quest users".to_string());
};

// Fetch all task IDs for the given quest_id
let task_filter = doc! { "quest_id": params.quest_id };
let task_cursor = match tasks_collection.find(task_filter, None).await {
Ok(cursor) => cursor,
Err(e) => return get_error(format!("Error fetching tasks: {}", e)),
};

let task_ids_result: Result<Vec<u32>, _> = task_cursor
.map_ok(|doc| doc.id as u32)
.try_collect()
.await;

let task_ids = match task_ids_result {
Ok(ids) => ids,
Err(e) => return get_error(format!("Error processing tasks: {}", e)),
};

if task_ids.is_empty() {
return get_error(format!("No tasks found for quest_id {}", params.quest_id));
}

// Fetch all completed tasks for these task IDs
let completed_task_filter = doc! { "task_id": { "$in": &task_ids } };
let completed_task_cursor = match completed_tasks_collection
.find(completed_task_filter, None)
.await
{
Ok(cursor) => cursor,
Err(e) => return get_error(format!("Error fetching completed tasks: {}", e)),
};

let completed_tasks_result: Result<Vec<CompletedTaskDocument>, _> =
completed_task_cursor.try_collect().await;

let users = match completed_tasks_result {
Ok(completed_tasks) => {
let user_set: HashSet<String> = completed_tasks
.into_iter()
.map(|task: CompletedTaskDocument| task.address().to_string())
.collect();

let users_list: Vec<String> = user_set.into_iter().collect();
users_list
}
Err(e) => return get_error(format!("Error processing completed tasks: {}", e)),
};

return (StatusCode::OK, Json(json!({ "users": users }))).into_response();
}
1 change: 1 addition & 0 deletions src/endpoints/admin/quest_boost/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod create_boost;
pub mod get_quest_users;
pub mod update_boost;
2 changes: 0 additions & 2 deletions src/endpoints/defi/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use futures::stream::{FuturesOrdered, StreamExt};
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, USER_AGENT};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, Error};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use reqwest_tracing::TracingMiddleware;
use serde::{Deserialize, Serialize};
use serde_json::json;
use starknet::{core::types::FieldElement, macros::selector};
Expand All @@ -37,7 +36,6 @@ pub async fn get_defi_rewards(
// Retry up to 3 times with increasing intervals between attempts.
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let client = ClientBuilder::new(reqwest::Client::new())
.with(TracingMiddleware::default())
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();

Expand Down
1 change: 0 additions & 1 deletion src/endpoints/quests/ekubo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod claimable;
pub mod discord_fw_callback;
pub mod verify_added_liquidity;
1 change: 0 additions & 1 deletion src/endpoints/quests/focustree/engagement/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod discord_fw_callback;
pub mod verify_twitter_rt;
1 change: 0 additions & 1 deletion src/endpoints/quests/nostra/liquidity_quest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod claimable;
pub mod discord_fw_callback;
pub mod verify_added_liquidity;
6 changes: 6 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ pub struct CompletedTaskDocument {
timestamp: i64,
}

impl CompletedTaskDocument {
pub fn address(&self) -> &str {
&self.address
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Call {
pub contract: String,
Expand Down

0 comments on commit 7ce6eb0

Please sign in to comment.