|
| 1 | +# Subscription API |
| 2 | + |
| 3 | +## Endpoints |
| 4 | + |
| 5 | +| Route | Method | Description | |
| 6 | +|-------|--------|-------------| |
| 7 | +| [/subscription](#post-subscription) | POST | Subscribe a user | |
| 8 | +| [/subscription](#patch-subscription) | PATCH | Unsubscribe a user | |
| 9 | +| [/subscription/notify](#get-subscriptionnotify) | GET | Send a test email | |
| 10 | + |
| 11 | +## POST /subscription |
| 12 | + |
| 13 | +Subscribe a user to notifications. |
| 14 | + |
| 15 | +- **Authentication**: Required |
| 16 | +- **Authorization**: Any authenticated user |
| 17 | + |
| 18 | +### Request |
| 19 | + |
| 20 | +- **Body**: |
| 21 | + - `email` (string, required): User's email address |
| 22 | + - `phoneNumber` (string, optional): User's phone number |
| 23 | + |
| 24 | +### Response |
| 25 | + |
| 26 | +- **Success Response**: |
| 27 | + - **Code**: 201 |
| 28 | + - **Content**: |
| 29 | + ```json |
| 30 | + { |
| 31 | + "message": "user subscribed successfully" |
| 32 | + } |
| 33 | + ``` |
| 34 | + |
| 35 | +- **Error Response**: |
| 36 | + - **Code**: 500 |
| 37 | + - **Content**: |
| 38 | + ```json |
| 39 | + { |
| 40 | + "statusCode": 500, |
| 41 | + "error": "Internal Server Error", |
| 42 | + "message": "An internal server error occurred" |
| 43 | + } |
| 44 | + ``` |
| 45 | + |
| 46 | +## PATCH /subscription |
| 47 | + |
| 48 | +Unsubscribe a user from notifications. |
| 49 | + |
| 50 | +- **Authentication**: Required |
| 51 | +- **Authorization**: Any authenticated user |
| 52 | + |
| 53 | +### Request |
| 54 | + |
| 55 | +- **Body**: None required |
| 56 | + |
| 57 | +### Response |
| 58 | + |
| 59 | +- **Success Response**: |
| 60 | + - **Code**: 200 |
| 61 | + - **Content**: |
| 62 | + ```json |
| 63 | + { |
| 64 | + "message": "user unsubscribed successfully" |
| 65 | + } |
| 66 | + ``` |
| 67 | + |
| 68 | +- **Error Response**: |
| 69 | + - **Code**: 500 |
| 70 | + - **Content**: |
| 71 | + ```json |
| 72 | + { |
| 73 | + "statusCode": 500, |
| 74 | + "error": "Internal Server Error", |
| 75 | + "message": "An internal server error occurred" |
| 76 | + } |
| 77 | + ``` |
| 78 | + |
| 79 | +## GET /subscription/notify |
| 80 | + |
| 81 | +Send a test email. This endpoint is for development and testing purposes only. |
| 82 | + |
| 83 | +- **Authentication**: Required |
| 84 | +- **Authorization**: SUPERUSER role only |
| 85 | + |
| 86 | +### Request |
| 87 | + |
| 88 | +- **Body**: None required |
| 89 | + |
| 90 | +### Response |
| 91 | + |
| 92 | +- **Success Response**: |
| 93 | + - **Code**: 200 |
| 94 | + - **Content**: |
| 95 | + ```json |
| 96 | + { |
| 97 | + "message": "Email sent", |
| 98 | + "info": { |
| 99 | + // Email sending information |
| 100 | + } |
| 101 | + } |
| 102 | + ``` |
| 103 | + |
| 104 | +- **Error Response**: |
| 105 | + - **Code**: 500 |
| 106 | + - **Content**: |
| 107 | + ```json |
| 108 | + { |
| 109 | + "message": "Failed to send email", |
| 110 | + "error": { |
| 111 | + // Error details |
| 112 | + } |
| 113 | + } |
| 114 | + ``` |
| 115 | + |
| 116 | +## Notes |
| 117 | + |
| 118 | +1. All routes require authentication. |
| 119 | +2. The `/subscription/notify` route is restricted to users with the SUPERUSER role. |
| 120 | +3. Error handling for authentication and authorization failures is not explicitly defined in the provided code, but would typically result in 401 (Unauthorized) or 403 (Forbidden) responses. |
| 121 | +4. The email sending functionality uses nodemailer and requires proper configuration of SMTP credentials. |
| 122 | +5. The API uses Express.js and includes middleware for authentication and role-based authorization. |
| 123 | + |
| 124 | + |
| 125 | +Common error codes: |
| 126 | +- 400: Bad Request (e.g., invalid input) |
| 127 | +- 401: Unauthorized (authentication required) |
| 128 | +- 403: Forbidden (insufficient permissions) |
| 129 | +- 500: Internal Server Error |
| 130 | + |
| 131 | +For detailed error messages and handling, please refer to the API implementation. |
0 commit comments