import { NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { isSuperAdmin } from '@/lib/department-permission' import { type NotificationCenterAdminActor, NotificationCenterAdminError, } from '@/lib/notification-center-admin' export async function requireNotificationCenterSuperAdmin(): Promise< { actor: NotificationCenterAdminActor; session: unknown } | { response: NextResponse } > { const session = await getServerSession(authOptions) const user = (session as { user?: { id?: string; username?: string; realName?: string | null; email?: string | null } } | null)?.user if (!user?.id) { return { response: NextResponse.json({ success: false, error: '未登录' }, { status: 401 }), } } if (!(await isSuperAdmin(user.id))) { return { response: NextResponse.json({ success: false, error: '仅超级管理员可访问通知中心管理接口' }, { status: 403 }), } } return { actor: { userId: user.id, username: user.username || null, realName: user.realName || null, email: user.email || null, }, session, } } export function toNotificationCenterAdminErrorResponse(error: unknown) { if (error instanceof NotificationCenterAdminError) { return NextResponse.json( { success: false, error: error.message, ...(error.details ? { details: error.details } : {}), code: error.code, }, { status: error.statusCode }, ) } const message = error instanceof Error ? error.message : '通知中心请求失败' console.error('[notification-center-admin] route error:', message) return NextResponse.json( { success: false, error: '通知中心请求失败,请稍后重试', code: 'NOTIFICATION_CENTER_ADMIN_ROUTE_ERROR' }, { status: 500 }, ) }