use response middleware in MonitorController
This commit is contained in:
1 parent
f0bf680f19
commit
378a190cd3
2 files changed
+19
-33
No files matched your search
@@ -42,8 +42,7 @@ class MonitorController {
|
|||||||
getAllMonitors = async (req, res, next) => {
|
getAllMonitors = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const monitors = await this.db.getAllMonitors();
|
const monitors = await this.db.getAllMonitors();
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_GET_ALL,
|
msg: successMessages.MONITOR_GET_ALL,
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
@@ -64,8 +63,7 @@ class MonitorController {
|
|||||||
getAllMonitorsWithUptimeStats = async (req, res, next) => {
|
getAllMonitorsWithUptimeStats = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const monitors = await this.db.getAllMonitorsWithUptimeStats();
|
const monitors = await this.db.getAllMonitorsWithUptimeStats();
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_GET_ALL,
|
msg: successMessages.MONITOR_GET_ALL,
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
@@ -77,8 +75,7 @@ class MonitorController {
|
|||||||
getUptimeDetailsById = async (req, res, next) => {
|
getUptimeDetailsById = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const monitor = await this.db.getUptimeDetailsById(req);
|
const monitor = await this.db.getUptimeDetailsById(req);
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_GET_BY_ID,
|
msg: successMessages.MONITOR_GET_BY_ID,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
@@ -107,7 +104,7 @@ class MonitorController {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const monitorStats = await this.db.getMonitorStatsById(req);
|
const monitorStats = await this.db.getMonitorStatsById(req);
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MONITOR_STATS_BY_ID,
|
msg: successMessages.MONITOR_STATS_BY_ID,
|
||||||
data: monitorStats,
|
data: monitorStats,
|
||||||
@@ -136,8 +133,7 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const monitor = await this.db.getHardwareDetailsById(req);
|
const monitor = await this.db.getHardwareDetailsById(req);
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_GET_BY_ID,
|
msg: successMessages.MONITOR_GET_BY_ID,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
@@ -158,8 +154,7 @@ class MonitorController {
|
|||||||
const monitor = await this.db.getMonitorById(monitorId);
|
const monitor = await this.db.getMonitorById(monitorId);
|
||||||
const certificate = await fetchMonitorCertificate(sslChecker, monitor);
|
const certificate = await fetchMonitorCertificate(sslChecker, monitor);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_CERTIFICATE,
|
msg: successMessages.MONITOR_CERTIFICATE,
|
||||||
data: {
|
data: {
|
||||||
certificateDate: new Date(certificate.validTo),
|
certificateDate: new Date(certificate.validTo),
|
||||||
@@ -192,8 +187,7 @@ class MonitorController {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const monitor = await this.db.getMonitorById(req.params.monitorId);
|
const monitor = await this.db.getMonitorById(req.params.monitorId);
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_GET_BY_ID,
|
msg: successMessages.MONITOR_GET_BY_ID,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
@@ -237,8 +231,7 @@ class MonitorController {
|
|||||||
await monitor.save();
|
await monitor.save();
|
||||||
// Add monitor to job queue
|
// Add monitor to job queue
|
||||||
this.jobQueue.addJob(monitor._id, monitor);
|
this.jobQueue.addJob(monitor._id, monitor);
|
||||||
return res.status(201).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_CREATE,
|
msg: successMessages.MONITOR_CREATE,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
@@ -272,11 +265,9 @@ class MonitorController {
|
|||||||
timeout: 5000,
|
timeout: 5000,
|
||||||
validateStatus: () => true,
|
validateStatus: () => true,
|
||||||
});
|
});
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
status: response.status,
|
||||||
code: response.status,
|
msg: response.statusText,
|
||||||
statusText: response.statusText,
|
|
||||||
msg: `URL resolved successfully`,
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "checkEndpointResolution"));
|
next(handleError(error, SERVICE_NAME, "checkEndpointResolution"));
|
||||||
@@ -319,7 +310,7 @@ class MonitorController {
|
|||||||
stack: error.stack,
|
stack: error.stack,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return res.status(200).json({ success: true, msg: successMessages.MONITOR_DELETE });
|
return res.success({ msg: successMessages.MONITOR_DELETE });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "deleteMonitor"));
|
next(handleError(error, SERVICE_NAME, "deleteMonitor"));
|
||||||
}
|
}
|
||||||
@@ -359,9 +350,7 @@ class MonitorController {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return res
|
return res.success({ msg: `Deleted ${deletedCount} monitors` });
|
||||||
.status(200)
|
|
||||||
.json({ success: true, msg: `Deleted ${deletedCount} monitors` });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "deleteAllMonitors"));
|
next(handleError(error, SERVICE_NAME, "deleteAllMonitors"));
|
||||||
}
|
}
|
||||||
@@ -412,8 +401,7 @@ class MonitorController {
|
|||||||
await this.jobQueue.deleteJob(monitorBeforeEdit);
|
await this.jobQueue.deleteJob(monitorBeforeEdit);
|
||||||
// Add the new job back to the queue
|
// Add the new job back to the queue
|
||||||
await this.jobQueue.addJob(editedMonitor._id, editedMonitor);
|
await this.jobQueue.addJob(editedMonitor._id, editedMonitor);
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_EDIT,
|
msg: successMessages.MONITOR_EDIT,
|
||||||
data: editedMonitor,
|
data: editedMonitor,
|
||||||
});
|
});
|
||||||
@@ -449,8 +437,7 @@ class MonitorController {
|
|||||||
monitor.isActive = !monitor.isActive;
|
monitor.isActive = !monitor.isActive;
|
||||||
monitor.status = undefined;
|
monitor.status = undefined;
|
||||||
monitor.save();
|
monitor.save();
|
||||||
return res.status(200).json({
|
return res.ssuccess({
|
||||||
success: true,
|
|
||||||
msg: monitor.isActive
|
msg: monitor.isActive
|
||||||
? successMessages.MONITOR_RESUME
|
? successMessages.MONITOR_RESUME
|
||||||
: successMessages.MONITOR_PAUSE,
|
: successMessages.MONITOR_PAUSE,
|
||||||
@@ -482,8 +469,7 @@ class MonitorController {
|
|||||||
demoMonitors.map((monitor) => this.jobQueue.addJob(monitor._id, monitor))
|
demoMonitors.map((monitor) => this.jobQueue.addJob(monitor._id, monitor))
|
||||||
);
|
);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_DEMO_ADDED,
|
msg: successMessages.MONITOR_DEMO_ADDED,
|
||||||
data: demoMonitors.length,
|
data: demoMonitors.length,
|
||||||
});
|
});
|
||||||
@@ -502,9 +488,8 @@ class MonitorController {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const monitors = await this.db.getMonitorsByTeamId(req);
|
const monitors = await this.db.getMonitorsByTeamId(req);
|
||||||
return res.status(200).json({
|
return res.success({
|
||||||
success: true,
|
msg: successMessages.MONITOR_GET_BY_TEAM_ID,
|
||||||
msg: "good",
|
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ const successMessages = {
|
|||||||
MONITOR_GET_ALL: "Got all monitors successfully",
|
MONITOR_GET_ALL: "Got all monitors successfully",
|
||||||
MONITOR_STATS_BY_ID: "Got monitor stats by Id successfully",
|
MONITOR_STATS_BY_ID: "Got monitor stats by Id successfully",
|
||||||
MONITOR_GET_BY_ID: "Got monitor by Id successfully",
|
MONITOR_GET_BY_ID: "Got monitor by Id successfully",
|
||||||
|
MONITOR_GET_BY_TEAM_ID: "Got monitors by Team Id successfully",
|
||||||
MONITOR_GET_BY_USER_ID: (userId) => `Got monitor for ${userId} successfully"`,
|
MONITOR_GET_BY_USER_ID: (userId) => `Got monitor for ${userId} successfully"`,
|
||||||
MONITOR_CREATE: "Monitor created successfully",
|
MONITOR_CREATE: "Monitor created successfully",
|
||||||
MONITOR_DELETE: "Monitor deleted successfully",
|
MONITOR_DELETE: "Monitor deleted successfully",
|
||||||
|
|||||||
Reference in new issue
Block a user