Skip to content

Commit e5ff5cd

Browse files
Copilotzaunist
andcommitted
Fix: Auto-create notification templates when missing (Issue #80)
When sending agent offline notifications, if the user has no notification templates (e.g., for users created before default templates feature was added), automatically create default Monitor and Agent templates. This ensures notifications can be sent even for older users who don't have templates configured. Co-authored-by: zaunist <38528079+zaunist@users.noreply.github.com>
1 parent fad473c commit e5ff5cd

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

backend/src/services/NotificationService.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,44 @@ async function sendWeComNotification(
652652

653653
registerSender("wecom", sendWeComNotification);
654654

655+
/**
656+
* 为用户创建默认通知模板(仅模板,不包括渠道和设置)
657+
* 用于在发送通知时,如果用户没有任何模板,则自动创建默认模板
658+
* @param userId 用户ID
659+
*/
660+
async function createDefaultTemplatesForUser(userId: number): Promise<void> {
661+
try {
662+
console.log(`为用户 ${userId} 创建默认通知模板...`);
663+
664+
// 创建 Monitor 监控模板
665+
await repositories.createNotificationTemplate({
666+
name: "Monitor监控模板",
667+
type: "monitor",
668+
subject: "【${status}】${name} 监控状态变更",
669+
content:
670+
"🔔 网站监控状态变更通知\n\n📊 服务: ${name}\n🔄 状态: ${status} (之前: ${previous_status})\n🕒 时间: ${time}\n\n🔗 地址: ${url}\n⏱️ 响应时间: ${response_time}\n📝 实际状态码: ${status_code}\n🎯 期望状态码: ${expected_status}\n\n❗ 错误信息: ${error}",
671+
is_default: true,
672+
created_by: userId,
673+
});
674+
675+
// 创建 Agent 监控模板
676+
await repositories.createNotificationTemplate({
677+
name: "Agent监控模板",
678+
type: "agent",
679+
subject: "【${status}】${name} 客户端状态变更",
680+
content:
681+
"🔔 客户端状态变更通知\n\n📊 主机: ${name}\n🔄 状态: ${status} (之前: ${previous_status})\n🕒 时间: ${time}\n\n🖥️ 主机信息:\n 主机名: ${hostname}\n IP地址: ${ip_addresses}\n 操作系统: ${os}\n\n❗ 错误信息: ${error}",
682+
is_default: true,
683+
created_by: userId,
684+
});
685+
686+
console.log(`为用户 ${userId} 创建默认通知模板成功`);
687+
} catch (error) {
688+
console.error(`为用户 ${userId} 创建默认通知模板失败:`, error);
689+
// 不抛出异常,让调用方继续执行
690+
}
691+
}
692+
655693
export async function sendNotification(
656694
type: "monitor" | "agent" | "system",
657695
targetId: number | null,
@@ -674,9 +712,23 @@ export async function sendNotification(
674712
}
675713

676714
// 获取默认的通知模板
677-
const templates = await repositories.getNotificationTemplates(userId);
715+
let templates = await repositories.getNotificationTemplates(userId);
678716
console.log(`[发送通知] 获取到${templates.length}个通知模板`);
679717

718+
// 如果用户没有任何模板,为其创建默认模板
719+
if (templates.length === 0) {
720+
console.log(`[发送通知] 用户${userId}没有通知模板,正在创建默认模板...`);
721+
await createDefaultTemplatesForUser(userId);
722+
// 重新获取模板
723+
templates = await repositories.getNotificationTemplates(userId);
724+
console.log(`[发送通知] 为用户${userId}创建默认模板后,获取到${templates.length}个通知模板`);
725+
726+
// 如果模板创建后仍然为空,记录警告
727+
if (templates.length === 0) {
728+
console.error(`[发送通知] 为用户${userId}创建默认模板失败,请检查日志`);
729+
}
730+
}
731+
680732
let defaultTemplate = templates.find(
681733
(t) => t.is_default && t.type === type
682734
);

0 commit comments

Comments
 (0)