add test notificaiton button and methods

This commit is contained in:
Alex Holliday committed 2025-06-10 12:07:31 +08:00
1 parent 1ed317d0cb
commit e6493df881
8 files changed
+135 -90

No files matched your search

+17 -77
View File
@@ -24,7 +24,6 @@ class NotificationController {
this.statusService = statusService;
this.db = db;
this.triggerNotification = this.triggerNotification.bind(this);
this.testWebhook = this.testWebhook.bind(this);
}
async triggerNotification(req, res, next) {
@@ -85,94 +84,35 @@ class NotificationController {
};
}
handleTelegramTest(botToken, chatId) {
if (!botToken || !chatId) {
return {
isValid: false,
error: {
msg: this.stringService.telegramRequiresBotTokenAndChatId,
status: 400,
},
};
}
return {
isValid: true,
notification: {
type: NOTIFICATION_TYPES.WEBHOOK,
platform: PLATFORMS.TELEGRAM,
config: { botToken, chatId },
},
};
}
handleWebhookTest(webhookUrl, platform) {
if (webhookUrl === null) {
return {
isValid: false,
error: {
msg: this.stringService.webhookUrlRequired,
status: 400,
},
};
}
return {
isValid: true,
notification: {
type: NOTIFICATION_TYPES.WEBHOOK,
platform: platform,
config: { webhookUrl },
},
};
}
async testWebhook(req, res, next) {
testNotification = async (req, res, next) => {
console.log("TESTING");
try {
const { webhookUrl, platform, botToken, chatId } = req.body;
const notification = req.body;
if (platform === null) {
return res.error({
msg: this.stringService.platformRequired,
status: 400,
if (notification?.type === "email") {
console.log("HANDLE EMAIL");
return res.success({
msg: this.stringService.emailSendSuccess,
});
}
// Platform-specific handling
const platformHandlers = {
[PLATFORMS.TELEGRAM]: () => this.handleTelegramTest(botToken, chatId),
// Default handler for webhook-based platforms (Slack, Discord, etc.)
default: () => this.handleWebhookTest(webhookUrl, platform),
};
const handler = platformHandlers[platform] || platformHandlers.default;
const handlerResult = handler();
if (!handlerResult.isValid) {
return res.error(handlerResult.error);
}
const networkResponse = this.createTestNetworkResponse();
const result = await this.notificationService.sendWebhookNotification(
networkResponse,
handlerResult.notification
);
if (result && result !== false) {
if (notification?.type === "webhook") {
const success =
await this.notificationService.sendTestWebhookNotification(notification);
if (!success) {
return res.error({
msg: this.stringService.webhookSendFailed,
status: 400,
});
}
return res.success({
msg: this.stringService.webhookSendSuccess,
});
} else {
return res.error({
msg: this.stringService.testNotificationFailed,
status: 400,
});
}
} catch (error) {
next(handleError(error, SERVICE_NAME, "testWebhook"));
}
}
};
createNotification = async (req, res, next) => {
try {