49 lines
1.7 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { isSuperAdmin } from '@/lib/department-permission'
import { listExternalNotificationDeliveries } 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
}
export async function GET(request: NextRequest) {
try {
const authError = await assertAdminPermission()
if (authError) {
return authError
}
const { searchParams } = new URL(request.url)
const rawLimit = searchParams.get('limit')
const parsedLimit = rawLimit ? Number(rawLimit) : undefined
const limit = Number.isFinite(parsedLimit) && parsedLimit! > 0 ? parsedLimit : undefined
const deliveries = await listExternalNotificationDeliveries({
status: searchParams.get('status') || undefined,
targetId: searchParams.get('targetId') || undefined,
materialId: searchParams.get('materialId') || undefined,
limit,
})
return NextResponse.json({
success: true,
data: deliveries,
})
} catch (error) {
console.error('[admin/external-notification-deliveries] GET error:', error)
return NextResponse.json({ success: false, error: '服务器错误' }, { status: 500 })
}
}