From 86b1e391ba16d2df99dd24e1c9140b38a58686ba Mon Sep 17 00:00:00 2001 From: Shankar Singh C Date: Sun, 22 Dec 2024 20:23:02 +0530 Subject: [PATCH] address pr comments --- .../src/merchant_connector_account.rs | 6 + crates/hyperswitch_domain_models/src/relay.rs | 63 ++++++- crates/router/src/core/relay.rs | 158 +++++++----------- crates/router/src/core/relay/utils.rs | 2 +- crates/router/src/db/relay.rs | 27 ++- crates/router/src/routes/relay.rs | 3 + 6 files changed, 147 insertions(+), 112 deletions(-) diff --git a/crates/hyperswitch_domain_models/src/merchant_connector_account.rs b/crates/hyperswitch_domain_models/src/merchant_connector_account.rs index b0c57fc70ac5..baec0e59f8a4 100644 --- a/crates/hyperswitch_domain_models/src/merchant_connector_account.rs +++ b/crates/hyperswitch_domain_models/src/merchant_connector_account.rs @@ -72,6 +72,9 @@ impl MerchantConnectorAccount { .clone() .parse_value("ConnectorAuthType") } + pub fn get_connector_test_mode(&self) -> Option { + self.test_mode + } } #[cfg(feature = "v2")] @@ -144,6 +147,9 @@ impl MerchantConnectorAccount { .clone() .parse_value("ConnectorAuthType") } + pub fn get_connector_test_mode(&self) -> Option { + todo!() + } } #[cfg(feature = "v1")] diff --git a/crates/hyperswitch_domain_models/src/relay.rs b/crates/hyperswitch_domain_models/src/relay.rs index b1f3833d49ce..959ac8e7f612 100644 --- a/crates/hyperswitch_domain_models/src/relay.rs +++ b/crates/hyperswitch_domain_models/src/relay.rs @@ -2,7 +2,8 @@ use common_enums::enums; use common_utils::{ self, errors::{CustomResult, ValidationError}, - id_type, pii, + id_type::{self, GenerateId}, + pii, types::{keymanager, MinorUnit}, }; use diesel_models::relay::RelayUpdateInternal; @@ -11,6 +12,8 @@ use masking::{ExposeInterface, Secret}; use serde::{self, Deserialize, Serialize}; use time::PrimitiveDateTime; +use crate::{router_data::ErrorResponse, router_response_types}; + #[derive(Debug, Clone, Deserialize, Serialize)] pub struct Relay { pub id: id_type::RelayId, @@ -31,6 +34,64 @@ pub struct Relay { pub response_data: Option, } +impl Relay { + pub fn new( + relay_request: &api_models::relay::RelayRequest, + merchant_id: &id_type::MerchantId, + profile_id: &id_type::ProfileId, + ) -> Self { + let relay_id = id_type::RelayId::generate(); + Self { + id: relay_id.clone(), + connector_resource_id: relay_request.connector_resource_id.clone(), + connector_id: relay_request.connector_id.clone(), + profile_id: profile_id.clone(), + merchant_id: merchant_id.clone(), + relay_type: common_enums::RelayType::Refund, + request_data: relay_request.data.clone().map(From::from), + status: common_enums::RelayStatus::Created, + connector_reference_id: None, + error_code: None, + error_message: None, + created_at: common_utils::date_time::now(), + modified_at: common_utils::date_time::now(), + response_data: None, + } + } +} + +impl From for RelayData { + fn from(relay: api_models::relay::RelayData) -> Self { + match relay { + api_models::relay::RelayData::Refund(relay_refund_request) => { + Self::Refund(RelayRefundData { + amount: relay_refund_request.amount, + currency: relay_refund_request.currency, + reason: relay_refund_request.reason, + }) + } + } + } +} + +impl RelayUpdate { + pub fn from( + response: Result, + ) -> Self { + match response { + Err(error) => Self::ErrorUpdate { + error_code: error.code, + error_message: error.message, + status: common_enums::RelayStatus::Failure, + }, + Ok(response) => Self::StatusUpdate { + connector_reference_id: Some(response.connector_refund_id), + status: common_enums::RelayStatus::from(response.refund_status), + }, + } + } +} + impl From for api_models::relay::RelayResponse { fn from(value: Relay) -> Self { let error = value diff --git a/crates/router/src/core/relay.rs b/crates/router/src/core/relay.rs index 9d1ad77aaeec..812709269d83 100644 --- a/crates/router/src/core/relay.rs +++ b/crates/router/src/core/relay.rs @@ -1,9 +1,5 @@ use api_models::relay as relay_models; -use common_utils::{ - self, - ext_traits::OptionExt, - id_type::{self, GenerateId}, -}; +use common_utils::{self, ext_traits::OptionExt, id_type}; use error_stack::ResultExt; use super::errors::{self, ConnectorErrorExt, RouterResponse, RouterResult, StorageErrorExt}; @@ -14,7 +10,6 @@ use crate::{ types::{ api::{self}, domain, - transformers::ForeignFrom, }, }; @@ -30,6 +25,7 @@ pub async fn relay( let db = state.store.as_ref(); let key_manager_state = &(&state).into(); let merchant_id = merchant_account.get_id(); + let connector_id = &req.connector_id; let profile_id_from_auth_layer = profile_id_optional .get_required_value("ProfileId") @@ -49,50 +45,72 @@ pub async fn relay( id: profile_id_from_auth_layer.get_string_repr().to_owned(), })?; + #[cfg(feature = "v1")] + let connector_account = db + .find_by_merchant_connector_account_merchant_id_merchant_connector_id( + key_manager_state, + merchant_id, + connector_id, + &key_store, + ) + .await + .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound { + id: connector_id.get_string_repr().to_string(), + })?; + + #[cfg(feature = "v2")] + let connector_account = db + .find_merchant_connector_account_by_id(key_manager_state, connector_id, &key_store) + .await + .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound { + id: connector_id.get_string_repr().to_string(), + })?; + + validate_relay_refund_request(&req).attach_printable("Invalid relay refund request")?; + + let relay_domain = + hyperswitch_domain_models::relay::Relay::new(&req, merchant_id, profile.get_id()); + + let relay_record = db + .insert_relay(key_manager_state, &key_store, relay_domain) + .await + .change_context(errors::ApiErrorResponse::InternalServerError) + .attach_printable("Failed to insert a relay record in db")?; + let relay_response = match req.relay_type { common_enums::RelayType::Refund => { Box::pin(relay_refund( - state, + &state, merchant_account, - profile, - key_store, - &req, + connector_account, + &relay_record, )) .await? } }; + + let relay_update_record = db + .update_relay(key_manager_state, &key_store, relay_record, relay_response) + .await + .change_context(errors::ApiErrorResponse::InternalServerError)?; + + let response = relay_models::RelayResponse::from(relay_update_record); + Ok(hyperswitch_domain_models::api::ApplicationResponse::Json( - relay_response, + response, )) } pub async fn relay_refund( - state: SessionState, + state: &SessionState, merchant_account: domain::MerchantAccount, - profile: domain::Profile, - key_store: domain::MerchantKeyStore, - req: &relay_models::RelayRequest, -) -> RouterResult { - validate_relay_refund_request(req).attach_printable("Invalid relay refund request")?; - - let db = state.store.as_ref(); - let key_manager_state = &(&state).into(); - let connector_id = &req.connector_id; + connector_account: domain::MerchantConnectorAccount, + relay_record: &hyperswitch_domain_models::relay::Relay, +) -> RouterResult { + let connector_id = &relay_record.connector_id; let merchant_id = merchant_account.get_id(); - let connector_account = db - .find_by_merchant_connector_account_merchant_id_merchant_connector_id( - key_manager_state, - merchant_account.get_id(), - connector_id, - &key_store, - ) - .await - .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound { - id: connector_id.get_string_repr().to_string(), - })?; - let connector_data = api::ConnectorData::get_connector_by_name( &state.conf.connectors, &connector_account.connector_name, @@ -106,25 +124,17 @@ pub async fn relay_refund( hyperswitch_domain_models::router_response_types::RefundsResponseData, > = connector_data.connector.get_connector_integration(); - let relay_domain = get_relay_domain_model(req, merchant_account.get_id(), profile.get_id()); - - let relay_record = db - .insert_relay(key_manager_state, &key_store, relay_domain) - .await - .change_context(errors::ApiErrorResponse::InternalServerError) - .attach_printable("Failed to insert a relay record in db")?; - let router_data = utils::construct_relay_refund_router_data( - &state, + state, &connector_account.connector_name, merchant_id, &connector_account, - &relay_record, + relay_record, ) .await?; let router_data_res = services::execute_connector_processing_step( - &state, + state, connector_integration, &router_data, payments::CallConnectorAction::Trigger, @@ -133,26 +143,10 @@ pub async fn relay_refund( .await .to_refund_failed_response()?; - let relay_response = match router_data_res.response { - Err(error) => hyperswitch_domain_models::relay::RelayUpdate::ErrorUpdate { - error_code: error.code, - error_message: error.message, - status: common_enums::RelayStatus::Failure, - }, - Ok(response) => hyperswitch_domain_models::relay::RelayUpdate::StatusUpdate { - connector_reference_id: Some(response.connector_refund_id), - status: common_enums::RelayStatus::from(response.refund_status), - }, - }; - - let relay_update = db - .update_relay(key_manager_state, &key_store, relay_record, relay_response) - .await - .change_context(errors::ApiErrorResponse::InternalServerError)?; - - let response = relay_models::RelayResponse::from(relay_update); + let relay_response = + hyperswitch_domain_models::relay::RelayUpdate::from(router_data_res.response); - Ok(response) + Ok(relay_response) } // validate relay request @@ -181,41 +175,3 @@ pub fn validate_relay_refund_data( } Ok(()) } - -pub fn get_relay_domain_model( - relay_request: &relay_models::RelayRequest, - merchant_id: &id_type::MerchantId, - profile_id: &id_type::ProfileId, -) -> hyperswitch_domain_models::relay::Relay { - let relay_id = id_type::RelayId::generate(); - hyperswitch_domain_models::relay::Relay { - id: relay_id.clone(), - connector_resource_id: relay_request.connector_resource_id.clone(), - connector_id: relay_request.connector_id.clone(), - profile_id: profile_id.clone(), - merchant_id: merchant_id.clone(), - relay_type: common_enums::RelayType::Refund, - request_data: relay_request.data.clone().map(ForeignFrom::foreign_from), - status: common_enums::RelayStatus::Created, - connector_reference_id: None, - error_code: None, - error_message: None, - created_at: common_utils::date_time::now(), - modified_at: common_utils::date_time::now(), - response_data: None, - } -} - -impl ForeignFrom for hyperswitch_domain_models::relay::RelayData { - fn foreign_from(relay: relay_models::RelayData) -> Self { - match relay { - relay_models::RelayData::Refund(relay_refund_request) => { - Self::Refund(hyperswitch_domain_models::relay::RelayRefundData { - amount: relay_refund_request.amount, - currency: relay_refund_request.currency, - reason: relay_refund_request.reason, - }) - } - } - } -} diff --git a/crates/router/src/core/relay/utils.rs b/crates/router/src/core/relay/utils.rs index fcd1fd4912a0..0753be1ec435 100644 --- a/crates/router/src/core/relay/utils.rs +++ b/crates/router/src/core/relay/utils.rs @@ -117,7 +117,7 @@ pub async fn construct_relay_refund_router_data<'a, F>( payout_method_data: None, #[cfg(feature = "payouts")] quote_id: None, - test_mode: connector_account.test_mode, + test_mode: connector_account.get_connector_test_mode(), payment_method_balance: None, connector_api_version, connector_http_status_code: None, diff --git a/crates/router/src/db/relay.rs b/crates/router/src/db/relay.rs index 2bd94e272aec..e869165aa339 100644 --- a/crates/router/src/db/relay.rs +++ b/crates/router/src/db/relay.rs @@ -107,20 +107,29 @@ impl RelayInterface for MockDb { impl RelayInterface for KafkaStore { async fn insert_relay( &self, - _key_manager_state: &KeyManagerState, - _merchant_key_store: &domain::MerchantKeyStore, - _new: hyperswitch_domain_models::relay::Relay, + key_manager_state: &KeyManagerState, + merchant_key_store: &domain::MerchantKeyStore, + new: hyperswitch_domain_models::relay::Relay, ) -> CustomResult { - Err(errors::StorageError::MockDbError)? + self.diesel_store + .insert_relay(key_manager_state, merchant_key_store, new) + .await } async fn update_relay( &self, - _key_manager_state: &KeyManagerState, - _merchant_key_store: &domain::MerchantKeyStore, - _current_state: hyperswitch_domain_models::relay::Relay, - _relay_update: hyperswitch_domain_models::relay::RelayUpdate, + key_manager_state: &KeyManagerState, + merchant_key_store: &domain::MerchantKeyStore, + current_state: hyperswitch_domain_models::relay::Relay, + relay_update: hyperswitch_domain_models::relay::RelayUpdate, ) -> CustomResult { - Err(errors::StorageError::KafkaError)? + self.diesel_store + .update_relay( + key_manager_state, + merchant_key_store, + current_state, + relay_update, + ) + .await } } diff --git a/crates/router/src/routes/relay.rs b/crates/router/src/routes/relay.rs index e8e6b30847b1..13c92eb19a6e 100644 --- a/crates/router/src/routes/relay.rs +++ b/crates/router/src/routes/relay.rs @@ -25,7 +25,10 @@ pub async fn relay( relay::relay( state, auth.merchant_account, + #[cfg(feature = "v1")] auth.profile_id, + #[cfg(feature = "v2")] + Some(auth.profile.get_id().clone()), auth.key_store, req, )