diff --git a/internal/customer/service.go b/internal/customer/service.go index c3635bf..7c68b7a 100644 --- a/internal/customer/service.go +++ b/internal/customer/service.go @@ -322,6 +322,7 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus return nil, errors.New("invalid username format") } + changeUsername := false // 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)) @@ -329,6 +330,7 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus return nil, errors.New("customer with this username already exists") } customer.Username = strings.ToLower(form.Username) + changeUsername = true } // Handle company assignments @@ -366,6 +368,16 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus return nil, errors.New("failed to update customer") } + if changeUsername { + go s.notification.SendEmail(context.Background(), notification.Model{ + Recipient: customer.Email, + Title: "Username Updated", + Message: fmt.Sprintf("Your username has been updated to %s", customer.Username), + Type: "info", + Priority: "important", + UserID: customer.ID.Hex(), + }) + } s.logger.Info("Customer updated successfully", map[string]interface{}{ "customer_id": customer.ID, "email": customer.Email,