import { NextRequest, NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { isSuperAdmin } from '@/lib/department-permission' import { buildExternalNotificationTargetAdminView, getExternalNotificationTargetById, updateExternalNotificationTarget, type ExternalNotificationTarget, } from '@/lib/external-auto-listing' async function assertAdminPermission() { const session = await getServerSession(authOptions) if (!session?.user || !(session.user as any).id) { return NextResponse.json({ success: false, error: '未登录' }, { status: 401 }) } const userId = (session.user as any).id if (!(await isSuperAdmin(userId))) { return NextResponse.json({ success: false, error: '仅超级管理员可操作' }, { status: 403 }) } return null } function normalizeAuthConfigInput( value: unknown, existing: ExternalNotificationTarget, nextAuthType: string, ): Record | undefined { const record = value && typeof value === 'object' && !Array.isArray(value) ? value as Record : null if (!record) return undefined if (nextAuthType === 'bearer') { const token = String(record.token || '').trim() return { token: token || String(existing.authConfig.token || '').trim() } } if (nextAuthType === 'header') { const headerName = String(record.headerName || existing.authConfig.headerName || '').trim() const headerValue = String(record.headerValue || '').trim() || String(existing.authConfig.headerValue || '').trim() const nextConfig: Record = {} if (headerName) nextConfig.headerName = headerName if (headerValue) nextConfig.headerValue = headerValue return nextConfig } return {} } function toErrorResponse(error: unknown) { const message = error instanceof Error ? error.message : '服务器错误' const status = message === '通知目标不存在' ? 404 : message.includes('不能为空') || message.includes('格式不正确') || message.includes('仅支持') || message.includes('必须提供') ? 400 : 500 return NextResponse.json({ success: false, error: message }, { status }) } export async function PUT( request: NextRequest, { params }: { params: { id: string } }, ) { try { const authError = await assertAdminPermission() if (authError) { return authError } const targetId = String(params?.id || '').trim() if (!targetId) { return NextResponse.json({ success: false, error: '通知目标不存在' }, { status: 404 }) } const body = await request.json() const existing = await getExternalNotificationTargetById(targetId) if (!existing) { return NextResponse.json({ success: false, error: '通知目标不存在' }, { status: 404 }) } const nextAuthType = typeof body?.authType === 'string' ? body.authType : existing.authType const target = await updateExternalNotificationTarget(targetId, { name: typeof body?.name === 'string' ? body.name : undefined, callbackUrl: typeof body?.callbackUrl === 'string' ? body.callbackUrl : undefined, isActive: typeof body?.isActive === 'boolean' ? body.isActive : undefined, authType: typeof body?.authType === 'string' ? body.authType : undefined, authConfig: normalizeAuthConfigInput(body?.authConfig, existing, nextAuthType), eventTypes: Array.isArray(body?.eventTypes) ? body.eventTypes : undefined, }) return NextResponse.json({ success: true, data: buildExternalNotificationTargetAdminView(target), message: '通知目标更新成功', }) } catch (error) { console.error('[admin/external-notification-targets/[id]] PUT error:', error) return toErrorResponse(error) } }