Added endponts and routes for fetching monitors with incidents
This commit is contained in:
1 parent
a85ae41557
commit
e63d577fa5
3 files changed
+87
-4
No files matched your search
@@ -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) => {
|
const getMonitorByIdForIncidents = async (req, res, next) => {
|
||||||
try {
|
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) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
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) => {
|
const getMonitorsByUserIdForIncidents = async (req, res, next) => {
|
||||||
try {
|
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) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
@@ -242,6 +281,8 @@ module.exports = {
|
|||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
getMonitorsByUserId,
|
getMonitorsByUserId,
|
||||||
|
getMonitorByIdForIncidents,
|
||||||
|
getMonitorsByUserIdForIncidents,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
deleteAllMonitors,
|
deleteAllMonitors,
|
||||||
|
|||||||
+37
-4
@@ -131,7 +131,6 @@ const updateUser = async (req, res) => {
|
|||||||
)
|
)
|
||||||
.select("-password")
|
.select("-password")
|
||||||
.select("-profileImage");
|
.select("-profileImage");
|
||||||
console.log(updatedUser);
|
|
||||||
return updatedUser;
|
return updatedUser;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
@@ -346,7 +345,21 @@ const getMonitorsByUserId = async (req, res) => {
|
|||||||
* @returns {Promise<Monitor>}
|
* @returns {Promise<Monitor>}
|
||||||
* @throws {Error}
|
* @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
|
* Get monitors by UserID
|
||||||
@@ -356,7 +369,26 @@ const getMonitorByIdForIncidents = async (req, res, next) => {};
|
|||||||
* @returns {Promise<Array<Monitor>>}
|
* @returns {Promise<Array<Monitor>>}
|
||||||
* @throws {Error}
|
* @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
|
* Create a monitor
|
||||||
@@ -468,7 +500,6 @@ const editMonitor = async (req, res) => {
|
|||||||
|
|
||||||
const createCheck = async (checkData) => {
|
const createCheck = async (checkData) => {
|
||||||
try {
|
try {
|
||||||
console.log(checkData);
|
|
||||||
const check = await new Check({ ...checkData }).save();
|
const check = await new Check({ ...checkData }).save();
|
||||||
return check;
|
return check;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -657,6 +688,8 @@ module.exports = {
|
|||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
getMonitorsByUserId,
|
getMonitorsByUserId,
|
||||||
|
getMonitorByIdForIncidents,
|
||||||
|
getMonitorsByUserIdForIncidents,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
deleteAllMonitors,
|
deleteAllMonitors,
|
||||||
|
|||||||
@@ -7,6 +7,15 @@ router.get("/", monitorController.getAllMonitors);
|
|||||||
router.get("/:monitorId", monitorController.getMonitorById);
|
router.get("/:monitorId", monitorController.getMonitorById);
|
||||||
router.get("/user/:userId", monitorController.getMonitorsByUserId);
|
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("/", monitorController.createMonitor);
|
||||||
router.post(
|
router.post(
|
||||||
"/delete/:monitorId",
|
"/delete/:monitorId",
|
||||||
|
|||||||
Reference in new issue
Block a user