Merge pull request #289 from MuhammadKhalilzadeh/limit-checks

Limit checks
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-07-11 22:58:07 -07:00
commit 6100fb9dac
3 files changed
+20 -9

No files matched your search

+16 -5
View File
@@ -293,15 +293,26 @@ const getMonitorById = async (req, res) => {
*/
const getMonitorsByUserId = async (req, res) => {
try {
const limit = req.body.limit;
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) => {
// Checks are order oldest -> newest
const checks = await Check.find({ monitorId: monitor._id }).sort({
createdAt: 1,
});
return { ...monitor.toObject(), checks };
if(limit) {
// Checks are order oldest -> newest
const checks = await Check.find({ monitorId: monitor._id }).sort({
createdAt: 1,
}).limit(limit);;
return { ...monitor.toObject(), checks };
} else {
// Checks are order oldest -> newest
const checks = await Check.find({ monitorId: monitor._id }).sort({
createdAt: 1,
});
return { ...monitor.toObject(), checks };
}
})
);