1
0
forked from baron/baron-sso

fix(backend): resolve unused variable error and optimize tenant lookup in bulk create

This commit is contained in:
2026-03-04 12:39:42 +09:00
parent 7c28bd4867
commit 03a4c553f8

View File

@@ -406,43 +406,61 @@ func (h *UserHandler) BulkCreateUsers(c *fiber.Ctx) error {
requester, _ := c.Locals("user_profile").(*domain.UserProfileResponse)
results := make([]bulkUserResult, 0, len(req.Users))
// Pre-fetch tenant schemas to avoid redundant DB calls
tenantSchemas := make(map[string][]interface{})
// Pre-fetch tenant data to avoid redundant DB calls
type tenantCacheItem struct {
ID string
Schema []interface{}
}
tenantCache := make(map[string]tenantCacheItem)
for _, item := range req.Users {
email := strings.TrimSpace(item.Email)
if email == "" {
results = append(results, bulkUserResult{Email: "unknown", Success: false, Message: "email is required"})
name := strings.TrimSpace(item.Name)
compCode := strings.TrimSpace(item.CompanyCode)
dept := strings.TrimSpace(item.Department)
if email == "" || name == "" {
results = append(results, bulkUserResult{Email: email, Success: false, Message: "email and name are required"})
continue
}
if compCode == "" {
results = append(results, bulkUserResult{Email: email, Success: false, Message: "companyCode (tenant) is required"})
continue
}
// Role-based access check
if requester != nil && requester.Role == domain.RoleTenantAdmin {
if item.CompanyCode != requester.CompanyCode {
if compCode != requester.CompanyCode {
results = append(results, bulkUserResult{Email: email, Success: false, Message: "forbidden: cannot add users to another tenant"})
continue
}
}
// Resolve Schema
var schema []interface{}
if item.CompanyCode != "" {
if s, ok := tenantSchemas[item.CompanyCode]; ok {
schema = s
} else if h.TenantService != nil {
tenant, err := h.TenantService.GetTenantBySlug(c.Context(), item.CompanyCode)
if err == nil && tenant != nil {
if s, ok := tenant.Config["userSchema"].([]interface{}); ok {
tenantSchemas[item.CompanyCode] = s
schema = s
}
// Verify Tenant Existence and Resolve ID (with Cache)
var tItem tenantCacheItem
var exists bool
if tItem, exists = tenantCache[compCode]; !exists {
if h.TenantService != nil {
tenant, err := h.TenantService.GetTenantBySlug(c.Context(), compCode)
if err != nil || tenant == nil {
results = append(results, bulkUserResult{Email: email, Success: false, Message: "invalid companyCode: tenant not found"})
continue
}
tItem.ID = tenant.ID
if s, ok := tenant.Config["userSchema"].([]interface{}); ok {
tItem.Schema = s
}
tenantCache[compCode] = tItem
} else {
results = append(results, bulkUserResult{Email: email, Success: false, Message: "tenant service unavailable"})
continue
}
}
// Validation
if schema != nil {
if err := h.validateMetadata(item.Metadata, schema, true); err != nil {
// Validation based on schema
if tItem.Schema != nil {
if err := h.validateMetadata(item.Metadata, tItem.Schema, true); err != nil {
results = append(results, bulkUserResult{Email: email, Success: false, Message: "validation failed: " + err.Error()})
continue
}
@@ -455,22 +473,14 @@ func (h *UserHandler) BulkCreateUsers(c *fiber.Ctx) error {
}
attributes := map[string]interface{}{
"department": item.Department,
"department": dept,
"affiliationType": "internal",
"companyCode": item.CompanyCode,
"companyCode": compCode,
"tenant_id": tItem.ID,
"grade": role,
"role": role,
}
// Resolve TenantID
var tenantID string
if item.CompanyCode != "" && h.TenantService != nil {
if tenant, err := h.TenantService.GetTenantBySlug(c.Context(), item.CompanyCode); err == nil && tenant != nil {
tenantID = tenant.ID
attributes["tenant_id"] = tenantID
}
}
// Merge metadata
for k, v := range item.Metadata {
if _, exists := attributes[k]; !exists {