From 20aee91467c4733775f32919fd2b0a97df33d61f Mon Sep 17 00:00:00 2001 From: "n.nakhostin" Date: Wed, 1 Oct 2025 13:19:34 +0330 Subject: [PATCH] Refactor Customer and User Repository Methods for ObjectID Handling - Updated GetByIDs methods in customer and user repositories to convert string IDs to bson.ObjectID, ensuring proper handling of MongoDB object IDs. - Enhanced error handling for ID conversion to provide clearer feedback when invalid IDs are provided. - Modified notification service calls in customer and user services to use context.Background() for asynchronous email sending, improving concurrency management. - Ensured consistent handling of company IDs in the customer repository for retrieving customers by companies. --- internal/customer/repository.go | 22 ++++++++++++++++++++-- internal/customer/service.go | 8 ++++---- internal/notification/handler.go | 4 +++- internal/notification/service.go | 4 ++++ internal/user/repository.go | 11 ++++++++++- internal/user/service.go | 6 +++--- 6 files changed, 44 insertions(+), 11 deletions(-) diff --git a/internal/customer/repository.go b/internal/customer/repository.go index afea8d7..1dd3dd8 100644 --- a/internal/customer/repository.go +++ b/internal/customer/repository.go @@ -178,7 +178,16 @@ func (r *customerRepository) GetByUsername(ctx context.Context, username string) // GetByIDs retrieves customers by IDs func (r *customerRepository) GetByIDs(ctx context.Context, ids []string) ([]Customer, error) { - customers, err := r.ormRepo.FindAll(ctx, bson.M{"_id": bson.M{"$in": ids}}, orm.NewPaginationBuilder().Limit(len(ids)).Build()) + objectIDs := make([]bson.ObjectID, 0) + for _, id := range ids { + objectID, err := bson.ObjectIDFromHex(id) + if err != nil { + return nil, err + } + objectIDs = append(objectIDs, objectID) + } + + customers, err := r.ormRepo.FindAll(ctx, bson.M{"_id": bson.M{"$in": objectIDs}}, orm.NewPaginationBuilder().Limit(len(ids)).Build()) if err != nil { if errors.Is(err, orm.ErrDocumentNotFound) { return nil, errors.New("customers not found") @@ -211,7 +220,16 @@ func (r *customerRepository) GetByRole(ctx context.Context, role CustomerRole) ( // GetByCompanies retrieves customers by companies func (r *customerRepository) GetByCompanies(ctx context.Context, companies []string) ([]Customer, error) { - customers, err := r.ormRepo.FindAll(ctx, bson.M{"companies": bson.M{"$in": companies}}, orm.NewPaginationBuilder().Build()) + companyIDs := make([]bson.ObjectID, 0) + for _, company := range companies { + id, err := bson.ObjectIDFromHex(company) + if err != nil { + return nil, err + } + companyIDs = append(companyIDs, id) + } + + customers, err := r.ormRepo.FindAll(ctx, bson.M{"companies": bson.M{"$in": companyIDs}}, orm.NewPaginationBuilder().Build()) if err != nil { return nil, err } diff --git a/internal/customer/service.go b/internal/customer/service.go index eb0d11a..c3635bf 100644 --- a/internal/customer/service.go +++ b/internal/customer/service.go @@ -152,7 +152,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm "company_ids": Companies, }) - s.notification.SendEmail(ctx, notification.Model{ + go s.notification.SendEmail(context.Background(), notification.Model{ Recipient: customer.Email, Title: "Welcome to Opplens", Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", strings.ToLower(customer.Username), form.Password), @@ -428,7 +428,7 @@ func (s *customerService) UpdateStatus(ctx context.Context, id string, form *Upd "status": form.Status, }) - s.notification.SendEmail(ctx, notification.Model{ + go s.notification.SendEmail(context.Background(), notification.Model{ Recipient: customer.Email, Title: "Account Status Updated", Message: fmt.Sprintf("Your account has been %s", strings.ToUpper(form.Status)), @@ -888,7 +888,7 @@ func (s *customerService) RequestResetPassword(ctx context.Context, form *Reques } // Send OTP via email - s.notification.SendEmail(ctx, notification.Model{ + go s.notification.SendEmail(context.Background(), notification.Model{ Recipient: form.Email, Title: "Password Reset", Message: fmt.Sprintf("Your password reset code is: %s", otpCode), @@ -1083,7 +1083,7 @@ func (s *customerService) ResetPassword(ctx context.Context, form *ResetPassword "customer_id": customer.ID, }) - s.notification.SendEmail(ctx, notification.Model{ + go s.notification.SendEmail(context.Background(), notification.Model{ Recipient: tokenData.Email, Title: "Password Reset Success", Message: "Your password has been reset successfully", diff --git a/internal/notification/handler.go b/internal/notification/handler.go index ba173a2..ede2ad4 100644 --- a/internal/notification/handler.go +++ b/internal/notification/handler.go @@ -1,6 +1,8 @@ package notification import ( + "context" + "github.com/asaskevich/govalidator" "github.com/labstack/echo/v4" @@ -48,7 +50,7 @@ func (h *NotificationHandler) Send(c echo.Context) error { } // Send notification - h.service.Send(c.Request().Context(), &req) + go h.service.Send(context.Background(), &req) return response.Created(c, nil, "Notification sent successfully") } diff --git a/internal/notification/service.go b/internal/notification/service.go index 9cad95f..b45f8c1 100644 --- a/internal/notification/service.go +++ b/internal/notification/service.go @@ -397,6 +397,10 @@ func (s *notificationService) getCustomers(ctx context.Context, values []string, recipients := make([]notificationRecipient, 0) if target == string(TargetAudienceSpecificRole) { + if len(values) == 0 { + return nil, errors.New("no role provided") + } + customers, err := s.customerService.GetCustomersByRole(ctx, values[0]) if err != nil { return nil, err diff --git a/internal/user/repository.go b/internal/user/repository.go index 0aa960e..88417db 100644 --- a/internal/user/repository.go +++ b/internal/user/repository.go @@ -218,7 +218,16 @@ func (r *userRepository) Delete(ctx context.Context, id string) error { // GetByIDs retrieves users by IDs func (r *userRepository) GetByIDs(ctx context.Context, userIDs []string) ([]User, error) { - users, err := r.ormRepo.FindAll(ctx, bson.M{"_id": bson.M{"$in": userIDs}}, orm.NewPaginationBuilder().Limit(len(userIDs)).Build()) + objectIDs := make([]bson.ObjectID, 0) + for _, id := range userIDs { + objectID, err := bson.ObjectIDFromHex(id) + if err != nil { + return nil, err + } + objectIDs = append(objectIDs, objectID) + } + + users, err := r.ormRepo.FindAll(ctx, bson.M{"_id": bson.M{"$in": objectIDs}}, orm.NewPaginationBuilder().Limit(len(userIDs)).Build()) if err != nil { return nil, err } diff --git a/internal/user/service.go b/internal/user/service.go index 15dc7f4..20d6e20 100644 --- a/internal/user/service.go +++ b/internal/user/service.go @@ -118,7 +118,7 @@ func (s *userService) Register(ctx context.Context, form *CreateUserForm) (*User "role": user.Role, }) - s.notification.SendEmail(ctx, notification.Model{ + go s.notification.SendEmail(context.Background(), notification.Model{ Recipient: form.Email, Title: "Welcome to Opplens", Message: fmt.Sprintf("Welcome to Opplens\nUsername: %s\nPassword: %s", strings.ToLower(form.Username), form.Password), @@ -229,7 +229,7 @@ func (s *userService) UpdateStatus(ctx context.Context, id string, form *UpdateU "status": form.Status, }) - s.notification.SendEmail(ctx, notification.Model{ + go s.notification.SendEmail(context.Background(), notification.Model{ Recipient: user.Email, Title: "Account Status Updated", Message: fmt.Sprintf("Your account has been %s", strings.ToUpper(form.Status)), @@ -495,7 +495,7 @@ func (s *userService) ChangePassword(ctx context.Context, userID string, form *C "user_id": userID, }) - s.notification.SendEmail(ctx, notification.Model{ + go s.notification.SendEmail(context.Background(), notification.Model{ Recipient: user.Email, Title: "Password Reset Success", Message: "Your password has been reset successfully",