From 4e8675df459bfe4a46345cf5c0c7715a5fa38a22 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 25 Sep 2024 14:07:33 +0800 Subject: [PATCH 1/3] Remove API endpoints from docs, link to swagger docs --- README.md | 1856 +---------------------------------------------------- 1 file changed, 5 insertions(+), 1851 deletions(-) diff --git a/README.md b/README.md index 3c96a0af4..28851a0f4 100644 --- a/README.md +++ b/README.md @@ -90,45 +90,7 @@ Also check other developer and contributor-friendly projects of BlueWave: - [Database](#databases) - [(Optional) Dockerised Databases](#optional-docker-databases) - [Start Server](#start-server) -1. [Endpoints](#endpoints) - ###### Auth - - POST [/api/v1/auth/register](#post-register) - - POST [/api/v1/auth/login](#post-login) - - PUT [/api/v1/auth/user/{userId}](#post-auth-user-edit-id) - - GET [/api/v1/auth/users](#get-all-users-id) - - GET [/api/v1/auth/users/admin](#get-all-admin-users-id) - - POST [/api/v1/auth/invite](#post-auth-invite-id) - - POST [/api/v1/auth/invite/verify/](#post-auth-invite-verify-id) - - POST [/api/v1/auth/recovery/request](#post-auth-recovery-request-id) - - POST [/api/v1/auth/recovery/validate](#post-auth-recovery-validate-id) - - POST [/api/v1/auth/recovery/reset](#post-auth-recovery-reset-id) - ###### Monitors - - GET [/api/v1/monitors](#get-monitors) - - GET [/api/v1/monitors/{monitorId}](#get-monitor-id) - - GET [/api/v1/monitors/user/{userId}?limit](#get-monitors-user-userid) - - POST [/api/v1/monitors](#post-monitors) - - PUT [/api/v1/monitors/{monitorId}](#put-monitors-id) - - DELETE [/api/v1/monitors/{monitorId}](#delete-monitors-id) - - DELETE [/api/v1/monitors/all](#delete-all-monitors) - ###### Checks - - POST [/api/v1/checks/{monitorId}](#post-checks) - - GET [/api/v1/checks/{monitorId}](#get-checks) - - DELETE [/api/v1/checks/{monitorId}](#delete-checks) - ###### Alerts - - POST [/api/v1/alerts/{monitorId}](#create-alert) - - GET [/api/v1/alerts/user/{userId}](#get-alerts-user-id) - - GET [/api/v1/alerts/monitor/{monitorId}](#get-alerts-monitor-id) - - GET [/api/v1/alerts/{alertId}](#get-alert-alert-id) - - PUT [/api/v1/alerts/{alertId}](#edit-alert) - - DELETE [/api/v1/alerts/{alertId}](#delete-alert) - ###### Page Speed - - POST [/api/v1/pagespeed/:monitorId](#post-pagespeed) - - GET [/api/v1/pagespeed/:monitorId](#get-pagespeed) - - DELETE [/api/v1/pagespeed/:monitorId](#delete-pagespeed) - ###### Maintenance Window - - POST [/api/v1/maintenance-window/:monitorId](#post-maintenance-window) - - GET [/api/v1/maintenance-window/user/:userId](#get-maintenance-window-by-userid) - - GET [/api/v1/maintenance-window/monitor/:monitorId](#get-maintenance-window-by-monitor-id) +1. [API Documentation](#api-documentation) 1. [Error Handling](#error-handling) 1. [Contributors](#contributors) @@ -332,1821 +294,13 @@ OR --- -#### Endpoints +#### API Documentation -All endpoints return a response in this format: +Our API is documented in accordance with the [OpenAPI spec](https://www.openapis.org/). -| Name | Type | Notes | -| ------- | --------- | ----------------------------- | -| success | `boolean` | Success or failure of request | -| msg | `string` | Message describing response | -| data | `Object` | Arbitrary Payload | +You can see the documentation on your local development server at [http://localhost:{port}/api-docs](http://localhost:5000/api-docs) -Example: - -``` -{success: true, msg: "Successful Request", data: {test: testData}} -``` - ---- - -##### Data Types - -
-User - -| Name | Type | Notes | -| ------------- | --------- | --------------------- | -| firstname | `string` | First name | -| lastname | `string` | Last name | -| email | `string` | User's email | -| profilePicUrl | `string` | URL to User's picture | -| isActive | `boolean` | Default to `true` | -| isVerified | `boolean` | Default to `false` | -| updated_at | `Date` | Last update time | -| created_at | `Date` | Time created at | - -
- -
-Monitor - -| Name | Type | Notes | -| ----------- | --------- | ---------------------------------------- | -| userId | `string` | Unique ID identifying monitor creator | -| name | `string` | Name of the monitor | -| description | `string` | Description of the monitor | -| url | `string` | Url the monitor will ping | -| isActive | `boolean` | Whether or not the monitor is active | -| interval | `integer` | Interval with which to ping monitor (ms) | -| updatedAt | `Date` | Last time the monitor was updated | -| CreatedAt | `Date` | When the monitor was updated | - -
- -
-Check - -| Name | Type | Notes | -| ------------ | --------- | ----------------------------------------------- | -| monitorId | `string` | Unique ID for the monitor | -| status | `boolean` | Indicates the service is Up or Down | -| responseTime | `integer` | Indicates the response time of the service (ms) | -| statusCode | `integer` | Status Code returned from the service | -| message | `string` | Message returned from the service | -| updatedAt | `Date` | Last time the check was updated | -| CreatedAt | `Date` | When the check was created | - -
- -
-Alert - -| Name | Type | Notes | -| ----------------- | --------- | ------------------------------------------------- | -| checkId | `string` | Unique ID for the associated check | -| monitorId | `string` | Unique ID for the associated monitor | -| userId | `string` | Unique ID for the associated user | -| status | `boolean` | Indicates the service is Up or Down | -| message | `string` | Message for the user about the down service | -| notifiedStatus | `boolean` | Indicates whether the user is notified | -| acknowledgeStatus | `boolean` | Indicates whether the user acknowledged the alert | -| updatedAt | `Date` | Last time the alert was updated | -| CreatedAt | `Date` | When the alert was created | - -
- ---- - -###### Auth - -
-POST /api/v1/auth/register - -##### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ------------------- | -> | Method | POST | -> | content-type | multipart/form-data | - -##### Form - -> | Name | Type | Notes | -> | --------- | --------------- | ----------------------------------------------- | -> | firstName | `string` | | -> | lastName | `string` | | -> | email | `string` | Valid email address | -> | password | `string` | Min 8 chars, One Upper, one number, one special | -> | role | `Array` | Array of user roles | - -##### Response Payload - -> | Type | Notes | -> | ---- | -------------- | -> | User | User data | -> | JWT | JSON web token | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/auth/register \ - --header 'Content-Type: multipart/form-data' \ - --form firstName=Alex \ - --form lastName=Hollidaty \ - --form email=ajhollid@gmail.com \ - --form 'password=Testtest1!' \ - --form 'role[]=admin' -``` - -##### Sample Response - -```json -{ - "success": true, - "msg": "User created successfully", - "data": { - "user": { - "_id": "66a1425b873da2207443f192", - "firstName": "First Name", - "lastName": "Last Name", - "email": "name@gmail.com", - "isActive": true, - "isVerified": false, - "role": ["admin"], - "createdAt": "2024-07-24T18:05:15.852Z", - "updatedAt": "2024-07-24T18:05:15.852Z", - "__v": 0 - }, - "token": "" - } -} -``` - -
- -
-POST /api/v1/auth/login - -##### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | POST | -> | content-type | application/json | - -##### Body - -> | Name | Type | Notes | -> | -------- | -------- | ------------------- | -> | email | `string` | Valid email address | -> | password | `string` | | - -##### Response Payload - -> | Type | Notes | -> | ---- | -------------- | -> | User | User data | -> | JWT | JSON web token | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/auth/login \ - --header 'Authorization: Bearer undefined' \ - --header 'Content-Type: application/json' \ - --data '{ - "email" : "name@gmail.com", - "password": "Testtest1!" -}' -``` - -##### Sample response - -```json -{ -{ - "success": true, - "msg": "User logged in successfully", - "data": { - "user": { - "_id": "66a1425b873da2207443f192", - "firstName": "First Name", - "lastName": "Last Name", - "email": "name@gmail.com", - "isActive": true, - "isVerified": false, - "role": ["admin"], - "createdAt": "2024-07-24T18:05:15.852Z", - "updatedAt": "2024-07-24T18:05:15.852Z", - "__v": 0 - }, - "token": "" - } -} -} -``` - -
- -
-POST/api/v1/auth/user/{userId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ------------------- | -> | Method | POST | -> | content-type | multipart/form-data | - -##### Form - -> | Name | Type | Notes | -> | ----------- | -------- | --------------------------- | -> | firstName | `string` | Optional | -> | lastName | `string` | Optional | -> | profileIame | `file` | Optional | -> | password | `string` | Required to change password | -> | newPassword | `string` | Required to change password | - -###### Response Payload - -> | Type | Notes | -> | ------ | ------------------------ | -> | `User` | Returns the updated user | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/auth/user/66a1425b873da2207443f192 \ - --header 'Authorization: ' \ - --header 'Content-Type: multipart/form-data' \ - --form firstName=Test \ - --form lastName=Test \ - --form profileImage=@/home/user/Desktop/cat.jpg \ - --form 'newPassword=Testtest1!' \ - --form 'password=Testtest2!' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "User updated successfully", - "data": { - "_id": "66a1425b873da2207443f192", - "firstName": "First name", - "lastName": "Last name", - "email": "name@gmail.com", - "isActive": true, - "isVerified": false, - "role": ["admin"], - "createdAt": "2024-07-24T18:05:15.852Z", - "updatedAt": "2024-07-24T18:31:32.314Z", - "__v": 0, - "avatarImage": "" - } -} -``` - -
- -
-GET/api/v1/auth/users - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | GET | -> | content-type | application/json | - -###### Response Payload - -> | Type | Notes | -> | ------------- | ------------------------------------- | -> | `Array` | Returns an array containing all users | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/auth/users \ - --header 'Authorization: \ -``` - -##### Sample Resonse - -```json -{ - "success": true, - "msg": "Got all users", - "data": [ - { - "_id": "669e90072d5663d25808bc7b", - "firstName": "First name", - "lastName": "Last name", - "email": "name@gmail.com", - "isActive": true, - "isVerified": false, - "role": ["admin"], - "createdAt": "2024-07-22T16:59:51.695Z", - "updatedAt": "2024-07-22T16:59:51.695Z", - "__v": 0 - } - ] -} -``` - -
- -
-POST/api/v1/auth/recovery/request - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | POST | -> | content-type | application/json | - -##### Body - -> | Name | Type | Notes | -> | ----- | -------- | ------------ | -> | email | `string` | User's email | - -###### Response Payload - -> | Type | Notes | -> | --------------- | --------------------------------------- | -> | `RecoveryToken` | Returns a recovery token if email found | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/auth/recovery/request \ - --header 'Content-Type: application/json' \ - --data '{ - "email" : "name@gmail.com" -}' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Created recovery token", - "data": { - "email": "name@gmail.com", - "token": "f519da5e4a9be40cfc3c0fde97e60c0e6d17bdaa613f5ba537a45073f3865193", - "_id": "6668878263587f30748e968e", - "expiry": "2024-06-11T17:21:06.984Z", - "createdAt": "2024-06-11T17:21:06.985Z", - "updatedAt": "2024-06-11T17:21:06.985Z", - "__v": 0 - } -} -``` - -
- -
-POST/api/v1/auth/recovery/validate - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | POST | -> | content-type | application/json | - -##### Body - -> | Name | Type | Notes | -> | ------------- | -------- | ----------------------------------- | -> | recoveryToken | `string` | Token issued in `/recovery/request` | - -###### Response Payload - -> | Type | Notes | -> | --------------- | -------------------------- | -> | `RecoveryToken` | Returns the recovery token | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/auth/recovery/validate \ - --header 'Content-Type: application/json' \ - --data '{ - "recoveryToken" : "f519da5e4a9be40cfc3c0fde97e60c0e6d17bdaa613f5ba537a45073f3865193" -}' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Token is valid", - "data": { - "_id": "6668894263587f30748e969a", - "email": "name@gmail.com", - "token": "457d9926b24dedf613f120eeb524ef00ac45b3f0fc5c70bd25b1cc8aa83a64a0", - "expiry": "2024-06-11T17:28:34.349Z", - "createdAt": "2024-06-11T17:28:34.349Z", - "updatedAt": "2024-06-11T17:28:34.349Z", - "__v": 0 - } -} -``` - -
- -
-POST/api/v1/auth/recovery/reset - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | POST | -> | content-type | application/json | - -##### Body - -> | Name | Type | Notes | -> | ------------- | -------- | --------------------------------------------- | -> | recoveryToken | `string` | Token issued returned by `/recovery/validate` | -> | password | `string` | User's new password` | - -###### Response Payload - -> | Type | Notes | -> | ------ | ------------------------ | -> | `User` | Returns the updated user | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/auth/recovery/reset \ - --header 'Content-Type: application/json' \ - --data '{ - "recoveryToken" : "f519da5e4a9be40cfc3c0fde97e60c0e6d17bdaa613f5ba537a45073f3865193", - "password": "testtest" -}' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Password reset", - "data": { - "_id": "66675891cb17336d84c25d9f", - "firstname": "First Name", - "lastname": "Last Name", - "email": "name@gmail.com", - "isActive": true, - "isVerified": false, - "createdAt": "2024-06-10T19:48:33.863Z", - "updatedAt": "2024-06-11T17:21:22.289Z", - "__v": 0 - } -} -``` - -
- ---- - -###### Monitors - -
-GET /api/v1/monitors - -##### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | GET | -> | content-type | application/json | - -##### Response Payload - -> | Type | Notes | -> | ---------------- | --------------------- | -> | `Array` | Array of all monitors | - -###### Sample cURL Request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/monitors \ - --header '' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Monitors found", - "data": [ - { - "_id": "664d070786e62625ac612ca1", - "userId": "6645079aae0b439371913972", - "name": "Wha3", - "description": "Description", - "url": "https://monitor0.com", - "isActive": true, - "interval": 60000, - "createdAt": "2024-05-21T20:41:43.051Z", - "updatedAt": "2024-05-21T20:45:10.496Z", - "__v": 0 - }, - { - "_id": "664e5ccf189c864800debc16", - "userId": "6645079aae0b439371913972", - "name": "Inserting a new Monitor", - "description": "Description", - "url": "https://monitor0.com", - "isActive": true, - "interval": 60000, - "createdAt": "2024-05-22T20:59:59.295Z", - "updatedAt": "2024-05-22T20:59:59.295Z", - "__v": 0 - } - ] -} -``` - -
- -
-GET /api/v1/monitor/{id}?status - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | GET | -> | content-type | application/json | - -###### Query Params - -> | Name | Type | Required | Notes | -> | --------- | --------- | -------- | ------------------------------------------------ | -> | status | `boolean` | Optional | Check status | -> | limit | `number` | Optional | Number of checks to return | -> | sortOrder | `string` | Optional | `desc`:Newest -> Oldest, `asc`: Oldest -> Newest | - -###### Response Payload - -> | Type | Notes | -> | --------- | --------------------------------------------------- | -> | `Monitor` | Single monitor with the id in the request parameter | - -###### Sample cURL Request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/monitors/664d070786e62625ac612ca1?status=true?limit=0 \ - --header '' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Got monitor by Id successfully", - "data": { - "_id": "6671eb54f7040ece47892f53", - "userId": "666c9146c9bfa20db790b1df", - "name": "Google Monitor", - "description": "Google", - "type": "http", - "url": "https://www.google.com/404", - "isActive": true, - "interval": 10000, - "createdAt": "2024-06-18T20:17:24.112Z", - "updatedAt": "2024-06-18T20:17:24.112Z", - "__v": 0, - "checks": [ - { - "_id": "6671eb5af7040ece47892f61", - "monitorId": "6671eb54f7040ece47892f53", - "status": false, - "responseTime": 145, - "expiry": "2024-06-18T20:17:30.246Z", - "statusCode": 404, - "createdAt": "2024-06-18T20:17:30.246Z", - "updatedAt": "2024-06-18T20:17:30.246Z", - "__v": 0 - }, - { - "_id": "6671eb64f7040ece47892f6b", - "monitorId": "6671eb54f7040ece47892f53", - "status": false, - "responseTime": 170, - "expiry": "2024-06-18T20:17:40.209Z", - "statusCode": 404, - "createdAt": "2024-06-18T20:17:40.210Z", - "updatedAt": "2024-06-18T20:17:40.210Z", - "__v": 0 - } - ] - } -} -``` - -
- -
-GET /api/v1/monitors/user/{userId}?limit - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | GET | -> | content-type | application/json | - -###### Query Params - -> | Name | Type | Required | Notes | -> | --------- | --------- | -------- | ------------------------------------------------- | -> | status | `boolean` | Optional | Check status | -> | type | `string` | Optional | Multiple allowed: `http` \| `ping` \| `pagespeed` | -> | limit | `number` | Optional | Monitor status | -> | sortOrder | `string` | Optional | `desc`:Newest -> Oldest, `asc`: Oldest -> Newest | - -###### Response Payload - -> | Type | Notes | -> | ---------------- | ------------------------------------------------------- | -> | `Array` | Array of monitors created by user with specified UserID | - -###### Sample cURL Request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/monitors/user/6645079aae0b439371913972?limit=25 \ - --header '' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Got monitor for 666c9146c9bfa20db790b1df successfully\"", - "data": [ - { - "_id": "6671eb54f7040ece47892f53", - "userId": "666c9146c9bfa20db790b1df", - "name": "Google Monitor", - "description": "Google", - "type": "http", - "url": "https://www.google.com/404", - "isActive": true, - "interval": 10000, - "createdAt": "2024-06-18T20:17:24.112Z", - "updatedAt": "2024-06-18T20:17:24.112Z", - "__v": 0, - "checks": [ - { - "_id": "6671eb5af7040ece47892f61", - "monitorId": "6671eb54f7040ece47892f53", - "status": false, - "responseTime": 145, - "expiry": "2024-06-18T20:17:30.246Z", - "statusCode": 404, - "createdAt": "2024-06-18T20:17:30.246Z", - "updatedAt": "2024-06-18T20:17:30.246Z", - "__v": 0 - } - ] - } - ] -} -``` - -
- -
-POST/api/v1/monitors - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | POST | -> | content-type | application/json | - -##### Body - -> | Name | Type | Notes | Accepted Values | -> | ----------- | --------- | ---------------------- | --------------------------------------- | -> | userId | `string` | UserId of current user | | -> | name | `string` | Monitor name | | -> | description | `string` | Monitor Description | | -> | type | `string` | Valid email address | `"ping"`|`"http"`|`pagespeed` | -> | url | `string` | URL of service or IP | | -> | isActive | `boolean` | | | -> | interval | `number` | In ms | | - -###### Response Payload - -> | Type | Notes | -> | --------- | ------------------------------- | -> | `Monitor` | Returns newly created `Monitor` | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/monitors \ - --header \ - --header 'Content-Type: application/json' \ - --data '{ - "userId": "66675891cb17336d84c25d9f", - "name": "Ping Google", - "description": "Google", - "type": "ping", - "url": "8.8.8.8", - "isActive": true, - "interval": 5000}' -``` - -##### Sample Response - -```json -{ - "success": true, - "msg": "Monitor created", - "data": { - "userId": "6645079aae0b439371913972", - "name": "Inserting a new Monitor", - "description": "Description", - "url": "https://monitor0.com", - "isActive": true, - "interval": 60000, - "_id": "664e5ccf189c864800debc16", - "createdAt": "2024-05-22T20:59:59.295Z", - "updatedAt": "2024-05-22T20:59:59.295Z", - "__v": 0 - } -} -``` - -
- -
-POST/api/v1/monitors/delete/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | POST | -> | content-type | application/json | - -###### Response Payload - -> | Type | Notes | -> | ---- | ------------------- | -> | None | No payload returned | - -###### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/monitors/delete/664e632a7a3ee9d620761938 \ - --header '' \ - --header 'Content-Type: application/json' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Monitor deleted" -} -``` - -##
- -
-POST/api/v1/monitors/edit/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ---------------- | -> | Method | POST | -> | content-type | application/json | - -##### Body - -> | Name | Type | Notes | Accepted Values | -> | ----------- | -------- | ------------------- | --------------- | -> | name | `string` | Monitor name | | -> | description | `string` | Monitor Description | | -> | interval | `number` | In ms | - -###### Response Payload - -> | Type | Notes | -> | --------- | --------------------------- | -> | `Monitor` | Returns the updated monitor | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/monitors/edit/664e5ccf189c864800debc16 \ - --header ' - ---- - -###### Checks - -
-POST/api/v1/checks/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | ------- | --------------------------- | -> | `Check` | Returns newly created check | - -###### Body - -> | Name | Type | Notes | -> | ------------ | --------- | -------------------------------------- | -> | monitorId | `string` | Monitor associated with Check | -> | status | `boolean` | `true` for up and `false` for down | -> | responseTime | `number` | How long it took the server to respond | -> | statusCode | `number` | HTTP Status code of response | -> | message | `string` | | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/checks/66562414035c4ce6a8a610ac \ - --header 'Authorization: ' \ - --header 'Content-Type: application/json' \ - --data '{ - "monitorId": "66562414035c4ce6a8a610ac", - "status": true, - "responseTime": 1, - "statusCode": 200, - "message": "good" -}' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Check created", - "data": { - "monitorId": "66562414035c4ce6a8a610ac", - "status": true, - "responseTime": 1, - "statusCode": 200, - "message": "good", - "_id": "66576decba9f70148ea1f354", - "createdAt": "2024-05-29T18:03:24.445Z", - "updatedAt": "2024-05-29T18:03:24.445Z", - "__v": 0 - } -} -``` - -
- -
-GET/api/v1/checks/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | GET | - -###### Response Payload - -> | Type | Notes | -> | --------------- | ------------------------ | -> | `Array` | Array of `Check` objects | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/checks/66562414035c4ce6a8a610ac \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Checks retrieved", - "data": [ - { - "_id": "66576c0194e11c0d4409d3c1", - "monitorId": "66562414035c4ce6a8a610ac", - "status": true, - "responseTime": 1, - "statusCode": 200, - "message": "good", - "createdAt": "2024-05-29T17:55:13.581Z", - "updatedAt": "2024-05-29T17:55:13.581Z", - "__v": 0 - }, - { - "_id": "66576c0994e11c0d4409d3c5", - "monitorId": "66562414035c4ce6a8a610ac", - "status": true, - "responseTime": 2, - "statusCode": 200, - "message": "good", - "createdAt": "2024-05-29T17:55:21.127Z", - "updatedAt": "2024-05-29T17:55:21.127Z", - "__v": 0 - } - ] -} -``` - -
- -
-POST/api/v1/checks/delete/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | -------- | -------------------------------------------------------------------- | -> | `Object` | `{deletedCount: n}` Returns an object showing how many items deleted | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/checks/delete/66562414035c4ce6a8a610ac \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Checks deleted", - "data": { - "deletedCount": 3 - } -} -``` - -
- ---- - -###### Alerts - -
-POST/api/v1/alerts/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | ------- | ----------------------------- | -> | `Alert` | Returns newly created `Alert` | - -###### Body - - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": "false", - "acknowledgeStatus": false - -> | Name | Type | Notes | -> | ----------------- | --------- | --------------------------------------- | -> | checkId | `string` | Id of `Check` associated with `Alert` | -> | monitorId | `string` | Id of `Monitor` associated with `Alert` | -> | userId | `string` | Id of `User` associated with `Alert` | -> | status | `boolean` | Status of `Alert` | -> | message | `string` | `Alert` message | -> | notifiedStatus | `boolean` | | -> | acknowledgeStatus | `boolean` | | - -##### Sample CURL request - -``` - -``` - -###### Sample Response - -```json - -``` - -
- -
-GET/api/v1/alerts/user/{userId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | GET | - -###### Response Payload - -> | Type | Notes | -> | -------------- | --------------------------------------- | -> | `Array` | Returns all `Alert` created by a `User` | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/alerts/user/6654d1a2634754f789e1f115 \ - --header 'Authorization: ' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Got alerts", - "data": [ - { - "_id": "6657813d809adfded891a6b7", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": false, - "createdAt": "2024-05-29T19:25:49.317Z", - "updatedAt": "2024-05-29T19:25:49.317Z", - "__v": 0 - } - ] -} -``` - -
-
-GET/api/v1/alerts/monitor/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | GET | - -###### Response Payload - -> | Type | Notes | -> | -------------- | -------------------------------------------------------------- | -> | `Array` | Returns an array of `Alert` belonging to a specified `Monitor` | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/alerts/monitor/6657789ebf6766ee8e2d2edb \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Got alerts by Monitor", - "data": [ - { - "_id": "6657813d809adfded891a6b7", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": false, - "createdAt": "2024-05-29T19:25:49.317Z", - "updatedAt": "2024-05-29T19:25:49.317Z", - "__v": 0 - } - ] -} -``` - -
- -
-GET/api/v1/alerts/{alertId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | GET | - -###### Response Payload - -> | Type | Notes | -> | ------- | ------------------------- | -> | `Alert` | Returns specified `Alert` | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/alerts/66577ddae5ff3c91437d0887 \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Got Alert By alertID", - "data": { - "_id": "66577ddae5ff3c91437d0887", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": false, - "createdAt": "2024-05-29T19:11:22.205Z", - "updatedAt": "2024-05-29T19:11:22.205Z", - "__v": 0 - } -} -``` - -
- -
-POST/api/v1/alerts/edit/{alertId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | ------- | ---------------------- | -> | `Alert` | Returns edited `Alert` | - -###### Body - -> | Name | Type | Notes | -> | ----------------- | --------- | ------------------------------------------ | -> | checkId | `string` | ID of `Check` associated with `Alert` | -> | monitorId | `string` | ID of `Monitor` id associated with `Alert` | -> | userId | `string` | ID of `User` associated with `Alert` | -> | status | `boolean` | Alert status | -> | message | `string` | Alert message | -> | notifiedStatus | `boolean` | | -> | acknowledgeStatus | `boolean` | | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/alerts/edit/66577ddae5ff3c91437d0887 \ - --header 'Authorization: ' \ - --header 'Content-Type: application/json' \ - --data '{ - "acknowledgeStatus": true -}' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Edited alert", - "data": { - "_id": "66577ddae5ff3c91437d0887", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": true, - "createdAt": "2024-05-29T19:11:22.205Z", - "updatedAt": "2024-05-29T19:12:23.951Z", - "__v": 0 - } -} -``` - -
-
-POST/api/v1/alerts/delete/{alertId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | ------- | --------------------------- | -> | `Alert` | Returns the deleted `Alert` | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/alerts/delete/66577ddae5ff3c91437d0887 \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Deleted alert", - "data": { - "_id": "66577ddae5ff3c91437d0887", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": true, - "createdAt": "2024-05-29T19:11:22.205Z", - "updatedAt": "2024-05-29T19:12:23.951Z", - "__v": 0 - } -} -``` - -
- ---- - -###### Checks - -
-POST/api/v1/checks/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | ------- | --------------------------- | -> | `Check` | Returns newly created check | - -###### Body - -> | Name | Type | Notes | -> | ------------ | --------- | -------------------------------------- | -> | monitorId | `string` | Monitor associated with Check | -> | status | `boolean` | `true` for up and `false` for down | -> | responseTime | `number` | How long it took the server to respond | -> | statusCode | `number` | HTTP Status code of response | -> | message | `string` | | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/checks/66562414035c4ce6a8a610ac \ - --header 'Authorization: ' \ - --header 'Content-Type: application/json' \ - --data '{ - "monitorId": "66562414035c4ce6a8a610ac", - "status": true, - "responseTime": 1, - "statusCode": 200, - "message": "good" -}' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Check created", - "data": { - "monitorId": "66562414035c4ce6a8a610ac", - "status": true, - "responseTime": 1, - "statusCode": 200, - "message": "good", - "_id": "66576decba9f70148ea1f354", - "createdAt": "2024-05-29T18:03:24.445Z", - "updatedAt": "2024-05-29T18:03:24.445Z", - "__v": 0 - } -} -``` - -
- -
-GET/api/v1/checks/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | GET | - -###### Response Payload - -> | Type | Notes | -> | --------------- | ------------------------ | -> | `Array` | Array of `Check` objects | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/checks/66562414035c4ce6a8a610ac \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Checks retrieved", - "data": [ - { - "_id": "66576c0194e11c0d4409d3c1", - "monitorId": "66562414035c4ce6a8a610ac", - "status": true, - "responseTime": 1, - "statusCode": 200, - "message": "good", - "createdAt": "2024-05-29T17:55:13.581Z", - "updatedAt": "2024-05-29T17:55:13.581Z", - "__v": 0 - }, - { - "_id": "66576c0994e11c0d4409d3c5", - "monitorId": "66562414035c4ce6a8a610ac", - "status": true, - "responseTime": 2, - "statusCode": 200, - "message": "good", - "createdAt": "2024-05-29T17:55:21.127Z", - "updatedAt": "2024-05-29T17:55:21.127Z", - "__v": 0 - } - ] -} -``` - -
- -
-POST/api/v1/checks/delete/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | -------- | -------------------------------------------------------------------- | -> | `Object` | `{deletedCount: n}` Returns an object showing how many items deleted | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/checks/delete/66562414035c4ce6a8a610ac \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Checks deleted", - "data": { - "deletedCount": 3 - } -} -``` - -
- ---- - -###### Alerts - -
-POST/api/v1/alerts/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | ------- | ----------------------------- | -> | `Alert` | Returns newly created `Alert` | - -###### Body - - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": "false", - "acknowledgeStatus": false - -> | Name | Type | Notes | -> | ----------------- | --------- | --------------------------------------- | -> | checkId | `string` | Id of `Check` associated with `Alert` | -> | monitorId | `string` | Id of `Monitor` associated with `Alert` | -> | userId | `string` | Id of `User` associated with `Alert` | -> | status | `boolean` | Status of `Alert` | -> | message | `string` | `Alert` message | -> | notifiedStatus | `boolean` | | -> | acknowledgeStatus | `boolean` | | - -##### Sample CURL request - -``` - -``` - -###### Sample Response - -```json - -``` - -
- -
-GET/api/v1/alerts/user/{userId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | GET | - -###### Response Payload - -> | Type | Notes | -> | -------------- | --------------------------------------- | -> | `Array` | Returns all `Alert` created by a `User` | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/alerts/user/6654d1a2634754f789e1f115 \ - --header 'Authorization: ' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Got alerts", - "data": [ - { - "_id": "6657813d809adfded891a6b7", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": false, - "createdAt": "2024-05-29T19:25:49.317Z", - "updatedAt": "2024-05-29T19:25:49.317Z", - "__v": 0 - } - ] -} -``` - -
-
-GET/api/v1/alerts/monitor/{monitorId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | GET | - -###### Response Payload - -> | Type | Notes | -> | -------------- | -------------------------------------------------------------- | -> | `Array` | Returns an array of `Alert` belonging to a specified `Monitor` | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/alerts/monitor/6657789ebf6766ee8e2d2edb \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Got alerts by Monitor", - "data": [ - { - "_id": "6657813d809adfded891a6b7", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": false, - "createdAt": "2024-05-29T19:25:49.317Z", - "updatedAt": "2024-05-29T19:25:49.317Z", - "__v": 0 - } - ] -} -``` - -
- -
-GET/api/v1/alerts/{alertId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | GET | - -###### Response Payload - -> | Type | Notes | -> | ------- | ------------------------- | -> | `Alert` | Returns specified `Alert` | - -##### Sample CURL request - -``` -curl --request GET \ - --url http://localhost:5000/api/v1/alerts/66577ddae5ff3c91437d0887 \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Got Alert By alertID", - "data": { - "_id": "66577ddae5ff3c91437d0887", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": false, - "createdAt": "2024-05-29T19:11:22.205Z", - "updatedAt": "2024-05-29T19:11:22.205Z", - "__v": 0 - } -} -``` - -
- -
-POST/api/v1/alerts/edit/{alertId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | ------- | ---------------------- | -> | `Alert` | Returns edited `Alert` | - -###### Body - -> | Name | Type | Notes | -> | ----------------- | --------- | ------------------------------------------ | -> | checkId | `string` | ID of `Check` associated with `Alert` | -> | monitorId | `string` | ID of `Monitor` id associated with `Alert` | -> | userId | `string` | ID of `User` associated with `Alert` | -> | status | `boolean` | Alert status | -> | message | `string` | Alert message | -> | notifiedStatus | `boolean` | | -> | acknowledgeStatus | `boolean` | | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/alerts/edit/66577ddae5ff3c91437d0887 \ - --header 'Authorization: ' \ - --header 'Content-Type: application/json' \ - --data '{ - "acknowledgeStatus": true -}' -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Edited alert", - "data": { - "_id": "66577ddae5ff3c91437d0887", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": true, - "createdAt": "2024-05-29T19:11:22.205Z", - "updatedAt": "2024-05-29T19:12:23.951Z", - "__v": 0 - } -} -``` - -
-
-POST/api/v1/alerts/delete/{alertId} - -###### Method/Headers - -> | Method/Headers | Value | -> | -------------- | ----- | -> | Method | POST | - -###### Response Payload - -> | Type | Notes | -> | ------- | --------------------------- | -> | `Alert` | Returns the deleted `Alert` | - -##### Sample CURL request - -``` -curl --request POST \ - --url http://localhost:5000/api/v1/alerts/delete/66577ddae5ff3c91437d0887 \ - --header 'Authorization: ' \ -``` - -###### Sample Response - -```json -{ - "success": true, - "msg": "Deleted alert", - "data": { - "_id": "66577ddae5ff3c91437d0887", - "checkId": "66577a3fd16dcf7c1ce35148", - "monitorId": "6657789ebf6766ee8e2d2edb", - "userId": "6654d1a2634754f789e1f115", - "status": false, - "message": "This is a test alert", - "notifiedStatus": false, - "acknowledgeStatus": true, - "createdAt": "2024-05-29T19:11:22.205Z", - "updatedAt": "2024-05-29T19:12:23.951Z", - "__v": 0 - } -} -``` - -
- ---- +You can also view the documentation on our demo server at [https://uptime-demo.bluewavelabs.ca/api-docs](https://uptime-demo.bluewavelabs.ca/api-docs) ### Error handling From 5dbd8378e28d3dedaeba6dd3e49d9b200e9855bf Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 25 Sep 2024 15:12:07 +0800 Subject: [PATCH 2/3] require invite token to register except for initial registration --- Client/src/Pages/Auth/Register/Register.jsx | 5 ++-- Client/src/Validation/validation.js | 1 + Server/controllers/authController.js | 5 ++++ Server/db/mongo/MongoDB.js | 2 ++ Server/db/mongo/modules/inviteModule.js | 30 ++++++++++++++++++++- Server/validation/joi.js | 2 ++ 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Client/src/Pages/Auth/Register/Register.jsx b/Client/src/Pages/Auth/Register/Register.jsx index 7f5ed6390..eb331304c 100644 --- a/Client/src/Pages/Auth/Register/Register.jsx +++ b/Client/src/Pages/Auth/Register/Register.jsx @@ -596,6 +596,7 @@ const Register = ({ isSuperAdmin }) => { let registerForm = { ...form, role: isSuperAdmin ? ["superadmin"] : form.role, + inviteToken: token ? token : "", }; let error = validateForm(registerForm, { context: { password: form.password }, @@ -608,8 +609,8 @@ const Register = ({ isSuperAdmin }) => { delete registerForm.confirm; const action = await dispatch(register(registerForm)); if (action.payload.success) { - const token = action.payload.data; - localStorage.setItem("token", token); + const authToken = action.payload.data; + localStorage.setItem("token", authToken); navigate("/"); createToast({ body: "Welcome! Your account was created successfully.", diff --git a/Client/src/Validation/validation.js b/Client/src/Validation/validation.js index 3d11482d1..25ad79923 100644 --- a/Client/src/Validation/validation.js +++ b/Client/src/Validation/validation.js @@ -77,6 +77,7 @@ const credentials = joi.object({ }), role: joi.array(), teamId: joi.string().allow("").optional(), + inviteToken: joi.string().allow(""), }); const monitorValidation = joi.object({ diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index f39ffb019..6357d25de 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -51,6 +51,11 @@ const registerController = async (req, res, next) => { } // Create a new user try { + const { inviteToken } = req.body; + const superAdminExists = await req.db.checkSuperadmin(req, res); + if (superAdminExists) { + await req.db.getInviteTokenAndDelete(inviteToken); + } const newUser = await req.db.insertUser({ ...req.body }, req.file); logger.info(successMessages.AUTH_CREATE_USER, { service: SERVICE_NAME, diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js index c5fcff08c..0cc2d5b57 100644 --- a/Server/db/mongo/MongoDB.js +++ b/Server/db/mongo/MongoDB.js @@ -47,6 +47,7 @@ const { const { requestInviteToken, getInviteToken, + getInviteTokenAndDelete, } = require("./modules/inviteModule"); //**************************************** @@ -128,6 +129,7 @@ module.exports = { logoutUser, requestInviteToken, getInviteToken, + getInviteTokenAndDelete, requestRecoveryToken, validateRecoveryToken, resetPassword, diff --git a/Server/db/mongo/modules/inviteModule.js b/Server/db/mongo/modules/inviteModule.js index 6e810ec8b..1b7d698cd 100644 --- a/Server/db/mongo/modules/inviteModule.js +++ b/Server/db/mongo/modules/inviteModule.js @@ -32,7 +32,7 @@ const requestInviteToken = async (userData) => { }; /** - * Retrieves and deletes an invite token. + * Retrieves an invite token * * This function searches for an invite token in the database and deletes it. * If the invite token is not found, it throws an error. @@ -42,6 +42,33 @@ const requestInviteToken = async (userData) => { * @throws {Error} If the invite token is not found or there is another error. */ const getInviteToken = async (token) => { + console.log(token); + try { + const invite = await InviteToken.findOne({ + token, + }); + if (invite === null) { + throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND); + } + return invite; + } catch (error) { + error.service = SERVICE_NAME; + error.method = "getInviteToken"; + throw error; + } +}; + +/** + * Retrieves and deletes an invite token + * + * This function searches for an invite token in the database and deletes it. + * If the invite token is not found, it throws an error. + * + * @param {string} token - The invite token to search for. + * @returns {Promise} The invite token data. + * @throws {Error} If the invite token is not found or there is another error. + */ +const getInviteTokenAndDelete = async (token) => { try { const invite = await InviteToken.findOneAndDelete({ token, @@ -60,4 +87,5 @@ const getInviteToken = async (token) => { module.exports = { requestInviteToken, getInviteToken, + getInviteTokenAndDelete, }; diff --git a/Server/validation/joi.js b/Server/validation/joi.js index f61432595..0a83b6e74 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -74,6 +74,7 @@ const registrationBodyValidation = joi.object({ .min(1) .required(), teamId: joi.string().allow("").required(), + inviteToken: joi.string().allow("").required(), }); const editUserParamValidation = joi.object({ @@ -137,6 +138,7 @@ const inviteBodyValidation = joi.object({ "string.email": "Must be a valid email address", }), role: joi.array().required(), + teamId: joi.string().required(), }); const inviteVerifciationBodyValidation = joi.object({ From ffd193437e1285f7b04318dace01e342b208dca7 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 25 Sep 2024 15:14:22 +0800 Subject: [PATCH 3/3] add comments --- Client/src/Pages/Auth/Register/Register.jsx | 2 +- Server/controllers/authController.js | 1 + Server/db/mongo/modules/inviteModule.js | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Client/src/Pages/Auth/Register/Register.jsx b/Client/src/Pages/Auth/Register/Register.jsx index eb331304c..c9f61adae 100644 --- a/Client/src/Pages/Auth/Register/Register.jsx +++ b/Client/src/Pages/Auth/Register/Register.jsx @@ -596,7 +596,7 @@ const Register = ({ isSuperAdmin }) => { let registerForm = { ...form, role: isSuperAdmin ? ["superadmin"] : form.role, - inviteToken: token ? token : "", + inviteToken: token ? token : "", // Add the token to the request for verification }; let error = validateForm(registerForm, { context: { password: form.password }, diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 6357d25de..4e589ac8a 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -52,6 +52,7 @@ const registerController = async (req, res, next) => { // Create a new user try { const { inviteToken } = req.body; + // If superAdmin exists, a token should be attached to all further register requests const superAdminExists = await req.db.checkSuperadmin(req, res); if (superAdminExists) { await req.db.getInviteTokenAndDelete(inviteToken); diff --git a/Server/db/mongo/modules/inviteModule.js b/Server/db/mongo/modules/inviteModule.js index 1b7d698cd..9a0d35621 100644 --- a/Server/db/mongo/modules/inviteModule.js +++ b/Server/db/mongo/modules/inviteModule.js @@ -42,7 +42,6 @@ const requestInviteToken = async (userData) => { * @throws {Error} If the invite token is not found or there is another error. */ const getInviteToken = async (token) => { - console.log(token); try { const invite = await InviteToken.findOne({ token,