forked from baron/baron-sso
내정보 멀티 테턴트 표시
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type UserRepository interface {
|
||||
@@ -35,7 +36,11 @@ func (r *userRepository) Create(ctx context.Context, user *domain.User) error {
|
||||
}
|
||||
|
||||
func (r *userRepository) Update(ctx context.Context, user *domain.User) error {
|
||||
return r.db.WithContext(ctx).Save(user).Error
|
||||
// Use Upsert logic: if email exists, update all fields
|
||||
return r.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "email"}},
|
||||
UpdateAll: true,
|
||||
}).Save(user).Error
|
||||
}
|
||||
|
||||
func (r *userRepository) FindByEmail(ctx context.Context, email string) (*domain.User, error) {
|
||||
@@ -81,13 +86,16 @@ func (r *userRepository) CountByTenant(ctx context.Context, tenantID string) (in
|
||||
|
||||
func (r *userRepository) CountByTenantIDs(ctx context.Context, tenantIDs []string) (map[string]int64, error) {
|
||||
type result struct {
|
||||
TenantID string
|
||||
TenantID *string
|
||||
Count int64
|
||||
}
|
||||
var results []result
|
||||
counts := make(map[string]int64)
|
||||
|
||||
if len(tenantIDs) == 0 {
|
||||
return make(map[string]int64), nil
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
if err := r.db.WithContext(ctx).Model(&domain.User{}).
|
||||
Select("tenant_id, count(*) as count").
|
||||
Where("tenant_id IN ?", tenantIDs).
|
||||
@@ -96,10 +104,9 @@ func (r *userRepository) CountByTenantIDs(ctx context.Context, tenantIDs []strin
|
||||
return nil, err
|
||||
}
|
||||
|
||||
counts := make(map[string]int64)
|
||||
for _, res := range results {
|
||||
if res.TenantID != "" {
|
||||
counts[res.TenantID] = res.Count
|
||||
if res.TenantID != nil && *res.TenantID != "" {
|
||||
counts[*res.TenantID] = res.Count
|
||||
}
|
||||
}
|
||||
// Ensure all requested tenant IDs are in the map, even if count is 0
|
||||
@@ -122,7 +129,7 @@ func (r *userRepository) CountByCompanyCodes(ctx context.Context, codes []string
|
||||
}
|
||||
var results []result
|
||||
|
||||
// Search by company_code directly. Normalize inputs.
|
||||
// Search by company_code directly. Normalize inputs using LOWER for robust matching.
|
||||
err := r.db.WithContext(ctx).Model(&domain.User{}).
|
||||
Select("LOWER(company_code) as company_code, count(*) as count").
|
||||
Where("LOWER(company_code) IN ?", lowerStrings(codes)).
|
||||
@@ -137,7 +144,7 @@ func (r *userRepository) CountByCompanyCodes(ctx context.Context, codes []string
|
||||
counts[res.CompanyCode] = res.Count
|
||||
}
|
||||
|
||||
// Ensure all requested codes are present in results
|
||||
// Ensure all requested codes are present in results (even if count is 0)
|
||||
for _, code := range codes {
|
||||
lower := strings.ToLower(strings.TrimSpace(code))
|
||||
if _, ok := counts[lower]; !ok {
|
||||
|
||||
Reference in New Issue
Block a user