forked from baron/baron-sso
feat: enhance multi-tenant UI and fix member aggregation
- adminfront: Update TenantListPage and UserListPage sorting logic to support all columns dynamically - adminfront: Add inline 'Move', 'Remove', and 'Delete' actions directly inside the Tenant Org Chart member table - adminfront: Remove redundant 'ACTIONS' column and profile icon from UserListPage, making the name clickable - adminfront: Remove 'New Member' creation link from Tenant Org Chart 'Add Member' dropdown - backend: Fix CountByCompanyCodes query to accurately aggregate user counts using both primary company_code and company_codes array
This commit is contained in:
@@ -157,13 +157,21 @@ func (r *userRepository) CountByCompanyCodes(ctx context.Context, codes []string
|
||||
}
|
||||
var results []result
|
||||
|
||||
// Search by company_codes array using unnest and overlap.
|
||||
// This ensures users with multiple memberships are counted in each tenant they belong to.
|
||||
err := r.db.WithContext(ctx).Table("users").
|
||||
Select("unnest(company_codes) as company_code, count(*) as count").
|
||||
Where("company_codes && ?", pq.Array(lowerStrings(codes))).
|
||||
Group("company_code").
|
||||
Scan(&results).Error
|
||||
lowerCodes := lowerStrings(codes)
|
||||
|
||||
// Combine singular company_code and array company_codes using a subquery
|
||||
// to ensure we count each user accurately per company code they belong to.
|
||||
query := `
|
||||
SELECT LOWER(comp_code) as company_code, count(DISTINCT id) as count
|
||||
FROM (
|
||||
SELECT id, company_code as comp_code FROM users WHERE LOWER(company_code) = ANY($1)
|
||||
UNION ALL
|
||||
SELECT id, unnest(company_codes) as comp_code FROM users WHERE company_codes && $1
|
||||
) as combined
|
||||
WHERE LOWER(comp_code) = ANY($1)
|
||||
GROUP BY LOWER(comp_code)
|
||||
`
|
||||
err := r.db.WithContext(ctx).Raw(query, pq.Array(lowerCodes)).Scan(&results).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user