Added checks to monitors for getMonitorsById and getMonitorsByUserId. Added TTL to checks (1 month)

This commit is contained in:
Alex Holliday committed 2024-06-18 12:45:25 -07:00
1 parent b4c5bcbb5f
commit 18df7d8fd5
2 files changed
+25 -6

No files matched your search

+15 -2
View File
@@ -195,7 +195,9 @@ const getAllMonitors = async (req, res) => {
const getMonitorById = async (req, res) => { const getMonitorById = async (req, res) => {
try { try {
const monitor = await Monitor.findById(req.params.monitorId); const monitor = await Monitor.findById(req.params.monitorId);
return monitor; const checks = await Check.find({ monitorId: monitor._id });
const monitorWithChecks = { ...monitor.toObject(), checks };
return monitorWithChecks;
} catch (error) { } catch (error) {
throw error; throw error;
} }
@@ -212,7 +214,18 @@ const getMonitorById = async (req, res) => {
const getMonitorsByUserId = async (req, res) => { const getMonitorsByUserId = async (req, res) => {
try { try {
const monitors = await Monitor.find({ userId: req.params.userId }); const monitors = await Monitor.find({ userId: req.params.userId });
return monitors; // 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 };
})
);
return monitorsWithChecks;
} catch (error) { } catch (error) {
throw error; throw error;
} }
+10 -4
View File
@@ -1,10 +1,10 @@
const mongoose = require('mongoose') const mongoose = require("mongoose");
const CheckSchema = mongoose.Schema( const CheckSchema = mongoose.Schema(
{ {
monitorId: { monitorId: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: 'Monitor', ref: "Monitor",
immutable: true, immutable: true,
}, },
status: { status: {
@@ -18,11 +18,17 @@ const CheckSchema = mongoose.Schema(
}, },
message: { message: {
type: String, type: String,
}
}, },
expiry: {
type: Date,
default: Date.now,
expires: 60 * 60 * 24 * 30, // 30 days in seconds
},
},
{ {
timestamps: true, timestamps: true,
} }
); );
module.exports = mongoose.model('Check', CheckSchema); module.exports = mongoose.model("Check", CheckSchema);