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:
@@ -95,6 +95,13 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
||||
return nil, errors.New("customer with this username already exists")
|
||||
}
|
||||
|
||||
if form.Phone != nil && *form.Phone != "" {
|
||||
existingCustomer, _ = s.repository.GetByPhone(ctx, *form.Phone)
|
||||
if existingCustomer != nil {
|
||||
return nil, errors.New("customer with this phone number already exists")
|
||||
}
|
||||
}
|
||||
|
||||
// Check validator
|
||||
if !s.validator.IsValidUsername(form.Username) {
|
||||
return nil, errors.New("invalid username format")
|
||||
@@ -373,6 +380,12 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus
|
||||
}
|
||||
|
||||
if form.Phone != nil && (customer.Phone == nil || *form.Phone != *customer.Phone) {
|
||||
if *form.Phone != "" {
|
||||
existingCustomer, _ := s.repository.GetByPhone(ctx, *form.Phone)
|
||||
if existingCustomer != nil && existingCustomer.ID.Hex() != id {
|
||||
return nil, errors.New("customer with this phone number already exists")
|
||||
}
|
||||
}
|
||||
customer.Phone = form.Phone
|
||||
infoChanged = true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user