32 lines
1.3 KiB
TypeScript
32 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { z } from 'zod'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { isSuperAdmin } from '@/lib/department-permission'
|
|
import { AutoListingCallbackError } from '@/lib/external-auto-listing-callback'
|
|
|
|
export const targetIdSchema = z.string().trim().min(1).max(128)
|
|
|
|
export function autoListingResponse(body: object, status = 200) {
|
|
return NextResponse.json(body, { status, headers: { 'Cache-Control': 'no-store' } })
|
|
}
|
|
|
|
export function autoListingFailure(error: string, status: number) {
|
|
return autoListingResponse({ success: false, error }, status)
|
|
}
|
|
|
|
export async function withAutoListingSuperAdmin(handle: () => Promise<NextResponse>) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
const userId = session?.user?.id
|
|
if (!userId || !(await isSuperAdmin(userId))) {
|
|
return autoListingFailure('仅超级管理员可操作', 403)
|
|
}
|
|
return await handle()
|
|
} catch (error) {
|
|
if (error instanceof AutoListingCallbackError) return autoListingFailure(error.message, error.status)
|
|
console.error('[admin/auto-listing-targets] request failed', error instanceof Error ? error.name : 'UnknownError')
|
|
return autoListingFailure('操作失败,请稍后重试', 500)
|
|
}
|
|
}
|