diff --git a/src/endpoints/renewal/get_non_subscribed_domains.rs b/src/endpoints/renewal/get_non_subscribed_domains.rs index 3edaeb0..d4b80b9 100644 --- a/src/endpoints/renewal/get_non_subscribed_domains.rs +++ b/src/endpoints/renewal/get_non_subscribed_domains.rs @@ -99,6 +99,29 @@ pub async fn handler( "preserveNullAndEmptyArrays": true } }, + doc! { + "$lookup": { + "from": "auto_renew_flows_altcoins", + "let": doc! { "domain_name": "$domainData.domain" }, + "pipeline": [ + doc! { + "$match": doc! { + "$expr": doc! { + "$eq": ["$domain", "$$domain_name"] + }, + "_cursor.to": null + } + } + ], + "as": "renew_flows_altcoins" + } + }, + doc! { + "$unwind": { + "path": "$renew_flows_altcoins", + "preserveNullAndEmptyArrays": true + } + }, doc! { "$match": { "$or": [ @@ -106,6 +129,11 @@ pub async fn handler( { "renew_flows.renewer_address": &addr, "renew_flows._cursor.to": null + }, + { "renew_flows_altcoins": { "$eq": null } }, + { + "renew_flows_altcoins.renewer_address": &addr, + "renew_flows_altcoins._cursor.to": null } ] } @@ -115,13 +143,20 @@ pub async fn handler( "_id": 0, "id": 1, "domain": "$domainData.domain", - "enabled": { + "enabled": { "$cond": { "if": { "$eq": ["$renew_flows", null] }, "then": false, "else": "$renew_flows.enabled" } }, + "enabled_altcoin": { + "$cond": { + "if": { "$eq": ["$renew_flows_altcoins", null] }, + "then": false, + "else": "$renew_flows_altcoins.enabled" + } + }, } }, ]; @@ -135,7 +170,8 @@ pub async fn handler( while let Some(doc) = cursor.next().await { if let Ok(doc) = doc { let enabled = doc.get_bool("enabled").unwrap_or(false); - if !enabled { + let enabled_altcoin = doc.get_bool("enabled_altcoin").unwrap_or(false); + if !enabled && !enabled_altcoin { if let Ok(domain) = doc.get_str("domain") { if DOMAIN_REGEX.is_match(domain) { results.push(domain.to_string()); diff --git a/src/endpoints/renewal/get_renewal_data.rs b/src/endpoints/renewal/get_renewal_data.rs index e669035..06729bf 100644 --- a/src/endpoints/renewal/get_renewal_data.rs +++ b/src/endpoints/renewal/get_renewal_data.rs @@ -34,20 +34,56 @@ pub async fn handler( State(state): State>, Query(query): Query, ) -> impl IntoResponse { - let renew_collection = state + let result_auto_renew_flows = find_renewal_data(&state, "auto_renew_flows", &query).await; + + let mut document_to_return = None; + + if let Ok(Some(doc)) = result_auto_renew_flows { + if doc.get_bool("enabled").unwrap_or(true) { + // If enabled is true, return this document + document_to_return = Some(doc); + } else { + // If enabled is false, check auto_renew_flows_altcoins but keep this document as a fallback. + let result_altcoins = find_renewal_data(&state, "auto_renew_flows_altcoins", &query) + .await + .ok() + .flatten(); + document_to_return = result_altcoins.or(Some(doc)); // Use the altcoins result or fallback to the original document. + } + } + + let mut headers = HeaderMap::new(); + headers.insert("Cache-Control", HeaderValue::from_static("max-age=30")); + + match document_to_return { + Some(mut doc) => { + doc.remove("_id"); + doc.remove("_cursor"); + (StatusCode::OK, headers, Json(doc)).into_response() + } + None => get_error("Error while fetching from database or no results found".to_string()), + } +} + +async fn find_renewal_data( + state: &AppState, + collection_name: &str, + query: &StarknetIdQuery, +) -> mongodb::error::Result> { + let collection = state .starknetid_db - .collection::("auto_renew_flows"); + .collection::(collection_name); - let find_options = FindOptions::builder() + let find_options = mongodb::options::FindOptions::builder() .sort(doc! {"_cursor.from": -1}) .limit(1) .build(); - let documents = renew_collection + let mut cursor = collection .find( doc! { "renewer_address": to_hex(&query.addr), - "domain": query.domain, + "domain": &query.domain, "$or": [ { "_cursor.to": { "$exists": false } }, { "_cursor.to": null }, @@ -55,27 +91,7 @@ pub async fn handler( }, find_options, ) - .await; + .await?; - match documents { - Ok(mut cursor) => { - let mut headers = HeaderMap::new(); - headers.insert("Cache-Control", HeaderValue::from_static("max-age=30")); - - if let Some(result) = cursor.next().await { - match result { - Ok(res) => { - let mut res = res; - res.remove("_id"); - res.remove("_cursor"); - (StatusCode::OK, headers, Json(res)).into_response() - } - Err(e) => get_error(format!("Error while processing the document: {:?}", e)), - } - } else { - get_error("no results founds".to_string()) - } - } - Err(_) => get_error("Error while fetching from database".to_string()), - } + Ok(cursor.next().await.transpose()?) }