Repository: juspay/hyperswitch
File: crates/router/src/core/admin.rs (Rust)

Code before:
            &key_store,
        )
        .await?;

        let org_id = match (&self.organization_id, &org_data_from_auth) {
            (Some(req_org_id), Some(auth)) => {
                if req_org_id != &auth.organization_id {
                    return Err(errors::ApiErrorResponse::InvalidRequestData {
                        message: "Mismatched organization_id in request and authenticated context"
                            .to_string(),
                    }
                    .into());
                }
                Some(req_org_id.clone())
            }
            (None, Some(auth)) => Some(auth.organization_id.clone()),
            (req_org_id, _) => req_org_id.clone(),
        };

        let organization = CreateOrValidateOrganization::new(org_id)
            .create_or_validate(db)
            .await?;

        let merchant_account_type = match organization.get_organization_type() {
            OrganizationType::Standard => {
                match self.merchant_account_type {
                    // Allow only if explicitly Standard or not provided
                    Some(MerchantAccountRequestType::Standard) | None => {
                        MerchantAccountType::Standard
                    }
                    Some(_) => {
                        return Err(errors::ApiErrorResponse::InvalidRequestData {
                            message:
                                "Merchant account type must be Standard for a Standard Organization"
                                    .to_string(),
                        }
                        .into());
                    }
                }
            }

            OrganizationType::Platform => {
                let accounts = state
                    .store
                    .list_merchant_accounts_by_organization_id(
                        &state.into(),
                        &organization.get_organization_id(),
                    )
                    .await
                    .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;


Review comment (suggestion):

```suggestion
                match self.merchant_account_type.unwrap_or_default() {
```

Code after:
            .create_or_validate(db)
            .await?;

        let merchant_account_type = match organization.get_organization_type() {
            OrganizationType::Standard => {
                match self.merchant_account_type.unwrap_or_default() {
                    // Allow only if explicitly Standard or not provided
                    MerchantAccountRequestType::Standard => MerchantAccountType::Standard,
                    MerchantAccountRequestType::Connected => {
                        return Err(errors::ApiErrorResponse::InvalidRequestData {
                            message:
                                "Merchant account type must be Standard for a Standard Organization"
                                    .to_string(),
                        }
                        .into());
                    }
                }
            }

            OrganizationType::Platform => {
                let accounts = state
                    .store
                    .list_merchant_accounts_by_organization_id(
                        &state.into(),
                        &organization.get_organization_id(),
                    )
                    .await
                    .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;

                let platform_account_exists = accounts
                    .iter()
                    .any(|account| account.merchant_account_type == MerchantAccountType::Platform);

                if accounts.is_empty() || !platform_account_exists {
                    // First merchant in a Platform org must be Platform
                    MerchantAccountType::Platform
                } else {
                    match self.merchant_account_type.unwrap_or_default() {
                        MerchantAccountRequestType::Standard => MerchantAccountType::Standard,
                        MerchantAccountRequestType::Connected => {
                            if state.conf.platform.allow_connected_merchants {
                                MerchantAccountType::Connected
                            } else {
                                return Err(errors::ApiErrorResponse::InvalidRequestData {
                                    message: "Connected merchant accounts are not allowed"
                                        .to_string(),
                                }
                                .into());
                            }
                        }
                    }