Implement phone number validation for customer and user registration and updates

Added functionality to check for existing customers and users by phone number during registration and updates. Updated the handler, service, and repository layers to include phone number checks, ensuring unique phone numbers across customers and users.
This commit is contained in:
Mazyar
2026-06-06 12:03:04 +03:30
parent 5af3bfaa1a
commit 0da0c8b8c9
5 changed files with 75 additions and 0 deletions
+19
View File
@@ -77,6 +77,13 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User
return nil, errors.New("username already exists")
}
if form.Phone != nil && *form.Phone != "" {
existingUser, _ = s.repository.GetByPhone(ctx, *form.Phone)
if existingUser != nil {
return nil, errors.New("phone number already exists")
}
}
// Hash password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(form.Password), bcrypt.DefaultCost)
if err != nil {
@@ -185,6 +192,12 @@ func (s *userService) Update(ctx context.Context, id string, form *UpdateUserFor
}
if form.Phone != nil {
if *form.Phone != "" {
existingUser, _ := s.repository.GetByPhone(ctx, *form.Phone)
if existingUser != nil && existingUser.ID.Hex() != id {
return nil, errors.New("phone number already exists")
}
}
user.Phone = form.Phone
}
@@ -574,6 +587,12 @@ func (s *userService) UpdateProfile(ctx context.Context, userID string, form *Up
user.Position = form.Position
}
if form.Phone != nil {
if *form.Phone != "" {
existingUser, _ := s.repository.GetByPhone(ctx, *form.Phone)
if existingUser != nil && existingUser.ID.Hex() != userID {
return errors.New("phone number already exists")
}
}
user.Phone = form.Phone
}
if form.ProfileImage != nil {