From 25bc1edda586d4740474eaabb491859abe10a851 Mon Sep 17 00:00:00 2001 From: ayushtom Date: Thu, 18 Apr 2024 15:04:07 +0530 Subject: [PATCH] feat: add updated db changes --- src/endpoints/get_quest.rs | 24 +++++++++++++++++++----- src/endpoints/get_quest_participants.rs | 25 +++---------------------- src/endpoints/get_quests.rs | 23 ++++++++++++++++++----- src/endpoints/get_trending_quests.rs | 6 +++++- src/models.rs | 8 +++++--- 5 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/endpoints/get_quest.rs b/src/endpoints/get_quest.rs index a3ab39e4..f816bd9f 100644 --- a/src/endpoints/get_quest.rs +++ b/src/endpoints/get_quest.rs @@ -25,11 +25,16 @@ pub async fn handler( Query(query): Query, ) -> impl IntoResponse { let collection = state.db.collection::("quests"); + let current_time = chrono::Utc::now().timestamp_millis(); + let pipeline = [ doc! { "$match": { "disabled": false, - "id": query.id + "id": query.id, + "start_time": doc! { + "$lte": current_time + }, } }, doc! { @@ -38,8 +43,18 @@ pub async fn handler( "$cond": [ { "$and": [ - { "$gte": ["$expiry", 0] }, - { "$lt": ["$expiry", "$$NOW"] }, + doc! { + "$gte": [ + "$expiry", + 0 + ] + }, + doc! { + "$lt": [ + "$expiry", + "$$NOW" + ] + } ] }, true, @@ -57,8 +72,7 @@ pub async fn handler( Ok(document) => { if let Ok(mut quest) = from_document::(document) { if let Some(expiry) = &quest.expiry { - let timestamp = expiry.timestamp_millis().to_string(); - quest.expiry_timestamp = Some(timestamp); + quest.expiry_timestamp = Some(expiry.to_string()); } return (StatusCode::OK, Json(quest)).into_response(); } diff --git a/src/endpoints/get_quest_participants.rs b/src/endpoints/get_quest_participants.rs index bef78920..a54ae7e1 100644 --- a/src/endpoints/get_quest_participants.rs +++ b/src/endpoints/get_quest_participants.rs @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize}; use std::sync::Arc; #[derive(Debug, Serialize, Deserialize)] + pub struct GetQuestParticipantsQuery { quest_id: u32, } @@ -48,30 +49,10 @@ pub async fn handler( let tasks_count = tasks_ids.len(); let pipeline = vec![ - doc! { - "$addFields": doc! { - "refactoredTimestamp": doc! { - "$toDate": "$timestamp" - } - } - }, doc! { "$match": { - "$expr": doc! { - "$and": [ - doc! { - "$in": [ - "$task_id", - tasks_ids - ] - }, - doc! { - "$lt": [ - "$refactoredTimestamp", - "$$expiry" - ] - } - ] + "task_id": { + "$in": tasks_ids } } }, diff --git a/src/endpoints/get_quests.rs b/src/endpoints/get_quests.rs index e629b027..0c71dbfe 100644 --- a/src/endpoints/get_quests.rs +++ b/src/endpoints/get_quests.rs @@ -23,11 +23,15 @@ pub struct NFTItem { #[route(get, "/get_quests", crate::endpoints::get_quests)] pub async fn handler(State(state): State>) -> impl IntoResponse { + let current_time = chrono::Utc::now().timestamp_millis(); + let pipeline = vec![ doc! { "$match": { "disabled": false, - "hidden": false, + "start_time": { + "$lte":current_time + } } }, doc! { @@ -36,8 +40,18 @@ pub async fn handler(State(state): State>) -> impl IntoResponse { "$cond": [ { "$and": [ - { "$gte": ["$expiry", 0] }, - { "$lt": ["$expiry", "$$NOW"] }, + doc! { + "$gte": [ + "$expiry", + 0 + ] + }, + doc! { + "$lt": [ + "$expiry", + "$$NOW" + ] + } ] }, true, @@ -57,8 +71,7 @@ pub async fn handler(State(state): State>) -> impl IntoResponse { Ok(document) => { if let Ok(mut quest) = from_document::(document) { if let Some(expiry) = &quest.expiry { - let timestamp = expiry.timestamp_millis().to_string(); - quest.expiry_timestamp = Some(timestamp); + quest.expiry_timestamp = Some(expiry.to_string()); } quests.push(quest); } diff --git a/src/endpoints/get_trending_quests.rs b/src/endpoints/get_trending_quests.rs index 75276946..8ec03cf0 100644 --- a/src/endpoints/get_trending_quests.rs +++ b/src/endpoints/get_trending_quests.rs @@ -30,12 +30,16 @@ pub async fn handler( Some(addr) => addr.to_string(), None => "".to_string(), }; + let current_time = chrono::Utc::now().timestamp_millis(); + let mut pipeline = vec![ doc! { "$match": { "disabled": false, - "hidden": false, "is_trending": true, + "start_time": doc! { + "$lte": current_time + } } }, doc! { diff --git a/src/models.rs b/src/models.rs index c171505a..db7832f8 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,4 +1,4 @@ -use mongodb::{bson, Database}; +use mongodb::{Database}; use serde::{Deserialize, Serialize}; use serde_json::Value; use starknet::{ @@ -34,13 +34,15 @@ pub_struct!(Debug, Serialize, Deserialize; QuestDocument { rewards_nfts: Vec, img_card: String, title_card: String, - hidden: bool, + hidden: Option, disabled: bool, - expiry: Option, + expiry: Option, expiry_timestamp: Option, mandatory_domain: Option, expired: Option, experience: i64, + start_time: i64, + }); pub_struct!(Deserialize; CompletedTasks {