// POST /api/alert-channels/[id]/test — 测试推送 import { NextRequest, NextResponse } from 'next/server' import { verifyJwt } from '@shared/lib/auth/jwt' import { authConfig } from '@/lib/auth-config' import { dbQuery } from '@/lib/db' import { WeChatPusher } from '@shared/lib/wechat/wechat-pusher' import { hasPermission } from '@/lib/permissions' import { getRole } from '@/lib/get-role' export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { const token = request.cookies.get('tlyq_session')?.value if (!token) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) const payload = verifyJwt(token, authConfig.jwtSecret) if (!payload || !hasPermission(getRole(payload), 'alerts:manage')) { return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) } const { id } = await params const channels = dbQuery<{ webhook_url: string }>(`SELECT webhook_url FROM alert_channels WHERE id = ${Number(id)}`) if (channels.length === 0) return NextResponse.json({ error: 'Channel not found' }, { status: 404 }) const pusher = new WeChatPusher(String(channels[0].webhook_url)) const result = await pusher.pushMarkdown('测试告警', `monitor-ai 告警测试消息\n时间: ${new Date().toLocaleString('zh-CN')}`) return NextResponse.json(result) }