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