reactor getMonitorsByTeamId for clarity

This commit is contained in:
Alex Holliday committed 2024-11-05 12:39:35 +08:00
1 parent 4601f95686
commit 2873f68868
1 file changed
+15 -31
+15 -31
View File
@@ -383,9 +383,11 @@ const getMonitorsByTeamId = async (req, res) => {
field, field,
order, order,
} = req.query || {}; } = req.query || {};
const monitorQuery = { teamId: req.params.teamId }; const monitorQuery = { teamId: req.params.teamId };
if (type !== undefined) { if (type !== undefined) {
monitorQuery.type = type; monitorQuery.type = Array.isArray(type) ? { $in: type } : type;
} }
// Add filter if provided // Add filter if provided
// $options: "i" makes the search case-insensitive // $options: "i" makes the search case-insensitive
@@ -395,29 +397,13 @@ const getMonitorsByTeamId = async (req, res) => {
{ url: { $regex: filter, $options: "i" } }, { url: { $regex: filter, $options: "i" } },
]; ];
} }
const monitorsCount = await Monitor.countDocuments(monitorQuery); const monitorCount = await Monitor.countDocuments(monitorQuery);
// Pagination // Pagination
let skip = 0; const skip = page && rowsPerPage ? page * rowsPerPage : 0;
if (page && rowsPerPage) {
skip = page * rowsPerPage;
}
if (type !== undefined) { // Build Sort option
const types = Array.isArray(type) ? type : [type]; const sort = field ? { [field]: order === "asc" ? 1 : -1 } : {};
monitorQuery.type = { $in: types };
}
// Default sort order is newest -> oldest
if (checkOrder === "asc") {
checkOrder = 1;
} else checkOrder = -1;
// Sort order for monitors
let sort = {};
if (field !== undefined && order !== undefined) {
sort[field] = order === "asc" ? 1 : -1;
}
const monitors = await Monitor.find(monitorQuery) const monitors = await Monitor.find(monitorQuery)
.skip(skip) .skip(skip)
@@ -426,16 +412,12 @@ const getMonitorsByTeamId = async (req, res) => {
// Early return if limit is set to -1, indicating we don't want any checks // Early return if limit is set to -1, indicating we don't want any checks
if (limit === "-1") { if (limit === "-1") {
return { monitors, monitorCount: monitorsCount }; return { monitors, monitorCount };
} }
// This effectively removes limit, returning all checks
if (limit === undefined) limit = 0;
// Map each monitor to include its associated checks // Map each monitor to include its associated checks
const monitorsWithChecks = await Promise.all( const monitorsWithChecks = await Promise.all(
monitors.map(async (monitor) => { monitors.map(async (monitor) => {
const checksQuery = { monitorId: monitor._id };
if (status !== undefined) { if (status !== undefined) {
checksQuery.status = status; checksQuery.status = status;
} }
@@ -444,11 +426,13 @@ const getMonitorsByTeamId = async (req, res) => {
// Checks are order newest -> oldest // Checks are order newest -> oldest
let checks = await model let checks = await model
.find(checksQuery) .find({
.sort({ monitorId: monitor._id,
createdAt: checkOrder, ...(status && { status }),
}) })
.limit(limit); .sort({ createdAt: checkOrder === "asc" ? 1 : -1 })
.limit(limit || 0);
//Normalize checks if requested //Normalize checks if requested
if (normalize !== undefined) { if (normalize !== undefined) {
@@ -457,7 +441,7 @@ const getMonitorsByTeamId = async (req, res) => {
return { ...monitor.toObject(), checks }; return { ...monitor.toObject(), checks };
}) })
); );
return { monitors: monitorsWithChecks, monitorCount: monitorsCount }; return { monitors: monitorsWithChecks, monitorCount };
} catch (error) { } catch (error) {
error.service = SERVICE_NAME; error.service = SERVICE_NAME;
error.method = "getMonitorsByTeamId"; error.method = "getMonitorsByTeamId";