feat(analytics): Revert api_event metrics and filters back to merchant_id authentication (#5821)

This commit is contained in:
Sandeep Kumar
2024-09-10 13:30:26 +05:30
committed by GitHub
parent aaeb192576
commit 283154d3f6
8 changed files with 65 additions and 208 deletions

View File

@ -215,14 +215,6 @@ pub mod routes {
web::resource("filters/disputes")
.route(web::post().to(get_org_dispute_filters)),
)
.service(
web::resource("filters/api_events")
.route(web::post().to(get_org_api_event_filters)),
)
.service(
web::resource("metrics/api_events")
.route(web::post().to(get_org_api_events_metrics)),
)
.service(
web::resource("report/dispute")
.route(web::post().to(generate_org_dispute_report)),
@ -257,14 +249,6 @@ pub mod routes {
web::resource("filters/refunds")
.route(web::post().to(get_profile_refund_filters)),
)
.service(
web::resource("metrics/api_events")
.route(web::post().to(get_profile_api_events_metrics)),
)
.service(
web::resource("filters/api_events")
.route(web::post().to(get_profile_api_event_filters)),
)
.service(
web::resource("metrics/disputes")
.route(web::post().to(get_profile_dispute_metrics)),
@ -1757,100 +1741,13 @@ pub mod routes {
&req,
payload,
|state, auth: AuthenticationData, req, _| async move {
let org_id = auth.merchant_account.get_org_id();
let merchant_id = auth.merchant_account.get_id();
let auth: AuthInfo = AuthInfo::MerchantLevel {
org_id: org_id.clone(),
merchant_ids: vec![merchant_id.clone()],
};
analytics::api_event::get_api_event_metrics(&state.pool, &auth, req)
.await
.map(ApplicationResponse::Json)
},
&auth::JWTAuth {
permission: Permission::Analytics,
minimum_entity_level: EntityType::Merchant,
},
api_locking::LockAction::NotApplicable,
))
.await
}
/// # Panics
///
/// Panics if `json_payload` array does not contain one `GetApiEventMetricRequest` element.
pub async fn get_org_api_events_metrics(
state: web::Data<AppState>,
req: actix_web::HttpRequest,
json_payload: web::Json<[GetApiEventMetricRequest; 1]>,
) -> impl Responder {
// safety: This shouldn't panic owing to the data type
#[allow(clippy::expect_used)]
let payload = json_payload
.into_inner()
.to_vec()
.pop()
.expect("Couldn't get GetApiEventMetricRequest");
let flow = AnalyticsFlow::GetApiEventMetrics;
Box::pin(api::server_wrap(
flow,
state.clone(),
&req,
payload,
|state, auth: AuthenticationData, req, _| async move {
let org_id = auth.merchant_account.get_org_id();
let auth: AuthInfo = AuthInfo::OrgLevel {
org_id: org_id.clone(),
};
analytics::api_event::get_api_event_metrics(&state.pool, &auth, req)
.await
.map(ApplicationResponse::Json)
},
&auth::JWTAuth {
permission: Permission::Analytics,
minimum_entity_level: EntityType::Merchant,
},
api_locking::LockAction::NotApplicable,
))
.await
}
/// # Panics
///
/// Panics if `json_payload` array does not contain one `GetApiEventMetricRequest` element.
pub async fn get_profile_api_events_metrics(
state: web::Data<AppState>,
req: actix_web::HttpRequest,
json_payload: web::Json<[GetApiEventMetricRequest; 1]>,
) -> impl Responder {
// safety: This shouldn't panic owing to the data type
#[allow(clippy::expect_used)]
let payload = json_payload
.into_inner()
.to_vec()
.pop()
.expect("Couldn't get GetApiEventMetricRequest");
let flow = AnalyticsFlow::GetApiEventMetrics;
Box::pin(api::server_wrap(
flow,
state.clone(),
&req,
payload,
|state, auth: AuthenticationData, req, _| async move {
let org_id = auth.merchant_account.get_org_id();
let merchant_id = auth.merchant_account.get_id();
let profile_id = auth
.profile_id
.ok_or(report!(UserErrors::JwtProfileIdMissing))
.change_context(AnalyticsError::AccessForbiddenError)?;
let auth: AuthInfo = AuthInfo::ProfileLevel {
org_id: org_id.clone(),
merchant_id: merchant_id.clone(),
profile_ids: vec![profile_id.clone()],
};
analytics::api_event::get_api_event_metrics(&state.pool, &auth, req)
.await
.map(ApplicationResponse::Json)
analytics::api_event::get_api_event_metrics(
&state.pool,
auth.merchant_account.get_id(),
req,
)
.await
.map(ApplicationResponse::Json)
},
&auth::JWTAuth {
permission: Permission::Analytics,
@ -1873,78 +1770,7 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, auth: AuthenticationData, req, _| async move {
let org_id = auth.merchant_account.get_org_id();
let merchant_id = auth.merchant_account.get_id();
let auth: AuthInfo = AuthInfo::MerchantLevel {
org_id: org_id.clone(),
merchant_ids: vec![merchant_id.clone()],
};
analytics::api_event::get_filters(&state.pool, req, &auth)
.await
.map(ApplicationResponse::Json)
},
&auth::JWTAuth {
permission: Permission::Analytics,
minimum_entity_level: EntityType::Merchant,
},
api_locking::LockAction::NotApplicable,
))
.await
}
pub async fn get_org_api_event_filters(
state: web::Data<AppState>,
req: actix_web::HttpRequest,
json_payload: web::Json<GetApiEventFiltersRequest>,
) -> impl Responder {
let flow = AnalyticsFlow::GetApiEventFilters;
Box::pin(api::server_wrap(
flow,
state.clone(),
&req,
json_payload.into_inner(),
|state, auth: AuthenticationData, req, _| async move {
let org_id = auth.merchant_account.get_org_id();
let auth: AuthInfo = AuthInfo::OrgLevel {
org_id: org_id.clone(),
};
analytics::api_event::get_filters(&state.pool, req, &auth)
.await
.map(ApplicationResponse::Json)
},
&auth::JWTAuth {
permission: Permission::Analytics,
minimum_entity_level: EntityType::Merchant,
},
api_locking::LockAction::NotApplicable,
))
.await
}
pub async fn get_profile_api_event_filters(
state: web::Data<AppState>,
req: actix_web::HttpRequest,
json_payload: web::Json<GetApiEventFiltersRequest>,
) -> impl Responder {
let flow = AnalyticsFlow::GetApiEventFilters;
Box::pin(api::server_wrap(
flow,
state.clone(),
&req,
json_payload.into_inner(),
|state, auth: AuthenticationData, req, _| async move {
let org_id = auth.merchant_account.get_org_id();
let merchant_id = auth.merchant_account.get_id();
let profile_id = auth
.profile_id
.ok_or(report!(UserErrors::JwtProfileIdMissing))
.change_context(AnalyticsError::AccessForbiddenError)?;
let auth: AuthInfo = AuthInfo::ProfileLevel {
org_id: org_id.clone(),
merchant_id: merchant_id.clone(),
profile_ids: vec![profile_id.clone()],
};
analytics::api_event::get_filters(&state.pool, req, &auth)
analytics::api_event::get_filters(&state.pool, req, auth.merchant_account.get_id())
.await
.map(ApplicationResponse::Json)
},