Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: altcoin support to renewal endpoints #65

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/endpoints/renewal/get_non_subscribed_domains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,41 @@ 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": [
{ "renew_flows": { "$eq": null } },
{
"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
}
]
}
Expand All @@ -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"
}
},
}
},
];
Expand All @@ -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());
Expand Down
70 changes: 43 additions & 27 deletions src/endpoints/renewal/get_renewal_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,64 @@ pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<StarknetIdQuery>,
) -> 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<Option<mongodb::bson::Document>> {
let collection = state
.starknetid_db
.collection::<mongodb::bson::Document>("auto_renew_flows");
.collection::<mongodb::bson::Document>(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 },
],
},
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()?)
}
Loading