Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(reports): implement programmatic API trigger for reports (#11289)

Rohan Mali1e7eff50bb83
diff --git a/crates/analytics/src/errors.rs b/crates/analytics/src/errors.rs
index d7b15a6db1..73b09ba291 100644
--- a/crates/analytics/src/errors.rs
+++ b/crates/analytics/src/errors.rs
@@ -14,6 +14,8 @@ pub enum AnalyticsError {
AccessForbiddenError,
#[error("Failed to fetch currency exchange rate")]
ForexFetchFailed,
+ #[error("Missing email")]
+ MissingEmail,
}
impl ErrorSwitch<ApiErrorResponse> for AnalyticsError {
@@ -40,6 +42,12 @@ impl ErrorSwitch<ApiErrorResponse> for AnalyticsError {
"Failed to fetch currency exchange rate",
None,
)),
+ Self::MissingEmail => ApiErrorResponse::BadRequest(ApiError::new(
+ "IR",
+ 6,
+ "Missing or invalid merchant email address.",
+ None,
+ )),
}
}
}
diff --git a/crates/api_models/src/analytics.rs b/crates/api_models/src/analytics.rs
index ea94684f10..e270c9bbbb 100644
--- a/crates/api_models/src/analytics.rs
+++ b/crates/api_models/src/analytics.rs
@@ -1,8 +1,7 @@
use std::collections::HashSet;
pub use common_utils::types::TimeRange;
-use common_utils::{events::ApiEventMetric, pii::EmailStrategy, types::authentication::AuthInfo};
-use masking::Secret;
+use common_utils::{events::ApiEventMetric, pii::Email, types::authentication::AuthInfo};
use self::{
active_payments::ActivePaymentsMetrics,
@@ -150,7 +149,7 @@ pub struct RefundDistributionBody {
#[serde(rename_all = "camelCase")]
pub struct ReportRequest {
pub time_range: TimeRange,
- pub emails: Option<Vec<Secret<String, EmailStrategy>>>,
+ pub emails: Option<Vec<Email>>,
#[cfg(feature = "v2")]
#[serde(default)]
pub report_type: ReportType,
@@ -162,7 +161,7 @@ pub struct GenerateReportRequest {
pub request: ReportRequest,
pub merchant_id: Option<common_utils::id_type::MerchantId>,
pub auth: AuthInfo,
- pub email: Secret<String, EmailStrategy>,
+ pub email: Email,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
diff --git a/crates/router/src/analytics.rs b/crates/router/src/analytics.rs
index 8f37484332..a631936651 100644
--- a/crates/router/src/analytics.rs
+++ b/crates/router/src/analytics.rs
@@ -26,15 +26,16 @@ pub mod routes {
GetRefundMetricRequest, GetSdkEventFiltersRequest, GetSdkEventMetricRequest, ReportRequest,
};
use common_enums::EntityType;
- use common_utils::types::TimeRange;
+ use common_utils::{pii::Email, types::TimeRange};
use error_stack::{report, ResultExt};
use futures::{stream::FuturesUnordered, StreamExt};
+ use masking::ExposeInterface;
use crate::{
analytics_validator::request_validator,
consts::opensearch::SEARCH_INDEXES,
core::{api_locking, errors::user::UserErrors, verification::utils},
- db::{user::UserInterface, user_role::ListUserRolesByUserIdPayload},
+ db::user_role::ListUserRolesByUserIdPayload,
routes::AppState,
services::{
api,
@@ -1864,13 +1865,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
@@ -1894,11 +1928,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::MerchantReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::MerchantReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -1917,13 +1958,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let lambda_req = GenerateReportRequest {
@@ -1945,11 +2019,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::OrganizationReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::OrganizationReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -1968,13 +2049,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
@@ -2005,11 +2119,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::ProfileReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2027,13 +2148,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
@@ -2057,11 +2211,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::MerchantReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::MerchantReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2079,13 +2240,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let lambda_req = GenerateReportRequest {
@@ -2107,11 +2301,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::OrganizationReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::OrganizationReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2130,13 +2331,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
@@ -2167,11 +2401,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::ProfileReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2190,13 +2431,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
@@ -2220,11 +2494,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::MerchantReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::MerchantReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2243,13 +2524,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let lambda_req = GenerateReportRequest {
@@ -2271,11 +2585,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::OrganizationReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::OrganizationReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2294,13 +2615,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
let profile_id = auth
@@ -2330,11 +2684,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::ProfileReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2352,13 +2713,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
@@ -2382,11 +2776,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::MerchantReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type::<auth::AuthenticationDataWithUserId, _>(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::MerchantReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2404,13 +2805,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let lambda_req = GenerateReportRequest {
@@ -2432,11 +2866,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::OrganizationReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type::<auth::AuthenticationDataWithUserId, _>(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::OrganizationReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2454,13 +2895,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
let profile_id = {
@@ -2498,11 +2972,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type::<auth::AuthenticationDataWithUserId, _>(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::ProfileReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2521,13 +3002,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
@@ -2551,11 +3065,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::MerchantReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::MerchantReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2574,13 +3095,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let lambda_req = GenerateReportRequest {
@@ -2602,11 +3156,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::OrganizationReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::OrganizationReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
@@ -2625,13 +3186,46 @@ pub mod routes {
&req,
json_payload.into_inner(),
|state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
- let user = UserInterface::find_user_by_id(&*state.global_store, &user_id)
- .await
- .change_context(AnalyticsError::UnknownError)?;
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_user_by_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
- let user_email = UserEmail::from_pii_email(user.email)
- .change_context(AnalyticsError::UnknownError)?
- .get_secret();
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
let org_id = auth.platform.get_processor().get_account().get_org_id();
let merchant_id = auth.platform.get_processor().get_account().get_id();
let profile_id = auth
@@ -2661,11 +3255,18 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileReportRead,
- allow_connected: true,
- allow_platform: false,
- },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::ProfileReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
api_locking::LockAction::NotApplicable,
))
.await
diff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rs
index 5feb9ac4e5..d3a4212bda 100644
--- a/crates/router/src/services/authentication.rs
+++ b/crates/router/src/services/authentication.rs
@@ -811,6 +811,23 @@ where
}
}
+#[async_trait]
+impl<A> AuthenticateAndFetch<AuthenticationDataWithUserId, A> for ApiKeyAuth
+where
+ A: SessionStateInfo + Sync,
+{
+ async fn authenticate_and_fetch(
+ &self,
+ request_headers: &HeaderMap,
+ state: &A,
+ ) -> RouterResult<(AuthenticationDataWithUserId, AuthenticationType)> {
+ let (auth_data, auth_type): (AuthenticationData, AuthenticationType) =
+ self.authenticate_and_fetch(request_headers, state).await?;
+
+ Ok(((auth_data, None), auth_type))
+ }
+}
+
#[derive(Debug)]
pub struct ApiKeyAuthWithMerchantIdFromRoute(pub id_type::MerchantId);
@@ -4961,7 +4978,7 @@ where
}
}
-pub type AuthenticationDataWithUserId = (AuthenticationData, String);
+pub type AuthenticationDataWithUserId = (AuthenticationData, Option<String>);
#[cfg(feature = "v1")]
#[async_trait]
@@ -5037,7 +5054,7 @@ where
client_secret: None,
};
Ok((
- (auth.clone(), payload.user_id.clone()),
+ (auth.clone(), Some(payload.user_id.clone())),
AuthenticationType::MerchantJwt {
merchant_id: payload.merchant_id,
user_id: None,
@@ -5115,7 +5132,7 @@ where
};
Ok((
- (auth.clone(), payload.user_id.clone()),
+ (auth.clone(), Some(payload.user_id.clone())),
AuthenticationType::MerchantJwt {
merchant_id: payload.merchant_id,
user_id: Some(payload.user_id),