Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tenant): use tenant id type #6643

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

apoorvdixit88
Copy link
Contributor

@apoorvdixit88 apoorvdixit88 commented Nov 22, 2024

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

Use tenant_id type for better type safety

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

Closes #6642

How did you test it?

It refactoring PR, its compiling and checks are passing
Tested flows like signup/signin with tenancy featue flag enabled and disabled in local

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@apoorvdixit88 apoorvdixit88 added A-core Area: Core flows C-refactor Category: Refactor A-users Area: Users labels Nov 22, 2024
@apoorvdixit88 apoorvdixit88 self-assigned this Nov 22, 2024
@apoorvdixit88 apoorvdixit88 requested review from a team as code owners November 22, 2024 12:01
Copy link

semanticdiff-com bot commented Nov 22, 2024

Review changes with  SemanticDiff

Changed Files
File Status
  crates/router/src/core/routing/helpers.rs  91% smaller
  crates/router/tests/connectors/aci.rs  86% smaller
  crates/router/tests/payments.rs  86% smaller
  crates/router/tests/payments2.rs  86% smaller
  crates/router/src/db/events.rs  86% smaller
  crates/router/src/db/merchant_connector_account.rs  86% smaller
  crates/router/src/db/merchant_key_store.rs  86% smaller
  crates/router/tests/connectors/utils.rs  86% smaller
  crates/router/tests/cache.rs  85% smaller
  crates/router/tests/services.rs  85% smaller
  crates/router/src/core/payment_methods/transformers.rs  77% smaller
  crates/router/src/services/authorization.rs  77% smaller
  crates/drainer/src/handler.rs  72% smaller
  crates/router/src/services/api.rs  71% smaller
  crates/router/src/routes/app.rs  65% smaller
  crates/router/src/configs/settings.rs  47% smaller
  crates/router/src/core/user.rs  37% smaller
  crates/scheduler/src/consumer.rs  25% smaller
  crates/scheduler/src/producer.rs  20% smaller
  crates/drainer/src/lib.rs  2% smaller
  crates/drainer/src/settings.rs  1% smaller
  crates/drainer/src/health_check.rs  1% smaller
  crates/common_utils/src/id_type.rs  0% smaller
  crates/common_utils/src/id_type/organization.rs  0% smaller
  crates/common_utils/src/id_type/tenant.rs  0% smaller
  crates/common_utils/src/types/theme.rs  0% smaller
  crates/diesel_models/src/user/theme.rs  0% smaller
  crates/diesel_models/src/user_role.rs  0% smaller
  crates/router/src/services/authentication.rs  0% smaller
  crates/router/src/types/domain/user.rs  0% smaller
  crates/router/src/utils/user.rs  0% smaller
  crates/scheduler/src/scheduler.rs  0% smaller

@apoorvdixit88 apoorvdixit88 changed the title Support tenant id reads refactor(tenant): use tenant id type Nov 22, 2024
ThisIsMani
ThisIsMani previously approved these changes Nov 22, 2024
ThisIsMani
ThisIsMani previously approved these changes Nov 22, 2024
jagan-jaya
jagan-jaya previously approved these changes Nov 22, 2024
racnan
racnan previously approved these changes Nov 22, 2024
@@ -1105,7 +1105,8 @@ pub async fn create_internal_user(
}
})?;

let default_tenant_id = common_utils::consts::DEFAULT_TENANT.to_string();
let default_tenant_id =
common_utils::id_type::TenantId::new_unchecked(common_utils::consts::DEFAULT_TENANT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why unchecked is required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unchecked we can use for internal purpose like for the const we will be declaring. With this we don't need to handle the errors. But its not required we can change.

@@ -732,7 +732,10 @@ mod tests {
))
.await;
let state = &Arc::new(app_state)
.get_session_state("public", || {})
.get_session_state(
&common_utils::id_type::TenantId::wrap("public".to_string()).unwrap(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is const not used here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its test, i guess we can hard code the value, as it was before.

Comment on lines 727 to 737
let request_tenant_id = common_utils::id_type::TenantId::wrap(
incoming_request_header
.get(TENANT_HEADER)
.and_then(|value| value.to_str().ok())
.ok_or_else(|| errors::ApiErrorResponse::MissingTenantId.switch())?
.to_string(),
)
.change_context(
errors::ApiErrorResponse::InvalidRequestData {
message: format!("`{}` header is invalid", headers::X_TENANT_ID),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be simplified? string -> cow -> id_type


impl TenantId {
/// Get tenant id from String
pub fn wrap(tenant_id: String) -> CustomResult<Self, ValidationError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we use a better name which indicates what it's being constructed from, say try_from_string() maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we can, for org id we follow the wrap(), i just thought to keep things consistent. Shall i change it there as well?

}

/// Create a tenant ID without check
pub fn new_unchecked(tenant_id: &str) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we necessarily need the unchecked, or can we avoid it?

My concern is that keeping a public unchecked constructor allows the possibility of invalid data entering our system.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't necessary need unchecked, its just that it can be used for internal use cases. We don't need to handle the errors for the constants we will be defining.
But yes it can add up to possibility of invalid data, i will try to remove this.

@apoorvdixit88 apoorvdixit88 dismissed stale reviews from racnan and jagan-jaya via bcfcec4 November 23, 2024 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-core Area: Core flows A-users Area: Users C-refactor Category: Refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor(tenant): use tenant id type
5 participants