Skip to content

Commit

Permalink
feat(routing): add domain type for Routing id (#5733)
Browse files Browse the repository at this point in the history
Co-authored-by: Sanchith Hegde <[email protected]>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 29, 2024
1 parent b66f910 commit e939db2
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 67 deletions.
13 changes: 8 additions & 5 deletions crates/api_models/src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ pub enum LinkedRoutingConfigRetrieveResponse {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
/// Routing Algorithm specific to merchants
pub struct MerchantRoutingAlgorithm {
pub id: String,
#[schema(value_type = String)]
pub id: common_utils::id_type::RoutingId,
#[schema(value_type = String)]
pub profile_id: common_utils::id_type::ProfileId,
pub name: String,
Expand Down Expand Up @@ -422,14 +423,14 @@ impl RoutingAlgorithm {

#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct RoutingAlgorithmRef {
pub algorithm_id: Option<String>,
pub algorithm_id: Option<common_utils::id_type::RoutingId>,
pub timestamp: i64,
pub config_algo_id: Option<String>,
pub surcharge_config_algo_id: Option<String>,
}

impl RoutingAlgorithmRef {
pub fn update_algorithm_id(&mut self, new_id: String) {
pub fn update_algorithm_id(&mut self, new_id: common_utils::id_type::RoutingId) {
self.algorithm_id = Some(new_id);
self.timestamp = common_utils::date_time::now_unix_timestamp();
}
Expand All @@ -456,7 +457,8 @@ impl RoutingAlgorithmRef {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]

pub struct RoutingDictionaryRecord {
pub id: String,
#[schema(value_type = String)]
pub id: common_utils::id_type::RoutingId,
#[schema(value_type = String)]
pub profile_id: common_utils::id_type::ProfileId,
pub name: String,
Expand Down Expand Up @@ -484,7 +486,8 @@ pub enum RoutingKind {

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, ToSchema)]
pub struct RoutingAlgorithmId {
pub routing_algorithm_id: String,
#[schema(value_type = String)]
pub routing_algorithm_id: common_utils::id_type::RoutingId,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
Expand Down
2 changes: 2 additions & 0 deletions crates/common_utils/src/id_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod merchant;
mod merchant_connector_account;
mod organization;
mod profile;
mod routing;

mod global_id;

Expand All @@ -23,6 +24,7 @@ pub use merchant::MerchantId;
pub use merchant_connector_account::MerchantConnectorAccountId;
pub use organization::OrganizationId;
pub use profile::ProfileId;
pub use routing::RoutingId;
use serde::{Deserialize, Serialize};
use thiserror::Error;

Expand Down
21 changes: 21 additions & 0 deletions crates/common_utils/src/id_type/routing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
crate::id_type!(
RoutingId,
" A type for routing_id that can be used for routing ids"
);

crate::impl_id_type_methods!(RoutingId, "routing_id");

// This is to display the `RoutingId` as RoutingId(abcd)
crate::impl_debug_id_type!(RoutingId);
crate::impl_try_from_cow_str_id_type!(RoutingId, "routing_id");

crate::impl_generate_id_id_type!(RoutingId, "routing");
crate::impl_serializable_secret_id_type!(RoutingId);
crate::impl_queryable_id_type!(RoutingId);
crate::impl_to_sql_from_sql_id_type!(RoutingId);

impl crate::events::ApiEventMetric for RoutingId {
fn get_api_event_type(&self) -> Option<crate::events::ApiEventsType> {
Some(crate::events::ApiEventsType::Routing)
}
}
6 changes: 6 additions & 0 deletions crates/common_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ pub fn generate_profile_id_of_default_length() -> id_type::ProfileId {
id_type::ProfileId::generate()
}

/// Generate a routing id with default length, with prefix as `routing`
pub fn generate_routing_id_of_default_length() -> id_type::RoutingId {
use id_type::GenerateId;

id_type::RoutingId::generate()
}
/// Generate a merchant_connector_account id with default length, with prefix as `mca`
pub fn generate_merchant_connector_account_id_of_default_length(
) -> id_type::MerchantConnectorAccountId {
Expand Down
12 changes: 6 additions & 6 deletions crates/diesel_models/src/business_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ pub struct BusinessProfile {
pub outgoing_webhook_custom_http_headers: Option<Encryption>,
pub always_collect_billing_details_from_wallet_connector: Option<bool>,
pub always_collect_shipping_details_from_wallet_connector: Option<bool>,
pub routing_algorithm_id: Option<String>,
pub routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
pub order_fulfillment_time: Option<i64>,
pub order_fulfillment_time_origin: Option<common_enums::OrderFulfillmentTimeOrigin>,
pub frm_routing_algorithm_id: Option<String>,
pub payout_routing_algorithm_id: Option<String>,
pub payout_routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
pub default_fallback_routing: Option<pii::SecretSerdeValue>,
pub tax_connector_id: Option<String>,
pub is_tax_connector_enabled: Option<bool>,
Expand Down Expand Up @@ -310,11 +310,11 @@ pub struct BusinessProfileNew {
pub outgoing_webhook_custom_http_headers: Option<Encryption>,
pub always_collect_billing_details_from_wallet_connector: Option<bool>,
pub always_collect_shipping_details_from_wallet_connector: Option<bool>,
pub routing_algorithm_id: Option<String>,
pub routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
pub order_fulfillment_time: Option<i64>,
pub order_fulfillment_time_origin: Option<common_enums::OrderFulfillmentTimeOrigin>,
pub frm_routing_algorithm_id: Option<String>,
pub payout_routing_algorithm_id: Option<String>,
pub payout_routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
pub default_fallback_routing: Option<pii::SecretSerdeValue>,
pub tax_connector_id: Option<String>,
pub is_tax_connector_enabled: Option<bool>,
Expand Down Expand Up @@ -348,11 +348,11 @@ pub struct BusinessProfileUpdateInternal {
pub outgoing_webhook_custom_http_headers: Option<Encryption>,
pub always_collect_billing_details_from_wallet_connector: Option<bool>,
pub always_collect_shipping_details_from_wallet_connector: Option<bool>,
pub routing_algorithm_id: Option<String>,
pub routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
pub order_fulfillment_time: Option<i64>,
pub order_fulfillment_time_origin: Option<common_enums::OrderFulfillmentTimeOrigin>,
pub frm_routing_algorithm_id: Option<String>,
pub payout_routing_algorithm_id: Option<String>,
pub payout_routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
pub default_fallback_routing: Option<pii::SecretSerdeValue>,
pub tax_connector_id: Option<String>,
pub is_tax_connector_enabled: Option<bool>,
Expand Down
14 changes: 7 additions & 7 deletions crates/diesel_models/src/query/routing_algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl RoutingAlgorithm {

pub async fn find_by_algorithm_id_merchant_id(
conn: &PgPooledConn,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
merchant_id: &common_utils::id_type::MerchantId,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
Expand All @@ -33,7 +33,7 @@ impl RoutingAlgorithm {

pub async fn find_by_algorithm_id_profile_id(
conn: &PgPooledConn,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
profile_id: &common_utils::id_type::ProfileId,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
Expand All @@ -47,7 +47,7 @@ impl RoutingAlgorithm {

pub async fn find_metadata_by_algorithm_id_profile_id(
conn: &PgPooledConn,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
profile_id: &common_utils::id_type::ProfileId,
) -> StorageResult<RoutingProfileMetadata> {
Self::table()
Expand All @@ -69,7 +69,7 @@ impl RoutingAlgorithm {
.limit(1)
.load_async::<(
common_utils::id_type::ProfileId,
String,
common_utils::id_type::RoutingId,
String,
Option<String>,
enums::RoutingAlgorithmKind,
Expand Down Expand Up @@ -128,7 +128,7 @@ impl RoutingAlgorithm {
.limit(limit)
.offset(offset)
.load_async::<(
String,
common_utils::id_type::RoutingId,
common_utils::id_type::ProfileId,
String,
Option<String>,
Expand Down Expand Up @@ -189,7 +189,7 @@ impl RoutingAlgorithm {
.order(dsl::modified_at.desc())
.load_async::<(
common_utils::id_type::ProfileId,
String,
common_utils::id_type::RoutingId,
String,
Option<String>,
enums::RoutingAlgorithmKind,
Expand Down Expand Up @@ -251,7 +251,7 @@ impl RoutingAlgorithm {
.order(dsl::modified_at.desc())
.load_async::<(
common_utils::id_type::ProfileId,
String,
common_utils::id_type::RoutingId,
String,
Option<String>,
enums::RoutingAlgorithmKind,
Expand Down
13 changes: 7 additions & 6 deletions crates/diesel_models/src/routing_algorithm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common_utils::id_type;
use diesel::{Identifiable, Insertable, Queryable, Selectable};
use serde::{Deserialize, Serialize};

Expand All @@ -6,9 +7,9 @@ use crate::{enums, schema::routing_algorithm};
#[derive(Clone, Debug, Identifiable, Insertable, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = routing_algorithm, primary_key(algorithm_id), check_for_backend(diesel::pg::Pg))]
pub struct RoutingAlgorithm {
pub algorithm_id: String,
pub profile_id: common_utils::id_type::ProfileId,
pub merchant_id: common_utils::id_type::MerchantId,
pub algorithm_id: id_type::RoutingId,
pub profile_id: id_type::ProfileId,
pub merchant_id: id_type::MerchantId,
pub name: String,
pub description: Option<String>,
pub kind: enums::RoutingAlgorithmKind,
Expand All @@ -19,7 +20,7 @@ pub struct RoutingAlgorithm {
}

pub struct RoutingAlgorithmMetadata {
pub algorithm_id: String,
pub algorithm_id: id_type::RoutingId,
pub name: String,
pub description: Option<String>,
pub kind: enums::RoutingAlgorithmKind,
Expand All @@ -29,8 +30,8 @@ pub struct RoutingAlgorithmMetadata {
}

pub struct RoutingProfileMetadata {
pub profile_id: common_utils::id_type::ProfileId,
pub algorithm_id: String,
pub profile_id: id_type::ProfileId,
pub algorithm_id: id_type::RoutingId,
pub name: String,
pub description: Option<String>,
pub kind: enums::RoutingAlgorithmKind,
Expand Down
8 changes: 4 additions & 4 deletions crates/hyperswitch_domain_models/src/business_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,11 @@ pub struct BusinessProfile {
pub outgoing_webhook_custom_http_headers: OptionalEncryptableValue,
pub always_collect_billing_details_from_wallet_connector: Option<bool>,
pub always_collect_shipping_details_from_wallet_connector: Option<bool>,
pub routing_algorithm_id: Option<String>,
pub routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
pub order_fulfillment_time: Option<i64>,
pub order_fulfillment_time_origin: Option<common_enums::OrderFulfillmentTimeOrigin>,
pub frm_routing_algorithm_id: Option<String>,
pub payout_routing_algorithm_id: Option<String>,
pub payout_routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
pub default_fallback_routing: Option<pii::SecretSerdeValue>,
pub tax_connector_id: Option<String>,
pub is_tax_connector_enabled: bool,
Expand Down Expand Up @@ -536,8 +536,8 @@ pub struct BusinessProfileGeneralUpdate {
pub enum BusinessProfileUpdate {
Update(Box<BusinessProfileGeneralUpdate>),
RoutingAlgorithmUpdate {
routing_algorithm_id: Option<String>,
payout_routing_algorithm_id: Option<String>,
routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
payout_routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
},
DefaultRoutingFallbackUpdate {
default_fallback_routing: Option<pii::SecretSerdeValue>,
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4073,7 +4073,7 @@ impl BusinessProfileWrapper {
db: &dyn StorageInterface,
key_manager_state: &KeyManagerState,
merchant_key_store: &domain::MerchantKeyStore,
algorithm_id: String,
algorithm_id: common_utils::id_type::RoutingId,
transaction_type: &storage::enums::TransactionType,
) -> RouterResult<()> {
let routing_cache_key = self.clone().get_routing_config_cache_key();
Expand Down Expand Up @@ -4114,7 +4114,7 @@ impl BusinessProfileWrapper {
pub fn get_routing_algorithm_id<'a, F>(
&'a self,
transaction_data: &'a routing::TransactionData<'_, F>,
) -> Option<String>
) -> Option<id_type::RoutingId>
where
F: Send + Clone,
{
Expand Down
8 changes: 4 additions & 4 deletions crates/router/src/core/payments/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Default for MerchantAccountRoutingAlgorithm {
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
enum MerchantAccountRoutingAlgorithm {
V1(Option<String>),
V1(Option<common_utils::id_type::RoutingId>),
}

#[cfg(feature = "payouts")]
Expand Down Expand Up @@ -289,7 +289,7 @@ where
pub async fn perform_static_routing_v1<F: Clone>(
state: &SessionState,
merchant_id: &common_utils::id_type::MerchantId,
algorithm_id: Option<String>,
algorithm_id: Option<common_utils::id_type::RoutingId>,
business_profile: &domain::BusinessProfile,
transaction_data: &routing::TransactionData<'_, F>,
) -> RoutingResult<Vec<routing_types::RoutableConnectorChoice>> {
Expand Down Expand Up @@ -352,7 +352,7 @@ pub async fn perform_static_routing_v1<F: Clone>(
async fn ensure_algorithm_cached_v1(
state: &SessionState,
merchant_id: &common_utils::id_type::MerchantId,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
profile_id: common_utils::id_type::ProfileId,
transaction_type: &api_enums::TransactionType,
) -> RoutingResult<Arc<CachedAlgorithm>> {
Expand Down Expand Up @@ -437,7 +437,7 @@ fn execute_dsl_and_get_connector_v1(
pub async fn refresh_routing_cache_v1(
state: &SessionState,
key: String,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
profile_id: common_utils::id_type::ProfileId,
) -> RoutingResult<Arc<CachedAlgorithm>> {
let algorithm = {
Expand Down
Loading

0 comments on commit e939db2

Please sign in to comment.