diff --git a/README.md b/README.md index 8767a3633..2fb1d944c 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,21 @@ BlueWave uptime monitoring application 2. [Installation (Server)](#server) 3. [Configuration(Server)](#config-server) 4. [Endpoints](#endpoints) + ###### Auth - POST [/api/v1/auth/register](#post-register) - POST [/api/v1/auth/login](#post-login) - POST [/api/v1/auth/user/{userId}](#post-auth-user-edit-id) + ###### Monitors - GET [/api/v1/monitors](#get-monitors) - GET [/api/v1/monitor/{id}](#get-monitor-id) - GET [/api/v1/monitors/user/{userId}](#get-monitors-user-userid) - POST [/api/v1/monitors](#post-monitors) - POST [/api/v1/monitors/delete/{monitorId}](#post-monitors-del-id) - POST [/api/v1/monitors/edit/{monitorId}](#post-monitors-edit-id) + ###### Checks + - POST [/api/v1/checks/{monitorId}](#post-checks) + - GET [/api/v1/checks/{monitorId}](#get-checks) + - POST [/api/v1/checks/delete/{monitorId}](#delete-checks) 5. [Error Handling](#error-handling) 6. [Contributors](#contributors) @@ -122,15 +128,15 @@ Example:
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 | +| 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 |
@@ -138,9 +144,9 @@ Example: Alert | Name | Type | Notes | -| ----------- | --------- | --------------------------------------------------| +| ----------------- | --------- | ------------------------------------------------- | | checkId | `string` | Unique ID for the check | -| status | `boolean` | Indicates the service is Up or Down | +| 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 | @@ -148,8 +154,11 @@ Example: | CreatedAt | `Date` | When the alert was created | + --- +###### Auth +
POST /api/v1/auth/register @@ -201,8 +210,6 @@ curl --request POST \
---- -
POST /api/v1/auth/login @@ -250,8 +257,6 @@ curl --request POST \
---- -
POST/api/v1/auth/user/{userId} @@ -313,6 +318,8 @@ curl --request POST \ --- +###### Monitors +
GET /api/v1/monitors @@ -373,8 +380,6 @@ curl --request GET \
---- -
GET /api/v1/monitor/{id} @@ -421,8 +426,6 @@ curl --request GET \
---- -
GET /api/v1/monitors/user/{userId} @@ -483,8 +486,6 @@ curl --request GET \
---- -
POST/api/v1/monitors @@ -538,8 +539,6 @@ curl --request POST \
---- -
POST/api/v1/monitors/delete/{monitorId} @@ -575,8 +574,6 @@ curl --request POST \ ##
---- -
POST/api/v1/monitors/edit/{monitorId} @@ -636,6 +633,101 @@ curl --request POST \ --- +###### Checks + +
+POST/api/v1/checks/{monitorId} + +###### Method/Headers + +> | Method/Headers | Value | +> | -------------- | ----- | +> | Method | POST | + +###### Response Payload + +> | Type | Notes | +> | ---- | ------------------- | +> | None | No payload returned | + +###### Body + +##### Sample CURL request + +``` + +``` + +###### Sample Response + +```json + +``` + +
+ +
+GET/api/v1/checks/{monitorId} + +###### Method/Headers + +> | Method/Headers | Value | +> | -------------- | ----- | +> | Method | POST | + +###### Response Payload + +> | Type | Notes | +> | --------------- | ------------------------ | +> | `Array[Checks]` | Array of `Check` objects | + +##### Sample CURL request + +``` + +``` + +###### Sample Response + +```json + +``` + +
+ +
+POST/api/v1/checks/delete/{monitorId} + +###### Method/Headers + +> | Method/Headers | Value | +> | -------------- | ----- | +> | Method | POST | + +###### Response Payload + +> | Type | Notes | +> | ---- | ------------------- | +> | None | No payload returned | + +###### Body + +##### Sample CURL request + +``` + +``` + +###### Sample Response + +```json + +``` + +
+ +--- + ### Error handling {#error-handling} Errors are returned in a standard format: diff --git a/Server/controllers/checkController.js b/Server/controllers/checkController.js new file mode 100644 index 000000000..ef1a1a071 --- /dev/null +++ b/Server/controllers/checkController.js @@ -0,0 +1,37 @@ +var jwt = require("jsonwebtoken"); +const SERVICE_NAME = "check"; + +const createCheck = (req, res, next) => { + try { + req.db.createCheck(req, res); + } catch (error) { + error.service = SERVICE_NAME; + next(error); + } +}; + +const getChecks = (req, res, next) => { + try { + req.db.getChecks(req, res); + // Return all checks for a monitor + } catch (error) { + error.service = SERVICE_NAME; + next(error); + } +}; + +const deleteChecks = (req, res, next) => { + try { + req.db.deleteChecks(req, res); + // Delete all checks for a monitor + } catch (error) { + error.service = SERVICE_NAME; + next(error); + } +}; + +module.exports = { + createCheck, + getChecks, + deleteChecks, +}; diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index 71b6fe44a..a4c9422a3 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -16,6 +16,10 @@ const connect = async () => { } }; +//**************************************** +// Users +//**************************************** + /** * Insert a User * @async @@ -55,6 +59,10 @@ const getUserByEmail = async (req, res) => { } }; +//**************************************** +// Monitors +//**************************************** + /** * Update a user by ID * @async @@ -194,6 +202,69 @@ const editMonitor = async (req, res) => { } }; +//**************************************** +// Checks +//**************************************** + +/** + * Create a check for a monitor + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @throws {Error} + */ + +const createCheck = async (req, res) => { + try { + // Create check with monitor id + res + .status(200) + .json({ success: true, msg: "Create check", data: req.params.monitorId }); + } catch (error) { + throw error; + } +}; + +/** + * Get all checks for a monitor + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @throws {Error} + */ + +const getChecks = async (req, res) => { + try { + // TODO get all checks for a monitor from DB + res + .status(200) + .json({ success: true, msg: "Get checks", data: req.params.monitorId }); + } catch (error) { + throw error; + } +}; + +/** + * Delete all checks for a monitor + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @throws {Error} + */ + +const deleteChecks = async (req, res) => { + try { + // TODO Delete all checks for a monitor + res.status(200).json({ + success: true, + msg: "Deleted checks", + data: req.params.monitorId, + }); + } catch (error) { + throw error; + } +}; + module.exports = { connect, insertUser, @@ -205,4 +276,7 @@ module.exports = { createMonitor, deleteMonitor, editMonitor, + createCheck, + getChecks, + deleteChecks, }; diff --git a/Server/index.js b/Server/index.js index 63d919b8d..1596e0862 100644 --- a/Server/index.js +++ b/Server/index.js @@ -3,6 +3,7 @@ const helmet = require("helmet"); const cors = require("cors"); const authRouter = require("./routes/authRoute"); const monitorRouter = require("./routes/monitorRoute"); +const checkRouter = require("./routes/checkRoute"); const { connectDbAndRunServer } = require("./configs/db"); require("dotenv").config(); const logger = require("./utils/logger"); @@ -62,6 +63,7 @@ app.use((req, res, next) => { //routes app.use("/api/v1/auth", authRouter); app.use("/api/v1/monitors", verifyJWT, monitorRouter); +app.use("/api/v1/checks", verifyJWT, checkRouter); // Testing email service // app.use('/sendEmail', async (req, res) => { diff --git a/Server/routes/checkRoute.js b/Server/routes/checkRoute.js new file mode 100644 index 000000000..6569dd49a --- /dev/null +++ b/Server/routes/checkRoute.js @@ -0,0 +1,24 @@ +const router = require("express").Router(); +const checkController = require("../controllers/checkController"); +const { verifyOwnership } = require("../middleware/verifyOwnership"); +const Monitor = require("../models/Monitor"); + +router.post( + "/:monitorId", + verifyOwnership(Monitor, "monitorId"), + checkController.createCheck +); + +router.get( + "/:monitorId", + verifyOwnership(Monitor, "monitorId"), + checkController.getChecks +); + +router.post( + "/delete/:monitorId", + verifyOwnership(Monitor, "monitorId"), + checkController.deleteChecks +); + +module.exports = router;