Skip to content

Commit

Permalink
refactor(routing): Refactor api v2 routes for deactivating and retrie…
Browse files Browse the repository at this point in the history
…ving the routing config (#5478)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Sanchith Hegde <[email protected]>
Co-authored-by: Arun Raj M <[email protected]>
  • Loading branch information
4 people authored Aug 5, 2024
1 parent 7743255 commit 3fea00c
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 58 deletions.
9 changes: 9 additions & 0 deletions crates/api_models/src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ impl ConnectorSelection {
}
}
}
#[cfg(all(feature = "v2", feature = "routing_v2"))]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct RoutingConfigRequest {
pub name: String,
pub description: String,
pub algorithm: RoutingAlgorithm,
pub profile_id: String,
}

#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "routing_v2")))]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct RoutingConfigRequest {
pub name: Option<String>,
Expand Down
159 changes: 114 additions & 45 deletions crates/router/src/core/routing.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
pub mod helpers;
pub mod transformers;

#[cfg(all(feature = "v2", feature = "routing_v2"))]
use api_models::routing::RoutingConfigRequest;
use api_models::{
enums,
routing::{
self as routing_types, RoutingAlgorithmId, RoutingRetrieveLinkQuery, RoutingRetrieveQuery,
},
routing::{self as routing_types, RoutingRetrieveLinkQuery, RoutingRetrieveQuery},
};
#[cfg(all(feature = "v2", feature = "routing_v2"))]
use diesel_models::routing_algorithm::RoutingAlgorithm;
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "routing_v2")))]
use diesel_models::routing_algorithm::RoutingAlgorithm;
use error_stack::ResultExt;
#[cfg(all(feature = "v2", feature = "routing_v2"))]
Expand All @@ -19,11 +16,8 @@ use rustc_hash::FxHashSet;
use super::payments;
#[cfg(feature = "payouts")]
use super::payouts;
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "routing_v2")))]
use crate::consts;
#[cfg(all(feature = "v2", feature = "routing_v2"))]
use crate::{consts, core::errors::RouterResult, db::StorageInterface};
use crate::{
consts,
core::{
errors::{self, RouterResponse, StorageErrorExt},
metrics, utils as core_utils,
Expand All @@ -36,6 +30,8 @@ use crate::{
},
utils::{self, OptionExt, ValueExt},
};
#[cfg(all(feature = "v2", feature = "routing_v2"))]
use crate::{core::errors::RouterResult, db::StorageInterface};
pub enum TransactionData<'a, F>
where
F: Clone,
Expand All @@ -51,10 +47,8 @@ struct RoutingAlgorithmUpdate(RoutingAlgorithm);
#[cfg(all(feature = "v2", feature = "routing_v2"))]
impl RoutingAlgorithmUpdate {
pub fn create_new_routing_algorithm(
algorithm: routing_types::RoutingAlgorithm,
request: &RoutingConfigRequest,
merchant_id: &common_utils::id_type::MerchantId,
name: String,
description: String,
profile_id: String,
transaction_type: &enums::TransactionType,
) -> Self {
Expand All @@ -67,10 +61,10 @@ impl RoutingAlgorithmUpdate {
algorithm_id,
profile_id,
merchant_id: merchant_id.clone(),
name,
description: Some(description),
kind: algorithm.get_kind().foreign_into(),
algorithm_data: serde_json::json!(algorithm),
name: request.name.clone(),
description: Some(request.description.clone()),
kind: request.algorithm.get_kind().foreign_into(),
algorithm_data: serde_json::json!(request.algorithm),
created_at: timestamp,
modified_at: timestamp,
algorithm_for: transaction_type.to_owned(),
Expand Down Expand Up @@ -150,36 +144,15 @@ pub async fn create_routing_config(
state: SessionState,
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
request: routing_types::RoutingConfigRequest,
request: RoutingConfigRequest,
transaction_type: &enums::TransactionType,
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
metrics::ROUTING_CREATE_REQUEST_RECEIVED.add(&metrics::CONTEXT, 1, &[]);
let db = &*state.store;
let name = request
.name
.get_required_value("name")
.change_context(errors::ApiErrorResponse::MissingRequiredField { field_name: "name" })
.attach_printable("Name of config not given")?;

let description = request
.description
.get_required_value("description")
.change_context(errors::ApiErrorResponse::MissingRequiredField {
field_name: "description",
})
.attach_printable("Description of config not given")?;

let algorithm = request
.algorithm
.get_required_value("algorithm")
.change_context(errors::ApiErrorResponse::MissingRequiredField {
field_name: "algorithm",
})
.attach_printable("Algorithm of config not given")?;

let business_profile = core_utils::validate_and_get_business_profile(
db,
request.profile_id.as_ref(),
Some(&request.profile_id),
merchant_account.get_id(),
)
.await?
Expand All @@ -205,16 +178,14 @@ pub async fn create_routing_config(
let algorithm_helper = helpers::RoutingAlgorithmHelpers {
name_mca_id_set,
name_set,
routing_algorithm: &algorithm,
routing_algorithm: &request.algorithm,
};

algorithm_helper.validate_connectors_in_routing_config()?;

let algo = RoutingAlgorithmUpdate::create_new_routing_algorithm(
algorithm,
&request,
merchant_account.get_id(),
name,
description,
business_profile.profile_id,
transaction_type,
);
Expand Down Expand Up @@ -327,11 +298,13 @@ pub async fn link_routing_config(
let routing_algorithm =
RoutingAlgorithmUpdate::fetch_routing_algo(merchant_account.get_id(), &algorithm_id, db)
.await?;

utils::when(routing_algorithm.0.profile_id != profile_id, || {
Err(errors::ApiErrorResponse::PreconditionFailed {
message: "Profile Id is invalid for the routing config".to_string(),
})
})?;

let business_profile = core_utils::validate_and_get_business_profile(
db,
Some(&profile_id),
Expand All @@ -348,6 +321,7 @@ pub async fn link_routing_config(
.unwrap_or_default();

routing_algorithm.update_routing_ref_with_algorithm_id(transaction_type, &mut routing_ref)?;

// TODO move to business profile
helpers::update_business_profile_active_algorithm_ref(
db,
Expand Down Expand Up @@ -432,10 +406,41 @@ pub async fn link_routing_config(
))
}

#[cfg(all(feature = "v2", feature = "routing_v2"))]
pub async fn retrieve_routing_config(
state: SessionState,
merchant_account: domain::MerchantAccount,
algorithm_id: routing_types::RoutingAlgorithmId,
) -> RouterResponse<routing_types::MerchantRoutingAlgorithm> {
metrics::ROUTING_RETRIEVE_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();

let routing_algorithm =
RoutingAlgorithmUpdate::fetch_routing_algo(merchant_account.get_id(), &algorithm_id.0, db)
.await?;
// TODO: Move to domain types of Business Profile
core_utils::validate_and_get_business_profile(
db,
Some(&routing_algorithm.0.profile_id),
merchant_account.get_id(),
)
.await?
.get_required_value("BusinessProfile")
.change_context(errors::ApiErrorResponse::ResourceIdNotFound)?;

let response = routing_types::MerchantRoutingAlgorithm::foreign_try_from(routing_algorithm.0)
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("unable to parse routing algorithm")?;

metrics::ROUTING_RETRIEVE_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(response))
}

#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "routing_v2")))]
pub async fn retrieve_routing_config(
state: SessionState,
merchant_account: domain::MerchantAccount,
algorithm_id: RoutingAlgorithmId,
algorithm_id: routing_types::RoutingAlgorithmId,
) -> RouterResponse<routing_types::MerchantRoutingAlgorithm> {
metrics::ROUTING_RETRIEVE_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();
Expand Down Expand Up @@ -465,6 +470,70 @@ pub async fn retrieve_routing_config(
Ok(service_api::ApplicationResponse::Json(response))
}

#[cfg(all(feature = "v2", feature = "routing_v2"))]
pub async fn unlink_routing_config(
state: SessionState,
merchant_account: domain::MerchantAccount,
profile_id: String,
transaction_type: &enums::TransactionType,
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
metrics::ROUTING_UNLINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();

let business_profile = core_utils::validate_and_get_business_profile(
db,
Some(&profile_id),
merchant_account.get_id(),
)
.await?
.get_required_value("BusinessProfile")?;

let routing_algo_ref = routing_types::RoutingAlgorithmRef::parse_routing_algorithm(
match transaction_type {
enums::TransactionType::Payment => business_profile.routing_algorithm.clone(),
#[cfg(feature = "payouts")]
enums::TransactionType::Payout => business_profile.payout_routing_algorithm.clone(),
}
.map(Secret::new),
)
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("unable to deserialize routing algorithm ref from merchant account")?
.unwrap_or_default();

if let Some(algorithm_id) = routing_algo_ref.algorithm_id {
let timestamp = common_utils::date_time::now_unix_timestamp();
let routing_algorithm: routing_types::RoutingAlgorithmRef =
routing_types::RoutingAlgorithmRef {
algorithm_id: None,
timestamp,
config_algo_id: routing_algo_ref.config_algo_id.clone(),
surcharge_config_algo_id: routing_algo_ref.surcharge_config_algo_id,
};
let record = RoutingAlgorithmUpdate::fetch_routing_algo(
merchant_account.get_id(),
&algorithm_id,
db,
)
.await?;
let response = record.0.foreign_into();
helpers::update_business_profile_active_algorithm_ref(
db,
business_profile,
routing_algorithm,
transaction_type,
)
.await?;

metrics::ROUTING_UNLINK_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(response))
} else {
Err(errors::ApiErrorResponse::PreconditionFailed {
message: "Algorithm is already inactive".to_string(),
})?
}
}

#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "routing_v2")))]
pub async fn unlink_routing_config(
state: SessionState,
merchant_account: domain::MerchantAccount,
Expand Down
22 changes: 10 additions & 12 deletions crates/router/src/routes/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1455,18 +1455,16 @@ impl BusinessProfile {
},
)),
)
.service(
web::resource("/deactivate_routing_algorithm").route(web::post().to(
|state, req, path| {
routing::routing_unlink_config(
state,
req,
path,
&TransactionType::Payment,
)
},
)),
),
.service(web::resource("/deactivate_routing_algorithm").route(
web::patch().to(|state, req, path| {
routing::routing_unlink_config(
state,
req,
path,
&TransactionType::Payment,
)
}),
)),
)
}
}
Expand Down
36 changes: 35 additions & 1 deletion crates/router/src/routes/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,41 @@ pub async fn list_routing_configs(
.await
}

#[cfg(feature = "olap")]
#[cfg(all(feature = "olap", feature = "v2", feature = "routing_v2"))]
#[instrument(skip_all)]
pub async fn routing_unlink_config(
state: web::Data<AppState>,
req: HttpRequest,
path: web::Path<String>,
transaction_type: &enums::TransactionType,
) -> impl Responder {
let flow = Flow::RoutingUnlinkConfig;
Box::pin(oss_api::server_wrap(
flow,
state,
&req,
path.into_inner(),
|state, auth: auth::AuthenticationData, path, _| {
routing::unlink_routing_config(state, auth.merchant_account, path, transaction_type)
},
#[cfg(not(feature = "release"))]
auth::auth_type(
&auth::ApiKeyAuth,
&auth::JWTAuth(Permission::RoutingWrite),
req.headers(),
),
#[cfg(feature = "release")]
&auth::JWTAuth(Permission::RoutingWrite),
api_locking::LockAction::NotApplicable,
))
.await
}

#[cfg(all(
feature = "olap",
any(feature = "v1", feature = "v2"),
not(feature = "routing_v2")
))]
#[instrument(skip_all)]
pub async fn routing_unlink_config(
state: web::Data<AppState>,
Expand Down

0 comments on commit 3fea00c

Please sign in to comment.