Automated validation via joi.
This commit is contained in:
1 parent
023d9434c1
commit
012483d14f
3 files changed
+90
-37
No files matched your search
@@ -6,53 +6,21 @@ class NotificationController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async triggerNotification(req, res) {
|
async triggerNotification(req, res) {
|
||||||
const { monitorId, type, config } = req.body;
|
|
||||||
|
|
||||||
if (!monitorId || !type) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
msg: "monitorId and type are required"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const { monitorId, type, 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") {
|
||||||
if (!config?.type) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
msg: "webhook type is required in config"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.type === "telegram") {
|
|
||||||
if (!config.botToken || !config.chatId) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
msg: "botToken and chatId are required for Telegram notifications"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else if (["discord", "slack"].includes(config.type)) {
|
|
||||||
if (!config.webhookUrl) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
msg: `webhookUrl is required for ${config.type} notifications`
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.notificationService.sendWebhookNotification(
|
await this.notificationService.sendWebhookNotification(
|
||||||
networkResponse,
|
networkResponse,
|
||||||
config
|
config
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ success: true, msg: "Notification sent successfully" });
|
res.json({ success: true, msg: "Notification sent successfully" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error({
|
logger.error({
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
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) {
|
||||||
@@ -8,8 +9,35 @@ 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('/trigger', verifyJWT, this.notificationController.triggerNotification.bind(this.notificationController));
|
this.router.post(
|
||||||
|
'/trigger',
|
||||||
|
verifyJWT,
|
||||||
|
this.validateRequest(triggerNotificationBodyValidation),
|
||||||
|
this.notificationController.triggerNotification.bind(this.notificationController)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getRouter() {
|
getRouter() {
|
||||||
@@ -17,4 +45,4 @@ class NotificationRoutes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NotificationRoutes;
|
export default NotificationRoutes;
|
||||||
@@ -475,6 +475,59 @@ const imageValidation = joi
|
|||||||
"any.required": "Image file is required",
|
"any.required": "Image file is required",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const telegramWebhookConfigValidation = joi.object({
|
||||||
|
type: joi.string().valid('telegram').required(),
|
||||||
|
botToken: joi.string().required().messages({
|
||||||
|
'string.empty': 'Telegram bot token is required',
|
||||||
|
'any.required': 'Telegram bot token is required'
|
||||||
|
}),
|
||||||
|
chatId: joi.string().required().messages({
|
||||||
|
'string.empty': 'Telegram chat ID is required',
|
||||||
|
'any.required': 'Telegram chat ID is required'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
const discordWebhookConfigValidation = joi.object({
|
||||||
|
type: joi.string().valid('discord').required(),
|
||||||
|
webhookUrl: joi.string().uri().required().messages({
|
||||||
|
'string.empty': 'Discord webhook URL is required',
|
||||||
|
'string.uri': 'Discord webhook URL must be a valid URL',
|
||||||
|
'any.required': 'Discord webhook URL is required'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
const slackWebhookConfigValidation = joi.object({
|
||||||
|
type: joi.string().valid('slack').required(),
|
||||||
|
webhookUrl: joi.string().uri().required().messages({
|
||||||
|
'string.empty': 'Slack webhook URL is required',
|
||||||
|
'string.uri': 'Slack webhook URL must be a valid URL',
|
||||||
|
'any.required': 'Slack webhook URL is required'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
const triggerNotificationBodyValidation = joi.object({
|
||||||
|
monitorId: joi.string().required().messages({
|
||||||
|
'string.empty': 'Monitor ID is required',
|
||||||
|
'any.required': 'Monitor ID is required'
|
||||||
|
}),
|
||||||
|
type: joi.string().valid('webhook').required().messages({
|
||||||
|
'string.empty': 'Notification type is required',
|
||||||
|
'any.required': 'Notification type is required',
|
||||||
|
'any.only': 'Notification type must be webhook'
|
||||||
|
}),
|
||||||
|
config: joi.alternatives()
|
||||||
|
.conditional('type', {
|
||||||
|
is: 'webhook',
|
||||||
|
then: joi.alternatives().try(
|
||||||
|
telegramWebhookConfigValidation,
|
||||||
|
discordWebhookConfigValidation,
|
||||||
|
slackWebhookConfigValidation
|
||||||
|
).required().messages({
|
||||||
|
'any.required': 'Webhook configuration is required'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
roleValidatior,
|
roleValidatior,
|
||||||
loginValidation,
|
loginValidation,
|
||||||
@@ -533,5 +586,9 @@ export {
|
|||||||
createStatusPageBodyValidation,
|
createStatusPageBodyValidation,
|
||||||
getStatusPageParamValidation,
|
getStatusPageParamValidation,
|
||||||
getStatusPageQueryValidation,
|
getStatusPageQueryValidation,
|
||||||
imageValidation,
|
imageValidation,
|
||||||
|
triggerNotificationBodyValidation,
|
||||||
|
telegramWebhookConfigValidation,
|
||||||
|
discordWebhookConfigValidation,
|
||||||
|
slackWebhookConfigValidation
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user