73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
import { getServerSession } from 'next-auth'
|
|
import { NextResponse } from 'next/server'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { PerformanceSurveyApiError } from '@/lib/performance-survey-service'
|
|
import {
|
|
WorkReviewApiError,
|
|
createWorkReviewProject,
|
|
createWorkReviewVersion,
|
|
getWorkReviewBoard,
|
|
submitWorkReview,
|
|
} from '@/lib/work-review-service'
|
|
|
|
export const runtime = 'nodejs'
|
|
export const dynamic = 'force-dynamic'
|
|
export const revalidate = 0
|
|
|
|
type RouteContext = {
|
|
params: {
|
|
path?: string[]
|
|
}
|
|
}
|
|
|
|
function ok(data: unknown, init?: ResponseInit) {
|
|
return NextResponse.json({ success: true, data }, init)
|
|
}
|
|
|
|
function errorResponse(error: unknown) {
|
|
if (error instanceof WorkReviewApiError || error instanceof PerformanceSurveyApiError) {
|
|
return NextResponse.json({ success: false, error: error.message }, { status: error.status })
|
|
}
|
|
console.error('[work-review] API error:', error)
|
|
return NextResponse.json({ success: false, error: '服务器错误,请稍后重试' }, { status: 500 })
|
|
}
|
|
|
|
function getPath(context: RouteContext) {
|
|
return context.params.path || []
|
|
}
|
|
|
|
export async function GET(request: Request, context: RouteContext) {
|
|
try {
|
|
const path = getPath(context)
|
|
if (path.length > 0) {
|
|
return NextResponse.json({ success: false, error: '接口不存在' }, { status: 404 })
|
|
}
|
|
const session = await getServerSession(authOptions)
|
|
return ok(await getWorkReviewBoard(session, new URL(request.url).searchParams))
|
|
} catch (error) {
|
|
return errorResponse(error)
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request, context: RouteContext) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
const path = getPath(context)
|
|
const body = await request.json().catch(() => ({}))
|
|
|
|
if (path[0] === 'projects' && path.length === 1) {
|
|
return ok(await createWorkReviewProject(session, body), { status: 201 })
|
|
}
|
|
if (path[0] === 'projects' && path[1] && path[2] === 'versions' && path.length === 3) {
|
|
return ok(await createWorkReviewVersion(session, path[1], body), { status: 201 })
|
|
}
|
|
if (path[0] === 'versions' && path[1] && path[2] === 'reviews' && path.length === 3) {
|
|
return ok(await submitWorkReview(session, path[1], body))
|
|
}
|
|
|
|
return NextResponse.json({ success: false, error: '接口不存在' }, { status: 404 })
|
|
} catch (error) {
|
|
return errorResponse(error)
|
|
}
|
|
}
|