106 lines
2.8 KiB
TypeScript
106 lines
2.8 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { prisma } from '@/libs/prisma'
|
|
|
|
export async function POST(request: Request) {
|
|
const body = await request.json()
|
|
// @ts-ignore
|
|
const res = await prisma.SD_SERVEY_SALES_BASIC_INFO.create({
|
|
data: body,
|
|
})
|
|
return NextResponse.json(res)
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url)
|
|
const keyword = searchParams.get('keyword')
|
|
const searchOption = searchParams.get('searchOption')
|
|
const isMySurvey = searchParams.get('isMySurvey')
|
|
const sort = searchParams.get('sort')
|
|
const offset = searchParams.get('offset')
|
|
const store = searchParams.get('store')
|
|
const construction_point = searchParams.get('construction_point')
|
|
|
|
const searchOptions = ['building_name', 'representative', 'store', 'construction_point', 'customer_name', 'post_code', 'address', 'address_detail']
|
|
try {
|
|
const where: any = {}
|
|
|
|
if (isMySurvey !== null && isMySurvey !== undefined) {
|
|
where.representative = isMySurvey
|
|
}
|
|
|
|
if (keyword && keyword.trim() !== '' && searchOption) {
|
|
if (searchOption === 'all') {
|
|
where.OR = []
|
|
if (keyword.match(/^\d+$/) || !isNaN(Number(keyword))) {
|
|
where.OR.push({
|
|
id: {
|
|
equals: Number(keyword),
|
|
},
|
|
})
|
|
}
|
|
where.OR.push(
|
|
...searchOptions.map((field) => ({
|
|
[field]: {
|
|
contains: keyword,
|
|
},
|
|
})),
|
|
)
|
|
} else if (searchOptions.includes(searchOption)) {
|
|
where[searchOption] = {
|
|
contains: keyword,
|
|
}
|
|
} else if (searchOption === 'id') {
|
|
where[searchOption] = {
|
|
equals: Number(keyword),
|
|
}
|
|
}
|
|
}
|
|
|
|
where.AND = []
|
|
if (store) {
|
|
where.AND.push({
|
|
store: {
|
|
equals: store,
|
|
},
|
|
})
|
|
}
|
|
if (construction_point) {
|
|
where.AND.push({
|
|
construction_point: {
|
|
equals: construction_point,
|
|
},
|
|
})
|
|
}
|
|
|
|
if (offset) {
|
|
// @ts-ignore
|
|
const res = await prisma.SD_SERVEY_SALES_BASIC_INFO.findMany({
|
|
where,
|
|
orderBy: sort === 'created' ? { created_at: 'desc' } : { updated_at: 'desc' },
|
|
skip: Number(offset),
|
|
take: 10,
|
|
})
|
|
return NextResponse.json(res)
|
|
} else {
|
|
// @ts-ignore
|
|
const count = await prisma.SD_SERVEY_SALES_BASIC_INFO.count({
|
|
where,
|
|
})
|
|
return NextResponse.json(count)
|
|
}
|
|
} catch (error) {
|
|
console.error(error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function PUT(request: Request) {
|
|
const body = await request.json()
|
|
const detailInfo = { ...body.detail_info, basic_info_id: body.id }
|
|
// @ts-ignore
|
|
const res = await prisma.SD_SERVEY_SALES_DETAIL_INFO.create({
|
|
data: detailInfo,
|
|
})
|
|
return NextResponse.json({ message: 'Survey sales updated successfully' })
|
|
}
|