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 {
|
class NotificationController {
|
||||||
constructor(notificationService) {
|
constructor(notificationService) {
|
||||||
this.notificationService = notificationService;
|
this.notificationService = notificationService;
|
||||||
@@ -5,20 +15,30 @@ 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;
|
||||||
|
|
||||||
const networkResponse = {
|
const networkResponse = {
|
||||||
monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" },
|
monitor: { _id: monitorId, name: "Test Monitor", url: "http://www.google.com" },
|
||||||
status: false,
|
status: false,
|
||||||
statusChanged: true,
|
statusChanged: true,
|
||||||
prevStatus: true,
|
prevStatus: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type === "webhook") {
|
if (type === "webhook") {
|
||||||
const notification = {
|
const notification = {
|
||||||
type,
|
type,
|
||||||
platform,
|
platform,
|
||||||
config
|
config
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -27,15 +47,13 @@ class NotificationController {
|
|||||||
notification
|
notification
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: "Notification sent successfully"
|
msg: "Notification sent successfully"
|
||||||
});
|
});
|
||||||
|
|
||||||
} 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