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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import express from 'express';
|
||||
import { verifyJWT } from '../middleware/verifyJWT.js';
|
||||
import { triggerNotificationBodyValidation } from '../validation/joi.js';
|
||||
|
||||
class NotificationRoutes {
|
||||
constructor(notificationController) {
|
||||
@@ -9,33 +8,10 @@ class NotificationRoutes {
|
||||
this.initializeRoutes();
|
||||
}
|
||||
|
||||
validateRequest(schema) {
|
||||
return (req, res, next) => {
|
||||
const { error } = schema.validate(req.body, {
|
||||
abortEarly: false,
|
||||
stripUnknown: true
|
||||
});
|
||||
|
||||
if (error) {
|
||||
const errorMessage = error.details
|
||||
.map(detail => detail.message)
|
||||
.join(', ');
|
||||
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
msg: errorMessage
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
}
|
||||
|
||||
initializeRoutes() {
|
||||
this.router.post(
|
||||
'/trigger',
|
||||
verifyJWT,
|
||||
this.validateRequest(triggerNotificationBodyValidation),
|
||||
this.notificationController.triggerNotification
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user