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

Code before:
    let destination_context = domain::Platform::new(
        destination_merchant_account.clone(),
        destination_key_store.clone(),
        destination_merchant_account.clone(),
        destination_key_store.clone(),
        None,
    );

    admin::create_connector(
        state,
        merchant_connector_create,
        destination_context.get_processor().clone(),
        Some(request.destination.profile_id),
    )
    .await
    .map_err(|e| {
        let message = e.current_context().error_message();
        e.change_context(UserErrors::ErrorCloningConnector(message))
    })
    .attach_printable("Failed to create cloned connector")
}

#[cfg(feature = "v1")]
pub async fn issue_embedded_token(
    state: SessionState,
    authentication_data: auth::AuthenticationData,
) -> UserResponse<user_api::IssueEmbeddedTokenResponse> {
    let profile_id = authentication_data
        .profile
        .ok_or(UserErrors::InvalidEmbeddedOperation(
            "No business profile ID provided".to_string(),
        ))
        .attach_printable("No profile_id provided in headers for embedded token")?
        .get_id()
        .clone();
    state
        .store
        .find_business_profile_by_merchant_id_profile_id(
            &authentication_data.platform.get_processor().get_key_store(),
            authentication_data
                .platform
                .get_processor()
                .get_account()
                .get_id(),
            &profile_id,
        )
        .await
        .map_err(|e| {
            if e.current_context().is_db_not_found() {
                e.change_context(UserErrors::InvalidEmbeddedOperation(
                    "Invalid business profile ID".to_string(),

Review comment (suggestion):
Can we send profile and processor as separate parameters to this function. This will help in type safety (provider should not be used unintentionally).

Code after:
    let destination_context = domain::Platform::new(
        destination_merchant_account.clone(),
        destination_key_store.clone(),
        destination_merchant_account.clone(),
        destination_key_store.clone(),
        None,
    );

    admin::create_connector(
        state,
        merchant_connector_create,
        destination_context.get_processor().clone(),
        Some(request.destination.profile_id),
    )
    .await
    .map_err(|e| {
        let message = e.current_context().error_message();
        e.change_context(UserErrors::ErrorCloningConnector(message))
    })
    .attach_printable("Failed to create cloned connector")
}

#[cfg(feature = "v1")]
pub async fn issue_embedded_token(
    state: SessionState,
    processor: domain::Processor,
    business_profile: Option<domain::Profile>,
) -> UserResponse<user_api::IssueEmbeddedTokenResponse> {
    let profile_id = business_profile
        .ok_or(UserErrors::InvalidEmbeddedOperation(
            "No business profile ID provided".to_string(),
        ))
        .attach_printable("No profile_id provided in headers for embedded token")?
        .get_id()
        .clone();
    state
        .store
        .find_business_profile_by_merchant_id_profile_id(
            processor.get_key_store(),
            processor.get_account().get_id(),
            &profile_id,
        )
        .await
        .to_not_found_response(UserErrors::InvalidEmbeddedOperation(
            "Invalid business profile ID".to_string(),
        ))?;
    auth::embedded::EmbeddedToken::new_token(
        state.tenant.tenant_id,
        processor.get_account().get_org_id().clone(),
        processor.get_account().get_id().clone(),
        profile_id,