mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-12 20:59:41 -06:00
Added endponts and routes for fetching monitors with incidents
This commit is contained in:
@@ -98,15 +98,54 @@ const getMonitorsByUserId = async (req, res, next) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns monitor with matching ID and incidents
|
||||
* @async
|
||||
* @param {Express.Request} req
|
||||
* @param {Express.Response} res
|
||||
* @returns {Promise<Express.Response>}
|
||||
* @throws {Error}
|
||||
*/
|
||||
const getMonitorByIdForIncidents = async (req, res, next) => {
|
||||
try {
|
||||
await getMonitorByIdValidation.validateAsync(req.params);
|
||||
|
||||
let monitorWithIncidents = await req.db.getMonitorByIdForIncidents(
|
||||
req,
|
||||
res
|
||||
);
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
msg: successMessages.MONTIOR_GET_BY_ID,
|
||||
data: monitorWithIncidents,
|
||||
});
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns all monitors with incidents that belong to User with UserID
|
||||
* @async
|
||||
* @param {Express.Request} req
|
||||
* @param {Express.Response} res
|
||||
* @returns {Promise<Express.Response>}
|
||||
* @throws {Error}
|
||||
*/
|
||||
const getMonitorsByUserIdForIncidents = async (req, res, next) => {
|
||||
try {
|
||||
await getMonitorsByUserIdValidation.validateAsync(req.params);
|
||||
let monitorsWithIncidents = await req.db.getMonitorsByUserIdForIncidents(
|
||||
req,
|
||||
res
|
||||
);
|
||||
return res.json({
|
||||
success: true,
|
||||
msg: successMessages.MONITOR_GET_BY_USER_ID(req.params.userId),
|
||||
data: monitorsWithIncidents,
|
||||
});
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
@@ -242,6 +281,8 @@ module.exports = {
|
||||
getAllMonitors,
|
||||
getMonitorById,
|
||||
getMonitorsByUserId,
|
||||
getMonitorByIdForIncidents,
|
||||
getMonitorsByUserIdForIncidents,
|
||||
createMonitor,
|
||||
deleteMonitor,
|
||||
deleteAllMonitors,
|
||||
|
||||
@@ -131,7 +131,6 @@ const updateUser = async (req, res) => {
|
||||
)
|
||||
.select("-password")
|
||||
.select("-profileImage");
|
||||
console.log(updatedUser);
|
||||
return updatedUser;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@@ -346,7 +345,21 @@ const getMonitorsByUserId = async (req, res) => {
|
||||
* @returns {Promise<Monitor>}
|
||||
* @throws {Error}
|
||||
*/
|
||||
const getMonitorByIdForIncidents = async (req, res, next) => {};
|
||||
const getMonitorByIdForIncidents = async (req, res, next) => {
|
||||
try {
|
||||
const monitor = await Monitor.findById(req.params.monitorId);
|
||||
const checks = await Check.find({
|
||||
monitorId: monitor._id,
|
||||
status: false,
|
||||
}).sort({
|
||||
createdAt: 1,
|
||||
});
|
||||
const monitorWithChecks = { ...monitor.toObject(), checks };
|
||||
return monitorWithChecks;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get monitors by UserID
|
||||
@@ -356,7 +369,26 @@ const getMonitorByIdForIncidents = async (req, res, next) => {};
|
||||
* @returns {Promise<Array<Monitor>>}
|
||||
* @throws {Error}
|
||||
*/
|
||||
const getMonitorsByUserIdForIncidents = async (req, res, next) => {};
|
||||
const getMonitorsByUserIdForIncidents = async (req, res) => {
|
||||
try {
|
||||
const monitors = await Monitor.find({ userId: req.params.userId });
|
||||
// Map each monitor to include its associated checks
|
||||
const monitorsWithChecks = await Promise.all(
|
||||
monitors.map(async (monitor) => {
|
||||
const checks = await Check.find({
|
||||
monitorId: monitor._id,
|
||||
status: false,
|
||||
}).sort({
|
||||
createdAt: 1,
|
||||
});
|
||||
return { ...monitor.toObject(), checks };
|
||||
})
|
||||
);
|
||||
return monitorsWithChecks;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a monitor
|
||||
@@ -468,7 +500,6 @@ const editMonitor = async (req, res) => {
|
||||
|
||||
const createCheck = async (checkData) => {
|
||||
try {
|
||||
console.log(checkData);
|
||||
const check = await new Check({ ...checkData }).save();
|
||||
return check;
|
||||
} catch (error) {
|
||||
@@ -657,6 +688,8 @@ module.exports = {
|
||||
getAllMonitors,
|
||||
getMonitorById,
|
||||
getMonitorsByUserId,
|
||||
getMonitorByIdForIncidents,
|
||||
getMonitorsByUserIdForIncidents,
|
||||
createMonitor,
|
||||
deleteMonitor,
|
||||
deleteAllMonitors,
|
||||
|
||||
@@ -7,6 +7,15 @@ router.get("/", monitorController.getAllMonitors);
|
||||
router.get("/:monitorId", monitorController.getMonitorById);
|
||||
router.get("/user/:userId", monitorController.getMonitorsByUserId);
|
||||
|
||||
router.get(
|
||||
"/incidents/:monitorId",
|
||||
monitorController.getMonitorByIdForIncidents
|
||||
);
|
||||
router.get(
|
||||
"/incidents/user/:userId",
|
||||
monitorController.getMonitorsByUserIdForIncidents
|
||||
);
|
||||
|
||||
router.post("/", monitorController.createMonitor);
|
||||
router.post(
|
||||
"/delete/:monitorId",
|
||||
|
||||
Reference in New Issue
Block a user