Bug: [FIX] fix tenant nomenclature in the code




diff --git a/config/config.example.toml b/config/config.example.toml
index 8f04ba49831..b0d3c743673 100644
--- a/config/config.example.toml
+++ b/config/config.example.toml
@@ -741,7 +741,7 @@ enabled = false
 global_tenant = { schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
 
 [multitenancy.tenants]
-public = { name = "hyperswitch", base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default" } # schema -> Postgres db schema, redis_key_prefix -> redis key distinguisher, base_url -> url of the tenant 
+public = { base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default" } # schema -> Postgres db schema, redis_key_prefix -> redis key distinguisher, base_url -> url of the tenant
 
 [user_auth_methods]
 encryption_key = "" # Encryption key used for encrypting data in user_authentication_methods table
diff --git a/config/deployments/env_specific.toml b/config/deployments/env_specific.toml
index cfefe9a130f..0eab330652a 100644
--- a/config/deployments/env_specific.toml
+++ b/config/deployments/env_specific.toml
@@ -305,7 +305,7 @@ enabled = false
 global_tenant = { schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
 
 [multitenancy.tenants]
-public = { name = "hyperswitch", base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default" }
+public = { base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default" }
 
 [user_auth_methods]
 encryption_key = "user_auth_table_encryption_key" # Encryption key used for encrypting data in user_authentication_methods table
diff --git a/config/development.toml b/config/development.toml
index d32c3610132..274e605193f 100644
--- a/config/development.toml
+++ b/config/development.toml
@@ -750,7 +750,7 @@ enabled = false
 global_tenant = { schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
 
 [multitenancy.tenants]
-public = { name = "hyperswitch", base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
+public = { base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
 
 [user_auth_methods]
 encryption_key = "A8EF32E029BC3342E54BF2E172A4D7AA43E8EF9D2C3A624A9F04E2EF79DC698F"
diff --git a/config/docker_compose.toml b/config/docker_compose.toml
index dde0902af91..c8436efd022 100644
--- a/config/docker_compose.toml
+++ b/config/docker_compose.toml
@@ -575,7 +575,7 @@ enabled = false
 global_tenant = { schema = "public", redis_key_prefix = "", clickhouse_database = "default" }
 
 [multitenancy.tenants]
-public = { name = "hyperswitch", base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default" }
+public = { base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default" }
 
 [user_auth_methods]
 encryption_key = "A8EF32E029BC3342E54BF2E172A4D7AA43E8EF9D2C3A624A9F04E2EF79DC698F"
diff --git a/crates/drainer/src/settings.rs b/crates/drainer/src/settings.rs
index 05226739504..5b391b492e0 100644
--- a/crates/drainer/src/settings.rs
+++ b/crates/drainer/src/settings.rs
@@ -125,21 +125,56 @@ impl Multitenancy {
     pub fn get_tenants(&self) -> &HashMap<String, Tenant> {
         &self.tenants.0
     }
-    pub fn get_tenant_names(&self) -> Vec<String> {
-        self.tenants.0.keys().cloned().collect()
+    pub fn get_tenant_ids(&self) -> Vec<String> {
+        self.tenants
+            .0
+            .values()
+            .map(|tenant| tenant.tenant_id.clone())
+            .collect()
     }
     pub fn get_tenant(&self, tenant_id: &str) -> Option<&Tenant> {
         self.tenants.0.get(tenant_id)
     }
 }
 
-#[derive(Debug, Deserialize, Clone, Default)]
-#[serde(transparent)]
+#[derive(Debug, Clone, Default)]
 pub struct TenantConfig(pub HashMap<String, Tenant>);
 
+impl<'de> Deserialize<'de> for TenantConfig {
+    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
+        #[derive(Deserialize)]
+        struct Inner {
+            base_url: String,
+            schema: String,
+            redis_key_prefix: String,
+            clickhouse_database: String,
+        }
+
+        let hashmap = <HashMap<String, Inner>>::deserialize(deserializer)?;
+
+        Ok(Self(
+            hashmap
+                .into_iter()
+                .map(|(key, value)| {
+                    (
+                        key.clone(),
+                        Tenant {
+                            tenant_id: key,
+                            base_url: value.base_url,
+                            schema: value.schema,
+                            redis_key_prefix: value.redis_key_prefix,
+                            clickhouse_database: value.clickhouse_database,
+                        },
+                    )
+                })
+                .collect(),
+        ))
+    }
+}
+
 #[derive(Debug, Deserialize, Clone, Default)]
 pub struct Tenant {
-    pub name: String,
+    pub tenant_id: String,
     pub base_url: String,
     pub schema: String,
     pub redis_key_prefix: String,
diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs
index 149ee2b8456..61e026ae2c5 100644
--- a/crates/router/src/configs/settings.rs
+++ b/crates/router/src/configs/settings.rs
@@ -141,8 +141,12 @@ impl Multitenancy {
     pub fn get_tenants(&self) -> &HashMap<String, Tenant> {
         &self.tenants.0
     }
-    pub fn get_tenant_names(&self) -> Vec<String> {
-        self.tenants.0.keys().cloned().collect()
+    pub fn get_tenant_ids(&self) -> Vec<String> {
+        self.tenants
+            .0
+            .values()
+            .map(|tenant| tenant.tenant_id.clone())
+            .collect()
     }
     pub fn get_tenant(&self, tenant_id: &str) -> Option<&Tenant> {
         self.tenants.0.get(tenant_id)
@@ -154,13 +158,12 @@ pub struct DecisionConfig {
     pub base_url: String,
 }
 
-#[derive(Debug, Deserialize, Clone, Default)]
-#[serde(transparent)]
+#[derive(Debug, Clone, Default)]
 pub struct TenantConfig(pub HashMap<String, Tenant>);
 
-#[derive(Debug, Deserialize, Clone, Default)]
+#[derive(Debug, Clone, Default)]
 pub struct Tenant {
-    pub name: String,
+    pub tenant_id: String,
     pub base_url: String,
     pub schema: String,
     pub redis_key_prefix: String,
@@ -1102,6 +1105,38 @@ where
     })?
 }
 
+impl<'de> Deserialize<'de> for TenantConfig {
+    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
+        #[derive(Deserialize)]
+        struct Inner {
+            base_url: String,
+            schema: String,
+            redis_key_prefix: String,
+            clickhouse_database: String,
+        }
+
+        let hashmap = <HashMap<String, Inner>>::deserialize(deserializer)?;
+
+        Ok(Self(
+            hashmap
+                .into_iter()
+                .map(|(key, value)| {
+                    (
+                        key.clone(),
+                        Tenant {
+                            tenant_id: key,
+                            base_url: value.base_url,
+                            schema: value.schema,
+                            redis_key_prefix: value.redis_key_prefix,
+                            clickhouse_database: value.clickhouse_database,
+                        },
+                    )
+                })
+                .collect(),
+        ))
+    }
+}
+
 #[cfg(test)]
 mod hashmap_deserialization_test {
     #![allow(clippy::unwrap_used)]
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index 067b35e1e5e..990a0f0e9ad 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -2058,7 +2058,7 @@ pub async fn get_payment_method_from_hs_locker<'a>(
             merchant_id,
             payment_method_reference,
             locker_choice,
-            state.tenant.name.clone(),
+            state.tenant.tenant_id.clone(),
             state.request_id,
         )
         .await
@@ -2112,7 +2112,7 @@ pub async fn add_card_to_hs_locker(
             locker,
             payload,
             locker_choice,
-            state.tenant.name.clone(),
+            state.tenant.tenant_id.clone(),
             state.request_id,
         )
         .await?;
@@ -2309,7 +2309,7 @@ pub async fn get_card_from_hs_locker<'a>(
             merchant_id,
             card_reference,
             Some(locker_choice),
-            state.tenant.name.clone(),
+            state.tenant.tenant_id.clone(),
             state.request_id,
         )
         .await
@@ -2355,7 +2355,7 @@ pub async fn delete_card_from_hs_locker<'a>(
         customer_id,
         merchant_id,
         card_reference,
-        state.tenant.name.clone(),
+        state.tenant.tenant_id.clone(),
         state.request_id,
     )
     .await
diff --git a/crates/router/src/core/routing/helpers.rs b/crates/router/src/core/routing/helpers.rs
index 6b584583174..31cd4234714 100644
--- a/crates/router/src/core/routing/helpers.rs
+++ b/crates/router/src/core/routing/helpers.rs
@@ -739,7 +739,7 @@ pub async fn push_metrics_for_success_based_routing(
             &metrics::CONTEXT,
             1,
             &add_attributes([
-                ("tenant", state.tenant.name.clone()),
+                ("tenant", state.tenant.tenant_id.clone()),
                 (
                     "merchant_id",
                     payment_attempt.merchant_id.get_string_repr().to_string(),
diff --git a/crates/router/src/middleware.rs b/crates/router/src/middleware.rs
index c80d14b9e25..0185d8a0768 100644
--- a/crates/router/src/middleware.rs
+++ b/crates/router/src/middleware.rs
@@ -147,10 +147,11 @@ where
             .and_then(|i| i.to_str().ok())
             .map(|s| s.to_owned());
         let response_fut = self.service.call(req);
+        let tenant_id_clone = tenant_id.clone();
         Box::pin(
             async move {
-                if let Some(tenant_id) = tenant_id {
-                    router_env::tracing::Span::current().record("tenant_id", &tenant_id);
+                if let Some(tenant) = tenant_id_clone {
+                    router_env::tracing::Span::current().record("tenant_id", tenant);
                 }
                 let response = response_fut.await;
                 router_env::tracing::Span::current().record("golden_log_line", true);
@@ -166,7 +167,7 @@ where
                     status_code = Empty,
                     flow = "UNKNOWN",
                     golden_log_line = Empty,
-                    tenant_id = "ta"
+                    tenant_id = &tenant_id
                 )
                 .or_current(),
             ),
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index 33699ed266a..f11840edf63 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -207,7 +207,7 @@ pub struct AppState {
 }
 impl scheduler::SchedulerAppState for AppState {
     fn get_tenants(&self) -> Vec<String> {
-        self.conf.multitenancy.get_tenant_names()
+        self.conf.multitenancy.get_tenant_ids()
     }
 }
 pub trait AppStateInfo {
diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rs
index 6f6eb381669..02d4950a47d 100644
--- a/crates/router/src/services/api.rs
+++ b/crates/router/src/services/api.rs
@@ -721,31 +721,27 @@ where
         .change_context(errors::ApiErrorResponse::InternalServerError.switch())?;
 
     let mut event_type = payload.get_api_event_type();
-    let tenants: HashSet<_> = state
-        .conf
-        .multitenancy
-        .get_tenant_names()
-        .into_iter()
-        .collect();
     let tenant_id = if !state.conf.multitenancy.enabled {
         DEFAULT_TENANT.to_string()
     } else {
-        incoming_request_header
+        let request_tenant_id = incoming_request_header
             .get(TENANT_HEADER)
             .and_then(|value| value.to_str().ok())
-            .ok_or_else(|| errors::ApiErrorResponse::MissingTenantId.switch())
-            .map(|req_tenant_id| {
-                if !tenants.contains(req_tenant_id) {
-                    Err(errors::ApiErrorResponse::InvalidTenant {
-                        tenant_id: req_tenant_id.to_string(),
-                    }
-                    .switch())
-                } else {
-                    Ok(req_tenant_id.to_string())
+            .ok_or_else(|| errors::ApiErrorResponse::MissingTenantId.switch())?;
+
+        state
+            .conf
+            .multitenancy
+            .get_tenant(request_tenant_id)
+            .map(|tenant| tenant.tenant_id.clone())
+            .ok_or(
+                errors::ApiErrorResponse::InvalidTenant {
+                    tenant_id: request_tenant_id.to_string(),
                 }
-            })??
+                .switch(),
+            )?
     };
-    // let tenant_id = "public".to_string();
+
     let mut session_state =
         Arc::new(app_state.clone()).get_session_state(tenant_id.as_str(), || {
             errors::ApiErrorResponse::InvalidTenant {
diff --git a/crates/router/src/types/storage/payment_attempt.rs b/crates/router/src/types/storage/payment_attempt.rs
index 98f51725bbe..0291374d54f 100644
--- a/crates/router/src/types/storage/payment_attempt.rs
+++ b/crates/router/src/types/storage/payment_attempt.rs
@@ -221,7 +221,7 @@ mod tests {
 
         let store = state
             .stores
-            .get(state.conf.multitenancy.get_tenant_names().first().unwrap())
+            .get(state.conf.multitenancy.get_tenant_ids().first().unwrap())
             .unwrap();
         let response = store
             .insert_payment_attempt(payment_attempt, enums::MerchantStorageScheme::PostgresOnly)
@@ -304,7 +304,7 @@ mod tests {
         };
         let store = state
             .stores
-            .get(state.conf.multitenancy.get_tenant_names().first().unwrap())
+            .get(state.conf.multitenancy.get_tenant_ids().first().unwrap())
             .unwrap();
         store
             .insert_payment_attempt(payment_attempt, enums::MerchantStorageScheme::PostgresOnly)
@@ -401,7 +401,7 @@ mod tests {
         };
         let store = state
             .stores
-            .get(state.conf.multitenancy.get_tenant_names().first().unwrap())
+            .get(state.conf.multitenancy.get_tenant_ids().first().unwrap())
             .unwrap();
         store
             .insert_payment_attempt(payment_attempt, enums::MerchantStorageScheme::PostgresOnly)
diff --git a/loadtest/config/development.toml b/loadtest/config/development.toml
index c8a5ccf9228..11268e15e54 100644
--- a/loadtest/config/development.toml
+++ b/loadtest/config/development.toml
@@ -369,7 +369,7 @@ enabled = false
 global_tenant = { schema = "public", redis_key_prefix = "" }
 
 [multitenancy.tenants]
-public = { name = "hyperswitch", base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
+public = { base_url = "http://localhost:8080", schema = "public", redis_key_prefix = "", clickhouse_database = "default"}
 
 [email]
 sender_email = "example@example.com"

## Type of Change
<!-- Put an `x` in the boxes that apply -->
- [x] Enhancement

## Description
<!-- Describe your changes in detail -->
This fixes the inconsistent nomenclature used for tenants in router and drainer code

### Additional Changes
- [x] This PR modifies application configuration/environment variables

## Motivation and Context
<!--
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.

If you don't have an issue, we'd recommend starting with one first so the PR
can focus on the implementation (unless it is an obvious bug or documentation fix
that will have little conversation).
-->
This will eradicate confusion between `name` and `tenant_id` names in the tenant config. In the config below
`public = { base_url = "http://localhost:8080/", schema = "public", redis_key_prefix = "", clickhouse_database = "default"}`

The key `public` will be the tenant_id. This PR adds a custom deserializer for this part

## How did you test it?
<!--
Did you write an integration/unit/API test to verify the code changes?
Or did you test this change manually (provide relevant screenshots)?
-->
**This cannot be tested on sandbox without enabling Multi Tenancy**

1. Application runs with the changed config and so does drainer
2. Do a request with  a valid `x-tenant-id` 

```bash
curl --location 'http://localhost:8080/accounts' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'x-tenant-id: public' \
--header 'api-key: test_admin' \
--data-raw '{
  "merchant_id": "1729601216",
  "locker_id": "m0010",
  "merchant_name": "NewAge Retailer",
  "merchant_details": {
    "primary_contact_person": "John Test",
    "primary_email": "JohnTest@test.com",
    "primary_phone": "sunt laborum",
    "secondary_contact_person": "John Test2",
    "secondary_email": "JohnTest2@test.com",
    "secondary_phone": "cillum do dolor id",
    "website": "www.example.com",
    "about_business": "Online Retail with a wide selection of organic products for North America",
    "address": {
      "line1": "1467",
      "line2": "Harrison Street",
      "line3": "Harrison Street",
      "city": "San Fransico",
      "state": "California",
      "zip": "94122",
      "country": "US"
    }
  },
  "return_url": "https://google.com/success",
  "webhook_details": {
    "webhook_version": "1.0.1",
    "webhook_username": "ekart_retail",
    "webhook_password": "password_ekart@123",
    "payment_created_enabled": true,
    "payment_succeeded_enabled": true,
    "payment_failed_enabled": true
  },
  "sub_merchants_enabled": false,
  "metadata": {
    "city": "NY",
    "unit": "245"
  },
  "primary_business_details": [
    {
      "country": "US",
      "business": "default"
    }
  ]
}'
```
It should return 200 and `tenant-id` should be the which has been passed

3. Try the request with invalid `tenant-id`, It should return an error

## Checklist
<!-- Put an `x` in the boxes that apply -->

- [x] I formatted the code `cargo +nightly fmt --all`
- [x] I addressed lints thrown by `cargo clippy`
- [x] I reviewed the submitted code