diff --git a/crates/analytics/src/opensearch.rs b/crates/analytics/src/opensearch.rs index cb06180ceb2f..b8c582df1bfc 100644 --- a/crates/analytics/src/opensearch.rs +++ b/crates/analytics/src/opensearch.rs @@ -131,7 +131,7 @@ impl ErrorSwitch for OpenSearchError { )) } Self::UnknownError => { - ApiErrorResponse::InternalServerError(ApiError::new("IR", 4, "Unknown error", None)) + ApiErrorResponse::InternalServerError(ApiError::new("IR", 6, "Unknown error", None)) } } } diff --git a/crates/api_models/src/analytics/search.rs b/crates/api_models/src/analytics/search.rs index b882e536bb8e..9ff71a639e4d 100644 --- a/crates/api_models/src/analytics/search.rs +++ b/crates/api_models/src/analytics/search.rs @@ -59,7 +59,9 @@ pub struct GetSearchResponse { #[derive(Debug, serde::Deserialize)] pub struct OpenMsearchOutput { + #[serde(default)] pub responses: Vec, + pub error: Option, } #[derive(Debug, serde::Deserialize)] diff --git a/crates/router/src/analytics.rs b/crates/router/src/analytics.rs index 77fdfcfb5e62..988870e8c068 100644 --- a/crates/router/src/analytics.rs +++ b/crates/router/src/analytics.rs @@ -20,6 +20,7 @@ pub mod routes { use error_stack::ResultExt; use crate::{ + consts::opensearch::OPENSEARCH_INDEX_PERMISSIONS, core::{api_locking, errors::user::UserErrors}, db::user::UserInterface, routes::AppState, @@ -661,28 +662,11 @@ pub mod routes { .change_context(UserErrors::InternalServerError) .change_context(OpenSearchError::UnknownError)?; let permissions = role_info.get_permissions_set(); - let accessible_indexes: Vec<_> = vec![ - ( - SearchIndex::PaymentAttempts, - vec![Permission::PaymentRead, Permission::PaymentWrite], - ), - ( - SearchIndex::PaymentIntents, - vec![Permission::PaymentRead, Permission::PaymentWrite], - ), - ( - SearchIndex::Refunds, - vec![Permission::RefundRead, Permission::RefundWrite], - ), - ( - SearchIndex::Disputes, - vec![Permission::DisputeRead, Permission::DisputeWrite], - ), - ] - .into_iter() - .filter(|(_, perm)| perm.iter().any(|p| permissions.contains(p))) - .map(|i| i.0) - .collect(); + let accessible_indexes: Vec<_> = OPENSEARCH_INDEX_PERMISSIONS + .iter() + .filter(|(_, perm)| perm.iter().any(|p| permissions.contains(p))) + .map(|(i, _)| *i) + .collect(); analytics::search::msearch_results( &state.opensearch_client, @@ -724,28 +708,11 @@ pub mod routes { .change_context(UserErrors::InternalServerError) .change_context(OpenSearchError::UnknownError)?; let permissions = role_info.get_permissions_set(); - let _ = vec![ - ( - SearchIndex::PaymentAttempts, - vec![Permission::PaymentRead, Permission::PaymentWrite], - ), - ( - SearchIndex::PaymentIntents, - vec![Permission::PaymentRead, Permission::PaymentWrite], - ), - ( - SearchIndex::Refunds, - vec![Permission::RefundRead, Permission::RefundWrite], - ), - ( - SearchIndex::Disputes, - vec![Permission::DisputeRead, Permission::DisputeWrite], - ), - ] - .into_iter() - .filter(|(ind, _)| *ind == index) - .find(|i| i.1.iter().any(|p| permissions.contains(p))) - .ok_or(OpenSearchError::IndexAccessNotPermittedError(index))?; + let _ = OPENSEARCH_INDEX_PERMISSIONS + .iter() + .filter(|(ind, _)| *ind == index) + .find(|i| i.1.iter().any(|p| permissions.contains(p))) + .ok_or(OpenSearchError::IndexAccessNotPermittedError(index))?; analytics::search::search_results(&state.opensearch_client, req, &auth.merchant_id) .await .map(ApplicationResponse::Json) diff --git a/crates/router/src/consts.rs b/crates/router/src/consts.rs index 73ff7c428e46..93c8139e1081 100644 --- a/crates/router/src/consts.rs +++ b/crates/router/src/consts.rs @@ -1,6 +1,7 @@ #[cfg(feature = "olap")] pub mod user; pub mod user_role; +pub mod opensearch; pub use hyperswitch_interfaces::consts::{NO_ERROR_CODE, NO_ERROR_MESSAGE}; // ID generation pub(crate) const ID_LENGTH: usize = 20; diff --git a/crates/router/src/consts/opensearch.rs b/crates/router/src/consts/opensearch.rs new file mode 100644 index 000000000000..61a3a78d122d --- /dev/null +++ b/crates/router/src/consts/opensearch.rs @@ -0,0 +1,21 @@ +use crate::services::authorization::permissions::Permission; +use api_models::analytics::search::SearchIndex; + +pub const OPENSEARCH_INDEX_PERMISSIONS: &[(SearchIndex, &[Permission])] = &[ + ( + SearchIndex::PaymentAttempts, + &[Permission::PaymentRead, Permission::PaymentWrite], + ), + ( + SearchIndex::PaymentIntents, + &[Permission::PaymentRead, Permission::PaymentWrite], + ), + ( + SearchIndex::Refunds, + &[Permission::RefundRead, Permission::RefundWrite], + ), + ( + SearchIndex::Disputes, + &[Permission::DisputeRead, Permission::DisputeWrite], + ), +];