109 lines
2.9 KiB
TypeScript

import { NextResponse } from 'next/server'
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()
try {
// @ts-ignore
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
where: { id: Number(id) },
data: {
...body,
},
})
return NextResponse.json(survey)
} catch (error) {
console.error(error)
throw error
}
}
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)
throw error
}
}
export async function PATCH(request: Request, context: { params: { id: string } }) {
const { id } = await context.params
const body = await request.json()
if (body.submit) {
// @ts-ignore
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
where: { id: Number(id) },
data: {
submission_status: true,
submission_date: new Date(),
},
})
return NextResponse.json({ message: 'Survey confirmed successfully' })
} else {
// @ts-ignore
const hasDetails = await prisma.SD_SERVEY_SALES_DETAIL_INFO.findUnique({
where: { basic_info_id: Number(id) },
})
if (hasDetails) {
//@ts-ignore
const result = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
where: { id: Number(id) },
data: {
updated_at: new Date(),
detail_info: {
update: body.detail_info,
},
},
})
return NextResponse.json(result)
} else {
// @ts-ignore
const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({
where: { id: Number(id) },
data: {
detail_info: {
create: body.detail_info,
},
},
})
return NextResponse.json({ message: 'Survey detail created successfully' })
}
}
}