diff --git a/src/app/api/comm-code/route.ts b/src/app/api/comm-code/route.ts index 6148a33..26340b6 100644 --- a/src/app/api/comm-code/route.ts +++ b/src/app/api/comm-code/route.ts @@ -1,12 +1,15 @@ import { NextRequest, NextResponse } from 'next/server' import { loggerWrapper } from '@/libs/api-wrapper' import { prisma } from '@/libs/prisma' -import type { CommCode } from '@/types/CommCode' +import { CommonCode } from '@/types/Inquiry' async function getCommCode(request: NextRequest): Promise { try { const searchParams = request.nextUrl.searchParams const headCode = searchParams.get('headCode') + if (headCode === 'QNA_CD') { + return getQnaCd() + } // @ts-ignore const headCd = await prisma.BC_COMM_H.findFirst({ @@ -24,23 +27,22 @@ async function getCommCode(request: NextRequest): Promise { if (headCode === 'SALES_OFFICE_CD') { return getSaleOffice(headCd.HEAD_CD) - } else { - // @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', - }, - }) - return NextResponse.json(roofMaterials) } + // @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', + }, + }) + return NextResponse.json(roofMaterials) } catch (error) { console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error) return NextResponse.json({ error: '데이터 조회 중 오류가 발생했습니다' }, { status: 500 }) @@ -64,4 +66,50 @@ const getSaleOffice = async (headCode: string) => { return NextResponse.json(commCodeSaleOffice) } +/** + * @description QNA 공통 코드 조회 + * @returns {CommonCode[]} QNA 공통 코드 목록 + */ +const getQnaCd = async () => { + // @ts-ignore + const headCdList: { HEAD_CD: string; HEAD_ID: string }[] = await prisma.BC_COMM_H.findMany({ + where: { + OR: [{ HEAD_ID: { in: ['QNA_CLS_LRG_CD'] } }, { HEAD_ID: { in: ['QNA_CLS_MID_CD'] } }, { HEAD_ID: { in: ['QNA_CLS_SML_CD'] } }], + }, + select: { + HEAD_CD: true, + HEAD_ID: true, + }, + }) + const result: CommonCode[] = [] + // @ts-ignore + const commCodeQna: CommCode[] = await prisma.BC_COMM_L.findMany({ + where: { + HEAD_CD: { + in: headCdList.map((item) => item.HEAD_CD).filter(Boolean), + }, + }, + select: { + HEAD_CD: true, + CODE: true, + CODE_JP: true, + REF_CHR1: true, + }, + }) + commCodeQna.forEach((item) => { + result.push({ + // @ts-ignore + headCd: item.HEAD_CD, + // @ts-ignore + headId: headCdList.find((headCd) => headCd.HEAD_CD === item.HEAD_CD)?.HEAD_ID ?? '', + // @ts-ignore + code: item.CODE, + // @ts-ignore + name: item.CODE_JP, + // @ts-ignore + refChar1: item.REF_CHR1 ?? '', + }) + }) + return NextResponse.json(result) +} export const GET = loggerWrapper(getCommCode) diff --git a/src/app/api/qna/route.ts b/src/app/api/qna/route.ts deleted file mode 100644 index 0fc1046..0000000 --- a/src/app/api/qna/route.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { NextResponse } from 'next/server' -import axios from 'axios' -import { loggerWrapper } from '@/libs/api-wrapper' -import { QnaService } from './service' -import { ApiError } from 'next/dist/server/api-utils' - -/** - * @api {GET} /api/qna 문의 유형 목록 조회 API - * @apiName GET /api/qna - * @apiGroup Qna - * @apiDescription 문의 유형 목록 조회 API - * - * @apiSuccess {Object} data 문의 유형 목록 - * @apiSuccess {String} data.headCd 문의 유형 헤드 코드 - * @apiSuccess {String} data.code 문의 유형 코드 - * @apiSuccess {String} data.codeJp 문의 유형 이름 - 일본어 - * @apiSuccess {String} data.refChr1 문의 유형 참조 - 유형 상위 구분 - * - * @apiExample {curl} Example usage: - * curl -X GET http://localhost:3000/api/qna - * - * @apiSuccessExample {json} Success-Response: - * { - * "data": [ - * { - * "headCd": "204200", - * "code": "1", - * "codeJp": "1", - * "refChr1": "1" - * } - * ], - * ... - * } - */ -async function getCommonCodeListData(): Promise { - const service = new QnaService() - const response = await service.tryFunction(() => axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/system/commonCodeListData`)) - if (response instanceof ApiError) { - return NextResponse.json({ error: response.message }, { status: response.statusCode }) - } - const result = service.getInquiryTypeList(response.data) - return NextResponse.json({ data: result }) -} - -export const GET = loggerWrapper(getCommonCodeListData) diff --git a/src/components/inquiry/RegistForm.tsx b/src/components/inquiry/RegistForm.tsx index e6eca3d..1624822 100644 --- a/src/components/inquiry/RegistForm.tsx +++ b/src/components/inquiry/RegistForm.tsx @@ -161,7 +161,7 @@ export default function RegistForm() { ))} - {commonCodeList.filter((code) => code.refChar1 === inquiryRequest.qnaClsLrgCd).length > 0 && ( + {commonCodeList.filter((code) => code.refChar1 === inquiryRequest.qnaClsLrgCd).length > 0 && inquiryRequest.qnaClsLrgCd && (
axiosInstance(null).get<{ data: CommonCode[] }>(`/api/qna`), isListQuery, shouldThrowError) - return resp.data + const resp = await tryFunction( + () => axiosInstance(null).get('/api/comm-code', { params: { headCode: 'QNA_CD' } }), + isListQuery, + shouldThrowError, + ) + return resp?.data ?? [] }, staleTime: Infinity, gcTime: Infinity, @@ -242,6 +247,6 @@ export function useInquiry( isSavingInquiry, saveInquiry, downloadFile, - commonCodeList: commonCodeList?.data ?? [], + commonCodeList: commonCodeList ?? [], } }