Add AllMarkSeen Functionality to Notification System
- Introduced the AllMarkSeen method in the notification service to mark all notifications as seen for a user, enhancing user experience by allowing bulk actions on notifications. - Implemented the PublicAllMarkSeen handler to handle HTTP requests for marking all notifications as seen, ensuring proper request validation and response handling. - Updated the notification client and SDK to support the new AllMarkSeen functionality, improving the overall API capabilities. - Enhanced API documentation with Swagger comments for the new endpoint, ensuring clarity for API consumers.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user