Enhance Customer Module with Validation and Username Handling

- Introduced a new ValidationService for customer operations to ensure valid username formats.
- Updated customer service and handler to utilize the new validation service, enhancing input validation during registration and updates.
- Modified CreateCustomerForm and UpdateCustomerForm to enforce username validation rules.
- Adjusted repository indexing to improve search efficiency by refining the text index for customer data.
- Ensured consistent handling of usernames across the customer module, improving overall data integrity and user experience.
This commit is contained in:
n.nakhostin
2025-09-24 12:57:23 +03:30
parent 080c1d18e5
commit 939f940499
6 changed files with 65 additions and 9 deletions
+13 -1
View File
@@ -60,10 +60,11 @@ type customerService struct {
companyService company.Service
redisClient redis.Client
notification notification.SDK
validator ValidationService
}
// New creates a new customer service
func NewService(repository Repository, logger logger.Logger, authService authorization.AuthorizationService, companyService company.Service, redisClient redis.Client, notificationSDK notification.SDK) Service {
func NewService(repository Repository, logger logger.Logger, authService authorization.AuthorizationService, companyService company.Service, redisClient redis.Client, notificationSDK notification.SDK, validator ValidationService) Service {
return &customerService{
repository: repository,
logger: logger,
@@ -71,6 +72,7 @@ func NewService(repository Repository, logger logger.Logger, authService authori
companyService: companyService,
redisClient: redisClient,
notification: notificationSDK,
validator: validator,
}
}
@@ -82,6 +84,11 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
return nil, errors.New("customer with this email already exists")
}
// Check validator
if !s.validator.IsValidUsername(form.Username) {
return nil, errors.New("invalid username format")
}
// Verify that all company IDs exist
var Companies []string
if len(form.Companies) > 0 {
@@ -307,6 +314,11 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus
customer.Email = form.Email
}
// Check validator
if !s.validator.IsValidUsername(form.Username) {
return nil, errors.New("invalid username format")
}
// Check if username already exists (if changing username)
if form.Username != "" && strings.ToLower(form.Username) != customer.Username {
existingCustomer, _ := s.repository.GetByUsername(ctx, strings.ToLower(form.Username))