Enhance Customer Update Functionality with Asynchronous Username Change Notification
- Added logic to send an asynchronous email notification when a customer's username is updated, improving user communication regarding account changes. - Introduced a flag to track if the username has changed, ensuring notifications are only sent when necessary. - Enhanced error handling and logging for successful customer updates, maintaining clarity in operational logs.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user