Repository: juspay/hyperswitch
File: crates/router/src/analytics.rs (Rust)

Code before:
                    &state.conf.report_download_config.authentication_function,
                    &state.conf.report_download_config.region,
                    &json_bytes,
                )
                .await
                .map(ApplicationResponse::Json)
            },
            &auth::JWTAuth {
                permission: Permission::ProfileReportRead,
            },
            api_locking::LockAction::NotApplicable,
        ))
        .await
    }

    #[cfg(feature = "v2")]
    pub async fn generate_profile_payment_report(
        state: web::Data<AppState>,
        req: actix_web::HttpRequest,
        json_payload: web::Json<ReportRequest>,
    ) -> impl Responder {
        let flow = AnalyticsFlow::GeneratePaymentReport;
        Box::pin(api::server_wrap(
            flow,
            state.clone(),
            &req,
            json_payload.into_inner(),
            |state, user_from_token: auth::UserFromToken, payload, _| async move {
                let user =
                    UserInterface::find_user_by_id(&*state.global_store, &user_from_token.user_id)
                        .await
                        .change_context(AnalyticsError::UnknownError)?;

                let user_email = UserEmail::from_pii_email(user.email)
                    .change_context(AnalyticsError::UnknownError)?
                    .get_secret();

                let lambda_req = GenerateReportRequest {
                    request: payload.clone(),
                    merchant_id: Some(user_from_token.merchant_id.clone()),
                    auth: AuthInfo::ProfileLevel {
                        org_id: user_from_token.org_id.clone(),
                        merchant_id: user_from_token.merchant_id.clone(),
                        profile_ids: vec![user_from_token.profile_id.clone()],
                    },
                    email: user_email,
                    report_type: payload.report_type.clone(),
                };

                // Convert to GenerateReportRequest  if report_type is present, otherwise use GenerateReportRequest
                let json_bytes =

Review comment (suggestion):
We don't require new handler functions for v2. You can remove the v1 feature flag from the existing handler functions, and reuse them for the v2 routes as well (by adding the report type field)

Code after:
    }

    #[cfg(feature = "v1")]
    pub async fn generate_org_authentication_report(
        state: web::Data<AppState>,
        req: actix_web::HttpRequest,
        json_payload: web::Json<ReportRequest>,
    ) -> impl Responder {
        let flow = AnalyticsFlow::GenerateAuthenticationReport;
        Box::pin(api::server_wrap(
            flow,
            state.clone(),
            &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 = UserEmail::from_pii_email(user.email)
                    .change_context(AnalyticsError::UnknownError)?
                    .get_secret();

                let org_id = auth.platform.get_processor().get_account().get_org_id();
                let lambda_req = GenerateReportRequest {
                    request: payload,
                    merchant_id: None,
                    auth: AuthInfo::OrgLevel {
                        org_id: org_id.clone(),
                    },
                    email: user_email,
                };

                let json_bytes =
                    serde_json::to_vec(&lambda_req).map_err(|_| AnalyticsError::UnknownError)?;
                invoke_lambda(
                    &state.conf.report_download_config.authentication_function,
                    &state.conf.report_download_config.region,
                    &json_bytes,
                )
                .await
                .map(ApplicationResponse::Json)
            },
            &auth::JWTAuth {
                permission: Permission::OrganizationReportRead,
                allow_connected: true,
                allow_platform: false,
            },
            api_locking::LockAction::NotApplicable,
        ))
        .await