Skip to content

Commit

Permalink
feat(router, analytics): implement requires_forex_functionality and i…
Browse files Browse the repository at this point in the history
…mprove request_validator
  • Loading branch information
maverox committed Dec 3, 2024
1 parent 247bbb2 commit 2de97c4
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 46 deletions.
23 changes: 23 additions & 0 deletions crates/api_models/src/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ impl Default for AnalyticsRequest {
}
}
}
impl AnalyticsRequest {
pub fn requires_forex_functionality(&self) -> bool {
self.payment_attempt
.as_ref()
.map(|req| req.metrics.iter().any(|metric| metric.is_forex_metric()))
.unwrap_or_default()
|| self
.payment_intent
.as_ref()
.map(|req| req.metrics.iter().any(|metric| metric.is_forex_metric()))
.unwrap_or_default()
|| self
.refund
.as_ref()
.map(|req| req.metrics.iter().any(|metric| metric.is_forex_metric()))
.unwrap_or_default()
|| self
.dispute
.as_ref()
.map(|req| req.metrics.iter().any(|metric| metric.is_forex_metric()))
.unwrap_or_default()
}
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetPaymentMetricRequest {
Expand Down
141 changes: 100 additions & 41 deletions crates/router/src/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@ pub mod routes {
search::{
GetGlobalSearchRequest, GetSearchRequest, GetSearchRequestWithIndex, SearchIndex,
},
GenerateReportRequest, GetActivePaymentsMetricRequest, GetApiEventFiltersRequest,
GetApiEventMetricRequest, GetAuthEventMetricRequest, GetDisputeMetricRequest,
GetFrmFilterRequest, GetFrmMetricRequest, GetPaymentFiltersRequest,
GetPaymentIntentFiltersRequest, GetPaymentIntentMetricRequest, GetPaymentMetricRequest,
GetRefundFilterRequest, GetRefundMetricRequest, GetSdkEventFiltersRequest,
GetSdkEventMetricRequest, ReportRequest, AnalyticsRequest,
AnalyticsRequest, GenerateReportRequest, GetActivePaymentsMetricRequest,
GetApiEventFiltersRequest, GetApiEventMetricRequest, GetAuthEventMetricRequest,
GetDisputeMetricRequest, GetFrmFilterRequest, GetFrmMetricRequest,
GetPaymentFiltersRequest, GetPaymentIntentFiltersRequest, GetPaymentIntentMetricRequest,
GetPaymentMetricRequest, GetRefundFilterRequest, GetRefundMetricRequest,
GetSdkEventFiltersRequest, GetSdkEventMetricRequest, ReportRequest,
};
use common_enums::EntityType;
use common_utils::types::TimeRange;

use error_stack::{report, ResultExt};
use futures::{stream::FuturesUnordered, StreamExt};

use crate::{
analytics_validator::request_validator,
consts::opensearch::SEARCH_INDEXES,
core::{
api_locking, errors::user::UserErrors,
verification::utils,
},
core::{api_locking, errors::user::UserErrors, verification::utils},
db::{user::UserInterface, user_role::ListUserRolesByUserIdPayload},
routes::AppState,
services::{
Expand All @@ -45,9 +41,9 @@ pub mod routes {
ApplicationResponse,
},
types::{domain::UserEmail, storage::UserRole},
analytics_validator::request_validator,
};

use error_stack::{report, ResultExt};
use futures::{stream::FuturesUnordered, StreamExt};

pub struct Analytics;

Expand Down Expand Up @@ -407,8 +403,15 @@ pub mod routes {
org_id: org_id.clone(),
merchant_ids: vec![merchant_id.clone()],
};
let validator_response = request_validator(AnalyticsRequest{payment_attempt: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;
let validator_response = request_validator(
AnalyticsRequest {
payment_attempt: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::payments::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down Expand Up @@ -447,9 +450,16 @@ pub mod routes {
let auth: AuthInfo = AuthInfo::OrgLevel {
org_id: org_id.clone(),
};

let validator_response = request_validator(AnalyticsRequest{payment_attempt: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;

let validator_response = request_validator(
AnalyticsRequest {
payment_attempt: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::payments::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down Expand Up @@ -496,9 +506,16 @@ let validator_response = request_validator(AnalyticsRequest{payment_attempt: Som
merchant_id: merchant_id.clone(),
profile_ids: vec![profile_id.clone()],
};

let validator_response = request_validator(AnalyticsRequest{payment_attempt: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;

let validator_response = request_validator(
AnalyticsRequest {
payment_attempt: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::payments::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down Expand Up @@ -539,9 +556,16 @@ let validator_response = request_validator(AnalyticsRequest{payment_attempt: Som
org_id: org_id.clone(),
merchant_ids: vec![merchant_id.clone()],
};

let validator_response = request_validator(AnalyticsRequest{payment_intent: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;

let validator_response = request_validator(
AnalyticsRequest {
payment_intent: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::payment_intents::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down Expand Up @@ -580,9 +604,16 @@ let validator_response = request_validator(AnalyticsRequest{payment_intent: Some
let auth: AuthInfo = AuthInfo::OrgLevel {
org_id: org_id.clone(),
};

let validator_response = request_validator(AnalyticsRequest{payment_intent: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;

let validator_response = request_validator(
AnalyticsRequest {
payment_intent: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::payment_intents::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down Expand Up @@ -629,9 +660,16 @@ let validator_response = request_validator(AnalyticsRequest{payment_intent: Some
merchant_id: merchant_id.clone(),
profile_ids: vec![profile_id.clone()],
};

let validator_response = request_validator(AnalyticsRequest{payment_intent: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;

let validator_response = request_validator(
AnalyticsRequest {
payment_intent: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::payment_intents::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down Expand Up @@ -672,9 +710,16 @@ let validator_response = request_validator(AnalyticsRequest{payment_intent: Some
org_id: org_id.clone(),
merchant_ids: vec![merchant_id.clone()],
};

let validator_response = request_validator(AnalyticsRequest{refund: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;

let validator_response = request_validator(
AnalyticsRequest {
refund: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::refunds::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down Expand Up @@ -713,9 +758,16 @@ let validator_response = request_validator(AnalyticsRequest{refund: Some(req.clo
let auth: AuthInfo = AuthInfo::OrgLevel {
org_id: org_id.clone(),
};

let validator_response = request_validator(AnalyticsRequest{refund: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;

let validator_response = request_validator(
AnalyticsRequest {
refund: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::refunds::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down Expand Up @@ -762,9 +814,16 @@ let validator_response = request_validator(AnalyticsRequest{refund: Some(req.clo
merchant_id: merchant_id.clone(),
profile_ids: vec![profile_id.clone()],
};

let validator_response = request_validator(AnalyticsRequest{refund: Some(req.clone()), ..Default::default()}, &state).await?;
let ex_rates = validator_response.1;

let validator_response = request_validator(
AnalyticsRequest {
refund: Some(req.clone()),
..Default::default()
},
&state,
)
.await?;
let ex_rates = validator_response;
analytics::refunds::get_metrics(&state.pool, &ex_rates, &auth, req)
.await
.map(ApplicationResponse::Json)
Expand Down
14 changes: 9 additions & 5 deletions crates/router/src/analytics_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ use analytics::errors::AnalyticsError;
use api_models::analytics::AnalyticsRequest;
use common_utils::errors::CustomResult;
use currency_conversion::types::ExchangeRates;
use router_env::logger;

use crate::core::currency::get_forex_exchange_rates;

pub async fn request_validator(
_req_type: AnalyticsRequest,
req_type: AnalyticsRequest,
state: &crate::routes::SessionState,
) -> CustomResult<(bool, Option<ExchangeRates>), AnalyticsError> {
) -> CustomResult<Option<ExchangeRates>, AnalyticsError> {
let forex_enabled = state.conf.analytics.get_inner().get_forex_enabled();
let require_forex_functionality= req_type.requires_forex_functionality();
// other validation logic based on `req_type` goes here

let ex_rates = if state.conf.analytics.get_inner().get_forex_enabled() {

let ex_rates = if forex_enabled && require_forex_functionality {
logger::info!("Fetching forex exchange rates");
Some(get_forex_exchange_rates(state.clone()).await?)
} else {
None
};

Ok((ex_rates.is_some(), ex_rates))
Ok(ex_rates)
}

0 comments on commit 2de97c4

Please sign in to comment.