mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 00:49:42 +08:00
refactor: use business profile domain models instead of diesel models (#5566)
This commit is contained in:
@ -149,9 +149,12 @@ pub async fn create_routing_config(
|
||||
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
|
||||
metrics::ROUTING_CREATE_REQUEST_RECEIVED.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = &*state.store;
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let business_profile = core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&request.profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
@ -167,7 +170,7 @@ pub async fn create_routing_config(
|
||||
|
||||
let name_mca_id_set = helpers::ConnectNameAndMCAIdForProfile(
|
||||
all_mcas.filter_by_profile(&business_profile.profile_id, |mca| {
|
||||
(&mca.connector_name, &mca.merchant_connector_id)
|
||||
(&mca.connector_name, &mca.id)
|
||||
}),
|
||||
);
|
||||
|
||||
@ -213,6 +216,8 @@ pub async fn create_routing_config(
|
||||
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
|
||||
metrics::ROUTING_CREATE_REQUEST_RECEIVED.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let name = request
|
||||
.name
|
||||
.get_required_value("name")
|
||||
@ -248,8 +253,14 @@ pub async fn create_routing_config(
|
||||
})
|
||||
.attach_printable("Profile_id not provided")?;
|
||||
|
||||
core_utils::validate_and_get_business_profile(db, Some(&profile_id), merchant_account.get_id())
|
||||
.await?;
|
||||
core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
helpers::validate_connectors_in_routing_config(
|
||||
&state,
|
||||
@ -288,12 +299,14 @@ pub async fn create_routing_config(
|
||||
pub async fn link_routing_config(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
profile_id: String,
|
||||
algorithm_id: String,
|
||||
transaction_type: &enums::TransactionType,
|
||||
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
|
||||
metrics::ROUTING_LINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let routing_algorithm =
|
||||
RoutingAlgorithmUpdate::fetch_routing_algo(merchant_account.get_id(), &algorithm_id, db)
|
||||
@ -307,6 +320,8 @@ pub async fn link_routing_config(
|
||||
|
||||
let business_profile = core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
@ -322,9 +337,10 @@ pub async fn link_routing_config(
|
||||
|
||||
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,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
business_profile,
|
||||
routing_ref,
|
||||
transaction_type,
|
||||
@ -340,11 +356,13 @@ pub async fn link_routing_config(
|
||||
pub async fn link_routing_config(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
algorithm_id: String,
|
||||
transaction_type: &enums::TransactionType,
|
||||
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
|
||||
metrics::ROUTING_LINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let routing_algorithm = db
|
||||
.find_routing_algorithm_by_algorithm_id_merchant_id(
|
||||
@ -356,6 +374,8 @@ pub async fn link_routing_config(
|
||||
|
||||
let business_profile = core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&routing_algorithm.profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
@ -394,6 +414,8 @@ pub async fn link_routing_config(
|
||||
routing_ref.update_algorithm_id(algorithm_id);
|
||||
helpers::update_business_profile_active_algorithm_ref(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
business_profile,
|
||||
routing_ref,
|
||||
transaction_type,
|
||||
@ -410,17 +432,20 @@ pub async fn link_routing_config(
|
||||
pub async fn retrieve_routing_config(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
algorithm_id: routing_types::RoutingAlgorithmId,
|
||||
) -> RouterResponse<routing_types::MerchantRoutingAlgorithm> {
|
||||
metrics::ROUTING_RETRIEVE_CONFIG.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
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,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&routing_algorithm.0.profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
@ -440,10 +465,12 @@ pub async fn retrieve_routing_config(
|
||||
pub async fn retrieve_routing_config(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
algorithm_id: routing_types::RoutingAlgorithmId,
|
||||
) -> RouterResponse<routing_types::MerchantRoutingAlgorithm> {
|
||||
metrics::ROUTING_RETRIEVE_CONFIG.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let routing_algorithm = db
|
||||
.find_routing_algorithm_by_algorithm_id_merchant_id(
|
||||
@ -455,6 +482,8 @@ pub async fn retrieve_routing_config(
|
||||
|
||||
core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&routing_algorithm.profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
@ -474,14 +503,18 @@ pub async fn retrieve_routing_config(
|
||||
pub async fn unlink_routing_config(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
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 key_manager_state = &(&state).into();
|
||||
|
||||
let business_profile = core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
@ -518,6 +551,8 @@ pub async fn unlink_routing_config(
|
||||
let response = record.0.foreign_into();
|
||||
helpers::update_business_profile_active_algorithm_ref(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
business_profile,
|
||||
routing_algorithm,
|
||||
transaction_type,
|
||||
@ -537,11 +572,14 @@ pub async fn unlink_routing_config(
|
||||
pub async fn unlink_routing_config(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
request: routing_types::RoutingConfigRequest,
|
||||
transaction_type: &enums::TransactionType,
|
||||
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
|
||||
metrics::ROUTING_UNLINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let profile_id = request
|
||||
.profile_id
|
||||
.get_required_value("profile_id")
|
||||
@ -551,6 +589,8 @@ pub async fn unlink_routing_config(
|
||||
.attach_printable("Profile_id not provided")?;
|
||||
let business_profile = core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
@ -590,6 +630,8 @@ pub async fn unlink_routing_config(
|
||||
let response = record.foreign_into();
|
||||
helpers::update_business_profile_active_algorithm_ref(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
business_profile,
|
||||
routing_algorithm,
|
||||
transaction_type,
|
||||
@ -687,15 +729,19 @@ pub async fn retrieve_default_routing_config(
|
||||
pub async fn retrieve_linked_routing_config(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
query_params: RoutingRetrieveLinkQuery,
|
||||
transaction_type: &enums::TransactionType,
|
||||
) -> RouterResponse<routing_types::LinkedRoutingConfigRetrieveResponse> {
|
||||
metrics::ROUTING_RETRIEVE_LINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let business_profiles = if let Some(profile_id) = query_params.profile_id {
|
||||
core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
@ -704,9 +750,13 @@ pub async fn retrieve_linked_routing_config(
|
||||
.get_required_value("BusinessProfile")
|
||||
.change_context(errors::ApiErrorResponse::BusinessProfileNotFound { id: profile_id })?
|
||||
} else {
|
||||
db.list_business_profile_by_merchant_id(merchant_account.get_id())
|
||||
.await
|
||||
.to_not_found_response(errors::ApiErrorResponse::ResourceIdNotFound)?
|
||||
db.list_business_profile_by_merchant_id(
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
.await
|
||||
.to_not_found_response(errors::ApiErrorResponse::ResourceIdNotFound)?
|
||||
};
|
||||
|
||||
let mut active_algorithms = Vec::new();
|
||||
@ -746,13 +796,19 @@ pub async fn retrieve_linked_routing_config(
|
||||
pub async fn retrieve_default_routing_config_for_profiles(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
transaction_type: &enums::TransactionType,
|
||||
) -> RouterResponse<Vec<routing_types::ProfileDefaultRoutingConfig>> {
|
||||
metrics::ROUTING_RETRIEVE_CONFIG_FOR_PROFILE.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let all_profiles = db
|
||||
.list_business_profile_by_merchant_id(merchant_account.get_id())
|
||||
.list_business_profile_by_merchant_id(
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
.await
|
||||
.to_not_found_response(errors::ApiErrorResponse::ResourceIdNotFound)
|
||||
.attach_printable("error retrieving all business profiles for merchant")?;
|
||||
@ -785,15 +841,19 @@ pub async fn retrieve_default_routing_config_for_profiles(
|
||||
pub async fn update_default_routing_config_for_profile(
|
||||
state: SessionState,
|
||||
merchant_account: domain::MerchantAccount,
|
||||
key_store: domain::MerchantKeyStore,
|
||||
updated_config: Vec<routing_types::RoutableConnectorChoice>,
|
||||
profile_id: String,
|
||||
transaction_type: &enums::TransactionType,
|
||||
) -> RouterResponse<routing_types::ProfileDefaultRoutingConfig> {
|
||||
metrics::ROUTING_UPDATE_CONFIG_FOR_PROFILE.add(&metrics::CONTEXT, 1, &[]);
|
||||
let db = state.store.as_ref();
|
||||
let key_manager_state = &(&state).into();
|
||||
|
||||
let business_profile = core_utils::validate_and_get_business_profile(
|
||||
db,
|
||||
key_manager_state,
|
||||
&key_store,
|
||||
Some(&profile_id),
|
||||
merchant_account.get_id(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user