90 lines
2.5 KiB
TypeScript
90 lines
2.5 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
|
|
export async function POST(request: Request, context: { params: { id: string } }) {
|
|
const body = await request.json()
|
|
const { id } = await context.params
|
|
|
|
// @ts-ignore
|
|
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
|
|
where: { id: Number(id) },
|
|
data: {
|
|
detail_info: {
|
|
create: body,
|
|
},
|
|
},
|
|
})
|
|
return NextResponse.json({ message: 'Survey detail created successfully' })
|
|
}
|
|
|
|
export async function GET(request: Request, context: { params: { id: string } }) {
|
|
const { id } = await context.params
|
|
// @ts-ignore
|
|
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.findUnique({
|
|
where: { id: Number(id) },
|
|
include: {
|
|
detail_info: true,
|
|
},
|
|
})
|
|
return NextResponse.json(survey)
|
|
}
|
|
|
|
export async function PUT(request: Request, context: { params: { id: string } }) {
|
|
const { id } = await context.params
|
|
const body = await request.json()
|
|
// @ts-ignore
|
|
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
|
|
where: { id: Number(id) },
|
|
data: {
|
|
...body,
|
|
detail_info: {
|
|
update: body.detail_info,
|
|
},
|
|
},
|
|
})
|
|
return NextResponse.json(survey)
|
|
}
|
|
|
|
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
|
const { id } = await context.params
|
|
|
|
try {
|
|
//@ts-ignore
|
|
await prisma.$transaction(async (tx) => {
|
|
// @ts-ignore
|
|
const detailData = await tx.SD_SERVEY_SALES_BASIC_INFO.findUnique({
|
|
where: { id: Number(id) },
|
|
select: {
|
|
detail_info: true,
|
|
},
|
|
})
|
|
console.log('detailData:: ', detailData)
|
|
if (detailData?.detail_info?.id) {
|
|
// @ts-ignore
|
|
await tx.SD_SERVEY_SALES_DETAIL_INFO.delete({
|
|
where: { id: Number(detailData?.detail_info?.id) },
|
|
})
|
|
}
|
|
// @ts-ignore
|
|
await tx.SD_SERVEY_SALES_BASIC_INFO.delete({
|
|
where: { id: Number(id) },
|
|
})
|
|
})
|
|
return NextResponse.json({ message: 'Survey deleted successfully' })
|
|
} catch (error) {
|
|
console.error(error)
|
|
return NextResponse.json({ message: 'Survey deletion failed' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function PATCH(request: Request, context: { params: { id: string } }) {
|
|
const { id } = await context.params
|
|
// @ts-ignore
|
|
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
|
|
where: { id: Number(id) },
|
|
data: {
|
|
submission_status: true,
|
|
},
|
|
})
|
|
return NextResponse.json({ message: 'Survey confirmed successfully' })
|
|
}
|