From 64fa21eb4fb265e122f97aaae7445fabd571be23 Mon Sep 17 00:00:00 2001 From: ItsMeShashank Date: Thu, 4 May 2023 19:24:34 +0530 Subject: [PATCH] refactor(router): nest the straight through algorithm column in payment attempt (#1040) --- Cargo.lock | 10 +++--- crates/api_models/src/admin.rs | 34 +++++++++++++++++++ crates/router/src/core/payments.rs | 10 +++--- crates/router/src/types/api/admin.rs | 4 +-- .../src/types/storage/payment_attempt.rs | 2 +- .../down.sql | 7 ++++ .../up.sql | 3 ++ 7 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 migrations/2023-05-03-121025_nest_straight_through_col_in_payment_attempt/down.sql create mode 100644 migrations/2023-05-03-121025_nest_straight_through_col_in_payment_attempt/up.sql diff --git a/Cargo.lock b/Cargo.lock index 321e402c34..0935e38b2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2949,7 +2949,7 @@ dependencies = [ [[package]] name = "opentelemetry" version = "0.18.0" -source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" +source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" dependencies = [ "opentelemetry_api", "opentelemetry_sdk", @@ -2958,7 +2958,7 @@ dependencies = [ [[package]] name = "opentelemetry-otlp" version = "0.11.0" -source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" +source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" dependencies = [ "async-trait", "futures", @@ -2975,7 +2975,7 @@ dependencies = [ [[package]] name = "opentelemetry-proto" version = "0.1.0" -source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" +source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" dependencies = [ "futures", "futures-util", @@ -2987,7 +2987,7 @@ dependencies = [ [[package]] name = "opentelemetry_api" version = "0.18.0" -source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" +source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" dependencies = [ "fnv", "futures-channel", @@ -3002,7 +3002,7 @@ dependencies = [ [[package]] name = "opentelemetry_sdk" version = "0.18.0" -source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" +source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" dependencies = [ "async-trait", "crossbeam-channel", diff --git a/crates/api_models/src/admin.rs b/crates/api_models/src/admin.rs index 52524aea8c..f943d536e1 100644 --- a/crates/api_models/src/admin.rs +++ b/crates/api_models/src/admin.rs @@ -263,6 +263,40 @@ pub enum RoutingAlgorithm { Single(api_enums::RoutableConnectors), } +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde( + tag = "type", + content = "data", + rename_all = "snake_case", + from = "StraightThroughAlgorithmSerde", + into = "StraightThroughAlgorithmSerde" +)] +pub enum StraightThroughAlgorithm { + Single(api_enums::RoutableConnectors), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(untagged)] +pub enum StraightThroughAlgorithmSerde { + Direct(StraightThroughAlgorithm), + Nested { algorithm: StraightThroughAlgorithm }, +} + +impl From for StraightThroughAlgorithm { + fn from(value: StraightThroughAlgorithmSerde) -> Self { + match value { + StraightThroughAlgorithmSerde::Direct(algorithm) => algorithm, + StraightThroughAlgorithmSerde::Nested { algorithm } => algorithm, + } + } +} + +impl From for StraightThroughAlgorithmSerde { + fn from(value: StraightThroughAlgorithm) -> Self { + Self::Nested { algorithm: value } + } +} + #[derive(Clone, Debug, Deserialize, ToSchema, Serialize)] #[serde(deny_unknown_fields)] pub struct PrimaryBusinessDetails { diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs index fbd8dba8ff..a22f91ba21 100644 --- a/crates/router/src/core/payments.rs +++ b/crates/router/src/core/payments.rs @@ -1079,8 +1079,8 @@ where .attach_printable("Invalid straight through algorithm format in payment attempt")?, }; - let request_straight_through: Option = request_straight_through - .map(|val| val.parse_value("RoutingAlgorithm")) + let request_straight_through: Option = request_straight_through + .map(|val| val.parse_value("StraightThroughAlgorithm")) .transpose() .change_context(errors::ApiErrorResponse::InternalServerError) .attach_printable("Invalid straight through routing rules format")?; @@ -1108,7 +1108,7 @@ where pub fn decide_connector( state: &AppState, merchant_account: &storage::MerchantAccount, - request_straight_through: Option, + request_straight_through: Option, routing_data: &mut storage::RoutingData, ) -> RouterResult { if let Some(ref connector_name) = routing_data.routed_through { @@ -1125,7 +1125,7 @@ pub fn decide_connector( if let Some(routing_algorithm) = request_straight_through { let connector_name = match &routing_algorithm { - api::RoutingAlgorithm::Single(conn) => conn.to_string(), + api::StraightThroughAlgorithm::Single(conn) => conn.to_string(), }; let connector_data = api::ConnectorData::get_connector_by_name( @@ -1143,7 +1143,7 @@ pub fn decide_connector( if let Some(ref routing_algorithm) = routing_data.algorithm { let connector_name = match routing_algorithm { - api::RoutingAlgorithm::Single(conn) => conn.to_string(), + api::StraightThroughAlgorithm::Single(conn) => conn.to_string(), }; let connector_data = api::ConnectorData::get_connector_by_name( diff --git a/crates/router/src/types/api/admin.rs b/crates/router/src/types/api/admin.rs index 9cfd4ea214..0a49d01e1a 100644 --- a/crates/router/src/types/api/admin.rs +++ b/crates/router/src/types/api/admin.rs @@ -2,8 +2,8 @@ pub use api_models::admin::{ MerchantAccountCreate, MerchantAccountDeleteResponse, MerchantAccountResponse, MerchantAccountUpdate, MerchantConnectorCreate, MerchantConnectorDeleteResponse, MerchantConnectorDetails, MerchantConnectorDetailsWrap, MerchantConnectorId, MerchantDetails, - MerchantId, PaymentMethodsEnabled, RoutingAlgorithm, ToggleKVRequest, ToggleKVResponse, - WebhookDetails, + MerchantId, PaymentMethodsEnabled, RoutingAlgorithm, StraightThroughAlgorithm, ToggleKVRequest, + ToggleKVResponse, WebhookDetails, }; use common_utils::ext_traits::ValueExt; diff --git a/crates/router/src/types/storage/payment_attempt.rs b/crates/router/src/types/storage/payment_attempt.rs index 6171bf6dde..bb14bbad81 100644 --- a/crates/router/src/types/storage/payment_attempt.rs +++ b/crates/router/src/types/storage/payment_attempt.rs @@ -5,7 +5,7 @@ pub use storage_models::payment_attempt::{ #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct RoutingData { pub routed_through: Option, - pub algorithm: Option, + pub algorithm: Option, } #[cfg(feature = "kv_store")] diff --git a/migrations/2023-05-03-121025_nest_straight_through_col_in_payment_attempt/down.sql b/migrations/2023-05-03-121025_nest_straight_through_col_in_payment_attempt/down.sql new file mode 100644 index 0000000000..192e951449 --- /dev/null +++ b/migrations/2023-05-03-121025_nest_straight_through_col_in_payment_attempt/down.sql @@ -0,0 +1,7 @@ +-- This file should undo anything in `up.sql` +UPDATE payment_attempt +SET straight_through_algorithm = CASE WHEN straight_through_algorithm->>'algorithm' IS NULL THEN + NULL +ELSE + straight_through_algorithm->'algorithm' +END; diff --git a/migrations/2023-05-03-121025_nest_straight_through_col_in_payment_attempt/up.sql b/migrations/2023-05-03-121025_nest_straight_through_col_in_payment_attempt/up.sql new file mode 100644 index 0000000000..63683b5924 --- /dev/null +++ b/migrations/2023-05-03-121025_nest_straight_through_col_in_payment_attempt/up.sql @@ -0,0 +1,3 @@ +-- Your SQL goes here +UPDATE payment_attempt +SET straight_through_algorithm = jsonb_build_object('algorithm', straight_through_algorithm);