Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(routing): resolve migration profiles under the merchant that owns them (#13808)

Jagan9be92d249022
diff --git a/crates/diesel_models/src/query/routing_algorithm.rs b/crates/diesel_models/src/query/routing_algorithm.rs
index 69ce7edb2e..fae45c9915 100644
--- a/crates/diesel_models/src/query/routing_algorithm.rs
+++ b/crates/diesel_models/src/query/routing_algorithm.rs
@@ -319,9 +319,16 @@ impl RoutingAlgorithm {
.collect())
}
- /// Every rule id held by the given profiles, with each profile's merchant — taken from here
- /// rather than `business_profile`, which is encrypted and needs the merchant to read. The
- /// kind rides along so callers can tell which rules a migration is expected to carry.
+ /// Every rule id held by the given profiles, with the merchant that owns each profile —
+ /// taken from here rather than `business_profile`, which is encrypted and needs the merchant
+ /// to read. The kind rides along so callers can tell which rules a migration is expected to
+ /// carry.
+ ///
+ /// The owner is `processor_merchant_id`, falling back to `merchant_id`. A rule written
+ /// through a platform records the provider that made the call in `merchant_id` and the
+ /// merchant the profile hangs off in `processor_merchant_id`; callers resolve the business
+ /// profile under what this returns, and under the provider there is no such profile. The
+ /// column is null on rules written before platform support, where the two are the same.
pub async fn rule_ids_for_profiles(
conn: &PgPooledConn,
profile_ids: &[common_utils::id_type::ProfileId],
@@ -336,6 +343,7 @@ impl RoutingAlgorithm {
Self::table()
.select((
dsl::profile_id,
+ dsl::processor_merchant_id,
dsl::merchant_id,
dsl::algorithm_id,
dsl::kind,
@@ -344,12 +352,27 @@ impl RoutingAlgorithm {
.order((dsl::profile_id.asc(), dsl::algorithm_id.asc()))
.load_async::<(
common_utils::id_type::ProfileId,
+ Option<common_utils::id_type::MerchantId>,
common_utils::id_type::MerchantId,
common_utils::id_type::RoutingId,
enums::RoutingAlgorithmKind,
)>(conn)
.await
.change_context(DatabaseError::Others)
+ .map(|rows| {
+ rows.into_iter()
+ .map(
+ |(profile_id, processor_merchant_id, merchant_id, algorithm_id, kind)| {
+ (
+ profile_id,
+ processor_merchant_id.unwrap_or(merchant_id),
+ algorithm_id,
+ kind,
+ )
+ },
+ )
+ .collect()
+ })
}
/// A page of the profiles that hold routing rules. Grouped so the page is of profiles —
diff --git a/crates/router/src/core/routing.rs b/crates/router/src/core/routing.rs
index cf94b275ca..1727af3639 100644
--- a/crates/router/src/core/routing.rs
+++ b/crates/router/src/core/routing.rs
@@ -2732,6 +2732,9 @@ pub async fn migrate_rules_for_profiles(
let limit = request.validated_limit();
let offset = request.offset.unwrap_or_default();
+ // The merchant that owns each profile, which is what the business profile is resolved
+ // under below. For a rule a platform wrote, that is its processor and not the provider the
+ // call was made by — `find_rule_ids_for_profiles` resolves which.
let merchant_of: HashMap<_, _> = state
.store
.find_rule_ids_for_profiles(&request.profile_ids)
@@ -3129,9 +3132,16 @@ pub async fn routing_migration_status(
// Rules the migration is not expected to carry, counted apart so holding one never leaves a
// finished profile reading as partial.
let mut out_of_scope: HashMap<common_utils::id_type::ProfileId, i64> = HashMap::new();
+ // The merchant that owns each profile. The page above is grouped by the rule's
+ // `merchant_id`, which for a platform-written rule is the provider that made the call
+ // rather than the merchant the profile hangs off — reporting that one names an account the
+ // profile is not under, and is the merchant a migration would then fail to find it in.
+ let mut owner_of: HashMap<common_utils::id_type::ProfileId, common_utils::id_type::MerchantId> =
+ HashMap::new();
match state.store.find_rule_ids_for_profiles(&page_profiles).await {
Ok(rows) => {
- for (profile_id, _, algorithm_id, kind) in rows {
+ for (profile_id, owner_merchant_id, algorithm_id, kind) in rows {
+ owner_of.insert(profile_id.clone(), owner_merchant_id);
if routing_types::RoutingAlgorithmKind::foreign_from(kind)
.rule_migration_exclusion()
.is_some()
@@ -3157,7 +3167,14 @@ pub async fn routing_migration_status(
..Default::default()
};
- for (profile_id, merchant_id) in scopes {
+ for (profile_id, provider_merchant_id) in scopes {
+ // Falls back to the grouping merchant only when the rule rows could not be read at all,
+ // which is the same failure that leaves the rule counts empty.
+ let merchant_id = owner_of
+ .get(&profile_id)
+ .cloned()
+ .unwrap_or_else(|| provider_merchant_id.clone());
+
// The listing already carries each rule's id, so comparing the sets is free.
let de_rule_ids = match list_de_euclid_routing_algorithms(
&state,
@@ -3203,7 +3220,7 @@ pub async fn routing_migration_status(
let dimensions = dimension_state::Dimensions::new()
.with_processor_merchant_id(merchant_id.clone().into())
.with_provider_merchant_id(
- hyperswitch_domain_models::platform::ProviderMerchantId::new(merchant_id.clone()),
+ hyperswitch_domain_models::platform::ProviderMerchantId::new(provider_merchant_id),
)
.with_profile_id(profile_id.clone());
let routing_source = Some(
diff --git a/crates/router/src/db/routing_algorithm.rs b/crates/router/src/db/routing_algorithm.rs
index 642b5abef9..efc1374973 100644
--- a/crates/router/src/db/routing_algorithm.rs
+++ b/crates/router/src/db/routing_algorithm.rs
@@ -71,6 +71,8 @@ pub trait RoutingAlgorithmInterface {
)>,
>;
+ /// Each profile's rules, with the merchant that **owns the profile** — the rule's
+ /// `processor_merchant_id` where a platform wrote it, its `merchant_id` otherwise.
async fn find_rule_ids_for_profiles(
&self,
profile_ids: &[common_utils::id_type::ProfileId],