Skip to content

Feat: Fetch All Subscribers to Newsletter #683

@Scepter00

Description

@Scepter00

Description: Fetch All Subscribers to Newsletter API Implementation

Overview:

This API endpoint allows authorized users (e.g., administrators) to retrieve a complete list of subscribers who have signed up for the newsletter. The response will include relevant subscriber details such as email and subscription date, enabling efficient management and communication.

Endpoint Details:

**Method: GET

  • URL: /api/v1/newsletter/subscribers

  • Authentication: Required (JWT token)**

Request Parameters:

  • Optional Query Parameters:

page (integer, optional): Page number for pagination.
size (integer, optional): Number of subscribers per page.

Simplified Sample Request:

GET /api/v1/newsletter/subscribers
Authorization: Bearer <your_jwt_token>
Content-Type: application/json

Responses:

  • 200 OK:
{
  "subscribers": [
    {
      "id": 1,
      "email": "[email protected]",
      "subscribedAt": "2021-01-01T00:00:00Z"
    },
    {
      "id": 2,
      "email": "[email protected]",
      "subscribedAt": "2021-02-15T12:30:00Z"
    }
  ],
  "page": 1,
  "size": 20,
  "totalElements": 150
}

401 Unauthorized:

{
  "error": "User authentication failed. Please provide a valid token."
}

400 Bad Request: (if query parameters are invalid)

{
  "error": "Invalid pagination parameters. Please check your request."
}

500 Internal Server Error:

{
  "error": "An unexpected error occurred while processing your request. Please try again later."
}

Security Considerations:

  • Enforce HTTPS to protect data in transit.
  • Restrict endpoint access to authenticated and authorized users only.
  • Implement rate limiting to prevent abuse of the endpoint.
  • Validate query parameters to ensure robustness.

Implementation Steps:

  1. Authenticate the User:
    Verify the JWT token provided in the request header.

  2. Fetch Subscriber Data:
    Retrieve subscriber records from the database, applying pagination if provided.

  3. Map Data to Response Model:
    Convert database records to a JSON-friendly response model containing subscriber details.

  4. Return Response:
    Send the list of subscribers along with pagination metadata.

  5. Error Handling:
    Handle authentication failures, invalid requests, and unexpected errors appropriately.

Testing:

Unit Tests:
Validate the logic for retrieving subscribers and mapping data correctly.

Integration Tests:
Ensure the endpoint responds with the proper HTTP status codes and payloads under various scenarios (authenticated, unauthorized, invalid parameters, etc.).

Security Tests:
Confirm that only authorized users can access the endpoint and that rate limiting is enforced.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions