Merge branch 'develop' into feat/incidents-clear

This commit is contained in:
Alex Holliday committed 2025-06-25 09:03:00 +08:00
commit 95b832c1c6
93 files changed
+3104 -2182

No files matched your search

+3 -11
View File
@@ -82,7 +82,7 @@ class AuthController {
}
const newUser = await this.db.insertUser({ ...req.body }, req.file);
logger.info({
this.logger.info({
message: this.stringService.authCreateUser,
service: SERVICE_NAME,
details: newUser._id,
@@ -111,7 +111,7 @@ class AuthController {
});
});
} catch (error) {
logger.warn({
this.logger.warn({
message: error.message,
service: SERVICE_NAME,
method: "registerUser",
@@ -412,8 +412,7 @@ class AuthController {
// 1. Find all the monitors associated with the team ID if superadmin
const result = await this.db.getMonitorsByTeamId({
query: {},
params: { teamId: user.teamId.toString() },
teamId: user.teamId.toString(),
});
if (user.role.includes("superadmin")) {
@@ -424,13 +423,6 @@ class AuthController {
await this.jobQueue.deleteJob(monitor);
})
));
// 3. Delete team
await this.db.deleteTeam(user.teamId);
// 4. Delete all other team members
await this.db.deleteAllOtherUsers();
// 5. Delete each monitor
await this.db.deleteMonitorsByUserId(user._id);
}
// 6. Delete the user by id
await this.db.deleteUser(user._id);
+23
View File
@@ -0,0 +1,23 @@
import { handleError } from "./controllerUtils.js";
const SERVICE_NAME = "JobQueueController";
class LogController {
constructor(logger) {
this.logger = logger;
}
getLogs = async (req, res, next) => {
try {
const logs = await this.logger.getLogs();
res.success({
msg: "Logs fetched successfully",
data: logs,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getLogs"));
return;
}
};
}
export default LogController;
+37 -1
View File
@@ -681,7 +681,7 @@ class MonitorController {
explain,
});
return res.success({
msg: "OK", // TODO
msg: "OK",
data: result,
});
} catch (error) {
@@ -698,6 +698,42 @@ class MonitorController {
next(handleError(error, SERVICE_NAME, "seedDb"));
}
};
exportMonitorsToCSV = async (req, res, next) => {
try {
const { teamId } = req.user;
const monitors = await this.db.getMonitorsByTeamId({ teamId });
if (!monitors || monitors.length === 0) {
return res.success({
msg: this.stringService.noMonitorsFound,
data: null,
});
}
const csvData = monitors?.filteredMonitors?.map((monitor) => ({
name: monitor.name,
description: monitor.description,
type: monitor.type,
url: monitor.url,
interval: monitor.interval,
port: monitor.port,
ignoreTlsErrors: monitor.ignoreTlsErrors,
isActive: monitor.isActive,
}));
const csv = pkg.unparse(csvData);
return res.file({
data: csv,
headers: {
"Content-Type": "text/csv",
"Content-Disposition": "attachment; filename=monitors.csv",
},
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "exportMonitorsToCSV"));
}
};
}
export default MonitorController;
+1 -73
View File
@@ -23,85 +23,13 @@ class NotificationController {
this.stringService = stringService;
this.statusService = statusService;
this.db = db;
this.triggerNotification = this.triggerNotification.bind(this);
}
async triggerNotification(req, res, next) {
try {
await triggerNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
stripUnknown: true,
});
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const { monitorId, type, platform, config, status = false } = req.body;
// Create a simplified networkResponse similar to what would come from monitoring
const networkResponse = {
monitorId,
status,
};
// Use the statusService to get monitor details and handle status change logic
// This returns { monitor, statusChanged, prevStatus } exactly like your job queue uses
const statusResult = await this.statusService.updateStatus(networkResponse);
if (type === NOTIFICATION_TYPES.WEBHOOK) {
const notification = {
type,
platform,
config,
};
await this.notificationService.sendWebhookNotification(
statusResult, // Contains monitor, statusChanged, and prevStatus
notification
);
}
return res.success({
msg: this.stringService.webhookSendSuccess,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "triggerNotification"));
}
}
createTestNetworkResponse() {
return {
monitor: {
_id: "test-monitor-id",
name: "Test Monitor",
url: "https://example.com",
},
status: true,
statusChanged: true,
prevStatus: false,
};
}
testNotification = async (req, res, next) => {
try {
const notification = req.body;
let success;
if (notification?.type === "email") {
success = await this.notificationService.sendTestEmail(notification);
}
if (notification?.type === "webhook") {
success =
await this.notificationService.sendTestWebhookNotification(notification);
}
if (notification?.type === "pager_duty") {
success =
await this.notificationService.sendTestPagerDutyNotification(notification);
}
const success = await this.notificationService.sendTestNotification(notification);
if (!success) {
return res.error({
+15 -1
View File
@@ -25,7 +25,7 @@ class JobQueueController {
try {
const jobs = await this.jobQueue.getJobs();
return res.success({
msg: this.stringService.queueGetMetrics,
msg: this.stringService.queueGetJobs,
data: jobs,
});
} catch (error) {
@@ -34,6 +34,20 @@ class JobQueueController {
}
};
getAllMetrics = async (req, res, next) => {
try {
const jobs = await this.jobQueue.getJobs();
const metrics = await this.jobQueue.getMetrics();
return res.success({
msg: this.stringService.queueGetAllMetrics,
data: { jobs, metrics },
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getAllMetrics"));
return;
}
};
addJob = async (req, res, next) => {
try {
await this.jobQueue.addJob(Math.random().toString(36).substring(7));