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 <noreply@anthropic.com>
This commit is contained in:
parent
2829a3e87c
commit
a5f50321b9
1 changed files with 41 additions and 25 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue