63 lines
1.6 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/libs/prisma'
import type { CommCode } from '@/types/CommCode'
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams
const headCode = searchParams.get('headCode')
// @ts-ignore
const headCd = await prisma.BC_COMM_H.findFirst({
where: {
HEAD_ID: headCode,
},
select: {
HEAD_CD: true,
},
})
if (!headCd) {
return NextResponse.json({ error: `${headCode}를 찾을 수 없습니다` }, { status: 404 })
}
// @ts-ignore
const roofMaterials: CommCode[] = await prisma.BC_COMM_L.findMany({
where: {
HEAD_CD: headCd.HEAD_CD,
},
select: {
HEAD_CD: true,
CODE: true,
CODE_JP: true,
},
orderBy: {
CODE: 'asc',
},
})
if (headCode === 'SALES_OFFICE_CD') {
return getSaleOffice(headCd.HEAD_CD)
}
return NextResponse.json(roofMaterials)
} catch (error) {
console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error)
return NextResponse.json({ error: '데이터 조회 중 오류가 발생했습니다' }, { status: 500 })
}
}
const getSaleOffice = async (headCode: string) => {
// @ts-ignore
const commCodeSaleOffice: CommCode[] = await prisma.BC_COMM_L.findMany({
where: {
HEAD_CD: headCode,
REF_NUM1: 1,
},
select: {
CODE: true,
CODE_JP: true,
REF_CHR1: true,
REF_NUM1: true,
},
})
return NextResponse.json(commCodeSaleOffice)
}