Skip to content

Commit

Permalink
Merge pull request #170 from starknet-id/feat/get_completed_boosts-route
Browse files Browse the repository at this point in the history
Feat/get completed boosts route
  • Loading branch information
Th0rgal authored Jan 10, 2024
2 parents 574191e + 3443691 commit 8f644fb
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
98 changes: 98 additions & 0 deletions src/endpoints/quest_boost/get_completed_boosts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use crate::{models::AppState, utils::get_error};
use axum::{
extract::{Query, State},
response::IntoResponse,
Json,
};

use futures::TryStreamExt;
use mongodb::bson::{doc, Document};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use starknet::core::types::FieldElement;
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]

pub struct GetCompletedQuestsQuery {
addr: FieldElement,
}

pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<GetCompletedQuestsQuery>,
) -> impl IntoResponse {
let address = query.addr.to_string();
let pipeline = vec![
// Existing pipeline to get completed quests
doc! {
"$lookup": doc! {
"from": "tasks",
"localField": "quests",
"foreignField": "quest_id", // Replace 'questId' with the actual field name in your tasks collection
"as": "tasks"
}
},
doc! {
"$lookup": doc! {
"from": "completed_tasks",
"let": {
"task_ids": {
"$map": {
"input": "$tasks",
"as": "taskObj",
"in": "$$taskObj.id" // Extract the id from each object in the tasks array
}
}
},
"pipeline" : [
{
"$match": {
"$expr": {
"$and": [
{
"$in": ["$task_id", "$$task_ids"],
},
{
"$eq": ["$address", address],
}
]
}
}
}
],
"as": "completed_tasks"
}
},
doc! {
"$match": doc! {
"$expr": {
"$eq": [
{
"$size": "$tasks",
},
{
"$size": "$completed_tasks",
},
],
},
}
},
doc! {
"$project": {
"id": 1,
}
},
];
let collection = state.db.collection::<Document>("boosts");
match collection.aggregate(pipeline, None).await {
Ok(mut cursor) => {
let mut boosts: Vec<u32> = Vec::new();
while let Some(result) = cursor.try_next().await.unwrap() {
boosts.push(result.get("id").unwrap().as_i64().unwrap() as u32);
}
(StatusCode::OK, Json(boosts)).into_response()
}
Err(_) => get_error("Error querying quests".to_string()),
}
}
3 changes: 2 additions & 1 deletion src/endpoints/quest_boost/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod get_boost;
pub mod get_boosts;
pub mod get_claim_params;
pub mod get_quests;
pub mod get_completed_boosts;
pub mod get_pending_claims;
pub mod get_quests;
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ async fn main() {
"/quests/nostra/verify_added_liquidity",
get(endpoints::quests::nostra::verify_added_liquidity::handler),
)
.route(
.route(
"/quests/rhino/check_bridge",
get(endpoints::quests::rhino::check_bridge::handler),
)
Expand All @@ -460,7 +460,7 @@ async fn main() {
"/quests/rhino/claimable",
get(endpoints::quests::rhino::claimable::handler),
)
.route(
.route(
"/quests/rango/verify_twitter_fw",
get(endpoints::quests::rango::verify_twitter_fw::handler),
)
Expand Down Expand Up @@ -540,6 +540,10 @@ async fn main() {
"/boost/get_boosts",
get(endpoints::quest_boost::get_boosts::handler),
)
.route(
"/boost/get_completed_boosts",
get(endpoints::quest_boost::get_completed_boosts::handler),
)
.route(
"/boost/get_quests",
get(endpoints::quest_boost::get_quests::handler),
Expand Down

0 comments on commit 8f644fb

Please sign in to comment.