Skip to content

Commit f8dea3a

Browse files
Return custom message if user hasn't pay yet
1 parent 2260abb commit f8dea3a

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

aggregation_mode/gateway/src/http.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,47 @@ impl GatewayServer {
360360

361361
let formatted_time_left = get_time_left_day_formatted();
362362

363-
HttpResponse::Ok().json(AppResponse::new_sucessfull(serde_json::json!({
364-
"proofs_submitted": daily_tasks_by_address,
365-
"quota_limit": state.config.max_daily_proofs_per_user,
366-
"quota_remaining": (state.config.max_daily_proofs_per_user - daily_tasks_by_address),
367-
"quota_resets_in": formatted_time_left.as_str()
368-
})))
363+
let now_epoch = match SystemTime::now().duration_since(UNIX_EPOCH) {
364+
Ok(duration) => duration.as_secs(),
365+
Err(_) => {
366+
return HttpResponse::InternalServerError()
367+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
368+
}
369+
};
370+
371+
let has_payment = match state
372+
.db
373+
.has_active_payment_event(
374+
&address,
375+
// safe unwrap the number comes from a valid u64 primitive
376+
BigDecimal::from_str(&now_epoch.to_string()).unwrap(),
377+
)
378+
.await
379+
{
380+
Ok(result) => result,
381+
Err(_) => {
382+
return HttpResponse::InternalServerError()
383+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
384+
}
385+
};
386+
387+
if has_payment {
388+
HttpResponse::Ok().json(AppResponse::new_sucessfull(serde_json::json!({
389+
"proofs_submitted": daily_tasks_by_address,
390+
"quota_limit": state.config.max_daily_proofs_per_user,
391+
"quota_remaining": (state.config.max_daily_proofs_per_user - daily_tasks_by_address),
392+
"quota_resets_in": formatted_time_left.as_str()
393+
})))
394+
} else {
395+
HttpResponse::Ok().json(AppResponse::new_sucessfull_with_message(
396+
serde_json::json!({
397+
"proofs_submitted": 0,
398+
"quota_limit": 0,
399+
"quota_remaining": 0,
400+
"quota_resets_in": formatted_time_left.as_str()
401+
}),
402+
"You have to pay before submitting a proof".to_string(),
403+
))
404+
}
369405
}
370406
}

aggregation_mode/gateway/src/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ impl AppResponse {
2020
}
2121
}
2222

23+
pub(super) fn new_sucessfull_with_message(data: Value, message: String) -> Self {
24+
Self {
25+
status: 200,
26+
message: message,
27+
data,
28+
}
29+
}
30+
2331
pub(super) fn new_unsucessfull(message: &str, status: u16) -> Self {
2432
Self {
2533
status,

0 commit comments

Comments
 (0)