Moved validation to the controller.
This commit is contained in:
1 parent
8409571d57
commit
55c1a1c233
2 files changed
+21
-27
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 {
|
class NotificationController {
|
||||||
constructor(notificationService) {
|
constructor(notificationService) {
|
||||||
this.notificationService = notificationService;
|
this.notificationService = notificationService;
|
||||||
@@ -5,6 +15,16 @@ class NotificationController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async triggerNotification(req, res, next) {
|
async triggerNotification(req, res, next) {
|
||||||
|
try {
|
||||||
|
await triggerNotificationBodyValidation.validateAsync(req.body, {
|
||||||
|
abortEarly: false,
|
||||||
|
stripUnknown: true
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { monitorId, type, platform, config } = req.body;
|
const { monitorId, type, platform, config } = req.body;
|
||||||
|
|
||||||
@@ -33,9 +53,7 @@ class NotificationController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = "NotificationController";
|
next(handleError(error, SERVICE_NAME, "triggerNotification"));
|
||||||
error.method = "triggerNotification";
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { verifyJWT } from '../middleware/verifyJWT.js';
|
import { verifyJWT } from '../middleware/verifyJWT.js';
|
||||||
import { triggerNotificationBodyValidation } from '../validation/joi.js';
|
|
||||||
|
|
||||||
class NotificationRoutes {
|
class NotificationRoutes {
|
||||||
constructor(notificationController) {
|
constructor(notificationController) {
|
||||||
@@ -9,33 +8,10 @@ class NotificationRoutes {
|
|||||||
this.initializeRoutes();
|
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() {
|
initializeRoutes() {
|
||||||
this.router.post(
|
this.router.post(
|
||||||
'/trigger',
|
'/trigger',
|
||||||
verifyJWT,
|
verifyJWT,
|
||||||
this.validateRequest(triggerNotificationBodyValidation),
|
|
||||||
this.notificationController.triggerNotification
|
this.notificationController.triggerNotification
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user