94 lines
2.7 KiB
TypeScript
94 lines
2.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
|
|
import {
|
|
deleteNotificationCenterProject,
|
|
getNotificationCenterProject,
|
|
updateNotificationCenterProject,
|
|
} from '@/lib/notification-center-admin'
|
|
import {
|
|
requireNotificationCenterSuperAdmin,
|
|
toNotificationCenterAdminErrorResponse,
|
|
} from '../../_shared'
|
|
|
|
export const runtime = 'nodejs'
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
function readProjectCode(params: { code?: string }) {
|
|
return String(params?.code || '').trim()
|
|
}
|
|
|
|
export async function GET(
|
|
_request: NextRequest,
|
|
{ params }: { params: { code: string } },
|
|
) {
|
|
try {
|
|
const auth = await requireNotificationCenterSuperAdmin()
|
|
if ('response' in auth) return auth.response
|
|
|
|
const code = readProjectCode(params)
|
|
if (!code) {
|
|
return NextResponse.json({ success: false, error: '通知中心项目不存在' }, { status: 404 })
|
|
}
|
|
|
|
const result = await getNotificationCenterProject(auth.actor, code)
|
|
return NextResponse.json({ success: true, data: result.data }, { status: result.status })
|
|
} catch (error) {
|
|
return toNotificationCenterAdminErrorResponse(error)
|
|
}
|
|
}
|
|
|
|
async function handlePatch(
|
|
request: NextRequest,
|
|
{ params }: { params: { code: string } },
|
|
) {
|
|
try {
|
|
const auth = await requireNotificationCenterSuperAdmin()
|
|
if ('response' in auth) return auth.response
|
|
|
|
const code = readProjectCode(params)
|
|
if (!code) {
|
|
return NextResponse.json({ success: false, error: '通知中心项目不存在' }, { status: 404 })
|
|
}
|
|
|
|
const body = await request.json().catch(() => ({}))
|
|
const result = await updateNotificationCenterProject(auth.actor, code, body)
|
|
return NextResponse.json({ success: true, data: result.data }, { status: result.status })
|
|
} catch (error) {
|
|
return toNotificationCenterAdminErrorResponse(error)
|
|
}
|
|
}
|
|
|
|
export async function PATCH(
|
|
request: NextRequest,
|
|
context: { params: { code: string } },
|
|
) {
|
|
return handlePatch(request, context)
|
|
}
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
context: { params: { code: string } },
|
|
) {
|
|
return handlePatch(request, context)
|
|
}
|
|
|
|
export async function DELETE(
|
|
_request: NextRequest,
|
|
{ params }: { params: { code: string } },
|
|
) {
|
|
try {
|
|
const auth = await requireNotificationCenterSuperAdmin()
|
|
if ('response' in auth) return auth.response
|
|
|
|
const code = readProjectCode(params)
|
|
if (!code) {
|
|
return NextResponse.json({ success: false, error: '通知中心项目不存在' }, { status: 404 })
|
|
}
|
|
|
|
const result = await deleteNotificationCenterProject(auth.actor, code)
|
|
return NextResponse.json({ success: true, data: result.data }, { status: result.status })
|
|
} catch (error) {
|
|
return toNotificationCenterAdminErrorResponse(error)
|
|
}
|
|
}
|