From 534213f10e9c8fd22ac949a07ec307c7922ad641 Mon Sep 17 00:00:00 2001 From: Mazyar Date: Sun, 28 Jun 2026 11:04:24 +0330 Subject: [PATCH] Enhance notification search functionality with user ID filtering - Added a new `UserID` field to the `SearchForm` to allow filtering notifications by user ID. - Implemented the `ResolvedRecipients` method to return user IDs based on the `user_id` or `recipient` query parameters, improving the flexibility of notification searches. - Updated the `GetNotifications` method in the service layer to utilize the new recipient resolution logic, ensuring accurate retrieval of notifications based on user ID. This update enhances the notification management capabilities, providing more granular control over notification searches. --- internal/notification/form.go | 9 +++++++++ internal/notification/handler.go | 1 + internal/notification/service.go | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/notification/form.go b/internal/notification/form.go index 1a15eb5..0b8c71e 100644 --- a/internal/notification/form.go +++ b/internal/notification/form.go @@ -30,6 +30,7 @@ type SearchForm struct { EventType string `query:"event_type" valid:"optional"` Type string `query:"type" valid:"optional"` Recipient []string `query:"recipient" json:"recipient" valid:"optional"` + UserID string `query:"user_id" valid:"optional"` Seen *bool `query:"seen" valid:"optional"` Priority string `query:"priority" valid:"optional"` } @@ -45,6 +46,14 @@ func (f *SearchForm) ResolvedSearch() string { return "" } +// ResolvedRecipients returns the user IDs to filter by from user_id or recipient query params. +func (f *SearchForm) ResolvedRecipients() []string { + if id := strings.TrimSpace(f.UserID); id != "" { + return []string{id} + } + return f.Recipient +} + type NotificationListResponse struct { Notifications []*NotificationResponse `json:"notifications"` Meta *response.Meta `json:"meta"` diff --git a/internal/notification/handler.go b/internal/notification/handler.go index f1fc528..ea4a024 100644 --- a/internal/notification/handler.go +++ b/internal/notification/handler.go @@ -65,6 +65,7 @@ func (h *NotificationHandler) Send(c echo.Context) error { // @Param event_type query string false "Event type filter" // @Param type query string false "Type filter" // @Param recipient query string false "Recipient filter" +// @Param user_id query string false "User ID filter" // @Param seen query bool false "Seen filter" // @Param priority query string false "Priority filter" // @Param limit query int false "Limit results" diff --git a/internal/notification/service.go b/internal/notification/service.go index 7f89a11..e14e072 100644 --- a/internal/notification/service.go +++ b/internal/notification/service.go @@ -197,14 +197,14 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF "method": req.Method, "event_type": req.EventType, "type": req.Type, - "recipient": req.Recipient, + "recipient": req.ResolvedRecipients(), "seen": req.Seen, "priority": req.Priority, "limit": pagination.Limit, "offset": pagination.Offset, }) - page, err := s.repository.GetByUserID(ctx, req.Recipient, search, req.Status, req.Seen, req.Priority, req.EventType, req.Type, pagination) + page, err := s.repository.GetByUserID(ctx, req.ResolvedRecipients(), search, req.Status, req.Seen, req.Priority, req.EventType, req.Type, pagination) if err != nil { s.logger.Error("Failed to get notifications", map[string]interface{}{ "error": err.Error(),