Updated status codes, added edit monitor endpoint
This commit is contained in:
1 parent
face3339cd
commit
c4dc57b9be
6 files changed
+126
-63
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
const {
|
const {
|
||||||
getMonitorByIdValidation,
|
getMonitorByIdValidation,
|
||||||
getMonitorsByUserIdValidation,
|
getMonitorsByUserIdValidation,
|
||||||
createMonitorValidation,
|
monitorValidation,
|
||||||
} = require("../validation/joi");
|
} = require("../validation/joi");
|
||||||
|
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
@@ -37,7 +37,7 @@ const getMonitorById = async (req, res) => {
|
|||||||
const { error } = getMonitorByIdValidation.validate(req.params);
|
const { error } = getMonitorByIdValidation.validate(req.params);
|
||||||
if (error) {
|
if (error) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(422)
|
||||||
.json({ success: false, msg: error.details[0].message });
|
.json({ success: false, msg: error.details[0].message });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ const getMonitorsByUserId = async (req, res) => {
|
|||||||
const { error } = getMonitorsByUserIdValidation.validate(req.params);
|
const { error } = getMonitorsByUserIdValidation.validate(req.params);
|
||||||
if (error) {
|
if (error) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(422)
|
||||||
.json({ success: false, msg: error.details[0].message });
|
.json({ success: false, msg: error.details[0].message });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,17 +95,17 @@ const getMonitorsByUserId = async (req, res) => {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const createMonitor = async (req, res) => {
|
const createMonitor = async (req, res) => {
|
||||||
const { error } = createMonitorValidation.validate(req.body);
|
const { error } = monitorValidation.validate(req.body);
|
||||||
if (error) {
|
if (error) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(422)
|
||||||
.json({ success: false, msg: error.details[0].message });
|
.json({ success: false, msg: error.details[0].message });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const monitor = await req.db.createMonitor(req, res);
|
const monitor = await req.db.createMonitor(req, res);
|
||||||
return res
|
return res
|
||||||
.status(200)
|
.status(201)
|
||||||
.json({ success: true, msg: "Monitor created", data: monitor });
|
.json({ success: true, msg: "Monitor created", data: monitor });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error.message, { service: "monitor" });
|
logger.error(error.message, { service: "monitor" });
|
||||||
@@ -113,7 +113,6 @@ const createMonitor = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delete Monitor
|
|
||||||
/**
|
/**
|
||||||
* Delete a monitor by ID
|
* Delete a monitor by ID
|
||||||
* @async
|
* @async
|
||||||
@@ -127,21 +126,57 @@ const deleteMonitor = async (req, res) => {
|
|||||||
const { error } = getMonitorByIdValidation.validate(req.params);
|
const { error } = getMonitorByIdValidation.validate(req.params);
|
||||||
if (error) {
|
if (error) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(422)
|
||||||
.json({ success: false, msg: error.details[0].message });
|
.json({ success: false, msg: error.details[0].message });
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const monitor = await req.db.deleteMonitor(req, res);
|
const monitor = await req.db.deleteMonitor(req, res);
|
||||||
return res
|
/**
|
||||||
.status(200)
|
* TODO
|
||||||
.json({ success: true, msg: "Monitor deleted", data: monitor });
|
* We should remove all checks and alerts associated with this monitor
|
||||||
|
* when it is deleted so there is no orphaned data
|
||||||
|
* We also need to make sure to stop all running services for this monitor
|
||||||
|
*/
|
||||||
|
return res.status(204).json({ success: true, msg: "Monitor deleted" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error.message, { service: "monitor" });
|
logger.error(error.message, { service: "monitor" });
|
||||||
return res.status(500).json({ success: false, msg: error.message });
|
return res.status(500).json({ success: false, msg: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Edit Monitor
|
/**
|
||||||
|
* Edit a monitor by ID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Express.Response>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const editMonitor = async (req, res) => {
|
||||||
|
let paramError = getMonitorByIdValidation.validate(req.params);
|
||||||
|
if (paramError.error) {
|
||||||
|
return res
|
||||||
|
.status(422)
|
||||||
|
.json({ success: false, msg: paramError.error.details[0].message });
|
||||||
|
}
|
||||||
|
|
||||||
|
let { error } = monitorValidation.validate(req.body);
|
||||||
|
if (error) {
|
||||||
|
return res
|
||||||
|
.status(422)
|
||||||
|
.json({ success: false, msg: error.details[0].message });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const editedMonitor = await req.db.editMonitor(req, res);
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Monitor edited", data: editedMonitor });
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error.message, { service: "monitor" });
|
||||||
|
return res.status(500).json({ success: false, msg: error.message });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
@@ -149,4 +184,5 @@ module.exports = {
|
|||||||
getMonitorsByUserId,
|
getMonitorsByUserId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
|
editMonitor,
|
||||||
};
|
};
|
||||||
+18
-15
@@ -27,21 +27,6 @@ const bcrypt = require("bcrypt");
|
|||||||
let FAKE_MONITOR_DATA = [];
|
let FAKE_MONITOR_DATA = [];
|
||||||
const USERS = [];
|
const USERS = [];
|
||||||
|
|
||||||
for (let i = 0; i < 10; i++) {
|
|
||||||
FAKE_MONITOR_DATA.push(
|
|
||||||
new Monitor({
|
|
||||||
userId: i % 2 === 0 ? 1 : 2,
|
|
||||||
name: `Monitor ${i}`,
|
|
||||||
description: `Description for Monitor ${i}`,
|
|
||||||
url: `https://monitor${i}.com`,
|
|
||||||
isActive: true,
|
|
||||||
interval: 60000,
|
|
||||||
updated_at: new Date(),
|
|
||||||
created_at: new Date(),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const connect = async () => {
|
const connect = async () => {
|
||||||
try {
|
try {
|
||||||
await console.log("Connected to FakeDB");
|
await console.log("Connected to FakeDB");
|
||||||
@@ -104,6 +89,8 @@ const getMonitorsByUserId = async (userId) => {
|
|||||||
|
|
||||||
const createMonitor = async (req, res) => {
|
const createMonitor = async (req, res) => {
|
||||||
const monitor = new Monitor(req.body);
|
const monitor = new Monitor(req.body);
|
||||||
|
monitor.createdAt = Date.now();
|
||||||
|
monitor.updatedAt = Date.now();
|
||||||
FAKE_MONITOR_DATA.push(monitor);
|
FAKE_MONITOR_DATA.push(monitor);
|
||||||
return monitor;
|
return monitor;
|
||||||
};
|
};
|
||||||
@@ -121,6 +108,21 @@ const deleteMonitor = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const editMonitor = async (req, res) => {
|
||||||
|
const monitorId = req.params.monitorId;
|
||||||
|
const idx = FAKE_MONITOR_DATA.findIndex((monitor) => {
|
||||||
|
return monitor._id.toString() === monitorId;
|
||||||
|
});
|
||||||
|
const oldMonitor = FAKE_MONITOR_DATA[idx];
|
||||||
|
const editedMonitor = new Monitor({ ...req.body });
|
||||||
|
editedMonitor._id = oldMonitor._id;
|
||||||
|
editedMonitor.userId = oldMonitor.userId;
|
||||||
|
editedMonitor.updatedAt = Date.now();
|
||||||
|
editedMonitor.createdAt = oldMonitor.createdAt;
|
||||||
|
FAKE_MONITOR_DATA[idx] = editedMonitor;
|
||||||
|
return FAKE_MONITOR_DATA[idx];
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
connect,
|
connect,
|
||||||
insertUser,
|
insertUser,
|
||||||
@@ -130,4 +132,5 @@ module.exports = {
|
|||||||
getMonitorsByUserId,
|
getMonitorsByUserId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
|
editMonitor,
|
||||||
};
|
};
|
||||||
+25
-2
@@ -61,7 +61,7 @@ const getUserByEmail = async (req, res) => {
|
|||||||
*/
|
*/
|
||||||
const getAllMonitors = async (req, res) => {
|
const getAllMonitors = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const monitors = await Monitor.find({ isActive: true });
|
const monitors = await Monitor.find();
|
||||||
return monitors;
|
return monitors;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
@@ -120,7 +120,6 @@ const createMonitor = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delete Monitor
|
|
||||||
/**
|
/**
|
||||||
* Delete a monitor by ID
|
* Delete a monitor by ID
|
||||||
* @async
|
* @async
|
||||||
@@ -142,6 +141,29 @@ const deleteMonitor = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit a monitor by ID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Monitor>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const editMonitor = async (req, res) => {
|
||||||
|
const candidateId = req.params.monitorId;
|
||||||
|
const candidateMonitor = req.body;
|
||||||
|
try {
|
||||||
|
const editedMonitor = await Monitor.findByIdAndUpdate(
|
||||||
|
candidateId,
|
||||||
|
candidateMonitor,
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
return editedMonitor;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
connect,
|
connect,
|
||||||
insertUser,
|
insertUser,
|
||||||
@@ -151,4 +173,5 @@ module.exports = {
|
|||||||
getMonitorsByUserId,
|
getMonitorsByUserId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
|
editMonitor,
|
||||||
};
|
};
|
||||||
+31
-32
@@ -1,37 +1,36 @@
|
|||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
const MonitorSchema = mongoose.Schema({
|
const MonitorSchema = mongoose.Schema(
|
||||||
userId: {
|
{
|
||||||
type: String,
|
userId: {
|
||||||
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
|
ref: "User",
|
||||||
|
immutable: true,
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
url: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
isActive: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true,
|
||||||
|
},
|
||||||
|
interval: {
|
||||||
|
// in milliseconds
|
||||||
|
type: Number,
|
||||||
|
default: 60000,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
name: {
|
{
|
||||||
type: String,
|
timestamps: true,
|
||||||
required: true,
|
}
|
||||||
},
|
);
|
||||||
description: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
url: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
isActive: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
interval: {
|
|
||||||
// in milliseconds
|
|
||||||
type: Number,
|
|
||||||
default: 60000,
|
|
||||||
},
|
|
||||||
updated_at: {
|
|
||||||
type: Date,
|
|
||||||
default: Date.now,
|
|
||||||
},
|
|
||||||
created_at: {
|
|
||||||
type: Date,
|
|
||||||
default: Date.now,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = mongoose.model("Monitor", MonitorSchema);
|
module.exports = mongoose.model("Monitor", MonitorSchema);
|
||||||
@@ -7,4 +7,5 @@ router.get("/user/:userId", monitorController.getMonitorsByUserId);
|
|||||||
|
|
||||||
router.post("/", monitorController.createMonitor);
|
router.post("/", monitorController.createMonitor);
|
||||||
router.post("/delete/:monitorId", monitorController.deleteMonitor);
|
router.post("/delete/:monitorId", monitorController.deleteMonitor);
|
||||||
|
router.post("/edit/:monitorId", monitorController.editMonitor);
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
@@ -20,7 +20,8 @@ const getMonitorsByUserIdValidation = joi.object({
|
|||||||
userId: joi.string().required(),
|
userId: joi.string().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const createMonitorValidation = joi.object({
|
const monitorValidation = joi.object({
|
||||||
|
_id: joi.string(),
|
||||||
userId: joi.string().required(),
|
userId: joi.string().required(),
|
||||||
name: joi.string().required(),
|
name: joi.string().required(),
|
||||||
description: joi.string().required(),
|
description: joi.string().required(),
|
||||||
@@ -34,5 +35,5 @@ module.exports = {
|
|||||||
registerValidation,
|
registerValidation,
|
||||||
getMonitorByIdValidation,
|
getMonitorByIdValidation,
|
||||||
getMonitorsByUserIdValidation,
|
getMonitorsByUserIdValidation,
|
||||||
createMonitorValidation,
|
monitorValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user