101 lines
3.8 KiB
TypeScript
101 lines
3.8 KiB
TypeScript
/**
|
||
* 任务-详情批量单图编辑接口
|
||
*
|
||
* POST body: { operations: Array<
|
||
* { kind: 'delete'; assetId } |
|
||
* { kind: 'rename'; assetId; newFileName } |
|
||
* { kind: 'replace'; assetId; uploadId }
|
||
* > }
|
||
*
|
||
* 前端把用户的一批编辑操作攒在本地,点「应用」后一次性提交;
|
||
* 服务端一次解压、一次重建分组 ZIP 与 all.zip、一次事务提交。
|
||
* 限制与权限口径与单图编辑一致(canManageMaterial)。
|
||
*/
|
||
|
||
import { NextRequest, NextResponse } from 'next/server'
|
||
import { getServerSession } from 'next-auth'
|
||
import { authOptions } from '@/lib/auth'
|
||
import {
|
||
TaskDetailAssetEditError,
|
||
type BatchTaskDetailImageEditOperation,
|
||
} from '@/lib/task-detail-asset-edit'
|
||
import {
|
||
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 getSessionUser(session: unknown): { id: string; role?: string | null; isSuperAdmin?: boolean } | null {
|
||
const user = (session as { user?: { id?: string; role?: string; isSuperAdmin?: boolean } } | null)?.user
|
||
if (!user?.id) {
|
||
return null
|
||
}
|
||
return { id: user.id, role: user.role ?? null, isSuperAdmin: Boolean(user.isSuperAdmin) }
|
||
}
|
||
|
||
export async function POST(request: NextRequest, { params }: RouteParams) {
|
||
try {
|
||
const session = await getServerSession(authOptions)
|
||
const operator = getSessionUser(session)
|
||
if (!operator) {
|
||
return NextResponse.json({ success: false, error: '未登录' }, { status: 401 })
|
||
}
|
||
|
||
const body = await request.json().catch(() => null)
|
||
const rawOperations = Array.isArray((body as { operations?: unknown } | null)?.operations)
|
||
? ((body as { operations: unknown[] }).operations)
|
||
: []
|
||
|
||
const operations: BatchTaskDetailImageEditOperation[] = []
|
||
for (const raw of rawOperations) {
|
||
const item = raw as Record<string, unknown>
|
||
const kind = String(item?.kind || '')
|
||
const assetId = String(item?.assetId || '').trim()
|
||
if (!assetId) {
|
||
return NextResponse.json({ success: false, error: '操作缺少图片标识' }, { status: 400 })
|
||
}
|
||
if (kind === 'delete') {
|
||
operations.push({ kind: 'delete', assetId })
|
||
} else if (kind === 'rename') {
|
||
const newFileName = String(item?.newFileName || '').trim()
|
||
if (!newFileName) {
|
||
return NextResponse.json({ success: false, error: '缺少新文件名' }, { status: 400 })
|
||
}
|
||
operations.push({ kind: 'rename', assetId, newFileName })
|
||
} else if (kind === 'replace') {
|
||
const uploadId = String(item?.uploadId || '').trim()
|
||
if (!uploadId) {
|
||
return NextResponse.json({ success: false, error: '缺少替换文件的上传会话,请重新选择文件' }, { status: 400 })
|
||
}
|
||
operations.push({ kind: 'replace', assetId, uploadId })
|
||
} else {
|
||
return NextResponse.json({ success: false, error: `不支持的操作类型:${kind}` }, { status: 400 })
|
||
}
|
||
}
|
||
if (operations.length === 0) {
|
||
return NextResponse.json({ success: false, error: '批量操作不能为空' }, { status: 400 })
|
||
}
|
||
|
||
const result = await submitTaskDetailChange(params.id, operator, operations)
|
||
|
||
return NextResponse.json({ success: true, ...result })
|
||
} catch (error) {
|
||
if (error instanceof TaskDetailAssetEditError || error instanceof TaskDetailChangeApprovalError) {
|
||
return NextResponse.json(
|
||
{ success: false, error: error.message },
|
||
{ status: error.statusCode },
|
||
)
|
||
}
|
||
console.error('[materials/asset-batch-edit] error:', error)
|
||
return NextResponse.json(
|
||
{ success: false, error: '批量编辑失败,请稍后重试' },
|
||
{ status: 500 },
|
||
)
|
||
}
|
||
}
|