Moved validation to the controller.
This commit is contained in:
1 parent
8409571d57
commit
55c1a1c233
2 files changed
+26
-32
No files matched your search
@@ -1,3 +1,13 @@
|
||||
import {
|
||||
triggerNotificationBodyValidation,
|
||||
telegramWebhookConfigValidation,
|
||||
discordWebhookConfigValidation,
|
||||
slackWebhookConfigValidation
|
||||
} from '../validation/joi.js';
|
||||
import { handleError, handleValidationError } from './controllerUtils.js';
|
||||
|
||||
const SERVICE_NAME = "NotificationController";
|
||||
|
||||
class NotificationController {
|
||||
constructor(notificationService) {
|
||||
this.notificationService = notificationService;
|
||||
@@ -5,20 +15,30 @@ class NotificationController {
|
||||
}
|
||||
|
||||
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 } = req.body;
|
||||
|
||||
|
||||
const networkResponse = {
|
||||
monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" },
|
||||
status: false,
|
||||
statusChanged: true,
|
||||
prevStatus: true,
|
||||
};
|
||||
|
||||
|
||||
if (type === "webhook") {
|
||||
const notification = {
|
||||
type,
|
||||
platform,
|
||||
platform,
|
||||
config
|
||||
};
|
||||
|
||||
@@ -27,15 +47,13 @@ class NotificationController {
|
||||
notification
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return res.success({
|
||||
msg: "Notification sent successfully"
|
||||
});
|
||||
|
||||
|
||||
} catch (error) {
|
||||
error.service = "NotificationController";
|
||||
error.method = "triggerNotification";
|
||||
next(error);
|
||||
next(handleError(error, SERVICE_NAME, "triggerNotification"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user