Align backend company registration and notification routes with frontend
This commit is contained in:
parent
aa544426a5
commit
4544a1d5b2
3 changed files with 161 additions and 142 deletions
|
|
@ -3,21 +3,25 @@ package dto
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type CreateCompanyRequest struct {
|
type CreateCompanyRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
CompanyName string `json:"companyName"` // Alternative field name
|
CompanyName string `json:"companyName"` // Alternative field name
|
||||||
Document string `json:"document"`
|
Document string `json:"document"`
|
||||||
Contact string `json:"contact"`
|
Contact string `json:"contact"`
|
||||||
AdminEmail string `json:"admin_email"`
|
AdminEmail string `json:"admin_email"`
|
||||||
Email string `json:"email"` // Alternative field name
|
Email string `json:"email"` // Alternative field name
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
Website *string `json:"website,omitempty"`
|
Website *string `json:"website,omitempty"`
|
||||||
Address *string `json:"address,omitempty"`
|
Address *string `json:"address,omitempty"`
|
||||||
EmployeeCount *string `json:"employeeCount,omitempty"`
|
City *string `json:"city,omitempty"`
|
||||||
FoundedYear *int `json:"foundedYear,omitempty"`
|
State *string `json:"state,omitempty"`
|
||||||
Description *string `json:"description,omitempty"`
|
ZipCode *string `json:"zip_code,omitempty"`
|
||||||
YearsInMarket *string `json:"years_in_market,omitempty"`
|
EmployeeCount *string `json:"employeeCount,omitempty"`
|
||||||
BirthDate *string `json:"birth_date,omitempty"`
|
FoundedYear *int `json:"foundedYear,omitempty"`
|
||||||
|
Description *string `json:"description,omitempty"`
|
||||||
|
YearsInMarket *string `json:"years_in_market,omitempty"`
|
||||||
|
BirthDate *string `json:"birth_date,omitempty"`
|
||||||
|
AdminBirthDate *string `json:"admin_birth_date,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CompanyResponse struct {
|
type CompanyResponse struct {
|
||||||
|
|
|
||||||
|
|
@ -1,127 +1,140 @@
|
||||||
package tenant
|
package tenant
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rede5/gohorsejobs/backend/internal/core/domain/entity"
|
"github.com/rede5/gohorsejobs/backend/internal/core/domain/entity"
|
||||||
"github.com/rede5/gohorsejobs/backend/internal/core/dto"
|
"github.com/rede5/gohorsejobs/backend/internal/core/dto"
|
||||||
"github.com/rede5/gohorsejobs/backend/internal/core/ports"
|
"github.com/rede5/gohorsejobs/backend/internal/core/ports"
|
||||||
"github.com/rede5/gohorsejobs/backend/internal/utils"
|
"github.com/rede5/gohorsejobs/backend/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CreateCompanyUseCase struct {
|
type CreateCompanyUseCase struct {
|
||||||
companyRepo ports.CompanyRepository
|
companyRepo ports.CompanyRepository
|
||||||
userRepo ports.UserRepository
|
userRepo ports.UserRepository
|
||||||
authService ports.AuthService
|
authService ports.AuthService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCreateCompanyUseCase(cRepo ports.CompanyRepository, uRepo ports.UserRepository, auth ports.AuthService) *CreateCompanyUseCase {
|
func NewCreateCompanyUseCase(cRepo ports.CompanyRepository, uRepo ports.UserRepository, auth ports.AuthService) *CreateCompanyUseCase {
|
||||||
return &CreateCompanyUseCase{
|
return &CreateCompanyUseCase{
|
||||||
companyRepo: cRepo,
|
companyRepo: cRepo,
|
||||||
userRepo: uRepo,
|
userRepo: uRepo,
|
||||||
authService: auth,
|
authService: auth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *CreateCompanyUseCase) Execute(ctx context.Context, input dto.CreateCompanyRequest) (*dto.CompanyResponse, error) {
|
func (uc *CreateCompanyUseCase) Execute(ctx context.Context, input dto.CreateCompanyRequest) (*dto.CompanyResponse, error) {
|
||||||
// 0. Sanitize inputs
|
if input.Name == "" && input.CompanyName != "" {
|
||||||
sanitizer := utils.DefaultSanitizer()
|
input.Name = input.CompanyName
|
||||||
input.Name = sanitizer.SanitizeName(input.Name)
|
}
|
||||||
input.Contact = sanitizer.SanitizeString(input.Contact)
|
|
||||||
input.AdminEmail = sanitizer.SanitizeEmail(input.AdminEmail)
|
// 0. Sanitize inputs
|
||||||
|
sanitizer := utils.DefaultSanitizer()
|
||||||
// Validate name
|
input.Name = sanitizer.SanitizeName(input.Name)
|
||||||
if input.Name == "" {
|
input.Contact = sanitizer.SanitizeString(input.Contact)
|
||||||
return nil, errors.New("nome da empresa é obrigatório")
|
input.AdminEmail = sanitizer.SanitizeEmail(input.AdminEmail)
|
||||||
}
|
|
||||||
|
// Validate name
|
||||||
// Validate document (flexible for global portal)
|
if input.Name == "" {
|
||||||
docValidator := utils.NewDocumentValidator("")
|
return nil, errors.New("nome da empresa é obrigatório")
|
||||||
if input.Document != "" {
|
}
|
||||||
result := docValidator.ValidateDocument(input.Document, "")
|
|
||||||
if !result.Valid {
|
// Validate document (flexible for global portal)
|
||||||
return nil, errors.New(result.Message)
|
docValidator := utils.NewDocumentValidator("")
|
||||||
}
|
if input.Document != "" {
|
||||||
input.Document = result.Clean
|
result := docValidator.ValidateDocument(input.Document, "")
|
||||||
}
|
if !result.Valid {
|
||||||
|
return nil, errors.New(result.Message)
|
||||||
// Ensure AdminEmail is set (fallback to Email)
|
}
|
||||||
if input.AdminEmail == "" {
|
input.Document = result.Clean
|
||||||
input.AdminEmail = input.Email
|
}
|
||||||
}
|
|
||||||
if input.Contact == "" && input.Email != "" {
|
// Ensure AdminEmail is set (fallback to Email)
|
||||||
input.Contact = input.Email
|
if input.AdminEmail == "" {
|
||||||
}
|
input.AdminEmail = input.Email
|
||||||
|
}
|
||||||
// Check if user already exists
|
if input.Contact == "" && input.Email != "" {
|
||||||
existingUser, _ := uc.userRepo.FindByEmail(ctx, input.AdminEmail)
|
input.Contact = input.Email
|
||||||
if existingUser != nil {
|
}
|
||||||
return nil, fmt.Errorf("user with email %s already exists", input.AdminEmail)
|
|
||||||
}
|
// Check if user already exists
|
||||||
|
existingUser, _ := uc.userRepo.FindByEmail(ctx, input.AdminEmail)
|
||||||
// 1. Create Company Entity
|
if existingUser != nil {
|
||||||
company := entity.NewCompany("", input.Name, &input.Document, &input.Contact)
|
return nil, fmt.Errorf("user with email %s already exists", input.AdminEmail)
|
||||||
|
}
|
||||||
// Map optional fields
|
|
||||||
if input.Phone != "" {
|
// 1. Create Company Entity
|
||||||
company.Phone = &input.Phone
|
company := entity.NewCompany("", input.Name, &input.Document, &input.Contact)
|
||||||
}
|
|
||||||
if input.Website != nil {
|
// Map optional fields
|
||||||
company.Website = input.Website
|
if input.Phone != "" {
|
||||||
}
|
company.Phone = &input.Phone
|
||||||
if input.Description != nil {
|
}
|
||||||
company.Description = input.Description
|
if input.Website != nil {
|
||||||
}
|
company.Website = input.Website
|
||||||
if input.Address != nil {
|
}
|
||||||
company.Address = input.Address
|
if input.Description != nil {
|
||||||
}
|
company.Description = input.Description
|
||||||
if input.YearsInMarket != nil {
|
}
|
||||||
company.YearsInMarket = input.YearsInMarket
|
if input.Address != nil {
|
||||||
}
|
company.Address = input.Address
|
||||||
|
}
|
||||||
savedCompany, err := uc.companyRepo.Save(ctx, company)
|
if input.YearsInMarket != nil {
|
||||||
if err != nil {
|
company.YearsInMarket = input.YearsInMarket
|
||||||
return nil, err
|
}
|
||||||
}
|
|
||||||
|
savedCompany, err := uc.companyRepo.Save(ctx, company)
|
||||||
// 2. Create Admin User
|
if err != nil {
|
||||||
pwd := input.Password
|
return nil, err
|
||||||
if pwd == "" {
|
}
|
||||||
pwd = "ChangeMe123!"
|
|
||||||
}
|
// 2. Create Admin User
|
||||||
hashedPassword, _ := uc.authService.HashPassword(pwd)
|
pwd := input.Password
|
||||||
|
if pwd == "" {
|
||||||
adminUser := entity.NewUser("", savedCompany.ID, "Admin", input.AdminEmail)
|
pwd = "ChangeMe123!"
|
||||||
adminUser.PasswordHash = hashedPassword
|
}
|
||||||
|
hashedPassword, _ := uc.authService.HashPassword(pwd)
|
||||||
if input.BirthDate != nil {
|
|
||||||
layout := "2006-01-02"
|
adminUser := entity.NewUser("", savedCompany.ID, "Admin", input.AdminEmail)
|
||||||
if parsed, err := time.Parse(layout, *input.BirthDate); err == nil {
|
adminUser.PasswordHash = hashedPassword
|
||||||
adminUser.BirthDate = &parsed
|
adminUser.Address = input.Address
|
||||||
}
|
adminUser.City = input.City
|
||||||
}
|
adminUser.State = input.State
|
||||||
|
adminUser.ZipCode = input.ZipCode
|
||||||
adminUser.AssignRole(entity.Role{Name: entity.RoleAdmin})
|
|
||||||
|
birthDate := input.BirthDate
|
||||||
_, err = uc.userRepo.Save(ctx, adminUser)
|
if birthDate == nil {
|
||||||
if err != nil {
|
birthDate = input.AdminBirthDate
|
||||||
return nil, err
|
}
|
||||||
}
|
|
||||||
|
if birthDate != nil {
|
||||||
// 3. Generate Token for Auto-Login
|
layout := "2006-01-02"
|
||||||
token, err := uc.authService.GenerateToken(adminUser.ID, savedCompany.ID, []string{entity.RoleAdmin})
|
if parsed, err := time.Parse(layout, *birthDate); err == nil {
|
||||||
if err != nil {
|
adminUser.BirthDate = &parsed
|
||||||
_ = err
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &dto.CompanyResponse{
|
adminUser.AssignRole(entity.Role{Name: entity.RoleAdmin})
|
||||||
ID: savedCompany.ID,
|
|
||||||
Name: savedCompany.Name,
|
_, err = uc.userRepo.Save(ctx, adminUser)
|
||||||
Status: savedCompany.Status,
|
if err != nil {
|
||||||
Token: token,
|
return nil, err
|
||||||
CreatedAt: savedCompany.CreatedAt,
|
}
|
||||||
}, nil
|
|
||||||
}
|
// 3. Generate Token for Auto-Login
|
||||||
|
token, err := uc.authService.GenerateToken(adminUser.ID, savedCompany.ID, []string{entity.RoleAdmin})
|
||||||
|
if err != nil {
|
||||||
|
_ = err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &dto.CompanyResponse{
|
||||||
|
ID: savedCompany.ID,
|
||||||
|
Name: savedCompany.Name,
|
||||||
|
Status: savedCompany.Status,
|
||||||
|
Token: token,
|
||||||
|
CreatedAt: savedCompany.CreatedAt,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -358,6 +358,8 @@ func NewRouter() http.Handler {
|
||||||
notificationHandler := handlers.NewNotificationHandler(notificationService)
|
notificationHandler := handlers.NewNotificationHandler(notificationService)
|
||||||
mux.Handle("PUT /api/v1/notifications/read-all", authMiddleware.HeaderAuthGuard(http.HandlerFunc(notificationHandler.MarkAllAsRead)))
|
mux.Handle("PUT /api/v1/notifications/read-all", authMiddleware.HeaderAuthGuard(http.HandlerFunc(notificationHandler.MarkAllAsRead)))
|
||||||
mux.Handle("PUT /api/v1/notifications/{id}/read", authMiddleware.HeaderAuthGuard(http.HandlerFunc(notificationHandler.MarkAsRead)))
|
mux.Handle("PUT /api/v1/notifications/{id}/read", authMiddleware.HeaderAuthGuard(http.HandlerFunc(notificationHandler.MarkAsRead)))
|
||||||
|
mux.Handle("PATCH /api/v1/notifications/read-all", authMiddleware.HeaderAuthGuard(http.HandlerFunc(notificationHandler.MarkAllAsRead)))
|
||||||
|
mux.Handle("PATCH /api/v1/notifications/{id}/read", authMiddleware.HeaderAuthGuard(http.HandlerFunc(notificationHandler.MarkAsRead)))
|
||||||
mux.Handle("POST /api/v1/notifications/fcm-token", authMiddleware.HeaderAuthGuard(http.HandlerFunc(notificationHandler.RegisterFCMToken)))
|
mux.Handle("POST /api/v1/notifications/fcm-token", authMiddleware.HeaderAuthGuard(http.HandlerFunc(notificationHandler.RegisterFCMToken)))
|
||||||
|
|
||||||
// Swagger Route - available at /docs
|
// Swagger Route - available at /docs
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue