From fdcf04abfbe0d03ba46309fe260afee4ed10e792 Mon Sep 17 00:00:00 2001 From: "n.nakhostin" Date: Wed, 1 Oct 2025 13:23:16 +0330 Subject: [PATCH] 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. --- internal/customer/service.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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,