60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
|
|
import { authOptions } from '@/lib/auth'
|
|
import {
|
|
listTaskDetailChanges,
|
|
submitTaskDetailChange,
|
|
TaskDetailChangeApprovalError,
|
|
} from '@/lib/task-detail-change-approval'
|
|
|
|
export const runtime = 'nodejs'
|
|
export const dynamic = 'force-dynamic'
|
|
export const maxDuration = 10800
|
|
|
|
type RouteParams = { params: { id: string } }
|
|
|
|
function actor(session: unknown) {
|
|
const user = (session as { user?: { id?: string; role?: string; isSuperAdmin?: boolean } } | null)?.user
|
|
return user?.id
|
|
? { id: user.id, role: user.role || null, isSuperAdmin: Boolean(user.isSuperAdmin) }
|
|
: null
|
|
}
|
|
|
|
function errorResponse(error: unknown) {
|
|
if (error instanceof TaskDetailChangeApprovalError) {
|
|
return NextResponse.json({ success: false, error: error.message }, { status: error.statusCode })
|
|
}
|
|
console.error('[task-detail-change] request error:', error)
|
|
return NextResponse.json({ success: false, error: '图片变更处理失败,请稍后重试' }, { status: 500 })
|
|
}
|
|
|
|
export async function GET(_request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
const current = actor(session)
|
|
if (!current) return NextResponse.json({ success: false, error: '未登录' }, { status: 401 })
|
|
const rows = await listTaskDetailChanges(params.id, current)
|
|
return NextResponse.json({ success: true, data: rows })
|
|
} catch (error) {
|
|
return errorResponse(error)
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
const current = actor(session)
|
|
if (!current) return NextResponse.json({ success: false, error: '未登录' }, { status: 401 })
|
|
const body = await request.json().catch(() => null)
|
|
const result = await submitTaskDetailChange(
|
|
params.id,
|
|
current,
|
|
(body as { operations?: unknown } | null)?.operations,
|
|
)
|
|
return NextResponse.json({ success: true, ...result })
|
|
} catch (error) {
|
|
return errorResponse(error)
|
|
}
|
|
}
|