feat: implement QNA common code retrieval and refactor inquiry handling in API routes
This commit is contained in:
parent
fd35348694
commit
d7b26be3cc
@ -1,12 +1,15 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||||
import { prisma } from '@/libs/prisma'
|
import { prisma } from '@/libs/prisma'
|
||||||
import type { CommCode } from '@/types/CommCode'
|
import { CommonCode } from '@/types/Inquiry'
|
||||||
|
|
||||||
async function getCommCode(request: NextRequest): Promise<NextResponse> {
|
async function getCommCode(request: NextRequest): Promise<NextResponse> {
|
||||||
try {
|
try {
|
||||||
const searchParams = request.nextUrl.searchParams
|
const searchParams = request.nextUrl.searchParams
|
||||||
const headCode = searchParams.get('headCode')
|
const headCode = searchParams.get('headCode')
|
||||||
|
if (headCode === 'QNA_CD') {
|
||||||
|
return getQnaCd()
|
||||||
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const headCd = await prisma.BC_COMM_H.findFirst({
|
const headCd = await prisma.BC_COMM_H.findFirst({
|
||||||
@ -24,7 +27,7 @@ async function getCommCode(request: NextRequest): Promise<NextResponse> {
|
|||||||
|
|
||||||
if (headCode === 'SALES_OFFICE_CD') {
|
if (headCode === 'SALES_OFFICE_CD') {
|
||||||
return getSaleOffice(headCd.HEAD_CD)
|
return getSaleOffice(headCd.HEAD_CD)
|
||||||
} else {
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const roofMaterials: CommCode[] = await prisma.BC_COMM_L.findMany({
|
const roofMaterials: CommCode[] = await prisma.BC_COMM_L.findMany({
|
||||||
where: {
|
where: {
|
||||||
@ -40,7 +43,6 @@ async function getCommCode(request: NextRequest): Promise<NextResponse> {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
return NextResponse.json(roofMaterials)
|
return NextResponse.json(roofMaterials)
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error)
|
console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error)
|
||||||
return NextResponse.json({ error: '데이터 조회 중 오류가 발생했습니다' }, { status: 500 })
|
return NextResponse.json({ error: '데이터 조회 중 오류가 발생했습니다' }, { status: 500 })
|
||||||
@ -64,4 +66,50 @@ const getSaleOffice = async (headCode: string) => {
|
|||||||
return NextResponse.json(commCodeSaleOffice)
|
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)
|
export const GET = loggerWrapper(getCommCode)
|
||||||
|
|||||||
@ -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<NextResponse> {
|
|
||||||
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)
|
|
||||||
@ -161,7 +161,7 @@ export default function RegistForm() {
|
|||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
{commonCodeList.filter((code) => code.refChar1 === inquiryRequest.qnaClsLrgCd).length > 0 && (
|
{commonCodeList.filter((code) => code.refChar1 === inquiryRequest.qnaClsLrgCd).length > 0 && inquiryRequest.qnaClsLrgCd && (
|
||||||
<div className="data-input mt5">
|
<div className="data-input mt5">
|
||||||
<select
|
<select
|
||||||
className="select-form"
|
className="select-form"
|
||||||
@ -183,7 +183,7 @@ export default function RegistForm() {
|
|||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{commonCodeList.filter((code) => code.refChar1 === inquiryRequest.qnaClsMidCd).length > 0 && (
|
{commonCodeList.filter((code) => code.refChar1 === inquiryRequest.qnaClsMidCd).length > 0 && inquiryRequest.qnaClsMidCd && (
|
||||||
<div className="data-input mt5">
|
<div className="data-input mt5">
|
||||||
<select
|
<select
|
||||||
className="select-form"
|
className="select-form"
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import { useInquiryFilterStore } from '@/store/inquiryFilterStore'
|
|||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
import { useAlertMsg } from '@/hooks/useAlertMsg'
|
import { useAlertMsg } from '@/hooks/useAlertMsg'
|
||||||
|
import { CommCode } from '@/types/CommCode'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 문의사항 관련 기능을 제공하는 커스텀 훅
|
* @description 문의사항 관련 기능을 제공하는 커스텀 훅
|
||||||
@ -227,8 +228,12 @@ export function useInquiry(
|
|||||||
const isListQuery = false
|
const isListQuery = false
|
||||||
const shouldThrowError = false
|
const shouldThrowError = false
|
||||||
|
|
||||||
const resp = await tryFunction(() => axiosInstance(null).get<{ data: CommonCode[] }>(`/api/qna`), isListQuery, shouldThrowError)
|
const resp = await tryFunction(
|
||||||
return resp.data
|
() => axiosInstance(null).get<CommonCode[]>('/api/comm-code', { params: { headCode: 'QNA_CD' } }),
|
||||||
|
isListQuery,
|
||||||
|
shouldThrowError,
|
||||||
|
)
|
||||||
|
return resp?.data ?? []
|
||||||
},
|
},
|
||||||
staleTime: Infinity,
|
staleTime: Infinity,
|
||||||
gcTime: Infinity,
|
gcTime: Infinity,
|
||||||
@ -242,6 +247,6 @@ export function useInquiry(
|
|||||||
isSavingInquiry,
|
isSavingInquiry,
|
||||||
saveInquiry,
|
saveInquiry,
|
||||||
downloadFile,
|
downloadFile,
|
||||||
commonCodeList: commonCodeList?.data ?? [],
|
commonCodeList: commonCodeList ?? [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user