From 080c1d18e5a366f20e13e4f3930b6de3c2a0954a Mon Sep 17 00:00:00 2001 From: "n.nakhostin" Date: Wed, 24 Sep 2025 12:29:49 +0330 Subject: [PATCH] Normalize Username Handling in Customer and User Services - Updated the Register, Update, and Login methods in both customer and user services to ensure usernames are consistently stored and processed in lowercase. - Modified notification messages to reflect the lowercase username format, enhancing clarity and consistency in user communications. - Improved username existence checks during registration and updates to prevent case-sensitive duplicates, ensuring a more robust user experience. --- internal/customer/service.go | 18 +++++++++--------- internal/user/service.go | 12 ++++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/internal/customer/service.go b/internal/customer/service.go index 1b69721..8607e8b 100644 --- a/internal/customer/service.go +++ b/internal/customer/service.go @@ -114,7 +114,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm Status: CustomerStatusActive, Companies: Companies, FullName: form.FullName, - Username: form.Username, + Username: strings.ToLower(form.Username), Email: form.Email, Password: string(hashedPassword), Phone: form.Phone, @@ -148,7 +148,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm s.notification.SendEmail(ctx, notification.Model{ Recipient: customer.Email, Title: "Welcome to Opplens", - Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", customer.Username, form.Password), + Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", strings.ToLower(customer.Username), form.Password), Type: "info", Priority: "important", UserID: customer.ID.Hex(), @@ -308,12 +308,12 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus } // Check if username already exists (if changing username) - if form.Username != "" && form.Username != customer.Username { - existingCustomer, _ := s.repository.GetByUsername(ctx, form.Username) + if form.Username != "" && strings.ToLower(form.Username) != customer.Username { + existingCustomer, _ := s.repository.GetByUsername(ctx, strings.ToLower(form.Username)) if existingCustomer != nil { return nil, errors.New("customer with this username already exists") } - customer.Username = form.Username + customer.Username = strings.ToLower(form.Username) } // Handle company assignments @@ -568,14 +568,14 @@ func (s *customerService) Activate(ctx context.Context, id string) error { // Login handles customer authentication func (s *customerService) Login(ctx context.Context, form *LoginForm) (*AuthResponse, error) { s.logger.Info("Customer login attempt", map[string]interface{}{ - "username": form.Username, + "username": strings.ToLower(form.Username), }) // Get customer by username - customer, err := s.repository.GetByUsername(ctx, form.Username) + customer, err := s.repository.GetByUsername(ctx, strings.ToLower(form.Username)) if err != nil { s.logger.Warn("Customer login failed - username not found", map[string]interface{}{ - "username": form.Username, + "username": strings.ToLower(form.Username), }) return nil, errors.New("invalid credentials") } @@ -583,7 +583,7 @@ func (s *customerService) Login(ctx context.Context, form *LoginForm) (*AuthResp // Check if customer is active if customer.Status != CustomerStatusActive { s.logger.Warn("Customer login failed - account not active", map[string]interface{}{ - "username": form.Username, + "username": strings.ToLower(form.Username), "customer_id": customer.ID, "status": string(customer.Status), }) diff --git a/internal/user/service.go b/internal/user/service.go index ecbff30..a3a3952 100644 --- a/internal/user/service.go +++ b/internal/user/service.go @@ -65,7 +65,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User } // Check if username already exists - existingUser, _ = s.repository.GetByUsername(ctx, form.Username) + existingUser, _ = s.repository.GetByUsername(ctx, strings.ToLower(form.Username)) if existingUser != nil { return nil, errors.New("username already exists") } @@ -88,7 +88,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User // Create user user := &User{ FullName: form.FullName, - Username: form.Username, + Username: strings.ToLower(form.Username), Email: form.Email, Password: string(hashedPassword), Role: role, @@ -121,7 +121,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User s.notification.SendEmail(ctx, notification.Model{ Recipient: form.Email, Title: "Welcome to Opplens", - Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", form.Username, form.Password), + Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", strings.ToLower(form.Username), form.Password), Type: "info", Priority: "important", UserID: user.ID.Hex(), @@ -145,11 +145,11 @@ func (s *userService) Update(ctx context.Context, id string, form *UpdateUserFor if form.Username != nil { // Check if username already exists - existingUser, _ := s.repository.GetByUsername(ctx, *form.Username) + existingUser, _ := s.repository.GetByUsername(ctx, strings.ToLower(*form.Username)) if existingUser != nil && existingUser.ID.Hex() != id { return nil, errors.New("username already exists") } - user.Username = *form.Username + user.Username = strings.ToLower(*form.Username) } if form.Email != nil { @@ -318,7 +318,7 @@ func (s *userService) Login(ctx context.Context, form *LoginForm) (*AuthResponse if strings.Contains(form.Username, "@") { user, err = s.repository.GetByEmail(ctx, form.Username) } else { - user, err = s.repository.GetByUsername(ctx, form.Username) + user, err = s.repository.GetByUsername(ctx, strings.ToLower(form.Username)) } if err != nil {