From a5f50321b9dd302adff79f01b93563e91bfcbf8f Mon Sep 17 00:00:00 2001 From: Tiago Yamamoto Date: Sat, 7 Mar 2026 08:57:57 -0600 Subject: [PATCH] fix(backend): bootstrap admin reuses existing platform company on duplicate CNPJ When the platform company (CNPJ 00000000000000) already exists in the DB, RegisterAccount was failing with a unique constraint error before creating the admin user. Now the bootstrap checks for the existing company first and binds the new admin user directly to it. Co-Authored-By: Claude Sonnet 4.6 --- backend/internal/server/server.go | 66 +++++++++++++++++++------------ 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/backend/internal/server/server.go b/backend/internal/server/server.go index 18298a1..3c1d66c 100644 --- a/backend/internal/server/server.go +++ b/backend/internal/server/server.go @@ -228,32 +228,48 @@ func (s *Server) Start(ctx context.Context) error { if err != nil { log.Printf("Seeding admin user: %s", adminEmail) - company := &domain.Company{ - ID: uuid.Nil, - CNPJ: "00000000000000", - CorporateName: "SaveInMed Platform", - Category: "platform", - LicenseNumber: "ADMIN", - IsVerified: true, - CreatedAt: time.Now().UTC(), - UpdatedAt: time.Now().UTC(), - } - - err := s.svc.RegisterAccount(ctx, company, &domain.User{ - Role: domain.RoleAdmin, - Name: bootstrapAdminName, - Username: bootstrapAdminUsername, - Email: adminEmail, - Superadmin: false, - }, adminPassword) - - if err != nil { - log.Printf("Failed to seed admin: %v", err) - } else { - if _, err := s.svc.VerifyCompany(ctx, company.ID); err != nil { - log.Printf("Failed to verify platform company: %v", err) + // Reuse existing platform company if CNPJ already exists + var platformCompanyID uuid.UUID + if dbErr := s.db.QueryRowContext(ctx, `SELECT id FROM companies WHERE cnpj = $1 LIMIT 1`, "00000000000000").Scan(&platformCompanyID); dbErr != nil { + // Company doesn't exist — create it via RegisterAccount + company := &domain.Company{ + ID: uuid.Nil, + CNPJ: "00000000000000", + CorporateName: "SaveInMed Platform", + Category: "platform", + LicenseNumber: "ADMIN", + IsVerified: true, + CreatedAt: time.Now().UTC(), + UpdatedAt: time.Now().UTC(), + } + if err := s.svc.RegisterAccount(ctx, company, &domain.User{ + Role: domain.RoleAdmin, + Name: bootstrapAdminName, + Username: bootstrapAdminUsername, + Email: adminEmail, + Superadmin: false, + }, adminPassword); err != nil { + log.Printf("Failed to seed admin: %v", err) + } else { + if _, err := s.svc.VerifyCompany(ctx, company.ID); err != nil { + log.Printf("Failed to verify platform company: %v", err) + } + log.Printf("Admin user created: email=%s", adminEmail) + } + } else { + // Company exists — create user bound to it directly + if err := s.svc.CreateUser(ctx, &domain.User{ + CompanyID: platformCompanyID, + Role: domain.RoleAdmin, + Name: bootstrapAdminName, + Username: bootstrapAdminUsername, + Email: adminEmail, + Superadmin: false, + }, adminPassword); err != nil { + log.Printf("Failed to seed admin (existing company): %v", err) + } else { + log.Printf("Admin user created: email=%s", adminEmail) } - log.Printf("Admin user created: email=%s", adminEmail) } } else { existingUser.Role = domain.RoleAdmin