forked from baron/baron-sso
조직도 기능 추가
This commit is contained in:
@@ -3,19 +3,43 @@ package service
|
||||
import (
|
||||
"baron-sso-backend/internal/domain"
|
||||
"baron-sso-backend/internal/repository"
|
||||
"baron-sso-backend/internal/utils"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
var whitespaceRegex = regexp.MustCompile(`\s+`)
|
||||
var nonAlphaNumRegex = regexp.MustCompile(`[^a-zA-Z0-9가-힣]+`)
|
||||
|
||||
type ProgressData struct {
|
||||
Current int `json:"current"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
var ImportProgressCache sync.Map
|
||||
|
||||
type ImportResult struct {
|
||||
TotalRows int `json:"totalRows"`
|
||||
Processed int `json:"processed"`
|
||||
UserCreated int `json:"userCreated"`
|
||||
UserUpdated int `json:"userUpdated"`
|
||||
TenantCreated int `json:"tenantCreated"`
|
||||
Errors []string `json:"errors"`
|
||||
}
|
||||
|
||||
type OrgChartService interface {
|
||||
ImportCSV(ctx context.Context, tenantID string, r io.Reader) error
|
||||
ImportOrgChart(ctx context.Context, tenantID string, r io.Reader, filename string, progressID string) (*ImportResult, error)
|
||||
}
|
||||
|
||||
type orgChartService struct {
|
||||
@@ -42,364 +66,443 @@ func NewOrgChartService(
|
||||
}
|
||||
}
|
||||
|
||||
func (s *orgChartService) ImportCSV(ctx context.Context, tenantID string, r io.Reader) error {
|
||||
reader := csv.NewReader(r)
|
||||
header, err := reader.Read()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read CSV header: %w", err)
|
||||
}
|
||||
func (s *orgChartService) ImportOrgChart(ctx context.Context, tenantID string, r io.Reader, filename string, progressID string) (*ImportResult, error) {
|
||||
result := &ImportResult{Errors: make([]string, 0)}
|
||||
var allSheetsRecords [][][]string
|
||||
var err error
|
||||
|
||||
// Map header columns (Support both English and Korean)
|
||||
colMap := make(map[string]int)
|
||||
for i, name := range header {
|
||||
cleanName := strings.ToLower(strings.TrimSpace(name))
|
||||
colMap[cleanName] = i
|
||||
}
|
||||
|
||||
// Dynamic column detection for hierarchy
|
||||
hierarchyCols := []string{"그룹", "디비젼", "팀", "셀"}
|
||||
hierarchyIdx := make([]int, 0)
|
||||
for _, col := range hierarchyCols {
|
||||
if idx, ok := colMap[col]; ok {
|
||||
hierarchyIdx = append(hierarchyIdx, idx)
|
||||
if strings.HasSuffix(strings.ToLower(filename), ".xlsx") {
|
||||
allSheetsRecords, err = s.readAllXLSXSheets(r)
|
||||
} else {
|
||||
csvRecords, csvErr := s.readCSV(r)
|
||||
if csvErr == nil {
|
||||
allSheetsRecords = [][][]string{csvRecords}
|
||||
}
|
||||
err = csvErr
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Map English keys for core fields
|
||||
fieldMapping := map[string][]string{
|
||||
"email": {"email", "이메일"},
|
||||
"name": {"name", "이름"},
|
||||
"position": {"position", "직급"},
|
||||
"jobtitle": {"jobtitle", "직무"},
|
||||
"company": {"company", "소속"},
|
||||
"is_owner": {"is_owner", "구분"},
|
||||
"email": {"email", "이메일", "e-mail", "loginid", "login_id", "id", "계정", "사용자id", "사용자계정", "사번", "employeeid", "empno", "mail", "이메일주소"},
|
||||
"name": {"name", "이름", "성함", "성명", "username", "사용자명", "성함/이름", "사용자", "사원명"},
|
||||
"position": {"position", "직급", "직위", "직급/직위", "직급명", "직위명", "rank"},
|
||||
"jobtitle": {"jobtitle", "직무", "담당업무", "업무", "담당", "수행직무", "직종"},
|
||||
"phone": {"phone", "전화번호", "연락처", "휴대폰", "휴대폰번호", "핸드폰", "tel", "mobile"},
|
||||
"company": {"company", "소속", "법인", "회사", "소속회사", "소속법인", "사업부", "co"},
|
||||
"is_owner": {"is_owner", "구분", "직책", "권한", "role", "직책명", "장급여부", "리더", "leader", "manager", "pos"},
|
||||
}
|
||||
|
||||
var dataRows [][]string
|
||||
actualMap := make(map[string]int)
|
||||
for key, aliases := range fieldMapping {
|
||||
for _, alias := range aliases {
|
||||
if idx, ok := colMap[alias]; ok {
|
||||
actualMap[key] = idx
|
||||
found := false
|
||||
var headerMap map[string]int
|
||||
|
||||
for sheetIdx, records := range allSheetsRecords {
|
||||
for i, row := range records {
|
||||
if len(row) < 2 { continue }
|
||||
tempMap := make(map[string]int)
|
||||
for j, cell := range row {
|
||||
clean := s.cleanHeader(cell)
|
||||
if clean != "" { tempMap[clean] = j }
|
||||
}
|
||||
emailIdx := s.findBestMatch(tempMap, fieldMapping["email"])
|
||||
nameIdx := s.findBestMatch(tempMap, fieldMapping["name"])
|
||||
if nameIdx != -1 && emailIdx == -1 {
|
||||
for j, cell := range row {
|
||||
c := s.cleanHeader(cell)
|
||||
if strings.Contains(c, "mail") || strings.Contains(c, "계정") || strings.Contains(c, "id") || strings.Contains(c, "사번") {
|
||||
emailIdx = j; break
|
||||
}
|
||||
}
|
||||
}
|
||||
if emailIdx != -1 && nameIdx != -1 {
|
||||
dataRows = records[i+1:]
|
||||
headerMap = tempMap
|
||||
for key, aliases := range fieldMapping {
|
||||
actualMap[key] = s.findBestMatch(tempMap, aliases)
|
||||
}
|
||||
if actualMap["email"] == -1 { actualMap["email"] = emailIdx }
|
||||
found = true
|
||||
slog.Info("Found header row", "sheet", sheetIdx, "row", i)
|
||||
break
|
||||
}
|
||||
}
|
||||
if found { break }
|
||||
}
|
||||
|
||||
if !found {
|
||||
return nil, fmt.Errorf("required columns (email/name) not found. please check your headers")
|
||||
}
|
||||
|
||||
// [MH-OrgChart-Standalone Architecture]
|
||||
// Hierarchy is explicitly ordered: 부서(part) -> 그룹(gr) -> 디비전(div) -> 팀(team) -> 셀(cell)
|
||||
hierarchyLevels := [][]string{
|
||||
{"department", "organization", "부서", "조직", "부서명", "조직명", "소속부서", "part", "파트", "본부", "실", "국"},
|
||||
{"gr", "grp", "group", "그룹"},
|
||||
{"div", "division", "디비젼", "디비전"},
|
||||
{"team", "팀", "teal", "팀명"},
|
||||
{"cell", "셀"},
|
||||
}
|
||||
|
||||
hierarchyIdx := make([]int, 0)
|
||||
for _, aliases := range hierarchyLevels {
|
||||
idx := s.findBestMatch(headerMap, aliases)
|
||||
hierarchyIdx = append(hierarchyIdx, idx) // Keep order, -1 means not found
|
||||
}
|
||||
|
||||
// Path cache for hierarchy
|
||||
pathCache := make(map[string]string)
|
||||
result.TotalRows = len(dataRows)
|
||||
|
||||
for {
|
||||
record, err := reader.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
slog.Error("Failed to read CSV record", "error", err)
|
||||
continue
|
||||
}
|
||||
if progressID != "" {
|
||||
ImportProgressCache.Store(progressID, ProgressData{Current: 0, Total: result.TotalRows})
|
||||
defer ImportProgressCache.Delete(progressID)
|
||||
}
|
||||
|
||||
email := strings.TrimSpace(record[actualMap["email"]])
|
||||
name := strings.TrimSpace(record[actualMap["name"]])
|
||||
position := strings.TrimSpace(record[actualMap["position"]])
|
||||
jobTitle := strings.TrimSpace(record[actualMap["jobtitle"]])
|
||||
companyName := strings.TrimSpace(record[actualMap["company"]])
|
||||
|
||||
// Determine if owner (e.g. "팀장", "그룹장", "센터장", "실장")
|
||||
isOwner := false
|
||||
if idx, ok := actualMap["is_owner"]; ok {
|
||||
val := record[idx]
|
||||
isOwner = strings.HasSuffix(val, "장") || strings.EqualFold(val, "true") || val == "1"
|
||||
if tenantID == "root" || tenantID == "" {
|
||||
t, _ := s.tenantRepo.FindBySlug(ctx, "root-group")
|
||||
if t == nil {
|
||||
tenantID = uuid.NewString()
|
||||
_ = s.tenantRepo.Create(ctx, &domain.Tenant{ID: tenantID, Name: "Root Group", Slug: "root-group", Type: domain.TenantTypeCompanyGroup, Status: domain.TenantStatusActive})
|
||||
result.TenantCreated++
|
||||
} else {
|
||||
tenantID = t.ID
|
||||
}
|
||||
}
|
||||
|
||||
if email == "" || name == "" {
|
||||
continue
|
||||
}
|
||||
for rowIdx, record := range dataRows {
|
||||
if len(record) == 0 { continue }
|
||||
email := s.getVal(record, actualMap["email"])
|
||||
name := s.getVal(record, actualMap["name"])
|
||||
if email == "" || name == "" { continue }
|
||||
|
||||
// Extract domain from email
|
||||
parts := strings.Split(email, "@")
|
||||
domainName := ""
|
||||
if len(parts) == 2 {
|
||||
domainName = parts[1]
|
||||
}
|
||||
position := s.getVal(record, actualMap["position"])
|
||||
jobTitle := s.getVal(record, actualMap["jobtitle"])
|
||||
phone := s.normalizePhone(s.getVal(record, actualMap["phone"]))
|
||||
|
||||
// 1. Ensure Company Tenant Exists (The Core Requirement)
|
||||
companyName := s.getVal(record, actualMap["company"])
|
||||
if companyName == "" { companyName = "Main" }
|
||||
companySlug := s.generateCompanySlug(companyName)
|
||||
companyTenantID, err := s.ensureCompanyTenant(ctx, tenantID, companyName, companySlug, domainName, pathCache)
|
||||
|
||||
companyTenantID, err := s.ensureCompanyTenant(ctx, tenantID, companyName, companySlug, email, pathCache, result)
|
||||
if err != nil {
|
||||
slog.Error("Failed to ensure company tenant", "company", companyName, "error", err)
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("Row %d: Company fail: %v", rowIdx+2, err))
|
||||
continue
|
||||
}
|
||||
|
||||
// 2. Process Hierarchy (Build path from multiple columns under the Company)
|
||||
// Build orgPath following the strict order: 부서 -> 그룹 -> 디비전 -> 팀 -> 셀
|
||||
var orgParts []string
|
||||
for _, idx := range hierarchyIdx {
|
||||
val := strings.TrimSpace(record[idx])
|
||||
if val != "" && val != "-" {
|
||||
orgParts = append(orgParts, val)
|
||||
val := s.getVal(record, idx)
|
||||
if val != "" && val != "-" {
|
||||
orgParts = append(orgParts, val)
|
||||
}
|
||||
}
|
||||
orgPath := strings.Join(orgParts, "/")
|
||||
|
||||
leafID := companyTenantID // Default to company tenant
|
||||
if orgPath != "" {
|
||||
leafID, err = s.ensureOrgPath(ctx, companyTenantID, orgPath, pathCache)
|
||||
leafID := companyTenantID
|
||||
if orgPath != "" && orgPath != "-" {
|
||||
// [Matrix Fix] Build hierarchy under the Root Tenant (tenantID), NOT the individual company.
|
||||
// This allows departments like '총괄기획실' to be shared across multiple companies without duplication.
|
||||
leafID, err = s.ensureOrgPath(ctx, tenantID, orgPath, pathCache, result)
|
||||
if err != nil {
|
||||
slog.Error("Failed to ensure hierarchy", "path", orgPath, "error", err)
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("Row %d: Hierarchy fail: %v", rowIdx+2, err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Ensure User exists in Kratos (Auto-create if missing)
|
||||
isOwner := false
|
||||
grade := "member"
|
||||
if idx := actualMap["is_owner"]; idx != -1 && idx < len(record) {
|
||||
grade = strings.TrimSpace(record[idx])
|
||||
isOwner = strings.HasSuffix(grade, "장") || strings.EqualFold(grade, "true") || grade == "1" ||
|
||||
strings.Contains(grade, "Manager") || strings.Contains(grade, "Leader") ||
|
||||
strings.Contains(grade, "팀장") || strings.Contains(grade, "본부장")
|
||||
}
|
||||
|
||||
kratosID, err := s.kratos.FindIdentityIDByIdentifier(ctx, email)
|
||||
if err != nil || kratosID == "" {
|
||||
slog.Info("User not found in Kratos, auto-creating...", "email", email)
|
||||
|
||||
if (err != nil || kratosID == "") && phone != "" {
|
||||
kratosID, _ = s.kratos.FindIdentityIDByIdentifier(ctx, phone)
|
||||
}
|
||||
|
||||
if kratosID == "" {
|
||||
brokerUser := &domain.BrokerUser{
|
||||
Email: email,
|
||||
Name: name,
|
||||
Email: email, Name: name, PhoneNumber: phone,
|
||||
Attributes: map[string]interface{}{
|
||||
"affiliationType": "AFFILIATE",
|
||||
"companyCode": companySlug,
|
||||
"department": orgPath,
|
||||
"grade": "member",
|
||||
"affiliationType": "AFFILIATE", "companyCode": companySlug,
|
||||
"department": orgPath, "grade": grade, "position": position,
|
||||
"tenant_id": leafID, // [Matrix Fix] Sync leafID to Kratos to prevent lazy sync overwrite
|
||||
},
|
||||
}
|
||||
// Default password for bulk import
|
||||
newID, createErr := s.kratos.CreateUser(ctx, brokerUser, "baron1234!@#")
|
||||
if createErr != nil {
|
||||
slog.Error("Failed to auto-create user in Kratos", "email", email, "error", createErr)
|
||||
kratosID, err = s.kratos.CreateUser(ctx, brokerUser, "baron1234!@#")
|
||||
if err != nil {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("Row %d: User creation failed: %v", rowIdx+2, err))
|
||||
continue
|
||||
}
|
||||
kratosID = newID
|
||||
result.UserCreated++
|
||||
} else {
|
||||
traits := map[string]interface{}{
|
||||
"name": name, "companyCode": companySlug, "department": orgPath,
|
||||
"grade": grade, "position": position, "affiliationType": "AFFILIATE",
|
||||
"tenant_id": leafID, // [Matrix Fix] Sync leafID to Kratos to prevent lazy sync overwrite
|
||||
}
|
||||
if phone != "" {
|
||||
traits["phone_number"] = phone
|
||||
}
|
||||
_, _ = s.kratos.UpdateIdentity(ctx, kratosID, traits, "active")
|
||||
result.UserUpdated++
|
||||
}
|
||||
|
||||
// 4. Update User in Local DB (Bind to Company Tenant)
|
||||
user := &domain.User{
|
||||
ID: kratosID,
|
||||
Email: email,
|
||||
Name: name,
|
||||
Position: position,
|
||||
JobTitle: jobTitle,
|
||||
Department: orgPath,
|
||||
TenantID: &companyTenantID, // 편입: 사용자의 메인 소속은 회사(COMPANY)
|
||||
CompanyCode: companySlug,
|
||||
AffiliationType: "AFFILIATE",
|
||||
Status: "active",
|
||||
UpdatedAt: time.Now(),
|
||||
err = s.userRepo.Update(ctx, &domain.User{
|
||||
ID: kratosID, Email: email, Name: name, Phone: phone, Position: position,
|
||||
JobTitle: jobTitle, Department: orgPath,
|
||||
TenantID: &leafID, // [Matrix Fix] Local DB points to Leaf Department, while CompanyCode points to the Legal Entity
|
||||
CompanyCode: companySlug, AffiliationType: "AFFILIATE", Status: "active", UpdatedAt: time.Now(), Role: domain.RoleUser,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("Row update failed", "row", rowIdx+2, "email", email, "error", err)
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("Row %d: DB Update fail: %v", rowIdx+2, err))
|
||||
}
|
||||
|
||||
if err := s.userRepo.Update(ctx, user); err != nil {
|
||||
slog.Error("Failed to update user in local DB", "email", email, "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 5. Sync Membership to Keto
|
||||
if s.ketoOutboxRepo != nil {
|
||||
// Add as member of the Company Tenant
|
||||
// 1. [Redundant Assignment] Always assign to the Legal Company Tenant
|
||||
_ = s.ketoOutboxRepo.Create(ctx, &domain.KetoOutbox{
|
||||
Namespace: "Tenant",
|
||||
Object: companyTenantID,
|
||||
Relation: "members",
|
||||
Subject: "User:" + kratosID,
|
||||
Namespace: "Tenant",
|
||||
Object: companyTenantID,
|
||||
Relation: "members",
|
||||
Subject: "User:" + kratosID,
|
||||
Action: domain.KetoOutboxActionCreate,
|
||||
})
|
||||
|
||||
// Add as member of the specific Department unit (if exists)
|
||||
// 2. [Redundant Assignment] ALSO assign to the Logical Department Tenant (if exists)
|
||||
if leafID != companyTenantID {
|
||||
_ = s.ketoOutboxRepo.Create(ctx, &domain.KetoOutbox{
|
||||
Namespace: "Tenant",
|
||||
Object: leafID,
|
||||
Relation: "members",
|
||||
Subject: "User:" + kratosID,
|
||||
Namespace: "Tenant",
|
||||
Object: leafID,
|
||||
Relation: "members",
|
||||
Subject: "User:" + kratosID,
|
||||
Action: domain.KetoOutboxActionCreate,
|
||||
})
|
||||
}
|
||||
|
||||
// If owner/leader, assign owner role to the leaf unit
|
||||
|
||||
// 3. Assign ownership if leader
|
||||
if isOwner {
|
||||
_ = s.ketoOutboxRepo.Create(ctx, &domain.KetoOutbox{
|
||||
Namespace: "Tenant",
|
||||
Object: leafID,
|
||||
Relation: "owners",
|
||||
Subject: "User:" + kratosID,
|
||||
Namespace: "Tenant",
|
||||
Object: leafID,
|
||||
Relation: "owners",
|
||||
Subject: "User:" + kratosID,
|
||||
Action: domain.KetoOutboxActionCreate,
|
||||
})
|
||||
}
|
||||
}
|
||||
result.Processed++
|
||||
if progressID != "" && (result.Processed%5 == 0 || result.Processed == result.TotalRows) {
|
||||
ImportProgressCache.Store(progressID, ProgressData{Current: result.Processed, Total: result.TotalRows})
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *orgChartService) cleanHeader(val string) string {
|
||||
clean := strings.ToLower(whitespaceRegex.ReplaceAllString(val, ""))
|
||||
clean = nonAlphaNumRegex.ReplaceAllString(clean, "")
|
||||
return strings.TrimPrefix(clean, "\ufeff")
|
||||
}
|
||||
|
||||
func (s *orgChartService) findBestMatch(tempMap map[string]int, aliases []string) int {
|
||||
for _, alias := range aliases {
|
||||
ca := s.cleanHeader(alias)
|
||||
if idx, ok := tempMap[ca]; ok { return idx }
|
||||
}
|
||||
for cleaned, idx := range tempMap {
|
||||
for _, alias := range aliases {
|
||||
ca := s.cleanHeader(alias)
|
||||
if len(ca) >= 2 && (strings.Contains(cleaned, ca) || strings.Contains(ca, cleaned)) { return idx }
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func (s *orgChartService) getVal(record []string, idx int) string {
|
||||
if idx == -1 || idx >= len(record) { return "" }
|
||||
return strings.TrimSpace(record[idx])
|
||||
}
|
||||
|
||||
func (s *orgChartService) normalizePhone(phone string) string {
|
||||
normalized := strings.ReplaceAll(phone, "-", "")
|
||||
normalized = strings.ReplaceAll(normalized, " ", "")
|
||||
|
||||
re := regexp.MustCompile(`[^0-9+]`)
|
||||
normalized = re.ReplaceAllString(normalized, "")
|
||||
|
||||
if len(normalized) < 8 {
|
||||
return ""
|
||||
}
|
||||
|
||||
if strings.HasPrefix(normalized, "010") {
|
||||
return "+82" + normalized[1:]
|
||||
}
|
||||
if strings.HasPrefix(normalized, "82") {
|
||||
return "+" + normalized
|
||||
}
|
||||
if !strings.HasPrefix(normalized, "+") && len(normalized) >= 9 {
|
||||
if strings.HasPrefix(normalized, "0") {
|
||||
return "+82" + normalized[1:]
|
||||
}
|
||||
return "+82" + normalized
|
||||
}
|
||||
|
||||
return normalized
|
||||
}
|
||||
|
||||
func (s *orgChartService) readCSV(r io.Reader) ([][]string, error) {
|
||||
data, err := io.ReadAll(r)
|
||||
if err != nil { return nil, err }
|
||||
reader := csv.NewReader(bytes.NewReader(bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))))
|
||||
reader.LazyQuotes = true
|
||||
reader.FieldsPerRecord = -1
|
||||
return reader.ReadAll()
|
||||
}
|
||||
|
||||
func (s *orgChartService) readAllXLSXSheets(r io.Reader) ([][][]string, error) {
|
||||
f, err := excelize.OpenReader(r)
|
||||
if err != nil { return nil, err }
|
||||
defer f.Close()
|
||||
var allRecords [][][]string
|
||||
for _, sheet := range f.GetSheetList() {
|
||||
if rows, err := f.GetRows(sheet); err == nil { allRecords = append(allRecords, rows) }
|
||||
}
|
||||
return allRecords, nil
|
||||
}
|
||||
|
||||
// [New] Maps korean names to official slugs
|
||||
func (s *orgChartService) generateCompanySlug(name string) string {
|
||||
n := strings.ToLower(strings.TrimSpace(name))
|
||||
if strings.Contains(n, "한맥") || strings.Contains(n, "hanmac") {
|
||||
return "hanmac"
|
||||
n := strings.ToLower(whitespaceRegex.ReplaceAllString(name, ""))
|
||||
slugs := map[string]string{
|
||||
"한맥": "hanmac", "삼안": "saman", "장헌": "jangheon",
|
||||
"ptc": "ptc", "피티씨": "ptc", "바론": "baron", "한라": "halla",
|
||||
}
|
||||
if strings.Contains(n, "삼안") || strings.Contains(n, "saman") {
|
||||
return "saman"
|
||||
for k, v := range slugs {
|
||||
if strings.Contains(n, k) || strings.Contains(n, v) { return v }
|
||||
}
|
||||
if strings.Contains(n, "장헌") || strings.Contains(n, "jangheon") {
|
||||
return "jangheon"
|
||||
}
|
||||
if strings.Contains(n, "평화") || strings.Contains(n, "ptc") {
|
||||
return "ptc"
|
||||
}
|
||||
if strings.Contains(n, "바론") || strings.Contains(n, "baron") {
|
||||
return "baron"
|
||||
}
|
||||
if strings.Contains(n, "한라") || strings.Contains(n, "halla") {
|
||||
return "halla"
|
||||
}
|
||||
// Fallback for unknown companies
|
||||
return "comp-" + uuid.NewString()[:8]
|
||||
return utils.GenerateSlug(name)
|
||||
}
|
||||
|
||||
// [New] Ensures the COMPANY tenant exists and binds the domain
|
||||
func (s *orgChartService) ensureCompanyTenant(ctx context.Context, rootTenantID, name, slug, domainName string, cache map[string]string) (string, error) {
|
||||
cacheKey := "company:" + slug
|
||||
if id, ok := cache[cacheKey]; ok {
|
||||
return id, nil
|
||||
func isAlphaNumeric(s string) bool {
|
||||
for _, r := range s {
|
||||
if (r < 'a' || r > 'z') && (r < '0' || r > '9') && r != '-' { return false }
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *orgChartService) ensureCompanyTenant(ctx context.Context, rootID, name, slug, email string, cache map[string]string, res *ImportResult) (string, error) {
|
||||
if rootID == "root" || rootID == "" {
|
||||
// Auto-provision a root group if none is provided
|
||||
rootSlug := "root-group"
|
||||
t, _ := s.tenantRepo.FindBySlug(ctx, rootSlug)
|
||||
if t == nil {
|
||||
t = &domain.Tenant{ID: uuid.NewString(), Name: "Root Group", Slug: rootSlug, Type: domain.TenantTypeCompanyGroup, Status: domain.TenantStatusActive}
|
||||
_ = s.tenantRepo.Create(ctx, t)
|
||||
res.TenantCreated++
|
||||
}
|
||||
rootID = t.ID
|
||||
}
|
||||
|
||||
tenant, err := s.tenantRepo.FindBySlug(ctx, slug)
|
||||
if err != nil && !strings.Contains(err.Error(), "record not found") {
|
||||
return "", err
|
||||
cacheKey := "company:" + slug
|
||||
if id, ok := cache[cacheKey]; ok { return id, nil }
|
||||
|
||||
tenant, _ := s.tenantRepo.FindBySlug(ctx, slug)
|
||||
if tenant == nil {
|
||||
tenant, _ = s.tenantRepo.FindByName(ctx, name)
|
||||
}
|
||||
|
||||
if tenant == nil {
|
||||
// Auto-create missing company tenant
|
||||
slog.Info("Auto-creating missing company tenant", "name", name, "slug", slug)
|
||||
tenant = &domain.Tenant{
|
||||
ID: uuid.NewString(),
|
||||
Name: name,
|
||||
Slug: slug,
|
||||
Type: domain.TenantTypeCompany,
|
||||
Status: domain.TenantStatusActive,
|
||||
ParentID: &rootTenantID, // Bind to the root COMPANY_GROUP
|
||||
}
|
||||
if err := s.tenantRepo.Create(ctx, tenant); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Sync hierarchy to Keto (Company belongs to Group)
|
||||
tenant = &domain.Tenant{ID: uuid.NewString(), Name: name, Slug: slug, Type: domain.TenantTypeCompany, Status: domain.TenantStatusActive, ParentID: &rootID}
|
||||
if err := s.tenantRepo.Create(ctx, tenant); err != nil { return "", err }
|
||||
if s.ketoOutboxRepo != nil {
|
||||
_ = s.ketoOutboxRepo.Create(ctx, &domain.KetoOutbox{
|
||||
Namespace: "Tenant",
|
||||
Object: tenant.ID,
|
||||
Relation: "parents",
|
||||
Subject: "Tenant:" + rootTenantID,
|
||||
Action: domain.KetoOutboxActionCreate,
|
||||
})
|
||||
_ = s.ketoOutboxRepo.Create(ctx, &domain.KetoOutbox{Namespace: "Tenant", Object: tenant.ID, Relation: "parents", Subject: "Tenant:" + rootID, Action: domain.KetoOutboxActionCreate})
|
||||
}
|
||||
res.TenantCreated++
|
||||
}
|
||||
|
||||
// Ensure the email domain is registered to this company
|
||||
if domainName != "" {
|
||||
_ = s.tenantRepo.AddDomain(ctx, tenant.ID, domainName, true)
|
||||
}
|
||||
|
||||
|
||||
domainPart := ""
|
||||
if parts := strings.Split(email, "@"); len(parts) == 2 { domainPart = parts[1] }
|
||||
if domainPart != "" { _ = s.tenantRepo.AddDomain(ctx, tenant.ID, domainPart, true) }
|
||||
|
||||
cache[cacheKey] = tenant.ID
|
||||
return tenant.ID, nil
|
||||
}
|
||||
|
||||
func (s *orgChartService) ensureOrgPath(ctx context.Context, rootTenantID string, path string, cache map[string]string) (string, error) {
|
||||
func (s *orgChartService) ensureOrgPath(ctx context.Context, rootTenantID, path string, cache map[string]string, res *ImportResult) (string, error) {
|
||||
parts := strings.Split(path, "/")
|
||||
currentParentID := rootTenantID
|
||||
currentPath := ""
|
||||
|
||||
for i, part := range parts {
|
||||
part = strings.TrimSpace(part)
|
||||
if part == "" {
|
||||
continue
|
||||
if part == "" || part == "-" { continue }
|
||||
if currentPath == "" { currentPath = part } else { currentPath += "/" + part }
|
||||
|
||||
cacheKey := rootTenantID + ":" + currentPath
|
||||
if id, ok := cache[cacheKey]; ok {
|
||||
currentParentID = id; continue
|
||||
}
|
||||
|
||||
if currentPath == "" {
|
||||
currentPath = part
|
||||
} else {
|
||||
currentPath = currentPath + "/" + part
|
||||
}
|
||||
|
||||
if id, ok := cache[currentPath]; ok {
|
||||
currentParentID = id
|
||||
continue
|
||||
}
|
||||
|
||||
// Check DB if already exists
|
||||
var existingID string
|
||||
if s.userGroupRepo != nil {
|
||||
groups, err := s.userGroupRepo.ListByTenantID(ctx, rootTenantID)
|
||||
if err == nil {
|
||||
for _, g := range groups {
|
||||
// Match by name and parent
|
||||
if g.Name == part && ((g.ParentID == nil && currentParentID == rootTenantID) || (g.ParentID != nil && *g.ParentID == currentParentID)) {
|
||||
existingID = g.ID
|
||||
break
|
||||
}
|
||||
if groups, err := s.userGroupRepo.ListByTenantID(ctx, rootTenantID); err == nil {
|
||||
for _, g := range groups {
|
||||
isTopMatch := (g.ParentID == nil && currentParentID == rootTenantID)
|
||||
isSubMatch := (g.ParentID != nil && *g.ParentID == currentParentID)
|
||||
if g.Name == part && (isTopMatch || isSubMatch) {
|
||||
existingID = g.ID; break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if existingID == "" {
|
||||
// Create new unit
|
||||
unitID := uuid.NewString()
|
||||
existingID = uuid.NewString()
|
||||
groupSlug := fmt.Sprintf("ug-%s", existingID[:13])
|
||||
|
||||
// 1. Create Tenant (Type: USER_GROUP)
|
||||
newTenant := &domain.Tenant{
|
||||
ID: unitID,
|
||||
Type: domain.TenantTypeUserGroup,
|
||||
ParentID: ¤tParentID,
|
||||
Name: part,
|
||||
Slug: fmt.Sprintf("ug-%s", unitID[:8]),
|
||||
if err := s.tenantRepo.Create(ctx, &domain.Tenant{
|
||||
ID: existingID,
|
||||
Type: domain.TenantTypeUserGroup,
|
||||
ParentID: ¤tParentID,
|
||||
Name: part,
|
||||
Slug: groupSlug,
|
||||
Status: domain.TenantStatusActive,
|
||||
}
|
||||
if err := s.tenantRepo.Create(ctx, newTenant); err != nil {
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var ugParentID *string
|
||||
if currentParentID != rootTenantID {
|
||||
pid := currentParentID
|
||||
ugParentID = &pid
|
||||
}
|
||||
|
||||
// 2. Create UserGroup metadata
|
||||
newUserGroup := &domain.UserGroup{
|
||||
ID: unitID,
|
||||
TenantID: rootTenantID,
|
||||
ParentID: ¤tParentID,
|
||||
Name: part,
|
||||
if err := s.userGroupRepo.Create(ctx, &domain.UserGroup{
|
||||
ID: existingID,
|
||||
TenantID: rootTenantID,
|
||||
ParentID: ugParentID,
|
||||
Name: part,
|
||||
UnitType: s.guessUnitType(i, len(parts)),
|
||||
}
|
||||
if err := s.userGroupRepo.Create(ctx, newUserGroup); err != nil {
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 3. Sync Hierarchy to Keto via Outbox
|
||||
if s.ketoOutboxRepo != nil {
|
||||
_ = s.ketoOutboxRepo.Create(ctx, &domain.KetoOutbox{
|
||||
Namespace: "Tenant",
|
||||
Object: unitID,
|
||||
Relation: "parents",
|
||||
Subject: "Tenant:" + currentParentID,
|
||||
Action: domain.KetoOutboxActionCreate,
|
||||
})
|
||||
_ = s.ketoOutboxRepo.Create(ctx, &domain.KetoOutbox{Namespace: "Tenant", Object: existingID, Relation: "parents", Subject: "Tenant:" + currentParentID, Action: domain.KetoOutboxActionCreate})
|
||||
}
|
||||
|
||||
existingID = unitID
|
||||
res.TenantCreated++
|
||||
}
|
||||
|
||||
cache[currentPath] = existingID
|
||||
cache[cacheKey] = existingID
|
||||
currentParentID = existingID
|
||||
}
|
||||
|
||||
return currentParentID, nil
|
||||
}
|
||||
|
||||
func (s *orgChartService) guessUnitType(index, total int) string {
|
||||
if total == 1 {
|
||||
return "Team"
|
||||
}
|
||||
if index == 0 {
|
||||
return "Division"
|
||||
}
|
||||
if index == total-1 {
|
||||
return "Team"
|
||||
}
|
||||
return "Department"
|
||||
if total == 1 { return "Team" }
|
||||
if index == 0 { return "Division" }
|
||||
return "Team"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user