diff --git a/internal/notification/handler.go b/internal/notification/handler.go index 97e96a7..c8761c2 100644 --- a/internal/notification/handler.go +++ b/internal/notification/handler.go @@ -295,6 +295,33 @@ func (h *NotificationHandler) PublicMarkSeen(c echo.Context) error { return response.Success(c, nil, "Notification marked as seen successfully") } +// AllMarkSeen marks all notifications as seen for a user +// @Summary Mark all notifications as seen for a user +// @Description Mark all notifications as seen for a user +// @Tags Notification +// @Accept json +// @Produce json +// @Param user_id path string true "User ID" +// @Success 200 {object} response.APIResponse +// @Failure 400 {object} response.APIResponse +// @Failure 500 {object} response.APIResponse +// @Security BearerAuth +// @Router /api/v1/notifications/all-seen/{user_id} [get] +func (h *NotificationHandler) PublicAllMarkSeen(c echo.Context) error { + userID, err := customer.GetCustomerIDFromContext(c) + if err != nil { + return response.BadRequest(c, "user ID is required", "User ID must be provided") + } + + // Mark all notifications as seen + err = h.service.AllMarkSeen(c.Request().Context(), userID) + if err != nil { + return response.InternalServerError(c, "Failed to mark all notifications as seen") + } + + return response.Success(c, nil, "All notifications marked as seen successfully") +} + // ViewNotification gets a single notification by ID // @Summary Get notification details // @Description Get detailed information about a specific notification diff --git a/internal/notification/service.go b/internal/notification/service.go index 4ed4210..354cdcb 100644 --- a/internal/notification/service.go +++ b/internal/notification/service.go @@ -20,6 +20,7 @@ type Service interface { GetNotifications(ctx context.Context, req *SearchForm, pagination *response.Pagination) (*NotificationListResponse, error) GetNotification(ctx context.Context, notificationID string) (*NotificationResponse, error) MarkSeen(ctx context.Context, notificationID, userID string) error + AllMarkSeen(ctx context.Context, userID string) error getUsers(ctx context.Context, userIDs []string) ([]notificationRecipient, error) getCustomers(ctx context.Context, values []string, target string) ([]notificationRecipient, error) } @@ -308,6 +309,18 @@ func (s *notificationService) MarkSeen(ctx context.Context, notificationID, user return nil } +func (s *notificationService) AllMarkSeen(ctx context.Context, userID string) error { + s.logger.Info("Marking all notifications as seen", map[string]interface{}{ + "user_id": userID, + }) + + _, err := s.sdk.AllMarkSeen(ctx, userID) + if err != nil { + return err + } + return nil +} + func (s *notificationService) getUsers(ctx context.Context, userIDs []string) ([]notificationRecipient, error) { recipients := make([]notificationRecipient, 0) offset := 0 diff --git a/pkg/notification/client.go b/pkg/notification/client.go index e451f22..6e0cfe8 100644 --- a/pkg/notification/client.go +++ b/pkg/notification/client.go @@ -433,6 +433,65 @@ func (c *Client) MarkSeen(ctx context.Context, notificationID string) (*MarkSeen return &markSeenResp, nil } +// MarkSeen marks a notification as seen +func (c *Client) AllMarkSeen(ctx context.Context, userID string) (*MarkSeenResponse, error) { + if c.config.EnableLogging && c.logger != nil { + c.logger.Debug("Marking notification as seen", map[string]interface{}{ + "user_id": userID, + "base_url": c.config.BaseURL, + }) + } + + // Create HTTP request + url := fmt.Sprintf("%s/api/v1/notifications/all-seen/%s", c.config.BaseURL, userID) + httpReq, err := http.NewRequestWithContext(ctx, "PUT", url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create HTTP request: %w", err) + } + + // Set headers + httpReq.Header.Set("Content-Type", "application/json") + httpReq.Header.Set("User-Agent", c.config.UserAgent) + + // Send request + resp, err := c.httpClient.Do(httpReq) + if err != nil { + return nil, fmt.Errorf("HTTP request failed: %w", err) + } + defer resp.Body.Close() + + // Read response body + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + + // Handle non-2xx status codes + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + // Try to parse error response + var errorResp NotificationErrorResponse + if json.Unmarshal(body, &errorResp) == nil && errorResp.Error != "" { + return nil, MapHTTPError(resp.StatusCode, errorResp.Error) + } + return nil, MapHTTPError(resp.StatusCode, string(body)) + } + + // Parse success response + var markSeenResp MarkSeenResponse + if err := json.Unmarshal(body, &markSeenResp); err != nil { + return nil, fmt.Errorf("failed to unmarshal response: %w", err) + } + + if c.config.EnableLogging && c.logger != nil { + c.logger.Info("Notification marked as seen successfully", map[string]interface{}{ + "user_id": userID, + "status": markSeenResp.Status, + }) + } + + return &markSeenResp, nil +} + // shouldRetry determines if a request should be retried based on the error func (c *Client) shouldRetry(err error) bool { switch e := err.(type) { diff --git a/pkg/notification/interfaces.go b/pkg/notification/interfaces.go index 4865af6..2226b41 100644 --- a/pkg/notification/interfaces.go +++ b/pkg/notification/interfaces.go @@ -30,6 +30,9 @@ type NotificationService interface { // MarkSeen marks a notification as seen MarkSeen(ctx context.Context, notificationID string) (*MarkSeenResponse, error) + // AllMarkSeen marks all notifications as seen for a user + AllMarkSeen(ctx context.Context, userID string) (*MarkSeenResponse, error) + // Health checks if the notification service is available Health(ctx context.Context) error diff --git a/pkg/notification/sdk.go b/pkg/notification/sdk.go index 8e64abe..f9873d1 100644 --- a/pkg/notification/sdk.go +++ b/pkg/notification/sdk.go @@ -89,6 +89,11 @@ func (s *SDK) MarkSeen(ctx context.Context, notificationID string) (*MarkSeenRes return s.service.MarkSeen(ctx, notificationID) } +// AllMarkSeen marks all notifications as seen for a user +func (s *SDK) AllMarkSeen(ctx context.Context, userID string) (*MarkSeenResponse, error) { + return s.service.AllMarkSeen(ctx, userID) +} + // NewBuilder creates a new notification builder for fluent API usage func (s *SDK) NewBuilder() Builder { return s.service.(*Service).NewBuilder() diff --git a/pkg/notification/service.go b/pkg/notification/service.go index 510b64f..074dc5a 100644 --- a/pkg/notification/service.go +++ b/pkg/notification/service.go @@ -119,6 +119,11 @@ func (s *Service) MarkSeen(ctx context.Context, notificationID string) (*MarkSee return s.client.MarkSeen(ctx, notificationID) } +// AllMarkSeen marks all notifications as seen for a user +func (s *Service) AllMarkSeen(ctx context.Context, userID string) (*MarkSeenResponse, error) { + return s.client.AllMarkSeen(ctx, userID) +} + // Health checks if the notification service is available func (s *Service) Health(ctx context.Context) error { // Create a simple test request to check service availability