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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user