Feat/monitor endpoint refactor (#463)

* Moved all methods to modules

* removed old mongoDB file

* Refactored endpoints to take query params, removed duplicate methods, updated routes, updated Client queries

* Added comment

* Add model differentiation

* Add sort order, fix minor bug, add validation for query params
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-07-26 18:56:39 -07:00
1 parent e47d3685ce
commit 477cad7cc9
11 files changed
+132 -166

No files matched your search

+68 -74
View File
@@ -1,6 +1,6 @@
const Monitor = require("../../../models/Monitor");
const Check = require("../../../models/Check");
const PageSpeedCheck = require("../../../models/PageSpeedCheck");
const { errorMessages } = require("../../../utils/messages");
/**
@@ -30,10 +30,40 @@ const getAllMonitors = async (req, res) => {
*/
const getMonitorById = async (req, res) => {
try {
const monitor = await Monitor.findById(req.params.monitorId);
const checks = await Check.find({ monitorId: monitor._id }).sort({
createdAt: 1,
});
const { monitorId } = req.params;
let { status, limit, sortOrder } = req.query;
// This effectively removes limit, returning all checks
if (limit === undefined) limit = 0;
// Default sort order is newest -> oldest
if (sortOrder === "asc") {
sortOrder = 1;
} else if (sortOrder === "desc") {
sortOrder = -1;
} else sortOrder = -1;
const monitor = await Monitor.findById(monitorId);
const checksQuery = { monitorId: monitor._id };
if (status !== undefined) {
checksQuery.status = status;
}
// Determine model type
let model =
monitor.type === "http" || monitor.type === "ping"
? Check
: PageSpeedCheck;
const checks = await model
.find(checksQuery)
.sort({
createdAt: sortOrder,
})
.limit(limit);
const monitorWithChecks = { ...monitor.toObject(), checks };
return monitorWithChecks;
} catch (error) {
@@ -51,79 +81,45 @@ const getMonitorById = async (req, res) => {
*/
const getMonitorsByUserId = async (req, res) => {
try {
const limit = req.query ? req.query.limit : undefined;
const monitors = await Monitor.find({ userId: req.params.userId });
console.log("monitors", monitors);
let { limit, type, status, sortOrder } = req.query;
const monitorQuery = { userId: req.params.userId };
if (type !== undefined) {
const types = Array.isArray(type) ? type : [type];
monitorQuery.type = { $in: types };
}
// Default sort order is newest -> oldest
if (sortOrder === "asc") {
sortOrder = 1;
} else if (sortOrder === "desc") {
sortOrder = -1;
} else sortOrder = -1;
// This effectively removes limit, returning all checks
if (limit === undefined) limit = 0;
const monitors = await Monitor.find(monitorQuery);
// Map each monitor to include its associated checks
const monitorsWithChecks = await Promise.all(
monitors.map(async (monitor) => {
if (limit) {
// Checks are order newest -> oldest
const checks = await Check.find({ monitorId: monitor._id })
.sort({
createdAt: -1,
})
.limit(limit);
return { ...monitor.toObject(), checks };
} else {
const checks = await Check.find({ monitorId: monitor._id }).sort({
createdAt: -1,
});
return { ...monitor.toObject(), checks };
const checksQuery = { monitorId: monitor._id };
if (status !== undefined) {
checksQuery.status = status;
}
})
);
return monitorsWithChecks;
} catch (error) {
throw error;
}
};
let model =
monitor.type === "http" || monitor.type === "ping"
? Check
: PageSpeedCheck;
/**
* Get monitors by UserID
* @async
* @param {Express.Request} req
* @param {Express.Response} res
* @returns {Promise<Monitor>}
* @throws {Error}
*/
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
* @async
* @param {Express.Request} req
* @param {Express.Response} res
* @returns {Promise<Array<Monitor>>}
* @throws {Error}
*/
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,
});
// Checks are order newest -> oldest
const checks = await model
.find(checksQuery)
.sort({
createdAt: sortOrder,
})
.limit(limit);
return { ...monitor.toObject(), checks };
})
);
@@ -228,8 +224,6 @@ module.exports = {
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
getMonitorByIdForIncidents,
getMonitorsByUserIdForIncidents,
createMonitor,
deleteMonitor,
deleteAllMonitors,