67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { CERTIFICATE_REPORT_MATERIAL_TYPE, normalizeReportTitle } from '@/lib/certificate-report'
|
|
import { INCOMPLETE_SHELL_CREATE_DATA } from '@/lib/material-query'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: '请先登录' }, { status: 401 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const productModel = String(body.productModel || '').trim()
|
|
const reportTitle = normalizeReportTitle(body.reportTitle)
|
|
const productCategoryName = String(body.productCategoryName || '').trim()
|
|
const platformName = String(body.platformName || '').trim()
|
|
|
|
if (!productModel && !reportTitle) {
|
|
return NextResponse.json({ error: 'SPU 和 报告名称/检测对象 至少填写一个' }, { status: 400 })
|
|
}
|
|
|
|
if (!productCategoryName) {
|
|
return NextResponse.json({ error: '品类不能为空' }, { status: 400 })
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: (session.user as any).id },
|
|
select: { username: true },
|
|
})
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: '用户不存在' }, { status: 404 })
|
|
}
|
|
|
|
const material = await prisma.material.create({
|
|
data: {
|
|
materialType: CERTIFICATE_REPORT_MATERIAL_TYPE,
|
|
productModel,
|
|
reportTitle,
|
|
categoryName: '通用',
|
|
sceneName: '通用',
|
|
platformName,
|
|
productCategoryName,
|
|
renderer: '',
|
|
authorName: user.username,
|
|
userId: (session.user as any).id,
|
|
previewUrl: '',
|
|
zipFilePath: '',
|
|
zipFileName: '',
|
|
taskStatus: 'uploading',
|
|
...INCOMPLETE_SHELL_CREATE_DATA,
|
|
} as any,
|
|
})
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
materialId: material.id,
|
|
})
|
|
} catch (error: any) {
|
|
console.error('[Certificate Report Init Error]', error)
|
|
return NextResponse.json({ error: error.message || '初始化失败' }, { status: 500 })
|
|
}
|
|
}
|