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

Code before:
            .tenant_id
            .as_ref()
            .unwrap_or(&state.tenant.tenant_id)
            .clone(),
    };

    let _ = state
        .global_store
        .update_user_by_user_id(
            &user_from_token.user_id,
            storage_user::UserUpdate::LineageContextUpdate {
                lineage_context: Some(
                    serde_json::to_value(&lineage_context)
                        .change_context(UserErrors::InternalServerError)
                        .attach_printable("Failed to serialize LineageContext to JSON")?,
                ),
            },
        )
        .await
        .map_err(|e| {
            logger::error!(
                "Failed to update lineage context for user {}: {:?}",
                user_from_token.user_id,
                e
            )
        });

    let token = utils::user::generate_jwt_auth_token_with_attributes(
        &state,
        user_from_token.user_id,
        merchant_id.clone(),
        request.org_id.clone(),
        role_id.clone(),
        profile_id.clone(),
        user_from_token.tenant_id.clone(),
    )
    .await?;

    utils::user_role::set_role_info_in_cache_by_role_id_org_id(
        &state,
        &role_id,
        &request.org_id,
        user_from_token
            .tenant_id
            .as_ref()
            .unwrap_or(&state.tenant.tenant_id),
    )
    .await;

    let response = user_api::TokenResponse {
        token: token.clone(),

Review comment (suggestion):
We can implement this on `LineageContext` or the `UserFromStorage` type if it is still big after the other changes.

Code after:
                .list_merchant_accounts_by_organization_id(&(&state).into(), &request.org_id)
                .await
                .change_context(UserErrors::InternalServerError)
                .attach_printable("Failed to get merchant list for org")?
                .pop()
                .ok_or(UserErrors::InvalidRoleOperation)
                .attach_printable("No merchants found for the org id")?
                .get_id()
                .to_owned();

            let key_store = state
                .store
                .get_merchant_key_store_by_merchant_id(
                    &(&state).into(),
                    &merchant_id,
                    &state.store.get_master_key().to_vec().into(),
                )
                .await
                .change_context(UserErrors::InternalServerError)?;

            let profile_id = state
                .store
                .list_profile_by_merchant_id(&(&state).into(), &key_store, &merchant_id)
                .await
                .change_context(UserErrors::InternalServerError)?
                .pop()
                .ok_or(UserErrors::InternalServerError)?
                .get_id()
                .to_owned();

            (merchant_id, profile_id, user_from_token.role_id)
        }
        EntityType::Organization | EntityType::Merchant | EntityType::Profile => {
            let user_role = state
                .global_store
                .list_user_roles_by_user_id(ListUserRolesByUserIdPayload {
                    user_id: &user_from_token.user_id,
                    tenant_id: user_from_token
                        .tenant_id
                        .as_ref()
                        .unwrap_or(&state.tenant.tenant_id),
                    org_id: Some(&request.org_id),
                    merchant_id: None,
                    profile_id: None,
                    entity_id: None,
                    version: None,
                    status: Some(UserStatus::Active),
                    limit: Some(1),
                })
                .await
                .change_context(UserErrors::InternalServerError)