feat(routing): add domain type for Routing id (#5733)

Co-authored-by: Sanchith Hegde <sanchith.hegde@juspay.in>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Amisha Prabhat
2024-08-29 19:34:29 +05:30
committed by GitHub
parent b66f91034b
commit e939db2fad
14 changed files with 93 additions and 67 deletions

View File

@ -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,
@ -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();
}
@ -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,
@ -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)]

View File

@ -8,6 +8,7 @@ mod merchant;
mod merchant_connector_account;
mod organization;
mod profile;
mod routing;
mod global_id;
@ -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;

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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>,
@ -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>,
@ -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>,

View File

@ -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, _, _>(
@ -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, _, _>(
@ -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()
@ -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,
@ -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>,
@ -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,
@ -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,

View File

@ -1,3 +1,4 @@
use common_utils::id_type;
use diesel::{Identifiable, Insertable, Queryable, Selectable};
use serde::{Deserialize, Serialize};
@ -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,
@ -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,
@ -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,

View File

@ -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,
@ -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>,

View File

@ -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();
@ -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,
{

View File

@ -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")]
@ -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>> {
@ -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>> {
@ -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 = {

View File

@ -14,8 +14,12 @@ use super::payments;
use super::payouts;
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "routing_v2")))]
use crate::utils::ValueExt;
#[cfg(all(feature = "v2", feature = "routing_v2"))]
use crate::{
core::{admin, errors::RouterResult},
db::StorageInterface,
};
use crate::{
consts,
core::{
errors::{self, RouterResponse, StorageErrorExt},
metrics, utils as core_utils,
@ -28,11 +32,6 @@ use crate::{
},
utils::{self, OptionExt},
};
#[cfg(all(feature = "v2", feature = "routing_v2"))]
use crate::{
core::{admin, errors::RouterResult},
db::StorageInterface,
};
pub enum TransactionData<'a, F>
where
F: Clone,
@ -53,10 +52,7 @@ impl RoutingAlgorithmUpdate {
profile_id: common_utils::id_type::ProfileId,
transaction_type: &enums::TransactionType,
) -> Self {
let algorithm_id = common_utils::generate_id(
consts::ROUTING_CONFIG_ID_LENGTH,
&format!("routing_{}", merchant_id.get_string_repr()),
);
let algorithm_id = common_utils::generate_routing_id_of_default_length();
let timestamp = common_utils::date_time::now();
let algo = RoutingAlgorithm {
algorithm_id,
@ -74,7 +70,7 @@ impl RoutingAlgorithmUpdate {
}
pub async fn fetch_routing_algo(
merchant_id: &common_utils::id_type::MerchantId,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
db: &dyn StorageInterface,
) -> RouterResult<Self> {
let routing_algo = db
@ -215,10 +211,7 @@ pub async fn create_routing_algorithm_under_profile(
})
.attach_printable("Algorithm of config not given")?;
let algorithm_id = common_utils::generate_id(
consts::ROUTING_CONFIG_ID_LENGTH,
&format!("routing_{}", merchant_account.get_id().get_string_repr()),
);
let algorithm_id = common_utils::generate_routing_id_of_default_length();
let profile_id = request
.profile_id
@ -280,7 +273,7 @@ pub async fn link_routing_config_under_profile(
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
profile_id: common_utils::id_type::ProfileId,
algorithm_id: String,
algorithm_id: common_utils::id_type::RoutingId,
transaction_type: &enums::TransactionType,
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
metrics::ROUTING_LINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
@ -352,7 +345,7 @@ pub async fn link_routing_config(
state: SessionState,
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
algorithm_id: String,
algorithm_id: common_utils::id_type::RoutingId,
transaction_type: &enums::TransactionType,
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
metrics::ROUTING_LINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
@ -428,7 +421,7 @@ pub async fn retrieve_routing_algorithm_from_algorithm_id(
state: SessionState,
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
algorithm_id: String,
algorithm_id: common_utils::id_type::RoutingId,
) -> RouterResponse<routing_types::MerchantRoutingAlgorithm> {
metrics::ROUTING_RETRIEVE_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();
@ -461,7 +454,7 @@ pub async fn retrieve_routing_algorithm_from_algorithm_id(
state: SessionState,
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
algorithm_id: String,
algorithm_id: common_utils::id_type::RoutingId,
) -> RouterResponse<routing_types::MerchantRoutingAlgorithm> {
metrics::ROUTING_RETRIEVE_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();

View File

@ -2495,7 +2495,7 @@ impl RoutingAlgorithmInterface for KafkaStore {
async fn find_routing_algorithm_by_profile_id_algorithm_id(
&self,
profile_id: &id_type::ProfileId,
algorithm_id: &str,
algorithm_id: &id_type::RoutingId,
) -> CustomResult<storage::RoutingAlgorithm, errors::StorageError> {
self.diesel_store
.find_routing_algorithm_by_profile_id_algorithm_id(profile_id, algorithm_id)
@ -2504,7 +2504,7 @@ impl RoutingAlgorithmInterface for KafkaStore {
async fn find_routing_algorithm_by_algorithm_id_merchant_id(
&self,
algorithm_id: &str,
algorithm_id: &id_type::RoutingId,
merchant_id: &id_type::MerchantId,
) -> CustomResult<storage::RoutingAlgorithm, errors::StorageError> {
self.diesel_store
@ -2514,7 +2514,7 @@ impl RoutingAlgorithmInterface for KafkaStore {
async fn find_routing_algorithm_metadata_by_algorithm_id_profile_id(
&self,
algorithm_id: &str,
algorithm_id: &id_type::RoutingId,
profile_id: &id_type::ProfileId,
) -> CustomResult<storage::RoutingProfileMetadata, errors::StorageError> {
self.diesel_store

View File

@ -21,18 +21,18 @@ pub trait RoutingAlgorithmInterface {
async fn find_routing_algorithm_by_profile_id_algorithm_id(
&self,
profile_id: &common_utils::id_type::ProfileId,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
) -> StorageResult<routing_storage::RoutingAlgorithm>;
async fn find_routing_algorithm_by_algorithm_id_merchant_id(
&self,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
merchant_id: &common_utils::id_type::MerchantId,
) -> StorageResult<routing_storage::RoutingAlgorithm>;
async fn find_routing_algorithm_metadata_by_algorithm_id_profile_id(
&self,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
profile_id: &common_utils::id_type::ProfileId,
) -> StorageResult<routing_storage::RoutingProfileMetadata>;
@ -77,7 +77,7 @@ impl RoutingAlgorithmInterface for Store {
async fn find_routing_algorithm_by_profile_id_algorithm_id(
&self,
profile_id: &common_utils::id_type::ProfileId,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
) -> StorageResult<routing_storage::RoutingAlgorithm> {
let conn = connection::pg_connection_write(self).await?;
routing_storage::RoutingAlgorithm::find_by_algorithm_id_profile_id(
@ -92,7 +92,7 @@ impl RoutingAlgorithmInterface for Store {
#[instrument(skip_all)]
async fn find_routing_algorithm_by_algorithm_id_merchant_id(
&self,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
merchant_id: &common_utils::id_type::MerchantId,
) -> StorageResult<routing_storage::RoutingAlgorithm> {
let conn = connection::pg_connection_write(self).await?;
@ -108,7 +108,7 @@ impl RoutingAlgorithmInterface for Store {
#[instrument(skip_all)]
async fn find_routing_algorithm_metadata_by_algorithm_id_profile_id(
&self,
algorithm_id: &str,
algorithm_id: &common_utils::id_type::RoutingId,
profile_id: &common_utils::id_type::ProfileId,
) -> StorageResult<routing_storage::RoutingProfileMetadata> {
let conn = connection::pg_connection_write(self).await?;
@ -186,14 +186,14 @@ impl RoutingAlgorithmInterface for MockDb {
async fn find_routing_algorithm_by_profile_id_algorithm_id(
&self,
_profile_id: &common_utils::id_type::ProfileId,
_algorithm_id: &str,
_algorithm_id: &common_utils::id_type::RoutingId,
) -> StorageResult<routing_storage::RoutingAlgorithm> {
Err(errors::StorageError::MockDbError)?
}
async fn find_routing_algorithm_by_algorithm_id_merchant_id(
&self,
_algorithm_id: &str,
_algorithm_id: &common_utils::id_type::RoutingId,
_merchant_id: &common_utils::id_type::MerchantId,
) -> StorageResult<routing_storage::RoutingAlgorithm> {
Err(errors::StorageError::MockDbError)?
@ -201,7 +201,7 @@ impl RoutingAlgorithmInterface for MockDb {
async fn find_routing_algorithm_metadata_by_algorithm_id_profile_id(
&self,
_algorithm_id: &str,
_algorithm_id: &common_utils::id_type::RoutingId,
_profile_id: &common_utils::id_type::ProfileId,
) -> StorageResult<routing_storage::RoutingProfileMetadata> {
Err(errors::StorageError::MockDbError)?

View File

@ -60,7 +60,7 @@ pub async fn routing_create_config(
pub async fn routing_link_config(
state: web::Data<AppState>,
req: HttpRequest,
path: web::Path<String>,
path: web::Path<common_utils::id_type::RoutingId>,
transaction_type: &enums::TransactionType,
) -> impl Responder {
let flow = Flow::RoutingLinkConfig;
@ -139,7 +139,7 @@ pub async fn routing_link_config(
pub async fn routing_retrieve_config(
state: web::Data<AppState>,
req: HttpRequest,
path: web::Path<String>,
path: web::Path<common_utils::id_type::RoutingId>,
) -> impl Responder {
let algorithm_id = path.into_inner();
let flow = Flow::RoutingRetrieveConfig;