forked from baron/baron-sso
내정보 멀티 테턴트 표시
This commit is contained in:
@@ -141,23 +141,31 @@ func (h *TenantHandler) ListTenants(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch member counts for all tenants in one query using slugs (company codes)
|
||||
// Fetch member counts for all tenants in one query using IDs
|
||||
tenantIDs := make([]string, 0, len(tenants))
|
||||
slugs := make([]string, 0, len(tenants))
|
||||
for _, t := range tenants {
|
||||
tenantIDs = append(tenantIDs, t.ID)
|
||||
slugs = append(slugs, t.Slug)
|
||||
}
|
||||
memberCounts, err := h.UserRepo.CountByCompanyCodes(c.Context(), slugs)
|
||||
if err != nil {
|
||||
slog.Warn("failed to count members for tenants", "error", err)
|
||||
memberCounts = make(map[string]int64)
|
||||
}
|
||||
|
||||
idCounts, _ := h.UserRepo.CountByTenantIDs(c.Context(), tenantIDs)
|
||||
slugCounts, _ := h.UserRepo.CountByCompanyCodes(c.Context(), slugs)
|
||||
|
||||
items := make([]tenantSummary, 0, len(tenants))
|
||||
for _, t := range tenants {
|
||||
summary := mapTenantSummary(t)
|
||||
// Ensure robust matching by trimming and lowercasing the slug key
|
||||
key := strings.ToLower(strings.TrimSpace(t.Slug))
|
||||
summary.MemberCount = memberCounts[key]
|
||||
|
||||
// Combine counts from both ID and Slug (Max to avoid double counting if some have one or the other)
|
||||
idCount := idCounts[t.ID]
|
||||
slugCount := slugCounts[strings.ToLower(t.Slug)]
|
||||
|
||||
if idCount > slugCount {
|
||||
summary.MemberCount = idCount
|
||||
} else {
|
||||
summary.MemberCount = slugCount
|
||||
}
|
||||
|
||||
items = append(items, summary)
|
||||
}
|
||||
|
||||
@@ -182,11 +190,17 @@ func (h *TenantHandler) GetTenant(c *fiber.Ctx) error {
|
||||
return errorJSON(c, fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
memberCounts, err := h.UserRepo.CountByCompanyCodes(c.Context(), []string{tenant.Slug})
|
||||
count := int64(0)
|
||||
if err == nil {
|
||||
count = memberCounts[strings.ToLower(tenant.Slug)]
|
||||
idCounts, _ := h.UserRepo.CountByTenantIDs(c.Context(), []string{tenant.ID})
|
||||
slugCounts, _ := h.UserRepo.CountByCompanyCodes(c.Context(), []string{tenant.Slug})
|
||||
|
||||
idCount := idCounts[tenant.ID]
|
||||
slugCount := slugCounts[strings.ToLower(tenant.Slug)]
|
||||
|
||||
count := idCount
|
||||
if slugCount > idCount {
|
||||
count = slugCount
|
||||
}
|
||||
|
||||
summary := mapTenantSummary(tenant)
|
||||
summary.MemberCount = count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user