Skip to content

Multitenancy #231

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

Merged
merged 66 commits into from
May 19, 2025
Merged

Multitenancy #231

merged 66 commits into from
May 19, 2025

Conversation

thelissimus-work
Copy link
Contributor

No description provided.

@thelissimus-work thelissimus-work changed the title Sdk 213 Multitenancy Mar 27, 2025
@diyor28 diyor28 requested a review from Copilot March 27, 2025 14:36
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces multitenancy support across multiple modules by adding tenant context propagation, repository, service, and domain logic.

  • Adds tenant context to the authentication middleware
  • Implements tenant service and repository for CRUD operations
  • Updates domain models and repository mappers to incorporate tenant IDs

Reviewed Changes

Copilot reviewed 13 out of 20 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/middleware/auth.go Adds tenant lookup in authentication flow
pkg/constants/middleware.go Introduces a TenantKey constant
pkg/composables/tenant.go Provides helper methods for using tenant data
modules/core/services/tenant_service.go Implements tenant service methods
modules/core/module.go Registers tenant and tab services; duplicate tab service registration observed
modules/core/infrastructure/persistence/user_repository.go Adds tenant_id handling in user repository records
modules/core/infrastructure/persistence/tenant_repository.go Implements tenant repository with query methods
modules/core/infrastructure/persistence/models/models.go Adds tenant fields to various models
modules/core/infrastructure/persistence/core_mappers.go Maps tenant_id for user entity conversions
modules/core/domain/entities/tenant/* Defines tenant entity and repository interface
modules/core/domain/aggregates/user/user.go Introduces tenantID in the user aggregate
Files not reviewed (7)
  • modules/bichat/infrastructure/persistence/schema/bichat-schema.sql: Language not supported
  • modules/core/infrastructure/persistence/schema/core-schema.sql: Language not supported
  • modules/crm/infrastructure/persistence/schema/crm-schema.sql: Language not supported
  • modules/finance/infrastructure/persistence/schema/finance-schema.sql: Language not supported
  • modules/hrm/infrastructure/persistence/schema/hrm-schema.sql: Language not supported
  • modules/logging/infrastructure/persistence/schema/logging-schema.sql: Language not supported
  • modules/warehouse/infrastructure/persistence/schema/warehouse-schema.sql: Language not supported
Comments suppressed due to low confidence (1)

modules/core/module.go:67

  • Duplicate registration of TabService is detected; consider removing the extra registration to avoid potential unexpected behavior.
services.NewTabService(persistence.NewTabRepository()),

@diyor28
Copy link
Contributor

diyor28 commented Mar 27, 2025

@thelissimus-work Use UUID for tenants table. We will be migrating to UUID pretty soon, so why not start using UUID for new tables already

sessionUpdateQuery = `
UPDATE sessions
SET expires_at = $1,
ip = $2,
user_agent = $3
WHERE token = $4`
sessionDeleteQuery = `DELETE FROM sessions WHERE token = $1`
WHERE token = $4 AND tenant_id = $5`
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 this necessary? Token is already a primary key

WHERE token = $4`
sessionDeleteQuery = `DELETE FROM sessions WHERE token = $1`
WHERE token = $4 AND tenant_id = $5`
sessionDeleteQuery = `DELETE FROM sessions WHERE token = $1 AND tenant_id = $2`
Copy link
Contributor

Choose a reason for hiding this comment

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

Here as well

var count int64
if err := tx.QueryRow(ctx, sessionCountQuery).Scan(&count); err != nil {
if err := tx.QueryRow(ctx, sessionCountQuery+" WHERE tenant_id = $1", tenant.ID).Scan(&count); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

For constructing sql queries in the runtime use methods from the repo package. Reference other usages of this package to get an idea on how to use it

"github.com/google/uuid"
)

type Tenant struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a particular reason Tenant isn't an interface like other domain entities?

if err := tx.Commit(ctx); err != nil {
t.Fatal(err)
// Rollback instead of commit to ensure clean state
// This is safer as it ensures tests don't affect each other
Copy link
Contributor

@diyor28 diyor28 Apr 14, 2025

Choose a reason for hiding this comment

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

They already don't affect each other since each test runs in a separate db. Committing changes is useful to be able to introspect the db state after a failed test

ErrNoTenantFound = errors.New("no tenant found in context")
)

type Tenant struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this duplicate type for?

Diyor Khaydarov added 19 commits May 16, 2025 15:29
# Conflicts:
#	go.sum
#	modules/core/domain/aggregates/group/group.go
#	modules/core/domain/aggregates/group/group_repository.go
#	modules/core/domain/aggregates/role/role.go
#	modules/core/domain/aggregates/role/role_repository.go
#	modules/core/domain/aggregates/user/user.go
#	modules/core/domain/aggregates/user/user_repository.go
#	modules/core/domain/entities/session/session.go
#	modules/core/infrastructure/persistence/core_mappers.go
#	modules/core/infrastructure/persistence/group_repository.go
#	modules/core/infrastructure/persistence/models/models.go
#	modules/core/infrastructure/persistence/role_repository.go
#	modules/core/infrastructure/persistence/schema/core-schema.sql
#	modules/core/infrastructure/persistence/session_repository.go
#	modules/core/infrastructure/persistence/user_repository.go
#	modules/core/module.go
#	modules/core/presentation/controllers/login_controller.go
#	modules/core/seed/seed_user.go
#	modules/crm/infrastructure/persistence/chat_repository.go
#	modules/crm/infrastructure/persistence/client_repository.go
#	modules/crm/infrastructure/persistence/client_repository_test.go
#	modules/crm/infrastructure/persistence/schema/crm-schema.sql
#	modules/finance/domain/aggregates/money_account/account.go
#	modules/finance/domain/aggregates/payment/payment.go
#	modules/finance/domain/aggregates/payment/payment_impl.go
#	modules/hrm/domain/aggregates/employee/employee.go
#	modules/warehouse/domain/aggregates/order/order.go
#	modules/warehouse/domain/aggregates/order/order_impl.go
#	modules/warehouse/domain/aggregates/position/position.go
#	modules/warehouse/domain/aggregates/product/product.go
#	modules/warehouse/domain/entities/inventory/inventory.go
#	modules/warehouse/infrastructure/persistence/models/models.go
#	pkg/constants/middleware.go
#	pkg/testutils/utils.go
# Conflicts:
#	modules/core/infrastructure/persistence/permission_repository.go
#	modules/core/infrastructure/persistence/session_repository.go
#	modules/core/infrastructure/persistence/upload_repository.go
#	modules/crm/infrastructure/persistence/chat_repository.go
- Add ID to sidebar navigation for easier testing
- Add test to verify newly created user sees tabs in sidebar after login
@diyor28 diyor28 merged commit a2e1a4c into staging May 19, 2025
1 check failed
@diyor28 diyor28 deleted the sdk-213 branch May 19, 2025 12:11
diyor28 added a commit that referenced this pull request Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants