Compare commits
15 Commits
3d5cb308a8
...
17003cb74a
| Author | SHA1 | Date | |
|---|---|---|---|
| 17003cb74a | |||
| d62caaf857 | |||
| 9afcc88670 | |||
| 9a590793ce | |||
| 3084f292a7 | |||
| d690c3e774 | |||
| 9a8b77d965 | |||
| 94da4f3452 | |||
| b116e6e5c1 | |||
| 12b9dd4216 | |||
| 1a42848ae9 | |||
| 6c1bd8775c | |||
| c92c1c2470 | |||
| f6729069d7 | |||
| 2edde77bae |
@ -2,6 +2,12 @@ import { queryStringFormatter } from '@/utils/common-utils'
|
||||
import axios from 'axios'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
import { QnaService } from '../service'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
import { getIronSession } from 'iron-session'
|
||||
import { SessionData } from '@/types/Auth'
|
||||
import { sessionOptions } from '@/libs/session'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
/**
|
||||
* @api {GET} /api/qna/detail 문의 상세 조회 API
|
||||
@ -20,61 +26,52 @@ import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
* @apiSuccessExample {json} Success-Response:
|
||||
* {
|
||||
* "data": {
|
||||
"compCd": "5200",
|
||||
"qnaNo": 51,
|
||||
"qstTitle": "Q.CAST TEST",
|
||||
"qstContents": "Q.CAST TEST CONTENTS",
|
||||
"regDt": "2025.04.29 16:16:51",
|
||||
"regId": "X112",
|
||||
"regNm": "株式会社アイ工務店",
|
||||
"regEmail": "x112@interplug.co.kr",
|
||||
"answerYn": "N",
|
||||
"ansContents": null,
|
||||
"ansRegDt": null,
|
||||
"ansRegNm": null,
|
||||
"ansListFile": null,
|
||||
"storeId": "X112",
|
||||
"storeNm": "株式会社アイ工務店",
|
||||
"regUserNm": "TEST",
|
||||
"regUserTelNo": "010-1111-1111",
|
||||
"qnaClsLrgCd": "A01",
|
||||
"qnaClsMidCd": "B02",
|
||||
"qnaClsSmlCd": "C05",
|
||||
"listFile": [
|
||||
{
|
||||
"fileNo": 853
|
||||
"encodeFileNo": 853,
|
||||
"srcFileNm": "Quotation_4500380_20240808145129.pdf",
|
||||
"fileCours": "/temp/20250428/"
|
||||
"fileSize":160982,
|
||||
"regDt":"2024.08.13"
|
||||
}
|
||||
...
|
||||
],
|
||||
...
|
||||
},
|
||||
* "compCd": "5200",
|
||||
* "qnaNo": 51,
|
||||
* "qstTitle": "Q.CAST TEST",
|
||||
* "qstContents": "Q.CAST TEST CONTENTS",
|
||||
* "regDt": "2025.04.29 16:16:51",
|
||||
* "regId": "X112",
|
||||
* "regNm": "株式会社アイ工務店",
|
||||
* "regEmail": "x112@interplug.co.kr",
|
||||
* "answerYn": "N",
|
||||
* "ansContents": null,
|
||||
* ...
|
||||
* "listFile": [
|
||||
* {
|
||||
* "fileNo": 853
|
||||
* "encodeFileNo": 853,
|
||||
* "srcFileNm": "Quotation_4500380_20240808145129.pdf",
|
||||
* "fileCours": "/temp/20250428/"
|
||||
* "fileSize":160982,
|
||||
* "regDt":"2024.08.13"
|
||||
* }
|
||||
* ...
|
||||
* ],
|
||||
* },
|
||||
* },
|
||||
* @apiError {Number} 401 세션 정보 없음 (로그인 필요)
|
||||
* @apiError {Number} 500 서버 오류
|
||||
*/
|
||||
async function getQnaDetail(request: Request): Promise<NextResponse> {
|
||||
const cookieStore = await cookies()
|
||||
const session = await getIronSession<SessionData>(cookieStore, sessionOptions)
|
||||
|
||||
const service = new QnaService(session)
|
||||
const { searchParams } = new URL(request.url)
|
||||
const params = {
|
||||
compCd: searchParams.get('compCd'),
|
||||
qnaNo: searchParams.get('qnoNo'),
|
||||
langCd: searchParams.get('langCd'),
|
||||
loginId: searchParams.get('loginId'),
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/qna/detail?${queryStringFormatter(params)}`)
|
||||
if (response.status === 200) {
|
||||
return NextResponse.json(response.data)
|
||||
}
|
||||
return NextResponse.json({ error: response.data.result }, { status: response.status })
|
||||
} catch (error: any) {
|
||||
console.error(error.response)
|
||||
return NextResponse.json({ error: 'route error' }, { status: 500 })
|
||||
const result = await service.tryFunction(() =>
|
||||
axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/qna/detail?loginId=${session.userId}&${queryStringFormatter(params)}`),
|
||||
)
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json(result.data)
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getQnaDetail)
|
||||
|
||||
@ -1,76 +1,55 @@
|
||||
import axios from 'axios'
|
||||
import { HttpStatusCode } from 'axios'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
import { ERROR_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
import { QnaService } from '../service'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
|
||||
// export async function GET(request: Request) {
|
||||
// const { searchParams } = new URL(request.url)
|
||||
// const encodeFileNo = searchParams.get('encodeFileNo')
|
||||
// const srcFileNm = searchParams.get('srcFileNm')
|
||||
|
||||
// if (!encodeFileNo) {
|
||||
// return NextResponse.json({ error: 'encodeFileNo is required' }, { status: 400 })
|
||||
// }
|
||||
|
||||
// try {
|
||||
// const response = await axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/file/downloadFile2`, {
|
||||
// params: {
|
||||
// encodeFileNo,
|
||||
// },
|
||||
// responseType: 'arraybuffer',
|
||||
// })
|
||||
|
||||
// if (response.headers['content-type'] === 'text/html;charset=utf-8') {
|
||||
// return NextResponse.json({ error: 'file not found' }, { status: 404 })
|
||||
// }
|
||||
|
||||
// const contentType = response.headers['content-type'] || 'application/octet-stream'
|
||||
// const contentDisposition = response.headers['content-disposition'] || 'inline'
|
||||
|
||||
// return new NextResponse(response.data, {
|
||||
// status: 200,
|
||||
// headers: {
|
||||
// 'Content-Type': contentType,
|
||||
// 'Content-Disposition': contentDisposition,
|
||||
// },
|
||||
// })
|
||||
// } catch (error: any) {
|
||||
// console.error('File download error:', error)
|
||||
// return NextResponse.json({ error: error.response?.data || 'Failed to download file' }, { status: 500 })
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* @api {GET} /api/qna/file 문의 첨부 파일 다운로드 API
|
||||
* @apiName GET /api/qna/file
|
||||
* @apiGroup Qna
|
||||
* @apiDescription 문의 첨부 파일 다운로드 API
|
||||
*
|
||||
* @apiParam {String} encodeFileNo 인코딩 파일 번호
|
||||
* @apiParam {String} srcFileNm 소스 파일 이름
|
||||
*
|
||||
* @apiExample {curl} Example usage:
|
||||
* curl -X GET http://localhost:3000/api/qna/file?encodeFileNo=1234567890&srcFileNm=test.pdf
|
||||
*
|
||||
* @apiSuccessExample {octet-stream} Success-Response:
|
||||
* file content
|
||||
*
|
||||
* @apiError {Number} 500 서버 오류
|
||||
* @apiError {Number} 400 잘못된 요청
|
||||
*/
|
||||
async function downloadFile(request: Request): Promise<NextResponse> {
|
||||
const service = new QnaService()
|
||||
const { searchParams } = new URL(request.url)
|
||||
const encodeFileNo = searchParams.get('encodeFileNo')
|
||||
const srcFileNm = searchParams.get('srcFileNm') || 'downloaded-file'
|
||||
|
||||
if (!encodeFileNo) {
|
||||
return NextResponse.json({ error: 'encodeFileNo is required' }, { status: 400 })
|
||||
return NextResponse.json({ error: ERROR_MESSAGE.BAD_REQUEST }, { status: HttpStatusCode.BadRequest })
|
||||
}
|
||||
|
||||
const url = `${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/file/downloadFile2?encodeFileNo=${encodeFileNo}`
|
||||
|
||||
try {
|
||||
const resp = await fetch(url)
|
||||
|
||||
if (!resp.ok) {
|
||||
return NextResponse.json({ error: 'Failed to download file' }, { status: 500 })
|
||||
}
|
||||
|
||||
const contentType = resp.headers.get('content-type') || 'application/octet-stream'
|
||||
const contentDisposition = resp.headers.get('content-disposition') || `attachment; filename="${srcFileNm}"`
|
||||
|
||||
return new NextResponse(resp.body, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Content-Disposition': contentDisposition,
|
||||
},
|
||||
})
|
||||
} catch (error: any) {
|
||||
console.error('File download error:', error)
|
||||
return NextResponse.json({ error: error.response?.data || 'Failed to download file' }, { status: 500 })
|
||||
const resp = await service.tryFunction(() => fetch(url), true)
|
||||
if (resp instanceof ApiError) {
|
||||
return NextResponse.json({ error: resp.message }, { status: resp.statusCode })
|
||||
}
|
||||
|
||||
const contentType = resp.headers.get('content-type') || 'application/octet-stream'
|
||||
const contentDisposition = resp.headers.get('content-disposition') || `attachment; filename="${srcFileNm}"`
|
||||
|
||||
return new NextResponse(resp.body, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Content-Disposition': contentDisposition,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(downloadFile)
|
||||
|
||||
@ -6,6 +6,8 @@ import { cookies } from 'next/headers'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
import { sessionOptions } from '@/libs/session'
|
||||
import { SessionData } from '@/types/Auth'
|
||||
import { QnaService } from '../service'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
|
||||
/**
|
||||
* @api {GET} /api/qna/list 문의 목록 조회 API
|
||||
@ -56,32 +58,16 @@ async function getQnaList(request: Request): Promise<NextResponse> {
|
||||
const cookieStore = await cookies()
|
||||
const session = await getIronSession<SessionData>(cookieStore, sessionOptions)
|
||||
|
||||
if (!session.isLoggedIn) {
|
||||
return NextResponse.json({ error: 'ログインしていません。' }, { status: 401 })
|
||||
}
|
||||
const service = new QnaService(session)
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const params = service.getSearchParams(searchParams)
|
||||
|
||||
const params: Record<string, string> = {}
|
||||
searchParams.forEach((value, key) => {
|
||||
const match = key.match(/inquiryListRequest\[(.*)\]/)
|
||||
if (match) {
|
||||
params[match[1]] = value
|
||||
} else {
|
||||
params[key] = value
|
||||
}
|
||||
})
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/qna/list?${queryStringFormatter(params)}`)
|
||||
if (response.status === 200) {
|
||||
return NextResponse.json(response.data)
|
||||
}
|
||||
return NextResponse.json({ error: 'Failed to fetch qna list' }, { status: response.status })
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching qna list:', error.response.data)
|
||||
return NextResponse.json({ error: 'route error' }, { status: 500 })
|
||||
const result = await service.tryFunction(() => axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/qna/list?${queryStringFormatter(params)}`), true)
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json(result.data.data)
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getQnaList)
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import axios from 'axios'
|
||||
import { CommonCode } from '@/types/Inquiry'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
import { QnaService } from './service'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
|
||||
/**
|
||||
* @api {GET} /api/qna 문의 유형 목록 조회 API
|
||||
@ -31,20 +32,14 @@ import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
* ...
|
||||
* }
|
||||
*/
|
||||
async function getCommonCodeListData(request: Request): Promise<NextResponse> {
|
||||
const response = await axios.get(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/system/commonCodeListData`)
|
||||
const codeList: CommonCode[] = []
|
||||
response.data.data.apiCommCdList.forEach((item: any) => {
|
||||
if (item.headCd === '204200' || item.headCd === '204300' || item.headCd === '204400') {
|
||||
codeList.push({
|
||||
headCd: item.headCd,
|
||||
code: item.code,
|
||||
name: item.codeJp,
|
||||
refChar1: item.refChr1,
|
||||
})
|
||||
}
|
||||
})
|
||||
return NextResponse.json({ data: codeList })
|
||||
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.apiCommCdList)
|
||||
return NextResponse.json({ data: result })
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getCommonCodeListData)
|
||||
|
||||
@ -1,6 +1,12 @@
|
||||
import axios from 'axios'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
import { QnaService } from '../service'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
import { getIronSession } from 'iron-session'
|
||||
import { SessionData } from '@/types/Auth'
|
||||
import { sessionOptions } from '@/libs/session'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
/**
|
||||
* @api {POST} /api/qna/save 문의 저장 API
|
||||
@ -24,22 +30,22 @@ import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
* @apiError {Number} 500 서버 오류
|
||||
*/
|
||||
async function setQna(request: Request): Promise<NextResponse> {
|
||||
const cookieStore = await cookies()
|
||||
const session = await getIronSession<SessionData>(cookieStore, sessionOptions)
|
||||
|
||||
const service = new QnaService(session)
|
||||
const formData = await request.formData()
|
||||
console.log(formData)
|
||||
try {
|
||||
const response = await axios.post(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/qna/save`, formData, {
|
||||
const result = await service.tryFunction(() =>
|
||||
axios.post(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/qna/save`, formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
})
|
||||
if (response.status === 200) {
|
||||
return NextResponse.json(response.data)
|
||||
}
|
||||
return NextResponse.json({ error: response.data }, { status: response.status })
|
||||
} catch (error: any) {
|
||||
console.error('error:: ', error.response)
|
||||
return NextResponse.json({ error: 'Failed to save qna' }, { status: 500 })
|
||||
}),
|
||||
)
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json(result.data)
|
||||
}
|
||||
|
||||
export const POST = loggerWrapper(setQna)
|
||||
|
||||
89
src/app/api/qna/service.ts
Normal file
89
src/app/api/qna/service.ts
Normal file
@ -0,0 +1,89 @@
|
||||
import { SessionData } from '@/types/Auth'
|
||||
import { CommonCode } from '@/types/Inquiry'
|
||||
import { ERROR_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
import { HttpStatusCode } from 'axios'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
|
||||
export class QnaService {
|
||||
private session?: SessionData
|
||||
constructor(session?: SessionData) {
|
||||
this.session = session
|
||||
}
|
||||
/**
|
||||
* @description API ROUTE 에러 처리
|
||||
* @param {any} error 에러 객체
|
||||
* @returns {ApiError} 에러 객체
|
||||
*/
|
||||
private handleRouteError(error: any): ApiError {
|
||||
console.error('❌ API ROUTE ERROR : ', error)
|
||||
return new ApiError(error.response.status, error.response.data.result.message ?? ERROR_MESSAGE.FETCH_ERROR)
|
||||
}
|
||||
/**
|
||||
* @description 비동기 함수 try-catch 처리 함수
|
||||
* @param {() => Promise<any>} func 비동기 함수
|
||||
* @returns {Promise<ApiError | any>} 에러 객체 또는 함수 결과
|
||||
*/
|
||||
async tryFunction(func: () => Promise<any>, isFile?: boolean): Promise<ApiError | any> {
|
||||
if (this.session !== undefined && !this.session?.isLoggedIn) {
|
||||
return new ApiError(HttpStatusCode.Unauthorized, ERROR_MESSAGE.UNAUTHORIZED)
|
||||
}
|
||||
try {
|
||||
const response = await func()
|
||||
if (isFile) return response
|
||||
return this.handleResult(response)
|
||||
} catch (error) {
|
||||
return this.handleRouteError(error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 함수 결과 처리 함수
|
||||
* @param {any} result 함수 결과
|
||||
* @returns {ApiError | any} 에러 객체 또는 함수 결과
|
||||
*/
|
||||
private handleResult(response: any): ApiError | any {
|
||||
if (response.status === HttpStatusCode.Ok) {
|
||||
if (response.data.data !== null) return response.data
|
||||
return new ApiError(HttpStatusCode.NotFound, ERROR_MESSAGE.NOT_FOUND)
|
||||
}
|
||||
return new ApiError(response.result.code, response.result.message)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 문의 유형 타입 목록 조회
|
||||
* @param {string[]} responseList 문의 유형 타입 목록
|
||||
* @returns {CommonCode[]} 문의 유형 타입 목록
|
||||
*/
|
||||
getInquiryTypeList(responseList: string[]): CommonCode[] {
|
||||
const codeList: CommonCode[] = []
|
||||
responseList.forEach((item: any) => {
|
||||
if (item.headCd === '204200' || item.headCd === '204300' || item.headCd === '204400') {
|
||||
codeList.push({
|
||||
headCd: item.headCd,
|
||||
code: item.code,
|
||||
name: item.codeJp,
|
||||
refChar1: item.refChr1,
|
||||
})
|
||||
}
|
||||
})
|
||||
return codeList
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 문의 목록 조회 파라미터 처리
|
||||
* @param {URLSearchParams} searchParams URLSearchParams 객체
|
||||
* @returns {Record<string, string>} 문의 목록 조회 파라미터
|
||||
*/
|
||||
getSearchParams(searchParams: URLSearchParams): Record<string, string> {
|
||||
const params: Record<string, string> = {}
|
||||
searchParams.forEach((value, key) => {
|
||||
const match = key.match(/inquiryListRequest\[(.*)\]/)
|
||||
if (match) {
|
||||
params[match[1]] = value
|
||||
} else {
|
||||
params[key] = value
|
||||
}
|
||||
})
|
||||
return params
|
||||
}
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
import { SubmitTargetResponse } from '@/types/Survey'
|
||||
|
||||
// 2차점이 자신에게 매핑 된 1차 판매점과 관리자 정보 조회
|
||||
async function getSubMissionAdminSub(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const id = searchParams.get('id')
|
||||
|
||||
const query = `
|
||||
OPEN SYMMETRIC KEY SYMMETRICKEY DECRYPTION BY CERTIFICATE CERTI_QSPJP;
|
||||
SELECT
|
||||
MCS.STORE_ID AS targetStoreId
|
||||
, MCS.STORE_QCAST_NM AS targetStoreNm
|
||||
, MCP.EOS_LOGIN_ID AS repUserId
|
||||
, CONVERT(NVARCHAR(100), DecryptByKey(MCP.EMAIL)) AS repUserEmail
|
||||
, MCP.AUTHORITY AS auth
|
||||
FROM MS_CUST_STOREID MCS WITH(NOLOCK)
|
||||
LEFT OUTER JOIN MS_CUST_PERSON MCP WITH(NOLOCK)
|
||||
ON MCS.COMP_CD = MCP.COMP_CD
|
||||
AND MCS.STORE_ID = MCP.STORE_ID
|
||||
AND MCP.DEL_YN = 'N'
|
||||
WHERE MCS.COMP_CD = '5200'
|
||||
AND MCS.STORE_ID = (SELECT STORE_ID FROM MS_CUST_AGENCY_STOREID WHERE AGENCY_STORE_ID = '${id}' AND DEL_YN = 'N')
|
||||
AND MCP.EMAIL IS NOT NULL
|
||||
AND MCS.DEL_YN = 'N';
|
||||
CLOSE SYMMETRIC KEY SYMMETRICKEY;
|
||||
`
|
||||
const data: SubmitTargetResponse[] = await prisma.$queryRawUnsafe(query)
|
||||
return NextResponse.json(data)
|
||||
} catch (error) {
|
||||
console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error)
|
||||
return NextResponse.json({ error: 'データの取得に失敗しました。' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getSubMissionAdminSub)
|
||||
@ -1,48 +0,0 @@
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
|
||||
type SuperPerson = {
|
||||
storeId: string
|
||||
salesOfficeCd: string
|
||||
fromEmail: string
|
||||
toEmail: string
|
||||
}
|
||||
|
||||
async function getSubmissionAdmin(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const id = searchParams.get('id')
|
||||
|
||||
const query = `
|
||||
OPEN SYMMETRIC KEY SYMMETRICKEY DECRYPTION BY CERTIFICATE CERTI_QSPJP;
|
||||
SELECT
|
||||
MCSA.STORE_ID
|
||||
, BCL.CODE AS SALES_OFFICE_CD
|
||||
, REF_CHR1 AS FROM_E_MAIL
|
||||
, REF_CHR2 AS TO_E_MAIL
|
||||
FROM MS_CUST_STOREID_ADDITNL MCSA WITH(NOLOCK)
|
||||
LEFT OUTER JOIN IF_PERSON_OFFICE_MAPPING IPOM WITH(NOLOCK)
|
||||
ON MCSA.KAM_ID = IPOM.LIFNR
|
||||
AND IF_STS = 'R'
|
||||
AND VKBUR IS NOT NULL
|
||||
AND VKBUR != ''
|
||||
INNER JOIN BC_COMM_L BCL WITH(NOLOCK)
|
||||
ON BCL.CODE = IPOM.VKBUR
|
||||
AND BCL.HEAD_CD = '103200'
|
||||
AND BCL.DEL_YN = 'N'
|
||||
WHERE MCSA.COMP_CD = '5200'
|
||||
AND MCSA.STORE_ID = 'A03'
|
||||
AND MCSA.DEL_YN = 'N'
|
||||
;
|
||||
CLOSE SYMMETRIC KEY SYMMETRICKEY;
|
||||
`
|
||||
const data: SuperPerson[] = await prisma.$queryRawUnsafe(query)
|
||||
return NextResponse.json(data)
|
||||
} catch (error) {
|
||||
console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error)
|
||||
return NextResponse.json({ error: 'データの取得に失敗しました。' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getSubmissionAdmin)
|
||||
@ -1,41 +0,0 @@
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { SubmitTargetResponse } from '@/types/Survey'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
|
||||
// 2차점의 시공권한 user가 해당 판매점의 관리자 정보 조회
|
||||
// N == 일반유저, S == 수퍼유저, B == 시공권한유저
|
||||
async function getSubmissionBuilder(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const id = searchParams.get('id')
|
||||
|
||||
const query = `
|
||||
OPEN SYMMETRIC KEY SYMMETRICKEY DECRYPTION BY CERTIFICATE CERTI_QSPJP;
|
||||
SELECT
|
||||
MCAS.AGENCY_STORE_ID AS targetStoreId
|
||||
, MCAS.AGENCY_QCAST_NM AS targetStoreNm
|
||||
, BQU.USER_ID AS repUserId
|
||||
, CONVERT(NVARCHAR(100), DecryptByKey(BQU.EMAIL)) AS repUserEmail
|
||||
, BQU.USER_AUTH_CD AS auth
|
||||
FROM MS_CUST_AGENCY_STOREID MCAS WITH(NOLOCK)
|
||||
LEFT OUTER JOIN BC_QM_USER BQU WITH(NOLOCK)
|
||||
ON MCAS.COMP_CD = BQU.COMP_CD
|
||||
AND MCAS.AGENCY_STORE_ID = BQU.AGENCY_STORE_ID
|
||||
AND MCAS.DEL_YN = 'N'
|
||||
WHERE MCAS.COMP_CD = '5200'
|
||||
AND MCAS.AGENCY_STORE_ID = '${id}'
|
||||
AND BQU.EMAIL IS NOT NULL
|
||||
AND BQU.USER_AUTH_CD != 'B'
|
||||
AND MCAS.DEL_YN = 'N';
|
||||
CLOSE SYMMETRIC KEY SYMMETRICKEY;
|
||||
`
|
||||
const data: SubmitTargetResponse[] = await prisma.$queryRawUnsafe(query)
|
||||
return NextResponse.json(data)
|
||||
} catch (error) {
|
||||
console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error)
|
||||
return NextResponse.json({ error: 'データの取得に失敗しました。' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getSubmissionBuilder)
|
||||
61
src/app/api/submission/route.ts
Normal file
61
src/app/api/submission/route.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { SubmissionService } from './service'
|
||||
import { HttpStatusCode } from 'axios'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
import { ERROR_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
|
||||
/**
|
||||
* @api {GET} /api/submission 제출 대상 조회
|
||||
* @apiName GET /api/submission
|
||||
* @apiGroup Submission
|
||||
* @apiDescription 제출 대상 조회
|
||||
*
|
||||
* @param {String} storeId 판매점 ID (required)
|
||||
* @param {String} role 권한 (required)
|
||||
*
|
||||
* @apiSuccess {Object} data 제출 대상 목록
|
||||
* @apiSuccess {String} data.targetStoreId 판매점 ID
|
||||
* @apiSuccess {String} data.salesOfficeCd 영업소 코드
|
||||
* @apiSuccess {String} data.fromEmail 발신자 이메일
|
||||
* @apiSuccess {String} data.toEmail 수신자 이메일
|
||||
*
|
||||
* @returns {Promise<SubmitTargetResponse[]>} 제출 대상 목록
|
||||
* @apiSuccessExample {json} Success-Response:
|
||||
* {
|
||||
* "data": [
|
||||
* {
|
||||
* "targetStoreId": "1234567890",
|
||||
* "salesOfficeCd": "1234567890",
|
||||
* "fromEmail": "1234567890",
|
||||
* "toEmail": "1234567890"
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
*
|
||||
* @apiExample {curl} Example usage:
|
||||
* curl -X GET \
|
||||
* -H "Content-Type: application/json" \
|
||||
* http://localhost:3000/api/submission?storeId=1234567890&role=admin
|
||||
*
|
||||
* @apiError {Object} error 에러 객체
|
||||
* @apiError {String} error.message 에러 메시지
|
||||
*/
|
||||
async function getSubmitTargetData(request: NextRequest): Promise<NextResponse> {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const storeId = searchParams.get('storeId')
|
||||
const role = searchParams.get('role')
|
||||
|
||||
if (!storeId || !role) {
|
||||
return NextResponse.json({ error: ERROR_MESSAGE.BAD_REQUEST }, { status: HttpStatusCode.BadRequest })
|
||||
}
|
||||
|
||||
const submissionService = new SubmissionService(storeId, role)
|
||||
const result = await submissionService.tryFunction(() => submissionService.getSubmissionTarget())
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json(result)
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getSubmitTargetData)
|
||||
121
src/app/api/submission/service.ts
Normal file
121
src/app/api/submission/service.ts
Normal file
@ -0,0 +1,121 @@
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { ERROR_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
import { SubmitTargetResponse } from '@/types/Survey'
|
||||
import { HttpStatusCode } from 'axios'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
import { Prisma } from '@prisma/client'
|
||||
|
||||
export class SubmissionService {
|
||||
private storeId: string
|
||||
private role: string
|
||||
private readonly BASE_QUERY = `
|
||||
DECLARE @storeId NVARCHAR(50);
|
||||
SET @storeId = @p1;
|
||||
OPEN SYMMETRIC KEY SYMMETRICKEY DECRYPTION BY CERTIFICATE CERTI_QSPJP;
|
||||
`
|
||||
|
||||
constructor(storeId: string, role: string) {
|
||||
this.storeId = storeId
|
||||
this.role = role
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 제출 대상 조회
|
||||
* @returns {Promise<SubmitTargetResponse[] | ApiError>} 제출 대상 데이터
|
||||
*/
|
||||
async getSubmissionTarget(): Promise<SubmitTargetResponse[] | ApiError> {
|
||||
switch (this.role) {
|
||||
case 'Admin_Sub':
|
||||
return this.getSubmissionTargetAdminSub()
|
||||
case 'Builder':
|
||||
return this.getSubmissionTargetBuilder()
|
||||
default:
|
||||
return new ApiError(HttpStatusCode.BadRequest, ERROR_MESSAGE.BAD_REQUEST)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 2차점의 매핑 된 제출 대상 판매점 조회 (Admin_Sub - Musubi)
|
||||
* @returns {Promise<SubmitTargetResponse[]>} 제출 대상 데이터
|
||||
*/
|
||||
private async getSubmissionTargetAdminSub(): Promise<SubmitTargetResponse[]> {
|
||||
const query = `
|
||||
SELECT
|
||||
MCS.STORE_ID AS targetStoreId
|
||||
, MCS.STORE_QCAST_NM AS targetStoreNm
|
||||
, MCP.EOS_LOGIN_ID AS repUserId
|
||||
, CONVERT(NVARCHAR(100), DecryptByKey(MCP.EMAIL)) AS repUserEmail
|
||||
, MCP.AUTHORITY AS auth
|
||||
FROM MS_CUST_STOREID MCS WITH(NOLOCK)
|
||||
LEFT OUTER JOIN MS_CUST_PERSON MCP WITH(NOLOCK)
|
||||
ON MCS.COMP_CD = MCP.COMP_CD
|
||||
AND MCS.STORE_ID = MCP.STORE_ID
|
||||
AND MCP.DEL_YN = 'N'
|
||||
WHERE MCS.COMP_CD = '5200'
|
||||
AND MCS.STORE_ID IN (SELECT STORE_ID FROM MS_CUST_AGENCY_STOREID WHERE AGENCY_STORE_ID = @storeId AND DEL_YN = 'N')
|
||||
AND MCP.EMAIL IS NOT NULL
|
||||
AND MCS.DEL_YN = 'N';
|
||||
CLOSE SYMMETRIC KEY SYMMETRICKEY;
|
||||
`
|
||||
return await prisma.$queryRawUnsafe(this.BASE_QUERY + query, this.storeId)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 2차점 시공권한 user의 매핑 된 제출 대상 판매점 조회 (Builder - Musubi)
|
||||
* @returns {Promise<SubmitTargetResponse[]>} 제출 대상 데이터
|
||||
*/
|
||||
private async getSubmissionTargetBuilder(): Promise<SubmitTargetResponse[]> {
|
||||
const query = `
|
||||
SELECT
|
||||
MCAS.AGENCY_STORE_ID AS targetStoreId
|
||||
, MCAS.AGENCY_QCAST_NM AS targetStoreNm
|
||||
, BQU.USER_ID AS repUserId
|
||||
, CONVERT(NVARCHAR(100), DecryptByKey(BQU.EMAIL)) AS repUserEmail
|
||||
, BQU.USER_AUTH_CD AS auth
|
||||
FROM MS_CUST_AGENCY_STOREID MCAS WITH(NOLOCK)
|
||||
LEFT OUTER JOIN BC_QM_USER BQU WITH(NOLOCK)
|
||||
ON MCAS.COMP_CD = BQU.COMP_CD
|
||||
AND MCAS.AGENCY_STORE_ID = BQU.AGENCY_STORE_ID
|
||||
AND MCAS.DEL_YN = 'N'
|
||||
WHERE MCAS.COMP_CD = '5200'
|
||||
AND MCAS.AGENCY_STORE_ID = @storeId
|
||||
AND BQU.EMAIL IS NOT NULL
|
||||
AND BQU.USER_AUTH_CD != 'B'
|
||||
AND MCAS.DEL_YN = 'N';
|
||||
CLOSE SYMMETRIC KEY SYMMETRICKEY;
|
||||
`
|
||||
return await prisma.$queryRawUnsafe(this.BASE_QUERY + query, this.storeId)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description API ROUTE 에러 처리
|
||||
* @param error 에러 객체
|
||||
* @returns {ApiError} 에러 객체
|
||||
*/
|
||||
handleRouteError(error: any): ApiError {
|
||||
console.error('❌ API ROUTE ERROR : ', error)
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientInitializationError ||
|
||||
error instanceof Prisma.PrismaClientUnknownRequestError ||
|
||||
error instanceof Prisma.PrismaClientRustPanicError ||
|
||||
error instanceof Prisma.PrismaClientValidationError ||
|
||||
error instanceof Prisma.PrismaClientKnownRequestError
|
||||
) {
|
||||
return new ApiError(HttpStatusCode.InternalServerError, ERROR_MESSAGE.PRISMA_ERROR)
|
||||
}
|
||||
return new ApiError(error.statusCode ?? HttpStatusCode.InternalServerError, error.message ?? ERROR_MESSAGE.FETCH_ERROR)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 비동기 함수 try-catch 처리 함수
|
||||
* @param func 비동기 함수
|
||||
* @returns {Promise<ApiError | any>} 에러 객체 또는 함수 결과
|
||||
*/
|
||||
async tryFunction(func: () => Promise<any>): Promise<ApiError | any> {
|
||||
try {
|
||||
return await func()
|
||||
} catch (error) {
|
||||
return this.handleRouteError(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
|
||||
type SuperPerson = {
|
||||
storeId: string
|
||||
userId: string
|
||||
eMail: string
|
||||
}
|
||||
|
||||
async function getSubmissionSuper(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const id = searchParams.get('id')
|
||||
|
||||
const query = `
|
||||
OPEN SYMMETRIC KEY SYMMETRICKEY DECRYPTION BY CERTIFICATE CERTI_QSPJP;
|
||||
SELECT
|
||||
MCSA.STORE_ID
|
||||
, BU.USER_ID
|
||||
, CONVERT(NVARCHAR(100), DecryptByKey(BU.E_MAIL)) AS E_MAIL
|
||||
FROM MS_CUST_STOREID_ADDITNL MCSA WITH(NOLOCK)
|
||||
LEFT OUTER JOIN BC_USER bu WITH(NOLOCK)
|
||||
ON MCSA.COMP_CD = BU.COMP_CD
|
||||
AND MCSA.KAM_ID = BU.KAM_ID
|
||||
AND BU.STAT_CD = 'A'
|
||||
AND BU.DEL_YN = 'N'
|
||||
WHERE MCSA.COMP_CD = '5200'
|
||||
AND MCSA.STORE_ID = 'A03'
|
||||
AND MCSA.DEL_YN = 'N';
|
||||
CLOSE SYMMETRIC KEY SYMMETRICKEY;
|
||||
`
|
||||
const data: SuperPerson[] = await prisma.$queryRawUnsafe(query)
|
||||
return NextResponse.json(data)
|
||||
} catch (error) {
|
||||
console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error);
|
||||
return NextResponse.json({ error: 'データの取得に失敗しました。' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getSubmissionSuper)
|
||||
@ -1,97 +1,12 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { convertToSnakeCase } from '@/utils/common-utils'
|
||||
import { getIronSession } from 'iron-session'
|
||||
import { sessionOptions } from '@/libs/session'
|
||||
import { cookies } from 'next/headers'
|
||||
import type { SessionData } from '@/types/Auth'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { HttpStatusCode } from 'axios'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
|
||||
/**
|
||||
* @description 조사 매물 조회 에러 메시지
|
||||
*/
|
||||
const ERROR_MESSAGES = {
|
||||
NOT_FOUND: 'データが見つかりません。',
|
||||
UNAUTHORIZED: 'Unauthorized',
|
||||
NO_PERMISSION: '該当物件の照会権限がありません。',
|
||||
FETCH_ERROR: 'データの取得に失敗しました。',
|
||||
} as const
|
||||
|
||||
/**
|
||||
* @description T01 조회 권한 체크
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @returns {boolean} 조사 매물 임시 저장 여부
|
||||
*/
|
||||
const checkT01Role = (survey: any): boolean => survey.SRL_NO !== '一時保存'
|
||||
|
||||
/**
|
||||
* @description Admin (1차 판매점) 권한 체크
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @param {string | null} storeId 판매점 ID
|
||||
* @returns {boolean} 권한 존재 여부
|
||||
*/
|
||||
const checkAdminRole = (survey: any, storeId: string | null): boolean => {
|
||||
if (!storeId) return false
|
||||
return survey.SUBMISSION_STATUS ? survey.SUBMISSION_TARGET_ID === storeId || survey.STORE_ID === storeId : survey.STORE_ID === storeId
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Admin_Sub (2차 판매점) 권한 체크
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @param {string | null} storeId 판매점 ID
|
||||
* @returns {boolean} 권한 존재 여부
|
||||
*/
|
||||
const checkAdminSubRole = (survey: any, storeId: string | null): boolean => {
|
||||
if (!storeId) return false
|
||||
return survey.SUBMISSION_STATUS
|
||||
? survey.SUBMISSION_TARGET_ID === storeId || (survey.STORE_ID === storeId && !survey.CONSTRUCTION_POINT_ID)
|
||||
: survey.STORE_ID === storeId && !survey.CONSTRUCTION_POINT_ID
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Partner (파트너) 또는 Builder (2차 판매점의 시공권한 회원) 권한 체크
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @param {string | null} builderId 시공점 ID
|
||||
* @returns {boolean} 권한 존재 여부
|
||||
*/
|
||||
const checkPartnerOrBuilderRole = (survey: any, builderId: string | null): boolean => {
|
||||
if (!builderId) return false
|
||||
return survey.CONSTRUCTION_POINT_ID === builderId
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 권한 체크
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @param {any} session 세션 데이터
|
||||
* @returns {boolean} 권한 존재 여부
|
||||
*/
|
||||
const checkRole = (survey: any, session: any): boolean => {
|
||||
if (!survey || !session.isLoggedIn) return false
|
||||
|
||||
const roleChecks = {
|
||||
T01: () => checkT01Role(survey),
|
||||
Admin: () => checkAdminRole(survey, session.storeId),
|
||||
Admin_Sub: () => checkAdminSubRole(survey, session.storeId),
|
||||
Partner: () => checkPartnerOrBuilderRole(survey, session.builderId),
|
||||
Builder: () => checkPartnerOrBuilderRole(survey, session.builderId),
|
||||
}
|
||||
|
||||
return roleChecks[session.role as keyof typeof roleChecks]?.() ?? false
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 조회
|
||||
* @param {number} id 조사 매물 ID
|
||||
* @returns {Promise<any>} 조사 매물 데이터
|
||||
*/
|
||||
const fetchSurvey = async (id: number) => {
|
||||
// @ts-ignore
|
||||
return await prisma.SD_SURVEY_SALES_BASIC_INFO.findFirst({
|
||||
where: { ID: id },
|
||||
include: { DETAIL_INFO: true },
|
||||
})
|
||||
}
|
||||
import { SurveySalesService } from '../service'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
|
||||
/**
|
||||
* @api {GET} /api/survey-sales/:id 조사 매물 조회 API
|
||||
@ -130,70 +45,18 @@ const fetchSurvey = async (id: number) => {
|
||||
* }
|
||||
*/
|
||||
async function getSurveySaleDetail(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const cookieStore = await cookies()
|
||||
const session = await getIronSession<SessionData>(cookieStore, sessionOptions)
|
||||
const id = request.nextUrl.pathname.split('/').pop() ?? ''
|
||||
const { searchParams } = new URL(request.url)
|
||||
const isPdf = searchParams.get('isPdf') === 'true'
|
||||
const cookieStore = await cookies()
|
||||
const session = await getIronSession<SessionData>(cookieStore, sessionOptions)
|
||||
const id = request.nextUrl.pathname.split('/').pop() ?? ''
|
||||
const { searchParams } = new URL(request.url)
|
||||
const isPdf = searchParams.get('isPdf') === 'true'
|
||||
|
||||
const survey = await fetchSurvey(Number(id))
|
||||
if (!survey) {
|
||||
return NextResponse.json({ error: ERROR_MESSAGES.NOT_FOUND }, { status: 404 })
|
||||
}
|
||||
|
||||
/** pdf 데이터 요청 여부, 권한 여부 확인 */
|
||||
if (isPdf || checkRole(survey, session)) {
|
||||
return NextResponse.json(survey)
|
||||
}
|
||||
|
||||
/** 로그인 여부 확인 */
|
||||
if (!session?.isLoggedIn || session?.role === null) {
|
||||
return NextResponse.json({ error: ERROR_MESSAGES.UNAUTHORIZED }, { status: 401 })
|
||||
}
|
||||
|
||||
/** 권한 없음 */
|
||||
return NextResponse.json({ error: ERROR_MESSAGES.NO_PERMISSION }, { status: 403 })
|
||||
} catch (error) {
|
||||
console.error('Error fetching survey:', error)
|
||||
return NextResponse.json({ error: ERROR_MESSAGES.FETCH_ERROR }, { status: 500 })
|
||||
const service = new SurveySalesService({}, session)
|
||||
const result = await service.tryFunction(() => service.fetchSurvey(Number(id), isPdf))
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 새로운 SRL_NO 생성
|
||||
* @param {string} srlNo 기존 SRL_NO
|
||||
* @param {string} storeId 판매점 ID
|
||||
* @param {string} role 권한
|
||||
* @returns {Promise<string>} 새로운 SRL_NO
|
||||
*/
|
||||
const getNewSrlNo = async (srlNo: string, storeId: string, role: string) => {
|
||||
const srlRole = role === 'T01' || role === 'Admin' ? 'HO' : role === 'Admin_Sub' || role === 'Builder' ? 'HM' : ''
|
||||
|
||||
let newSrlNo = srlNo
|
||||
if (srlNo.startsWith('一時保存')) {
|
||||
//@ts-ignore
|
||||
const lastSurvey = await prisma.SD_SURVEY_SALES_BASIC_INFO.findFirst({
|
||||
where: {
|
||||
SRL_NO: {
|
||||
startsWith: srlRole,
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
ID: 'desc',
|
||||
},
|
||||
})
|
||||
const lastNo = lastSurvey ? parseInt(lastSurvey.SRL_NO.slice(-3)) : 0
|
||||
|
||||
newSrlNo =
|
||||
srlRole +
|
||||
storeId +
|
||||
new Date().getFullYear().toString().slice(-2) +
|
||||
(new Date().getMonth() + 1).toString().padStart(2, '0') +
|
||||
new Date().getDate().toString().padStart(2, '0') +
|
||||
(lastNo + 1).toString().padStart(3, '0')
|
||||
}
|
||||
return newSrlNo
|
||||
return NextResponse.json(result)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -230,33 +93,15 @@ const getNewSrlNo = async (srlNo: string, storeId: string, role: string) => {
|
||||
* }
|
||||
* */
|
||||
async function updateSurveySaleDetail(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const id = request.nextUrl.pathname.split('/').pop() ?? ''
|
||||
const body = await request.json()
|
||||
const { detailInfo, ...basicInfo } = body.survey
|
||||
const id = request.nextUrl.pathname.split('/').pop() ?? ''
|
||||
const body = await request.json()
|
||||
const service = new SurveySalesService({})
|
||||
|
||||
// PUT 요청 시 임시저장 여부 확인 후 임시저장 시 기존 SRL_NO 사용, 기본 저장 시 새로운 SRL_NO 생성
|
||||
const newSrlNo = body.isTemporary ? body.survey.srlNo : await getNewSrlNo(body.survey.srlNo, body.storeId, body.role)
|
||||
// @ts-ignore
|
||||
const survey = await prisma.SD_SURVEY_SALES_BASIC_INFO.update({
|
||||
where: { ID: Number(id) },
|
||||
data: {
|
||||
...convertToSnakeCase(basicInfo),
|
||||
SRL_NO: newSrlNo,
|
||||
UPT_DT: new Date(),
|
||||
DETAIL_INFO: {
|
||||
update: convertToSnakeCase(detailInfo),
|
||||
},
|
||||
},
|
||||
include: {
|
||||
DETAIL_INFO: true,
|
||||
},
|
||||
})
|
||||
return NextResponse.json(survey)
|
||||
} catch (error) {
|
||||
console.error('Error updating survey:', error)
|
||||
return NextResponse.json({ error: 'データ保存に失敗しました。' }, { status: 500 })
|
||||
const result = await service.tryFunction(() => service.updateSurvey(Number(id), body.survey, body.isTemporary, body.storeId, body.role))
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json(result)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -278,36 +123,13 @@ async function updateSurveySaleDetail(request: NextRequest): Promise<NextRespons
|
||||
* "message": "success"
|
||||
*/
|
||||
async function deleteSurveySaleDetail(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const id = request.nextUrl.pathname.split('/').pop() ?? ''
|
||||
|
||||
await prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
||||
// @ts-ignore
|
||||
const detailData = await tx.SD_SURVEY_SALES_BASIC_INFO.findUnique({
|
||||
where: { ID: Number(id) },
|
||||
select: {
|
||||
DETAIL_INFO: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (detailData?.DETAIL_INFO?.ID) {
|
||||
// @ts-ignore
|
||||
await tx.SD_SURVEY_SALES_DETAIL_INFO.delete({
|
||||
where: { ID: Number(detailData.DETAIL_INFO.ID) },
|
||||
})
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
await tx.SD_SURVEY_SALES_BASIC_INFO.delete({
|
||||
where: { ID: Number(id) },
|
||||
})
|
||||
})
|
||||
|
||||
return NextResponse.json({ message: 'success' })
|
||||
} catch (error) {
|
||||
console.error('Error deleting survey:', error)
|
||||
return NextResponse.json({ error: 'データ削除に失敗しました。' }, { status: 500 })
|
||||
const id = request.nextUrl.pathname.split('/').pop() ?? ''
|
||||
const service = new SurveySalesService({})
|
||||
const result = await service.tryFunction(() => service.deleteSurvey(Number(id)))
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json({ status: HttpStatusCode.Ok })
|
||||
}
|
||||
|
||||
/**
|
||||
@ -346,25 +168,15 @@ async function deleteSurveySaleDetail(request: NextRequest): Promise<NextRespons
|
||||
* }
|
||||
*/
|
||||
async function submitSurveySaleDetail(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const id = request.nextUrl.pathname.split('/').pop() ?? ''
|
||||
const body = await request.json()
|
||||
// @ts-ignore
|
||||
const survey = await prisma.SD_SURVEY_SALES_BASIC_INFO.update({
|
||||
where: { ID: Number(id) },
|
||||
data: {
|
||||
SUBMISSION_STATUS: true,
|
||||
SUBMISSION_DATE: new Date(),
|
||||
SUBMISSION_TARGET_ID: body.targetId,
|
||||
SUBMISSION_TARGET_NM: body.targetNm,
|
||||
UPT_DT: new Date(),
|
||||
},
|
||||
})
|
||||
return NextResponse.json({ message: 'Survey confirmed successfully', data: survey })
|
||||
} catch (error) {
|
||||
console.error('Error updating survey:', error)
|
||||
return NextResponse.json({ error: 'データ保存に失敗しました。' }, { status: 500 })
|
||||
const id = request.nextUrl.pathname.split('/').pop() ?? ''
|
||||
const body = await request.json()
|
||||
const service = new SurveySalesService({})
|
||||
|
||||
const result = await service.tryFunction(() => service.submitSurvey(Number(id), body.targetId, body.targetNm))
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json({ status: HttpStatusCode.Ok, data: result })
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getSurveySaleDetail)
|
||||
|
||||
@ -1,155 +1,12 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { convertToSnakeCase } from '@/utils/common-utils'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
/**
|
||||
* @description 검색 파라미터 타입
|
||||
*/
|
||||
type SearchParams = {
|
||||
keyword?: string | null
|
||||
searchOption?: string | null
|
||||
isMySurvey?: string | null
|
||||
sort?: string | null
|
||||
offset?: string | null
|
||||
role?: string | null
|
||||
storeId?: string | null
|
||||
builderId?: string | null
|
||||
}
|
||||
|
||||
type WhereCondition = {
|
||||
AND: any[]
|
||||
OR?: any[]
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
/** 검색 가능한 필드 옵션 */
|
||||
const SEARCH_OPTIONS = [
|
||||
'BUILDING_NAME',
|
||||
'REPRESENTATIVE',
|
||||
'STORE',
|
||||
'STORE_ID',
|
||||
'CONSTRUCTION_POINT',
|
||||
'CONSTRUCTION_POINT_ID',
|
||||
'CUSTOMER_NAME',
|
||||
'POST_CODE',
|
||||
'ADDRESS',
|
||||
'ADDRESS_DETAIL',
|
||||
'SRL_NO',
|
||||
] as const
|
||||
|
||||
/** 페이지당 기본 항목 수 */
|
||||
const ITEMS_PER_PAGE = 10
|
||||
|
||||
/**
|
||||
* @description 키워드 검색 조건 생성 함수
|
||||
* @param {string} keyword 검색 키워드
|
||||
* @param {string} searchOption 검색 옵션
|
||||
* @returns {WhereCondition} 검색 조건 객체
|
||||
*/
|
||||
const createKeywordSearchCondition = (keyword: string, searchOption: string): WhereCondition => {
|
||||
const where: WhereCondition = { AND: [] }
|
||||
|
||||
if (searchOption === 'all') {
|
||||
/** 모든 필드 검색 시 OR 조건 사용 */
|
||||
where.OR = []
|
||||
|
||||
where.OR.push(
|
||||
...SEARCH_OPTIONS.map((field) => ({
|
||||
[field]: { contains: keyword },
|
||||
})),
|
||||
)
|
||||
} else if (SEARCH_OPTIONS.includes(searchOption.toUpperCase() as any)) {
|
||||
/** 특정 필드 검색 */
|
||||
where[searchOption.toUpperCase()] = { contains: keyword }
|
||||
}
|
||||
return where
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 회원 역할별 검색 조건 생성 함수
|
||||
* @param {SearchParams} params 검색 파라미터
|
||||
* @returns {WhereCondition} 검색 조건 객체
|
||||
*/
|
||||
const createMemberRoleCondition = (params: SearchParams): WhereCondition => {
|
||||
const where: WhereCondition = { AND: [] }
|
||||
|
||||
switch (params.role) {
|
||||
case 'Admin':
|
||||
where.OR = [
|
||||
{
|
||||
AND: [{ STORE_ID: { equals: params.storeId } }],
|
||||
},
|
||||
{
|
||||
AND: [{ SUBMISSION_TARGET_ID: { equals: params.storeId } }, { SUBMISSION_STATUS: { equals: true } }],
|
||||
},
|
||||
]
|
||||
break
|
||||
|
||||
case 'Admin_Sub':
|
||||
where.OR = [
|
||||
{
|
||||
AND: [{ STORE_ID: { equals: params.storeId } }, { CONSTRUCTION_POINT_ID: { equals: params.builderId } }],
|
||||
},
|
||||
{
|
||||
AND: [
|
||||
{ SUBMISSION_TARGET_ID: { equals: params.storeId } },
|
||||
{ CONSTRUCTION_POINT_ID: { not: null } },
|
||||
{ CONSTRUCTION_POINT_ID: { not: '' } },
|
||||
{ SUBMISSION_STATUS: { equals: true } },
|
||||
],
|
||||
},
|
||||
]
|
||||
break
|
||||
|
||||
case 'Builder':
|
||||
case 'Partner':
|
||||
where.AND?.push({
|
||||
CONSTRUCTION_POINT_ID: { equals: params.builderId },
|
||||
})
|
||||
break
|
||||
|
||||
case 'T01':
|
||||
where.OR = [
|
||||
{
|
||||
NOT: {
|
||||
SRL_NO: {
|
||||
startsWith: '一時保存',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
STORE_ID: {
|
||||
equals: params.storeId,
|
||||
},
|
||||
},
|
||||
]
|
||||
break
|
||||
case 'User':
|
||||
break
|
||||
}
|
||||
|
||||
return where
|
||||
}
|
||||
/**
|
||||
* @description 권한 별 필수 값 존재 여부 확인, 없을 시 빈 데이터 반환
|
||||
* @param {SearchParams} params 검색 파라미터
|
||||
* @returns {NextResponse} 세션 체크 결과
|
||||
*/
|
||||
const checkSession = (params: SearchParams) => {
|
||||
if (params.role === null) {
|
||||
return NextResponse.json({ data: [], count: 0 })
|
||||
}
|
||||
if (params.role === 'Builder' || params.role === 'Partner') {
|
||||
if (params.builderId === null) {
|
||||
return NextResponse.json({ data: [], count: 0 })
|
||||
}
|
||||
} else {
|
||||
if (params.storeId === null) {
|
||||
return NextResponse.json({ data: [], count: 0 })
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
import { SurveySearchParams } from '@/types/Survey'
|
||||
import { SurveySalesService } from './service'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
import { getIronSession } from 'iron-session'
|
||||
import { sessionOptions } from '@/libs/session'
|
||||
import { cookies } from 'next/headers'
|
||||
import { SessionData } from '@/types/Auth'
|
||||
|
||||
/**
|
||||
* @api {GET} /api/survey-sales 설문 목록 조회 API
|
||||
@ -194,110 +51,24 @@ const checkSession = (params: SearchParams) => {
|
||||
*
|
||||
*/
|
||||
async function getSurveySales(request: Request) {
|
||||
try {
|
||||
/** URL 파라미터 파싱 */
|
||||
const { searchParams } = new URL(request.url)
|
||||
const params: SearchParams = {
|
||||
keyword: searchParams.get('keyword'),
|
||||
searchOption: searchParams.get('searchOption'),
|
||||
isMySurvey: searchParams.get('isMySurvey'),
|
||||
sort: searchParams.get('sort'),
|
||||
offset: searchParams.get('offset'),
|
||||
role: searchParams.get('role'),
|
||||
storeId: searchParams.get('storeId'),
|
||||
builderId: searchParams.get('builderId'),
|
||||
}
|
||||
const cookieStore = await cookies()
|
||||
const session = await getIronSession<SessionData>(cookieStore, sessionOptions)
|
||||
|
||||
/** 세션 체크 결과 처리 */
|
||||
const sessionCheckResult = checkSession(params)
|
||||
if (sessionCheckResult) {
|
||||
return sessionCheckResult
|
||||
}
|
||||
|
||||
/** 검색 조건 구성 */
|
||||
const where: WhereCondition = { AND: [] }
|
||||
|
||||
/** 내가 작성한 매물 조건 적용 */
|
||||
if (params.isMySurvey) {
|
||||
where.AND.push({ REPRESENTATIVE_ID: params.isMySurvey })
|
||||
}
|
||||
|
||||
/** 키워드 검색 조건 적용 */
|
||||
if (params.keyword && params.searchOption) {
|
||||
where.AND.push(createKeywordSearchCondition(params.keyword, params.searchOption))
|
||||
}
|
||||
|
||||
/** 회원 유형 조건 적용 */
|
||||
const roleCondition = createMemberRoleCondition(params)
|
||||
if (Object.keys(roleCondition).length > 0) {
|
||||
where.AND.push(roleCondition)
|
||||
}
|
||||
/** 페이지네이션 데이터 조회 */
|
||||
//@ts-ignore
|
||||
const surveys = await prisma.SD_SURVEY_SALES_BASIC_INFO.findMany({
|
||||
where,
|
||||
orderBy: params.sort === 'created' ? { REG_DT: 'desc' } : { UPT_DT: 'desc' },
|
||||
skip: Number(params.offset),
|
||||
take: ITEMS_PER_PAGE,
|
||||
})
|
||||
/** 전체 개수만 조회 */
|
||||
//@ts-ignore
|
||||
const count = await prisma.SD_SURVEY_SALES_BASIC_INFO.count({ where })
|
||||
return NextResponse.json({ data: { data: surveys, count: count } })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return NextResponse.json({ error: 'データ照会に失敗しました。' }, { status: 500 })
|
||||
const { searchParams } = new URL(request.url)
|
||||
const params: SurveySearchParams = {
|
||||
keyword: searchParams.get('keyword'),
|
||||
searchOption: searchParams.get('searchOption'),
|
||||
isMySurvey: searchParams.get('isMySurvey'),
|
||||
sort: searchParams.get('sort'),
|
||||
offset: searchParams.get('offset'),
|
||||
}
|
||||
}
|
||||
const surveySalesService = new SurveySalesService(params, session)
|
||||
|
||||
/**
|
||||
* @api {PUT} /api/survey-sales 설문 상세 정보 추가 API
|
||||
* @apiName PUT /api/survey-sales
|
||||
* @apiGroup SurveySales
|
||||
* @apiDescription 설문 상세 정보 추가 API
|
||||
*
|
||||
* @apiParam {Number} id 설문 목록 ID (required)
|
||||
* @apiBody {Object} detail_info 상세 정보 (required)
|
||||
*
|
||||
* @apiSuccess {String} message 성공 메시지
|
||||
*
|
||||
* @apiExample {curl} Example usage:
|
||||
* curl -X PUT \
|
||||
* -H "Content-Type: application/json" \
|
||||
* -d '{"id": 1, "detail_info": {"memo": "1234567890"}}' \
|
||||
* http://localhost:3000/api/survey-sales
|
||||
*
|
||||
* @apiSuccessExample {json} Success-Response:
|
||||
* {
|
||||
* "message": "Success Update Survey"
|
||||
* }
|
||||
*
|
||||
* @apiError {Number} 500 서버 오류
|
||||
*/
|
||||
async function updateSurveySales(request: Request) {
|
||||
try {
|
||||
/** 요청 바디 파싱 */
|
||||
const body = await request.json()
|
||||
|
||||
/** 상세 정보 생성을 위한 데이터 구성 */
|
||||
const detailInfo = {
|
||||
...body.detail_info,
|
||||
BASIC_INFO_ID: body.id,
|
||||
}
|
||||
|
||||
/** 상세 정보 생성 */
|
||||
//@ts-ignore
|
||||
await prisma.SD_SURVEY_SALES_DETAIL_INFO.create({
|
||||
data: detailInfo,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
message: 'Success Update Survey',
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return NextResponse.json({ error: 'Fail Update Survey' }, { status: 500 })
|
||||
const result = await surveySalesService.tryFunction(() => surveySalesService.getSurveySales())
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json({ data: result })
|
||||
}
|
||||
|
||||
/**
|
||||
@ -337,64 +108,14 @@ async function updateSurveySales(request: Request) {
|
||||
* @apiError {Number} 500 서버 오류
|
||||
*/
|
||||
async function createSurveySales(request: Request) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
|
||||
const role =
|
||||
body.role === 'T01' || body.role === 'Admin'
|
||||
? 'HO'
|
||||
: body.role === 'Admin_Sub' || body.role === 'Builder'
|
||||
? 'HM'
|
||||
: body.role === 'Partner'
|
||||
? ''
|
||||
: null
|
||||
|
||||
/** 임시 저장 시 임시저장으로 저장 */
|
||||
/** 기본 저장 시 (HO/HM) + 판매점ID + yyMMdd + 000 으로 저장 */
|
||||
const baseSrlNo =
|
||||
body.survey.srlNo ??
|
||||
role +
|
||||
body.storeId +
|
||||
new Date().getFullYear().toString().slice(-2) +
|
||||
(new Date().getMonth() + 1).toString().padStart(2, '0') +
|
||||
new Date().getDate().toString().padStart(2, '0')
|
||||
|
||||
// @ts-ignore
|
||||
const lastSurvey = await prisma.SD_SURVEY_SALES_BASIC_INFO.findFirst({
|
||||
where: {
|
||||
SRL_NO: {
|
||||
startsWith: role + body.storeId,
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
SRL_NO: 'desc',
|
||||
},
|
||||
})
|
||||
|
||||
/** 마지막 번호 추출 */
|
||||
const lastNumber = lastSurvey ? parseInt(lastSurvey.SRL_NO.slice(-3)) : 0
|
||||
|
||||
/** 새로운 srlNo 생성 - 임시저장일 경우 '임시저장' 으로 저장 */
|
||||
const newSrlNo = baseSrlNo.startsWith('一時保存') ? baseSrlNo : baseSrlNo + (lastNumber + 1).toString().padStart(3, '0')
|
||||
|
||||
const { detailInfo, ...basicInfo } = body.survey
|
||||
// @ts-ignore
|
||||
const result = await prisma.SD_SURVEY_SALES_BASIC_INFO.create({
|
||||
data: {
|
||||
...convertToSnakeCase(basicInfo),
|
||||
SRL_NO: newSrlNo,
|
||||
DETAIL_INFO: {
|
||||
create: convertToSnakeCase(detailInfo),
|
||||
},
|
||||
},
|
||||
})
|
||||
return NextResponse.json(result)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return NextResponse.json({ error: 'データ保存に失敗しました。' }, { status: 500 })
|
||||
const body = await request.json()
|
||||
const surveySalesService = new SurveySalesService({})
|
||||
const result = await surveySalesService.tryFunction(() => surveySalesService.createSurvey(body.survey, body.role, body.storeId))
|
||||
if (result instanceof ApiError) {
|
||||
return NextResponse.json({ error: result.message }, { status: result.statusCode })
|
||||
}
|
||||
return NextResponse.json(result)
|
||||
}
|
||||
|
||||
export const GET = loggerWrapper(getSurveySales)
|
||||
export const PUT = loggerWrapper(updateSurveySales)
|
||||
export const POST = loggerWrapper(createSurveySales)
|
||||
456
src/app/api/survey-sales/service.ts
Normal file
456
src/app/api/survey-sales/service.ts
Normal file
@ -0,0 +1,456 @@
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { SurveyBasicInfo, SurveyRegistRequest, SurveySearchParams } from '@/types/Survey'
|
||||
import { convertToSnakeCase } from '@/utils/common-utils'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import type { SessionData } from '@/types/Auth'
|
||||
import { HttpStatusCode } from 'axios'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
import { ERROR_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
|
||||
type WhereCondition = {
|
||||
AND: any[]
|
||||
OR?: any[]
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
/** 검색 옵션 */
|
||||
const SEARCH_OPTIONS = [
|
||||
'BUILDING_NAME',
|
||||
'REPRESENTATIVE',
|
||||
'STORE',
|
||||
'STORE_ID',
|
||||
'CONSTRUCTION_POINT',
|
||||
'CONSTRUCTION_POINT_ID',
|
||||
'CUSTOMER_NAME',
|
||||
'POST_CODE',
|
||||
'ADDRESS',
|
||||
'ADDRESS_DETAIL',
|
||||
'SRL_NO',
|
||||
] as const
|
||||
|
||||
/** 페이지당 기본 항목 수 */
|
||||
const ITEMS_PER_PAGE = 10
|
||||
|
||||
/**
|
||||
* @description 조사 매물 서비스
|
||||
* @param {SurveySearchParams} params 검색 파라미터
|
||||
*/
|
||||
export class SurveySalesService {
|
||||
private params!: SurveySearchParams
|
||||
private session?: SessionData
|
||||
|
||||
/**
|
||||
* @description 생성자
|
||||
* @param {SurveySearchParams} params 검색 파라미터
|
||||
*/
|
||||
constructor(params: SurveySearchParams, session?: SessionData) {
|
||||
this.params = params
|
||||
this.session = session
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 권한 별 필수 값 존재 여부 확인, 없을 시 빈 데이터 반환
|
||||
* @param {SearchParams} params 검색 파라미터
|
||||
* @returns {NextResponse} 세션 체크 결과
|
||||
*/
|
||||
checkSession(): ApiError | null {
|
||||
if (!this.session?.isLoggedIn || this.session?.role === null) {
|
||||
return new ApiError(HttpStatusCode.Unauthorized, ERROR_MESSAGE.UNAUTHORIZED)
|
||||
}
|
||||
if (this.session?.role === 'Builder' || this.session?.role === 'Partner') {
|
||||
if (this.params.builderId === null) {
|
||||
return new ApiError(HttpStatusCode.Forbidden, ERROR_MESSAGE.FORBIDDEN)
|
||||
}
|
||||
} else {
|
||||
if (this.session?.storeId === null) {
|
||||
return new ApiError(HttpStatusCode.Forbidden, ERROR_MESSAGE.FORBIDDEN)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 내가 작성한 매물 조건 생성
|
||||
* @returns {WhereCondition} 내가 작성한 매물 조건
|
||||
*/
|
||||
private createMySurveyCondition(): WhereCondition {
|
||||
if (!this.params.isMySurvey) return { AND: [] }
|
||||
return { AND: [{ REPRESENTATIVE_ID: this.params.isMySurvey }] }
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 키워드 검색 조건 생성
|
||||
* @returns {WhereCondition} 키워드 검색 조건
|
||||
*/
|
||||
private createKeywordCondition(): WhereCondition {
|
||||
if (!this.params.keyword || !this.params.searchOption) return { AND: [] }
|
||||
|
||||
const where: WhereCondition = { AND: [] }
|
||||
if (this.params.searchOption === 'all') {
|
||||
where.OR = SEARCH_OPTIONS.map((field) => ({
|
||||
[field]: { contains: this.params.keyword },
|
||||
}))
|
||||
} else if (SEARCH_OPTIONS.includes(this.params.searchOption?.toUpperCase() as (typeof SEARCH_OPTIONS)[number])) {
|
||||
where[this.params.searchOption?.toUpperCase() as (typeof SEARCH_OPTIONS)[number]] = { contains: this.params.keyword }
|
||||
}
|
||||
return where
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 역할 기반 조건 생성
|
||||
* @returns {WhereCondition} 역할 기반 조건
|
||||
* @exampleResult { AND: [{ STORE_ID: { equals: '1234567890' } }] }
|
||||
*
|
||||
* @description T01 : 임시저장되지 않은 전체 매물 조회
|
||||
* @description Admin : 같은 판매점에서 작성된 매물, 2차점에게 제출받은 매물 조회
|
||||
* @description Admin_Sub : 같은 판매점에서 작성된 매물, 시공권한 user에게 제출받은 매물 조회
|
||||
* @description Builder : 같은 시공점에서 작성된 매물 조회
|
||||
* @description Partner : 같은 시공점에서 작성된 매물 조회
|
||||
*/
|
||||
private createRoleCondition(): WhereCondition {
|
||||
const where: WhereCondition = { AND: [] }
|
||||
|
||||
switch (this.session?.role) {
|
||||
case 'Admin':
|
||||
where.OR = [
|
||||
{ AND: [{ STORE_ID: { equals: this.session?.storeId } }] },
|
||||
{ AND: [{ SUBMISSION_TARGET_ID: { equals: this.session?.storeId } }, { SUBMISSION_STATUS: { equals: true } }] },
|
||||
]
|
||||
break
|
||||
case 'Admin_Sub':
|
||||
where.OR = [
|
||||
{
|
||||
AND: [{ STORE_ID: { equals: this.session?.storeId } }, { CONSTRUCTION_POINT_ID: { equals: this.session?.builderId } }],
|
||||
},
|
||||
{
|
||||
AND: [
|
||||
{ SUBMISSION_TARGET_ID: { equals: this.session?.storeId } },
|
||||
{ CONSTRUCTION_POINT_ID: { not: null } },
|
||||
{ CONSTRUCTION_POINT_ID: { not: '' } },
|
||||
{ SUBMISSION_STATUS: { equals: true } },
|
||||
],
|
||||
},
|
||||
]
|
||||
break
|
||||
case 'Builder':
|
||||
case 'Partner':
|
||||
where.AND.push({ CONSTRUCTION_POINT_ID: { equals: this.session?.builderId } })
|
||||
break
|
||||
case 'T01':
|
||||
where.OR = [{ NOT: { SRL_NO: { startsWith: '一時保存' } } }, { STORE_ID: { equals: this.session?.storeId } }]
|
||||
break
|
||||
}
|
||||
return where
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 검색 조건 생성
|
||||
* @returns {WhereCondition} 조사 매물 검색 조건
|
||||
*/
|
||||
createFilterSurvey(): WhereCondition {
|
||||
const where: WhereCondition = { AND: [] }
|
||||
|
||||
/** 내가 작성한 매물 조건 */
|
||||
const mySurveyCondition = this.createMySurveyCondition()
|
||||
if (mySurveyCondition.AND.length > 0) {
|
||||
where.AND.push(mySurveyCondition)
|
||||
}
|
||||
|
||||
/** 키워드 검색 조건 */
|
||||
const keywordCondition = this.createKeywordCondition()
|
||||
if (Object.keys(keywordCondition).length > 0) {
|
||||
where.AND.push(keywordCondition)
|
||||
}
|
||||
|
||||
/** 역할 기반 조건 */
|
||||
const roleCondition = this.createRoleCondition()
|
||||
if (Object.keys(roleCondition).length > 0) {
|
||||
where.AND.push(roleCondition)
|
||||
}
|
||||
return where
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 검색
|
||||
* @param {WhereCondition} where 조사 매물 검색 조건
|
||||
* @returns {Promise<{ data: SurveyBasicInfo[], count: number }>} 조사 매물 데이터
|
||||
*/
|
||||
async getSurveySales(): Promise<{ data: SurveyBasicInfo[]; count: number } | ApiError> {
|
||||
const sessionCheckResult = this.checkSession()
|
||||
if (sessionCheckResult) {
|
||||
return sessionCheckResult
|
||||
}
|
||||
|
||||
const where = this.createFilterSurvey()
|
||||
/** 조사 매물 조회 */
|
||||
//@ts-ignore
|
||||
const surveys = await prisma.SD_SURVEY_SALES_BASIC_INFO.findMany({
|
||||
where,
|
||||
orderBy: this.params.sort === 'created' ? { REG_DT: 'desc' } : { UPT_DT: 'desc' },
|
||||
skip: Number(this.params.offset),
|
||||
take: ITEMS_PER_PAGE,
|
||||
})
|
||||
|
||||
/** 조사 매물 개수 조회 */
|
||||
//@ts-ignore
|
||||
const count = await prisma.SD_SURVEY_SALES_BASIC_INFO.count({ where })
|
||||
return { data: surveys as SurveyBasicInfo[], count }
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 생성
|
||||
* @param {SurveyRegistRequest} survey 조사 매물 데이터
|
||||
* @param {string} role 권한
|
||||
* @param {string} storeId 판매점 ID
|
||||
* @returns {Promise<SurveyBasicInfo>} 생성된 조사 매물 데이터
|
||||
*/
|
||||
async createSurvey(survey: SurveyRegistRequest, role: string, storeId: string) {
|
||||
const { detailInfo, ...basicInfo } = survey
|
||||
const newSrlNo = survey.srlNo ?? (await this.getNewSrlNo(storeId, role))
|
||||
// @ts-ignore
|
||||
return await prisma.SD_SURVEY_SALES_BASIC_INFO.create({
|
||||
data: {
|
||||
...convertToSnakeCase(basicInfo),
|
||||
SRL_NO: newSrlNo,
|
||||
DETAIL_INFO: {
|
||||
create: convertToSnakeCase(detailInfo),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 새로운 srlNo 생성 함수
|
||||
* @param {string} role 세션에 저장된 권한
|
||||
* @param {string} tempSrlNo 임시 srlNo (임시저장 시 사용)
|
||||
* @param {string} storeId 세션에 저장된 판매점 ID
|
||||
* @returns {Promise<string>} 새로운 srlNo
|
||||
*
|
||||
* @exampleResult HO250617001 (HO + 250617 + 001)
|
||||
*/
|
||||
async getNewSrlNo(storeId: string, role: string) {
|
||||
const srlRole = role === 'T01' || role === 'Admin' ? 'HO' : role === 'Admin_Sub' || role === 'Builder' ? 'HM' : ''
|
||||
|
||||
//@ts-ignore
|
||||
const index = await prisma.SD_SURVEY_SALES_BASIC_INFO.count({
|
||||
where: {
|
||||
SRL_NO: {
|
||||
startsWith: srlRole + storeId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return (
|
||||
srlRole +
|
||||
storeId +
|
||||
new Date().getFullYear().toString().slice(-2) +
|
||||
(new Date().getMonth() + 1).toString().padStart(2, '0') +
|
||||
new Date().getDate().toString().padStart(2, '0') +
|
||||
(index + 1).toString().padStart(3, '0')
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 상세 조회
|
||||
* @param {number} id 조사 매물 ID
|
||||
* @returns {Promise<SurveyBasicInfo>} 조사 매물 데이터
|
||||
*/
|
||||
async fetchSurvey(id: number, isPdf: boolean): Promise<SurveyBasicInfo | ApiError> {
|
||||
// @ts-ignore
|
||||
const result = await prisma.SD_SURVEY_SALES_BASIC_INFO.findFirst({
|
||||
where: { ID: id },
|
||||
include: { DETAIL_INFO: true },
|
||||
})
|
||||
if (!result) {
|
||||
return new ApiError(HttpStatusCode.NotFound, ERROR_MESSAGE.NOT_FOUND)
|
||||
}
|
||||
if (!isPdf) {
|
||||
if (!this.session?.isLoggedIn) return new ApiError(HttpStatusCode.Unauthorized, ERROR_MESSAGE.UNAUTHORIZED)
|
||||
if (!this.checkRole(result, this.session as SessionData)) return new ApiError(HttpStatusCode.Forbidden, ERROR_MESSAGE.FORBIDDEN)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 수정
|
||||
* @param {number} id 조사 매물 ID
|
||||
* @param {SurveyRegistRequest} survey 조사 매물 데이터
|
||||
* @param {boolean} isTemporary 임시 저장 여부
|
||||
* @param {string} storeId 판매점 ID
|
||||
* @param {string} role 권한
|
||||
* @returns {Promise<SurveyBasicInfo>} 수정된 조사 매물 데이터
|
||||
*/
|
||||
async updateSurvey(id: number, survey: SurveyRegistRequest, isTemporary: boolean, storeId: string, role: string) {
|
||||
const { detailInfo, ...basicInfo } = survey
|
||||
const newSrlNo = isTemporary ? survey.srlNo ?? '一時保存' : await this.getNewSrlNo(storeId, role)
|
||||
|
||||
// @ts-ignore
|
||||
return (await prisma.SD_SURVEY_SALES_BASIC_INFO.update({
|
||||
where: { ID: Number(id) },
|
||||
data: {
|
||||
...convertToSnakeCase(basicInfo),
|
||||
SRL_NO: newSrlNo,
|
||||
UPT_DT: new Date(),
|
||||
DETAIL_INFO: {
|
||||
update: convertToSnakeCase(detailInfo),
|
||||
},
|
||||
},
|
||||
include: {
|
||||
DETAIL_INFO: true,
|
||||
},
|
||||
})) as SurveyBasicInfo
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 삭제
|
||||
* @param {number} id 조사 매물 ID
|
||||
*/
|
||||
async deleteSurvey(id: number) {
|
||||
await prisma.$transaction(async (tx: Prisma.TransactionClient) => {
|
||||
// @ts-ignore
|
||||
const detailData = await tx.SD_SURVEY_SALES_BASIC_INFO.findUnique({
|
||||
where: { ID: Number(id) },
|
||||
select: {
|
||||
DETAIL_INFO: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (detailData?.DETAIL_INFO?.ID) {
|
||||
// @ts-ignore
|
||||
await tx.SD_SURVEY_SALES_DETAIL_INFO.delete({
|
||||
where: { ID: Number(detailData.DETAIL_INFO.ID) },
|
||||
})
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
await tx.SD_SURVEY_SALES_BASIC_INFO.delete({
|
||||
where: { ID: Number(id) },
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 제출
|
||||
* @param {number} id 조사 매물 ID
|
||||
* @param {string} targetId 제출 대상 ID
|
||||
* @param {string} targetNm 제출 대상 이름
|
||||
* @returns {Promise<SurveyBasicInfo>} 제출된 조사 매물 데이터
|
||||
*/
|
||||
async submitSurvey(id: number, targetId: string, targetNm: string) {
|
||||
// @ts-ignore
|
||||
return (await prisma.SD_SURVEY_SALES_BASIC_INFO.update({
|
||||
where: { ID: Number(id) },
|
||||
data: {
|
||||
SUBMISSION_STATUS: true,
|
||||
SUBMISSION_DATE: new Date(),
|
||||
SUBMISSION_TARGET_ID: targetId,
|
||||
SUBMISSION_TARGET_NM: targetNm,
|
||||
UPT_DT: new Date(),
|
||||
},
|
||||
})) as SurveyBasicInfo
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 권한 체크
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @param {SessionData} session 세션 데이터
|
||||
* @returns {boolean} 해당 매물의 조회 권한 여부 (true: 권한 있음, false: 권한 없음)
|
||||
*/
|
||||
checkRole(survey: any, session: SessionData): boolean {
|
||||
if (!survey || !session) return false
|
||||
|
||||
const roleChecks = {
|
||||
T01: () => this.checkT01Role(survey),
|
||||
Admin: () => this.checkAdminRole(survey, session.storeId),
|
||||
Admin_Sub: () => this.checkAdminSubRole(survey, session.storeId),
|
||||
Partner: () => this.checkPartnerOrBuilderRole(survey, session.builderId),
|
||||
Builder: () => this.checkPartnerOrBuilderRole(survey, session.builderId),
|
||||
}
|
||||
|
||||
return roleChecks[session.role as keyof typeof roleChecks]?.() ?? false
|
||||
}
|
||||
|
||||
/**
|
||||
* @description T01 권한 체크
|
||||
* - 임시저장 매물을 제외한 전 매물 조회 가능
|
||||
*
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @returns {boolean} 해당 매물의 조회 권한 여부 (true: 권한 있음, false: 권한 없음)
|
||||
*/
|
||||
private checkT01Role(survey: any): boolean {
|
||||
return survey.SRL_NO !== '一時保存'
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Admin 권한 체크 (1차점 - Order)
|
||||
* - 같은 판매점에서 작성한 매물, 제출 받은 매물 조회 가능
|
||||
*
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @param {string | null} storeId 판매점 ID
|
||||
* @returns {boolean} 해당 매물의 조회 권한 여부 (true: 권한 있음, false: 권한 없음)
|
||||
*/
|
||||
private checkAdminRole(survey: any, storeId: string | null): boolean {
|
||||
if (!storeId) return false
|
||||
return survey.SUBMISSION_STATUS ? survey.SUBMISSION_TARGET_ID === storeId || survey.STORE_ID === storeId : survey.STORE_ID === storeId
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Admin_Sub 권한 체크 (2차점 - Musubi)
|
||||
* - 같은 판매점에서 작성한 매물, 시공권한 user에게 제출받은 매물 조회 가능
|
||||
*
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @param {string | null} storeId 판매점 ID
|
||||
* @returns {boolean} 해당 매물의 조회 권한 여부 (true: 권한 있음, false: 권한 없음)
|
||||
*/
|
||||
private checkAdminSubRole(survey: any, storeId: string | null): boolean {
|
||||
if (!storeId) return false
|
||||
return survey.SUBMISSION_STATUS
|
||||
? survey.SUBMISSION_TARGET_ID === storeId || (survey.STORE_ID === storeId && !survey.CONSTRUCTION_POINT_ID)
|
||||
: survey.STORE_ID === storeId && !survey.CONSTRUCTION_POINT_ID
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Partner 또는 Builder 권한 체크
|
||||
* - 같은 시공점에서 작성한 매물 조회 가능
|
||||
*
|
||||
* @param {any} survey 조사 매물 데이터
|
||||
* @param {string | null} builderId 시공점 ID
|
||||
* @returns {boolean} 해당 매물의 조회 권한 여부 (true: 권한 있음, false: 권한 없음)
|
||||
*/
|
||||
private checkPartnerOrBuilderRole(survey: any, builderId: string | null): boolean {
|
||||
if (!builderId) return false
|
||||
return survey.CONSTRUCTION_POINT_ID === builderId
|
||||
}
|
||||
|
||||
/**
|
||||
* @description API ROUTE 에러 처리
|
||||
* @param {any} error 에러 객체
|
||||
* @returns {ApiError} 에러 객체
|
||||
*/
|
||||
handleRouteError(error: any): ApiError {
|
||||
console.error('❌ API ROUTE ERROR : ', error)
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientInitializationError ||
|
||||
error instanceof Prisma.PrismaClientUnknownRequestError ||
|
||||
error instanceof Prisma.PrismaClientRustPanicError ||
|
||||
error instanceof Prisma.PrismaClientValidationError ||
|
||||
error instanceof Prisma.PrismaClientKnownRequestError
|
||||
) {
|
||||
return new ApiError(HttpStatusCode.InternalServerError, ERROR_MESSAGE.PRISMA_ERROR)
|
||||
}
|
||||
return new ApiError(error.statusCode ?? HttpStatusCode.InternalServerError, error.message ?? ERROR_MESSAGE.FETCH_ERROR)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 비동기 함수 try-catch 처리 함수
|
||||
* @param {() => Promise<any>} func 비동기 함수
|
||||
* @returns {Promise<ApiError | any>} 에러 객체 또는 함수 결과
|
||||
*/
|
||||
async tryFunction(func: () => Promise<any>): Promise<ApiError | any> {
|
||||
try {
|
||||
return await func()
|
||||
} catch (error) {
|
||||
return this.handleRouteError(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,8 +9,7 @@ export default function Detail() {
|
||||
const params = useParams()
|
||||
const id = params.id
|
||||
|
||||
const { inquiryDetail, downloadFile } = useInquiry(Number(id), '5200')
|
||||
const { commonCodeList } = useInquiry()
|
||||
const { inquiryDetail, downloadFile, commonCodeList } = useInquiry(Number(id), false)
|
||||
const router = useRouter()
|
||||
|
||||
const { session } = useSessionStore()
|
||||
|
||||
@ -3,11 +3,13 @@
|
||||
import { useInquiry } from '@/hooks/useInquiry'
|
||||
import { useSessionStore } from '@/store/session'
|
||||
import { InquiryRequest } from '@/types/Inquiry'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { CONFIRM_MESSAGE, SUCCESS_MESSAGE, useAlertMsg, WARNING_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
|
||||
export default function RegistForm() {
|
||||
const { saveInquiry, isSavingInquiry } = useInquiry()
|
||||
const { saveInquiry, isSavingInquiry, commonCodeList } = useInquiry(undefined, false)
|
||||
const { showErrorAlert, showSuccessAlert, showConfirm } = useAlertMsg()
|
||||
const { session } = useSessionStore()
|
||||
const router = useRouter()
|
||||
|
||||
@ -46,8 +48,6 @@ export default function RegistForm() {
|
||||
}
|
||||
}, [session])
|
||||
|
||||
const { commonCodeList } = useInquiry()
|
||||
|
||||
const [attachedFiles, setAttachedFiles] = useState<File[]>([])
|
||||
|
||||
/** 파일 첨부 처리 */
|
||||
@ -74,23 +74,23 @@ export default function RegistForm() {
|
||||
const handleSubmit = async () => {
|
||||
const emptyField = requiredFieldNames.find((field) => inquiryRequest[field.id as keyof InquiryRequest] === '')
|
||||
if (emptyField) {
|
||||
alert(`${emptyField?.name}を入力してください。`)
|
||||
showErrorAlert(WARNING_MESSAGE.REQUIRED_FIELD_IS_EMPTY, emptyField?.name)
|
||||
focusOnRequiredField(emptyField?.id ?? '')
|
||||
return
|
||||
}
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
if (!emailRegex.test(inquiryRequest.qstMail)) {
|
||||
alert('有効なメールアドレスを入力してください。')
|
||||
showErrorAlert(WARNING_MESSAGE.EMAIL_PREFIX_IS_INVALID)
|
||||
focusOnRequiredField('qstMail')
|
||||
return
|
||||
}
|
||||
if (inquiryRequest.title.length > 100) {
|
||||
alert('お問い合わせタイトルは100文字以内で入力してください。')
|
||||
showErrorAlert(WARNING_MESSAGE.TITLE_MAX_LENGTH)
|
||||
focusOnRequiredField('title')
|
||||
return
|
||||
}
|
||||
if (inquiryRequest.contents.length > 2000) {
|
||||
alert('お問い合わせ内容は2,000文字以内で入力してください。')
|
||||
showErrorAlert(WARNING_MESSAGE.CONTENTS_MAX_LENGTH)
|
||||
focusOnRequiredField('contents')
|
||||
return
|
||||
}
|
||||
@ -102,11 +102,11 @@ export default function RegistForm() {
|
||||
Object.entries(inquiryRequest).forEach(([key, value]) => {
|
||||
formData.append(key, value ?? '')
|
||||
})
|
||||
window.neoConfirm(
|
||||
'お問い合わせを登録しますか? Hanwha Japanの担当者にお問い合わせメールが送信されます。',
|
||||
showConfirm(
|
||||
CONFIRM_MESSAGE.SAVE_INQUIRY_CONFIRM,
|
||||
async () => {
|
||||
const res = await saveInquiry(formData)
|
||||
alert('保存されました。')
|
||||
showSuccessAlert(SUCCESS_MESSAGE.SAVE_SUCCESS)
|
||||
router.push(`/inquiry/${res.qnaNo}`)
|
||||
},
|
||||
() => null,
|
||||
|
||||
@ -2,10 +2,12 @@
|
||||
import { useInquiryFilterStore } from '@/store/inquiryFilterStore'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { useState } from 'react'
|
||||
import { useAlertMsg, WARNING_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
|
||||
export default function ListForm() {
|
||||
const router = useRouter()
|
||||
const { inquiryListRequest, setInquiryListRequest, reset, setOffset } = useInquiryFilterStore()
|
||||
const { showErrorAlert } = useAlertMsg()
|
||||
const [searchKeyword, setSearchKeyword] = useState(inquiryListRequest.schTitle ?? '')
|
||||
|
||||
const handleSearch = () => {
|
||||
@ -13,7 +15,7 @@ export default function ListForm() {
|
||||
reset()
|
||||
setInquiryListRequest({ ...inquiryListRequest, schTitle: searchKeyword })
|
||||
} else {
|
||||
alert('2文字以上入力してください')
|
||||
showErrorAlert(WARNING_MESSAGE.KEYWORD_MINIMUM_LENGTH)
|
||||
}
|
||||
}
|
||||
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
|
||||
@ -25,7 +25,7 @@ export default function ListTable() {
|
||||
const router = useRouter()
|
||||
const pathname = usePathname()
|
||||
|
||||
const { inquiryList, isLoadingInquiryList } = useInquiry()
|
||||
const { inquiryList, isLoadingInquiryList } = useInquiry(undefined, true)
|
||||
const { inquiryListRequest, setInquiryListRequest, reset, offset, setOffset } = useInquiryFilterStore()
|
||||
|
||||
const [hasMore, setHasMore] = useState(false)
|
||||
|
||||
@ -7,6 +7,7 @@ import { useSurvey } from '@/hooks/useSurvey'
|
||||
import { radioEtcData, roofMaterial, selectBoxOptions, supplementaryFacilities } from '../survey-sale/detail/RoofForm'
|
||||
import { useSpinnerStore } from '@/store/spinnerStore'
|
||||
import { useSessionStore } from '@/store/session'
|
||||
import { ERROR_MESSAGE, SUCCESS_MESSAGE, useAlertMsg } from '@/hooks/useAlertMsg'
|
||||
|
||||
export default function SurveySaleDownloadPdf() {
|
||||
const params = useParams()
|
||||
@ -14,6 +15,7 @@ export default function SurveySaleDownloadPdf() {
|
||||
const router = useRouter()
|
||||
|
||||
const { surveyDetail, isLoadingSurveyDetail } = useSurvey(Number(id), true)
|
||||
const { showErrorAlert, showSuccessAlert } = useAlertMsg()
|
||||
const { setIsShow } = useSpinnerStore()
|
||||
const { session } = useSessionStore()
|
||||
|
||||
@ -23,11 +25,6 @@ export default function SurveySaleDownloadPdf() {
|
||||
/** 페이지 랜더링 이후 PDF 생성 */
|
||||
useEffect(() => {
|
||||
if (isLoadingSurveyDetail || isGeneratedRef.current) return
|
||||
if (surveyDetail === null) {
|
||||
alert('データが見つかりません。')
|
||||
router.replace('/')
|
||||
return
|
||||
}
|
||||
isGeneratedRef.current = true
|
||||
handleDownPdf()
|
||||
}, [surveyDetail?.id, isLoadingSurveyDetail])
|
||||
@ -65,10 +62,11 @@ export default function SurveySaleDownloadPdf() {
|
||||
} else {
|
||||
router.replace('/')
|
||||
}
|
||||
alert('PDFの生成が完了しました。 ポップアップウィンドウからダウンロードしてください。')
|
||||
showSuccessAlert(SUCCESS_MESSAGE.PDF_GENERATION_SUCCESS)
|
||||
})
|
||||
.catch((error: any) => {
|
||||
console.error('error', error)
|
||||
console.error('❌ PDF GENERATION ERROR', error)
|
||||
showErrorAlert(ERROR_MESSAGE.PDF_GENERATION_ERROR)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ import { useCommCode } from '@/hooks/useCommCode'
|
||||
import { CommCode } from '@/types/CommCode'
|
||||
import { sendEmail } from '@/libs/mailer'
|
||||
import { useSpinnerStore } from '@/store/spinnerStore'
|
||||
import { CONFIRM_MESSAGE, SUCCESS_MESSAGE, ERROR_MESSAGE, useAlertMsg, WARNING_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
|
||||
interface SubmitFormData {
|
||||
saleBase: string | null
|
||||
@ -35,6 +36,7 @@ export default function SurveySaleSubmitPopup() {
|
||||
const { setIsShow } = useSpinnerStore()
|
||||
const { getCommCode } = useCommCode()
|
||||
const { surveyDetail, getSubmitTarget } = useSurvey(Number(routeId))
|
||||
const { showErrorAlert, showSuccessAlert, showConfirm } = useAlertMsg()
|
||||
|
||||
const [submitData, setSubmitData] = useState<SubmitFormData>({
|
||||
saleBase: null,
|
||||
@ -109,7 +111,7 @@ export default function SurveySaleSubmitPopup() {
|
||||
|
||||
for (const field of requiredFields) {
|
||||
if (data[field.id] === '' || data[field.id] === null || data[field.id]?.length === 0) {
|
||||
alert(`${field.name}は必須入力項目です。`)
|
||||
showErrorAlert(WARNING_MESSAGE.REQUIRED_FIELD_IS_EMPTY, field.name)
|
||||
const element = document.getElementById(field.id)
|
||||
if (element) {
|
||||
element.focus()
|
||||
@ -123,7 +125,7 @@ export default function SurveySaleSubmitPopup() {
|
||||
/** 제출 처리 - 데이터 검증 이후 메일 전송 완료되면 데이터 저장 */
|
||||
const handleSubmit = () => {
|
||||
if (validateData(submitData)) {
|
||||
window.neoConfirm('送信しますか? 送信後は変更・修正することはできません。', () => {
|
||||
showConfirm(CONFIRM_MESSAGE.SUBMIT_CONFIRM, () => {
|
||||
setIsShow(true)
|
||||
sendEmail({
|
||||
to: submitData.receiver,
|
||||
@ -133,14 +135,14 @@ export default function SurveySaleSubmitPopup() {
|
||||
})
|
||||
.then(() => {
|
||||
if (!isSubmittingSurvey) {
|
||||
alert('提出が完了しました。')
|
||||
showSuccessAlert(SUCCESS_MESSAGE.SUBMIT_SUCCESS)
|
||||
submitSurvey({ targetId: submitData.targetId, targetNm: submitData.targetNm })
|
||||
popupController.setSurveySaleSubmitPopup(false)
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error sending email:', error)
|
||||
alert('メール送信に失敗しました。 再度送信してください。')
|
||||
console.error('❌ SEND EMAIL ERROR : ', error)
|
||||
showErrorAlert(ERROR_MESSAGE.EMAIL_SEND_ERROR)
|
||||
})
|
||||
.finally(() => {
|
||||
setIsShow(false)
|
||||
|
||||
@ -6,6 +6,7 @@ import { useEffect, useState } from 'react'
|
||||
import { useParams, useRouter } from 'next/navigation'
|
||||
import { requiredFields, useSurvey } from '@/hooks/useSurvey'
|
||||
import { usePopupController } from '@/store/popupController'
|
||||
import { CONFIRM_MESSAGE, SUCCESS_MESSAGE, useAlertMsg, WARNING_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
|
||||
interface ButtonFormProps {
|
||||
mode: Mode
|
||||
@ -49,6 +50,7 @@ export default function ButtonForm({ mode, setMode, data }: ButtonFormProps) {
|
||||
|
||||
const { deleteSurvey, updateSurvey, isDeletingSurvey, isUpdatingSurvey } = useSurvey(id)
|
||||
const { validateSurveyDetail, createSurvey, isCreatingSurvey } = useSurvey()
|
||||
const { showErrorAlert, showSuccessAlert, showConfirm } = useAlertMsg()
|
||||
|
||||
useEffect(() => {
|
||||
if (!session?.isLoggedIn) return
|
||||
@ -116,7 +118,7 @@ export default function ButtonForm({ mode, setMode, data }: ButtonFormProps) {
|
||||
router.push(`/survey-sale/${savedId}`)
|
||||
}
|
||||
}
|
||||
alert('一時保存されました。')
|
||||
showSuccessAlert(SUCCESS_MESSAGE.TEMP_SAVE_SUCCESS)
|
||||
}
|
||||
|
||||
/** 입력 필드 포커스 처리 */
|
||||
@ -128,7 +130,13 @@ export default function ButtonForm({ mode, setMode, data }: ButtonFormProps) {
|
||||
/** 저장 로직 */
|
||||
const saveProcess = async (emptyField: string | null, isSubmitProcess?: boolean) => {
|
||||
if (emptyField?.trim() === '') {
|
||||
await handleSuccessfulSave(isSubmitProcess)
|
||||
if (!isSubmitProcess) {
|
||||
showConfirm(CONFIRM_MESSAGE.SAVE_CONFIRM, async () => {
|
||||
await handleSuccessfulSave(isSubmitProcess)
|
||||
})
|
||||
} else {
|
||||
await handleSuccessfulSave(isSubmitProcess)
|
||||
}
|
||||
} else {
|
||||
handleFailedSave(emptyField)
|
||||
}
|
||||
@ -147,6 +155,8 @@ export default function ButtonForm({ mode, setMode, data }: ButtonFormProps) {
|
||||
setMode('READ')
|
||||
if (isSubmitProcess) {
|
||||
popupController.setSurveySaleSubmitPopup(true)
|
||||
} else {
|
||||
showSuccessAlert(SUCCESS_MESSAGE.SAVE_SUCCESS)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -156,7 +166,7 @@ export default function ButtonForm({ mode, setMode, data }: ButtonFormProps) {
|
||||
await router.push(`/survey-sale/${savedId}?show=true`)
|
||||
} else {
|
||||
await router.push(`/survey-sale/${savedId}`)
|
||||
alert('保存されました。')
|
||||
showSuccessAlert(SUCCESS_MESSAGE.SAVE_SUCCESS)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -164,9 +174,10 @@ export default function ButtonForm({ mode, setMode, data }: ButtonFormProps) {
|
||||
/** 필수값 미입력 처리 */
|
||||
const handleFailedSave = (emptyField: string | null) => {
|
||||
if (emptyField?.includes('Unit')) {
|
||||
alert('電気契約容量の単位を入力してください。')
|
||||
showErrorAlert(WARNING_MESSAGE.REQUIRED_UNIT_IS_EMPTY)
|
||||
} else {
|
||||
alert(requiredFields.find((field) => field.field === emptyField)?.name + ' 項目が空です。')
|
||||
const fieldInfo = requiredFields.find((field) => field.field === emptyField)
|
||||
showErrorAlert(WARNING_MESSAGE.REQUIRED_FIELD_IS_EMPTY, fieldInfo?.name || '')
|
||||
}
|
||||
focusInput(emptyField as keyof SurveyDetailInfo)
|
||||
}
|
||||
@ -174,10 +185,10 @@ export default function ButtonForm({ mode, setMode, data }: ButtonFormProps) {
|
||||
/** 삭제 로직 */
|
||||
const handleDelete = async () => {
|
||||
if (!Number.isNaN(id)) {
|
||||
window.neoConfirm('削除しますか?', async () => {
|
||||
showConfirm(CONFIRM_MESSAGE.DELETE_CONFIRM, async () => {
|
||||
await deleteSurvey()
|
||||
if (!isDeletingSurvey) {
|
||||
alert('削除されました。')
|
||||
showSuccessAlert(SUCCESS_MESSAGE.DELETE_SUCCESS)
|
||||
router.push('/survey-sale')
|
||||
}
|
||||
})
|
||||
@ -187,16 +198,16 @@ export default function ButtonForm({ mode, setMode, data }: ButtonFormProps) {
|
||||
/** 제출 로직 */
|
||||
const handleSubmit = async () => {
|
||||
if (data.basic.srlNo?.startsWith('一時保存') && Number.isNaN(id)) {
|
||||
alert('一時保存されたデータは提出できません。')
|
||||
showErrorAlert(WARNING_MESSAGE.TEMP_CANNOT_SUBMIT)
|
||||
return
|
||||
}
|
||||
|
||||
if (mode === 'READ') {
|
||||
window.neoConfirm('提出しますか?', async () => {
|
||||
showConfirm(CONFIRM_MESSAGE.SUBMIT_CONFIRM, async () => {
|
||||
popupController.setSurveySaleSubmitPopup(true)
|
||||
})
|
||||
} else {
|
||||
window.neoConfirm('記入した情報を保存して送信しますか?', async () => {
|
||||
showConfirm(CONFIRM_MESSAGE.SAVE_AND_SUBMIT_CONFIRM, async () => {
|
||||
handleSave(false, true)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { useState } from 'react'
|
||||
import type { Mode, SurveyDetailInfo, SurveyDetailRequest } from '@/types/Survey'
|
||||
import { useAlertMsg, WARNING_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
|
||||
type RadioEtcKeys =
|
||||
| 'structureOrder'
|
||||
@ -247,6 +248,7 @@ export default function RoofForm(props: {
|
||||
mode: Mode
|
||||
}) {
|
||||
const { roofInfo, setRoofInfo, mode } = props
|
||||
const { showErrorAlert } = useAlertMsg()
|
||||
const [isFlip, setIsFlip] = useState<boolean>(true)
|
||||
|
||||
const handleNumberInput = (key: keyof SurveyDetailRequest, value: number | string) => {
|
||||
@ -254,13 +256,13 @@ export default function RoofForm(props: {
|
||||
if (key === 'roofSlope' || key === 'openFieldPlateThickness') {
|
||||
const stringValue = value.toString()
|
||||
if (stringValue.length > 5) {
|
||||
alert('保存できるサイズを超えました。')
|
||||
showErrorAlert(WARNING_MESSAGE.SAVE_SIZE_OVERFLOW)
|
||||
return
|
||||
}
|
||||
if (stringValue.includes('.')) {
|
||||
const decimalPlaces = stringValue.split('.')[1].length
|
||||
if (decimalPlaces > 1) {
|
||||
alert('小数点以下1桁までしか許されません。')
|
||||
showErrorAlert(WARNING_MESSAGE.DECIMAL_POINT_CANNOT_EXCEED)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -732,6 +734,7 @@ const MultiCheck = ({
|
||||
roofInfo: SurveyDetailInfo
|
||||
setRoofInfo: (roofInfo: SurveyDetailRequest) => void
|
||||
}) => {
|
||||
const { showErrorAlert } = useAlertMsg()
|
||||
const multiCheckData = column === 'supplementaryFacilities' ? supplementaryFacilities : roofMaterial
|
||||
const etcValue = roofInfo?.[`${column}Etc` as keyof SurveyDetailInfo]
|
||||
const [isOtherCheck, setIsOtherCheck] = useState<boolean>(Boolean(etcValue))
|
||||
@ -751,7 +754,7 @@ const MultiCheck = ({
|
||||
if (isRoofMaterial) {
|
||||
const totalSelected = selectedValues.length + (isOtherSelected || isOtherCheck ? 1 : 0)
|
||||
if (totalSelected >= 2) {
|
||||
alert('屋根材は最大2個まで選択できます。')
|
||||
showErrorAlert(WARNING_MESSAGE.ROOF_MATERIAL_MAX_SELECT)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -765,7 +768,7 @@ const MultiCheck = ({
|
||||
if (isRoofMaterial) {
|
||||
const currentSelected = selectedValues.length
|
||||
if (!isOtherCheck && currentSelected >= 2) {
|
||||
alert('屋根材は最大2個まで選択できます。')
|
||||
showErrorAlert(WARNING_MESSAGE.ROOF_MATERIAL_MAX_SELECT)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,16 +3,18 @@
|
||||
import { SEARCH_OPTIONS, SEARCH_OPTIONS_ENUM, SEARCH_OPTIONS_PARTNERS, useSurveyFilterStore } from '@/store/surveyFilterStore'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { useState } from 'react'
|
||||
import { useAlertMsg, WARNING_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
|
||||
export default function SearchForm({ memberRole, userId }: { memberRole: string; userId: string }) {
|
||||
const router = useRouter()
|
||||
const { setSearchOption, setSort, setIsMySurvey, setKeyword, reset, isMySurvey, keyword, searchOption, sort, setOffset } = useSurveyFilterStore()
|
||||
const { showErrorAlert } = useAlertMsg()
|
||||
const { setSearchOption, setSort, setIsMySurvey, setKeyword, isMySurvey, keyword, searchOption, sort, setOffset } = useSurveyFilterStore()
|
||||
const [searchKeyword, setSearchKeyword] = useState(keyword)
|
||||
const [option, setOption] = useState(searchOption)
|
||||
|
||||
const handleSearch = () => {
|
||||
if (option !== 'id' && searchKeyword.trim().length < 2) {
|
||||
alert('2文字以上入力してください')
|
||||
showErrorAlert(WARNING_MESSAGE.KEYWORD_MINIMUM_LENGTH)
|
||||
return
|
||||
}
|
||||
setOffset(0)
|
||||
@ -62,7 +64,7 @@ export default function SearchForm({ memberRole, userId }: { memberRole: string;
|
||||
placeholder="タイトルを入力してください. (2文字以上)"
|
||||
onChange={(e) => {
|
||||
if (e.target.value.length > 30) {
|
||||
alert('30文字以内で入力してください')
|
||||
showErrorAlert(WARNING_MESSAGE.KEYWORD_MAX_LENGTH)
|
||||
return
|
||||
}
|
||||
setSearchKeyword(e.target.value)
|
||||
|
||||
147
src/hooks/useAlertMsg.ts
Normal file
147
src/hooks/useAlertMsg.ts
Normal file
@ -0,0 +1,147 @@
|
||||
/**
|
||||
* @description 성공 메세지 상수 객체
|
||||
*/
|
||||
export const SUCCESS_MESSAGE = {
|
||||
/** 저장 성공 - "저장되었습니다." */
|
||||
SAVE_SUCCESS: '保存されました。',
|
||||
|
||||
/** 임시 저장 성공 - "임시 저장되었습니다." */
|
||||
TEMP_SAVE_SUCCESS: '一時保存されました。',
|
||||
|
||||
/** 삭제 성공 - "삭제되었습니다." */
|
||||
DELETE_SUCCESS: '削除されました。',
|
||||
|
||||
/** 제출 성공 - "제출이 완료되었습니다." */
|
||||
SUBMIT_SUCCESS: '提出が完了しました。',
|
||||
|
||||
/** 문의 제목 최대값 메세지 - "문의 제목은 100자 이내로 입력하세요." */
|
||||
INQUIRY_TITLE_MAX_LENGTH: 'お問い合わせタイトルは100文字以内で入力してください。',
|
||||
|
||||
/** 문의 내용 최대값 메세지 - "문의 내용은 2,000자 이내로 입력하세요." */
|
||||
INQUIRY_CONTENTS_MAX_LENGTH: 'お問い合わせ内容は2,000文字以内で入力してください。',
|
||||
|
||||
/** PDF 생성 성공 - "PDF 생성이 완료되었습니다.\n팝업 창에서 다운로드 하세요." */
|
||||
PDF_GENERATION_SUCCESS: 'PDFの生成が完了しました。\nポップアップウィンドウからダウンロードしてください。',
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 확인 메세지 상수 객체
|
||||
*/
|
||||
export const CONFIRM_MESSAGE = {
|
||||
/** 제출 확인 - "제출하시겠습니까?" */
|
||||
SUBMIT_CONFIRM: '提出しますか?',
|
||||
|
||||
/** 저장 확인 - "저장하시겠습니까?" */
|
||||
SAVE_CONFIRM: '保存しますか?',
|
||||
|
||||
/** 삭제 확인 - "삭제하시겠습니까?" */
|
||||
DELETE_CONFIRM: '削除しますか?',
|
||||
|
||||
/** 저장 및 제출 확인 - "입력한 정보를 저장하고 보내시겠습니까?" */
|
||||
SAVE_AND_SUBMIT_CONFIRM: '記入した情報を保存して送信しますか?',
|
||||
|
||||
/** 문의 저장 확인 메세지 - "문의를 등록 하시겠습니까? 한화재팬 담당자에게 문의 메일이 발송됩니다." */
|
||||
SAVE_INQUIRY_CONFIRM: 'お問い合わせを登録しますか? Hanwha Japanの担当者にお問い合わせメールが送信されます。',
|
||||
}
|
||||
|
||||
export const WARNING_MESSAGE = {
|
||||
/** 입력 오류 경고 메세지 */
|
||||
/** 필수 입력 메세지 - "~ 항목이 비어 있습니다."*/
|
||||
REQUIRED_FIELD_IS_EMPTY: '項目が空です。',
|
||||
|
||||
/* 전기계약 용량 단위 입력 메세지 - "전기 계약 용량의 단위를 입력하세요."*/
|
||||
REQUIRED_UNIT_IS_EMPTY: '電気契約容量の単位を入力してください。',
|
||||
|
||||
/** 이메일 유효성 에러 - "유효한 이메일 주소를 입력해 주세요." */
|
||||
EMAIL_PREFIX_IS_INVALID: '有効なメールアドレスを入力してください。',
|
||||
|
||||
/** 최소값 오류 - "2자 이상 입력하세요" */
|
||||
KEYWORD_MINIMUM_LENGTH: '2文字以上入力してください',
|
||||
|
||||
/** 최대값 오류 - "30자 이내로 입력하세요" */
|
||||
KEYWORD_MAX_LENGTH: '30文字以内で入力してください',
|
||||
|
||||
/** 제목 최대값 오류 - "100자 이내로 입력하세요" */
|
||||
TITLE_MAX_LENGTH: '100文字以内で入力してください',
|
||||
|
||||
/** 내용 최대값 오류 - "2,000자 이내로 입력하세요" */
|
||||
CONTENTS_MAX_LENGTH: '2,000文字以内で入力してください',
|
||||
|
||||
/** 소수점 오류 - "소수점 이하 1자리까지만 허용됩니다." */
|
||||
DECIMAL_POINT_CANNOT_EXCEED: '小数点以下1桁までしか許されません。',
|
||||
|
||||
/** 숫자 최대값 오류 - "저장할 수 있는 크기를 초과했습니다." */
|
||||
SAVE_SIZE_OVERFLOW: '保存できるサイズを超えました。',
|
||||
|
||||
/** 최대 선택 오류 메세지 - "지붕재는 최대 2개까지 선택할 수 있습니다." */
|
||||
ROOF_MATERIAL_MAX_SELECT: '屋根材は最大2個まで選択できます。',
|
||||
|
||||
/** 임시 저장 제출 오류 - "임시 저장된 데이터는 제출할 수 없습니다." */
|
||||
TEMP_CANNOT_SUBMIT: '一時保存されたデータは提出できません。',
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 에러 메세지 상수 객체
|
||||
* - 입력 오류 경고 메세지
|
||||
* - 결과 실패 메세지
|
||||
* - 서버 에러 메세지
|
||||
*/
|
||||
export const ERROR_MESSAGE = {
|
||||
/** 결과 실패 메세지 */
|
||||
/** PDF 생성 오류 - "PDF 생성에 실패했습니다." */
|
||||
PDF_GENERATION_ERROR: 'PDF 生成に失敗しました。',
|
||||
|
||||
/** 이메일 전송 오류 - "이메일 전송에 실패했습니다. 다시 시도해주세요." */
|
||||
EMAIL_SEND_ERROR: 'メール送信に失敗しました。 再度送信してください。',
|
||||
|
||||
/** 서버 에러 메세지 */
|
||||
/** 데이터를 찾을 수 없습니다. - 404 */
|
||||
NOT_FOUND: 'データが見つかりません。',
|
||||
|
||||
/** 승인되지 않았습니다. - 401 */
|
||||
UNAUTHORIZED: '承認されていません。',
|
||||
|
||||
/** 권한이 없습니다. - 403 */
|
||||
FORBIDDEN: '権限がありません。',
|
||||
|
||||
/** 데이터의 조회에 실패했습니다. - 500 */
|
||||
FETCH_ERROR: 'データの取得に失敗しました。',
|
||||
|
||||
/** 서버 에러 - "일시적인 오류가 발생했습니다. 계속되는 경우 관리자에게 문의하시기 바랍니다." - 500 */
|
||||
SERVER_ERROR: '一時的なエラーが発生しました。 継続的な場合は、管理者に連絡してください。',
|
||||
|
||||
/** 잘못된 요청입니다. - 400 */
|
||||
BAD_REQUEST: '間違ったリクエストです。',
|
||||
|
||||
/** 데이터베이스 오류가 발생했습니다. - 500 */
|
||||
PRISMA_ERROR: 'データベース エラーが発生しました。',
|
||||
}
|
||||
|
||||
export function useAlertMsg(): {
|
||||
showErrorAlert: (message: string, requiredField?: string) => void
|
||||
showConfirm: (message: string, onConfirm: () => void, onCancel?: () => void) => void
|
||||
showSuccessAlert: (message: string) => void
|
||||
} {
|
||||
const showErrorAlert = (message: string, requiredField?: string) => {
|
||||
if (message.trim() === '' || message === null || message === undefined) return
|
||||
if (message.length > 100) {
|
||||
alert(ERROR_MESSAGE.SERVER_ERROR)
|
||||
return
|
||||
}
|
||||
requiredField ? alert(`${requiredField} ${message}`) : alert(message)
|
||||
}
|
||||
|
||||
const showConfirm = (message: string, onConfirm: () => void, onCancel?: () => void) => {
|
||||
window.neoConfirm(message, onConfirm, onCancel)
|
||||
}
|
||||
|
||||
const showSuccessAlert = (message: string) => {
|
||||
alert(message)
|
||||
}
|
||||
|
||||
return {
|
||||
showErrorAlert,
|
||||
showConfirm,
|
||||
showSuccessAlert,
|
||||
}
|
||||
}
|
||||
@ -3,8 +3,8 @@ import { useAxios } from '@/hooks/useAxios'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useInquiryFilterStore } from '@/store/inquiryFilterStore'
|
||||
import { useMemo } from 'react'
|
||||
import { useSessionStore } from '@/store/session'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { useAlertMsg } from '@/hooks/useAlertMsg'
|
||||
|
||||
/**
|
||||
* @description 문의사항 관련 기능을 제공하는 커스텀 훅
|
||||
@ -23,7 +23,7 @@ import { useRouter } from 'next/navigation'
|
||||
*/
|
||||
export function useInquiry(
|
||||
qnoNo?: number,
|
||||
compCd?: string,
|
||||
isList?: boolean,
|
||||
): {
|
||||
inquiryList: InquiryList[]
|
||||
isLoadingInquiryList: boolean
|
||||
@ -36,20 +36,24 @@ export function useInquiry(
|
||||
} {
|
||||
const queryClient = useQueryClient()
|
||||
const { inquiryListRequest, offset } = useInquiryFilterStore()
|
||||
const { session } = useSessionStore()
|
||||
const { axiosInstance } = useAxios()
|
||||
const router = useRouter()
|
||||
|
||||
const { showErrorAlert } = useAlertMsg()
|
||||
/**
|
||||
* @description API 에러 처리 및 라우팅
|
||||
*
|
||||
* @param {any} error 에러 객체
|
||||
* @returns {void} 라우팅 처리
|
||||
*/
|
||||
const errorRouter = (error: any) => {
|
||||
const handleError = (error: any, isThrow?: boolean) => {
|
||||
const status = error.response?.status
|
||||
if (error.response?.data.error) {
|
||||
alert(error.response?.data.error)
|
||||
const errorMsg = error.response?.data.error
|
||||
console.error('❌ AXIOS INSTANCE ERROR : ', error)
|
||||
if (errorMsg) {
|
||||
showErrorAlert(errorMsg)
|
||||
}
|
||||
if (isThrow) {
|
||||
throw new Error(error)
|
||||
}
|
||||
switch (status) {
|
||||
// session 없는 경우
|
||||
@ -73,6 +77,25 @@ export function useInquiry(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 비동기 함수 try-catch 처리 함수
|
||||
* @param {() => Promise<any>} func 비동기 함수
|
||||
* @param {boolean} isList 목록 조회 여부
|
||||
* @param {boolean} isThrow 에러 던지기 여부
|
||||
* @returns {Promise<any>} 함수 결과 또는 빈 데이터
|
||||
*/
|
||||
const tryFunction = async (func: () => Promise<any>, isList?: boolean, isThrow?: boolean): Promise<any> => {
|
||||
try {
|
||||
return await func()
|
||||
} catch (error) {
|
||||
handleError(error, isThrow)
|
||||
if (isList) {
|
||||
return { data: [], count: 0 }
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 문의사항 목록 조회
|
||||
*
|
||||
@ -83,17 +106,20 @@ export function useInquiry(
|
||||
const { data: inquiryList, isLoading: isLoadingInquiryList } = useQuery({
|
||||
queryKey: ['inquiryList', inquiryListRequest, offset],
|
||||
queryFn: async () => {
|
||||
try {
|
||||
const resp = await axiosInstance(null).get<{ data: InquiryList[] }>(`/api/qna/list`, {
|
||||
params: { inquiryListRequest, startRow: offset, endRow: offset + 9 },
|
||||
})
|
||||
return resp.data.data
|
||||
} catch (error: any) {
|
||||
errorRouter(error)
|
||||
return []
|
||||
}
|
||||
const isListQuery = true
|
||||
const shouldThrowError = false
|
||||
|
||||
const resp = await tryFunction(
|
||||
() =>
|
||||
axiosInstance(null).get<{ data: InquiryList[] }>(`/api/qna/list`, {
|
||||
params: { inquiryListRequest, startRow: offset, endRow: offset + 9 },
|
||||
}),
|
||||
isListQuery,
|
||||
shouldThrowError,
|
||||
)
|
||||
return resp.data
|
||||
},
|
||||
enabled: !!inquiryListRequest,
|
||||
enabled: isList,
|
||||
})
|
||||
|
||||
/**
|
||||
@ -119,19 +145,22 @@ export function useInquiry(
|
||||
* @returns {boolean} isLoading - 문의사항 상세 정보 로딩 상태
|
||||
*/
|
||||
const { data: inquiryDetail, isLoading: isLoadingInquiryDetail } = useQuery({
|
||||
queryKey: ['inquiryDetail', qnoNo, compCd, session?.userId],
|
||||
queryKey: ['inquiryDetail', qnoNo],
|
||||
queryFn: async () => {
|
||||
try {
|
||||
const resp = await axiosInstance(null).get<{ data: Inquiry }>(`/api/qna/detail`, {
|
||||
params: { qnoNo, compCd, langCd: 'JA', loginId: session?.userId ?? '' },
|
||||
})
|
||||
return resp.data.data
|
||||
} catch (error: any) {
|
||||
errorRouter(error)
|
||||
return null
|
||||
}
|
||||
const isListQuery = false
|
||||
const shouldThrowError = false
|
||||
|
||||
const resp = await tryFunction(
|
||||
() =>
|
||||
axiosInstance(null).get<{ data: Inquiry }>(`/api/qna/detail`, {
|
||||
params: { qnoNo, compCd: '5200', langCd: 'JA' },
|
||||
}),
|
||||
isListQuery,
|
||||
shouldThrowError,
|
||||
)
|
||||
return resp?.data ?? null
|
||||
},
|
||||
enabled: qnoNo !== undefined && compCd !== undefined,
|
||||
enabled: qnoNo !== undefined,
|
||||
})
|
||||
|
||||
/**
|
||||
@ -142,14 +171,21 @@ export function useInquiry(
|
||||
*/
|
||||
const { mutateAsync: saveInquiry, isPending: isSavingInquiry } = useMutation({
|
||||
mutationFn: async (formData: FormData) => {
|
||||
const resp = await axiosInstance(null).post<{ data: InquirySaveResponse }>('/api/qna/save', formData)
|
||||
return resp.data.data
|
||||
const isListQuery = false
|
||||
const shouldThrowError = true
|
||||
|
||||
const resp = await tryFunction(
|
||||
() => axiosInstance(null).post<{ data: InquirySaveResponse }>('/api/qna/save', formData),
|
||||
isListQuery,
|
||||
shouldThrowError,
|
||||
)
|
||||
return resp?.data ?? null
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['inquiryList'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
errorRouter(error)
|
||||
handleError(error, true)
|
||||
},
|
||||
})
|
||||
|
||||
@ -161,24 +197,21 @@ export function useInquiry(
|
||||
* @returns {Promise<Blob|null>} 다운로드된 파일 데이터 또는 null
|
||||
*/
|
||||
const downloadFile = async (encodeFileNo: number, srcFileNm: string) => {
|
||||
try {
|
||||
const resp = await fetch(`/api/qna/file?encodeFileNo=${encodeFileNo}&srcFileNm=${srcFileNm}`)
|
||||
const isListQuery = false
|
||||
const shouldThrowError = true
|
||||
|
||||
const blob = await resp.blob()
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = srcFileNm
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
URL.revokeObjectURL(url)
|
||||
const resp = await tryFunction(() => fetch(`/api/qna/file?encodeFileNo=${encodeFileNo}&srcFileNm=${srcFileNm}`), isListQuery, shouldThrowError)
|
||||
const blob = await resp.blob()
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = srcFileNm
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
URL.revokeObjectURL(url)
|
||||
|
||||
return blob
|
||||
} catch (error) {
|
||||
alert('ファイルのダウンロードに失敗しました')
|
||||
return null
|
||||
}
|
||||
return blob
|
||||
}
|
||||
|
||||
/**
|
||||
@ -191,7 +224,10 @@ export function useInquiry(
|
||||
const { data: commonCodeList, isLoading: isLoadingCommonCodeList } = useQuery({
|
||||
queryKey: ['commonCodeList'],
|
||||
queryFn: async () => {
|
||||
const resp = await axiosInstance(null).get<{ data: CommonCode[] }>(`/api/qna`)
|
||||
const isListQuery = false
|
||||
const shouldThrowError = false
|
||||
|
||||
const resp = await tryFunction(() => axiosInstance(null).get<{ data: CommonCode[] }>(`/api/qna`), isListQuery, shouldThrowError)
|
||||
return resp.data
|
||||
},
|
||||
staleTime: Infinity,
|
||||
|
||||
@ -6,6 +6,7 @@ import { useSessionStore } from '@/store/session'
|
||||
import { useAxios } from './useAxios'
|
||||
import { queryStringFormatter } from '@/utils/common-utils'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { ERROR_MESSAGE, useAlertMsg } from './useAlertMsg'
|
||||
|
||||
export const requiredFields = [
|
||||
{
|
||||
@ -99,17 +100,23 @@ export function useSurvey(
|
||||
const { session } = useSessionStore()
|
||||
const { axiosInstance } = useAxios()
|
||||
const router = useRouter()
|
||||
|
||||
const { showErrorAlert } = useAlertMsg()
|
||||
/**
|
||||
* @description 조사 매물 목록, 상세 데이터 조회 에러 처리
|
||||
*
|
||||
* @param {any} error 에러 객체
|
||||
* @returns {void} 라우팅 처리
|
||||
* @param {boolean} isThrow 에러 Throw 처리 여부
|
||||
* @returns {void} 라우팅 처리 / 에러 Throw 처리
|
||||
*/
|
||||
const errorRouter = (error: any) => {
|
||||
const handleError = (error: any, isThrow?: boolean) => {
|
||||
const status = error.response?.status
|
||||
if (error.response?.data.error) {
|
||||
alert(error.response?.data.error)
|
||||
const errorMsg = error.response?.data.error
|
||||
console.error('❌ AXIOS INSTANCE ERROR : ', error)
|
||||
if (errorMsg) {
|
||||
showErrorAlert(errorMsg)
|
||||
}
|
||||
if (isThrow) {
|
||||
throw new Error(error)
|
||||
}
|
||||
switch (status) {
|
||||
/** session 없는 경우 */
|
||||
@ -133,6 +140,27 @@ export function useSurvey(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 try catch 처리 함수
|
||||
*
|
||||
* @param {Function} func 조사 매물 API 함수
|
||||
* @param {boolean} isList 조사 매물 목록 여부
|
||||
* @param {boolean} isThrow 조사 매물 데이터 조회 에러 처리 여부
|
||||
* @returns {Promise<any>} API 응답 데이터
|
||||
*/
|
||||
const tryFunction = async (func: () => Promise<any>, isList?: boolean, isThrow?: boolean): Promise<any> => {
|
||||
try {
|
||||
const resp = await func()
|
||||
return resp.data
|
||||
} catch (error) {
|
||||
handleError(error, isThrow)
|
||||
if (isList) {
|
||||
return { data: [], count: 0 }
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사 매물 목록 조회
|
||||
*
|
||||
@ -147,27 +175,24 @@ export function useSurvey(
|
||||
isLoading: isLoadingSurveyList,
|
||||
refetch: refetchSurveyList,
|
||||
} = useQuery({
|
||||
queryKey: ['survey', 'list', keyword, searchOption, isMySurvey, sort, offset, session?.storeNm, session?.builderId, session?.role],
|
||||
queryKey: ['survey', 'list', keyword, searchOption, isMySurvey, sort, offset],
|
||||
queryFn: async () => {
|
||||
try {
|
||||
const resp = await axiosInstance(null).get<{ data: SurveyBasicInfo[]; count: number }>('/api/survey-sales', {
|
||||
params: {
|
||||
keyword,
|
||||
searchOption,
|
||||
isMySurvey,
|
||||
sort,
|
||||
offset,
|
||||
storeId: session?.storeId,
|
||||
builderId: session?.builderId,
|
||||
role: session?.role,
|
||||
},
|
||||
})
|
||||
return resp.data
|
||||
} catch (error: any) {
|
||||
errorRouter(error)
|
||||
return { data: [], count: 0 }
|
||||
}
|
||||
return await tryFunction(
|
||||
() =>
|
||||
axiosInstance(null).get<{ data: SurveyBasicInfo[]; count: number }>('/api/survey-sales', {
|
||||
params: {
|
||||
keyword,
|
||||
searchOption,
|
||||
isMySurvey,
|
||||
sort,
|
||||
offset,
|
||||
},
|
||||
}),
|
||||
true,
|
||||
false,
|
||||
)
|
||||
},
|
||||
enabled: !isPdf,
|
||||
})
|
||||
|
||||
/**
|
||||
@ -200,17 +225,16 @@ export function useSurvey(
|
||||
queryKey: ['survey', id],
|
||||
queryFn: async () => {
|
||||
if (Number.isNaN(id) || id === undefined || id === 0) return null
|
||||
try {
|
||||
const resp = await axiosInstance(null).get<SurveyBasicInfo>(`/api/survey-sales/${id}`, {
|
||||
params: {
|
||||
isPdf: isPdf,
|
||||
},
|
||||
})
|
||||
return resp.data
|
||||
} catch (error: any) {
|
||||
errorRouter(error)
|
||||
return null
|
||||
}
|
||||
return await tryFunction(
|
||||
() =>
|
||||
axiosInstance(null).get<SurveyBasicInfo>(`/api/survey-sales/${id}`, {
|
||||
params: {
|
||||
isPdf: isPdf,
|
||||
},
|
||||
}),
|
||||
false,
|
||||
false,
|
||||
)
|
||||
},
|
||||
enabled: id !== 0 && id !== undefined && id !== null,
|
||||
})
|
||||
@ -234,6 +258,9 @@ export function useSurvey(
|
||||
queryClient.invalidateQueries({ queryKey: ['survey', 'list'] })
|
||||
queryClient.invalidateQueries({ queryKey: ['survey', id] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
handleError(error, true)
|
||||
},
|
||||
})
|
||||
|
||||
/**
|
||||
@ -248,7 +275,7 @@ export function useSurvey(
|
||||
*/
|
||||
const { mutate: updateSurvey, isPending: isUpdatingSurvey } = useMutation({
|
||||
mutationFn: async ({ survey, isTemporary, storeId }: { survey: SurveyRegistRequest; isTemporary: boolean; storeId?: string | null }) => {
|
||||
if (id === undefined) throw new Error('id is required')
|
||||
if (id === undefined) throw new Error()
|
||||
const resp = await axiosInstance(null).put<SurveyRegistRequest>(`/api/survey-sales/${id}`, {
|
||||
survey: survey,
|
||||
isTemporary: isTemporary,
|
||||
@ -262,7 +289,7 @@ export function useSurvey(
|
||||
queryClient.invalidateQueries({ queryKey: ['survey', 'list'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
alert(error.response?.data.error)
|
||||
handleError(error, true)
|
||||
},
|
||||
})
|
||||
|
||||
@ -278,7 +305,7 @@ export function useSurvey(
|
||||
*/
|
||||
const { mutateAsync: deleteSurvey, isPending: isDeletingSurvey } = useMutation({
|
||||
mutationFn: async () => {
|
||||
if (id === null) throw new Error('id is required')
|
||||
if (id === null) throw new Error()
|
||||
const resp = await axiosInstance(null).delete<boolean>(`/api/survey-sales/${id}`)
|
||||
return resp.data
|
||||
},
|
||||
@ -286,7 +313,7 @@ export function useSurvey(
|
||||
queryClient.invalidateQueries({ queryKey: ['survey', 'list'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
alert(error.response?.data.error)
|
||||
handleError(error, true)
|
||||
},
|
||||
})
|
||||
|
||||
@ -301,7 +328,7 @@ export function useSurvey(
|
||||
*/
|
||||
const { mutateAsync: submitSurvey, isPending: isSubmittingSurvey } = useMutation({
|
||||
mutationFn: async ({ targetId, targetNm }: { targetId?: string | null; targetNm?: string | null }) => {
|
||||
if (!id) throw new Error('id is required')
|
||||
if (!id) throw new Error()
|
||||
const resp = await axiosInstance(null).patch<boolean>(`/api/survey-sales/${id}`, {
|
||||
targetId,
|
||||
targetNm,
|
||||
@ -313,7 +340,7 @@ export function useSurvey(
|
||||
queryClient.invalidateQueries({ queryKey: ['survey', id] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
alert(error.response?.data.error)
|
||||
handleError(error, true)
|
||||
},
|
||||
})
|
||||
|
||||
@ -370,16 +397,12 @@ export function useSurvey(
|
||||
* @throws {Error} 우편번호 검색 실패 시 에러 발생
|
||||
*/
|
||||
const getZipCode = async (zipCode: string): Promise<ZipCode[] | null> => {
|
||||
try {
|
||||
const { data } = await axiosInstance(null).get<ZipCodeResponse>(
|
||||
`https://zipcloud.ibsnet.co.jp/api/search?${queryStringFormatter({ zipcode: zipCode.trim() })}`,
|
||||
)
|
||||
return data.results
|
||||
} catch (error: any) {
|
||||
console.error('Failed to fetch zipcode data:', error)
|
||||
alert(error.response?.data.error)
|
||||
throw new Error('Failed to fetch zipcode data')
|
||||
}
|
||||
const data = await tryFunction(
|
||||
() => axiosInstance(null).get<ZipCodeResponse>(`https://zipcloud.ibsnet.co.jp/api/search?${queryStringFormatter({ zipcode: zipCode.trim() })}`),
|
||||
false,
|
||||
true,
|
||||
)
|
||||
return data ? data.results : null
|
||||
}
|
||||
|
||||
/**
|
||||
@ -391,29 +414,18 @@ export function useSurvey(
|
||||
* @returns {Promise<SubmitTargetResponse[]|null>} 제출 대상 목록
|
||||
*/
|
||||
const getSubmitTarget = async (params: { storeId: string; role: string }): Promise<SubmitTargetResponse[] | null> => {
|
||||
try {
|
||||
if (!params.storeId) {
|
||||
alert('販売店IDがありません。')
|
||||
return null
|
||||
}
|
||||
|
||||
const endpoints = {
|
||||
Admin_Sub: `/api/submission/admin-sub?id=${params.storeId}`,
|
||||
Builder: `/api/submission/builder?id=${params.storeId}`,
|
||||
} as const
|
||||
|
||||
const endpoint = endpoints[params.role as keyof typeof endpoints]
|
||||
if (!endpoint) {
|
||||
alert('権限が間違っています。')
|
||||
return null
|
||||
}
|
||||
|
||||
const { data } = await axiosInstance(null).get<SubmitTargetResponse[]>(endpoint)
|
||||
return data
|
||||
} catch (error: any) {
|
||||
alert(error.response?.data.error)
|
||||
if (!params.storeId) {
|
||||
/** 판매점 ID 없는 경우 */
|
||||
showErrorAlert(ERROR_MESSAGE.BAD_REQUEST)
|
||||
return null
|
||||
}
|
||||
const endpoint = `/api/submission?storeId=${params.storeId}&role=${params.role}`
|
||||
if (!endpoint) {
|
||||
/** 권한 오류 */
|
||||
showErrorAlert(ERROR_MESSAGE.FORBIDDEN)
|
||||
return null
|
||||
}
|
||||
return await tryFunction(() => axiosInstance(null).get<SubmitTargetResponse[]>(endpoint), false, true)
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@ -15,9 +15,14 @@ interface ApiLogData {
|
||||
body: string | undefined
|
||||
}
|
||||
|
||||
/* 현재 날짜 반환 함수 (YYYY-MM-DD 형식) */
|
||||
const getCurrentDate = (): string => {
|
||||
return new Date().toISOString().split('T')[0]
|
||||
}
|
||||
|
||||
/* 날짜별 로그 파일 경로 생성 함수 */
|
||||
const getLogFilePath = (): string => {
|
||||
const today = new Date().toISOString().split('T')[0] // YYYY-MM-DD 형식
|
||||
const today: string = getCurrentDate()
|
||||
return join(process.cwd(), 'logs', `onsite-survey-${today}.log`)
|
||||
}
|
||||
|
||||
@ -25,15 +30,10 @@ const getLogFilePath = (): string => {
|
||||
class DailyLogger {
|
||||
private currentDate: string
|
||||
private logger: pino.Logger
|
||||
private destination: ReturnType<typeof pino.destination>
|
||||
private destination?: ReturnType<typeof pino.destination>
|
||||
|
||||
constructor() {
|
||||
this.currentDate = new Date().toISOString().split('T')[0]
|
||||
this.destination = pino.destination({
|
||||
dest: getLogFilePath(),
|
||||
mkdir: true,
|
||||
sync: false,
|
||||
})
|
||||
this.currentDate = getCurrentDate()
|
||||
this.logger = this.createLogger()
|
||||
|
||||
/* kill signal 핸들러 등록 */
|
||||
@ -42,14 +42,31 @@ class DailyLogger {
|
||||
}
|
||||
|
||||
private async handleShutdown(): Promise<void> {
|
||||
this.destination.flushSync()
|
||||
this.destination.end()
|
||||
if (isProduction && this.destination) {
|
||||
this.destination.flushSync()
|
||||
this.destination.end()
|
||||
}
|
||||
this.logger.flush()
|
||||
}
|
||||
|
||||
private createLogger(): pino.Logger {
|
||||
if (!isProduction) return pino({ level: 'silent' })
|
||||
|
||||
/* 기존 destination 종료 */
|
||||
if (this.destination) {
|
||||
this.destination.flushSync()
|
||||
this.destination.end()
|
||||
}
|
||||
/* 새로운 destination 생성 */
|
||||
this.destination = pino.destination({
|
||||
dest: getLogFilePath(),
|
||||
mkdir: true,
|
||||
sync: false,
|
||||
})
|
||||
|
||||
return pino(
|
||||
{
|
||||
level: isProduction ? 'info' : 'silent',
|
||||
level: 'info',
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
},
|
||||
this.destination,
|
||||
@ -57,24 +74,16 @@ class DailyLogger {
|
||||
}
|
||||
|
||||
public info(obj: any, msg?: string): void {
|
||||
const today = new Date().toISOString().split('T')[0]
|
||||
|
||||
if (today !== this.currentDate) {
|
||||
/* 기존 destination 종료 */
|
||||
this.destination.flushSync()
|
||||
this.destination.end()
|
||||
|
||||
/* 새로운 destination 생성 */
|
||||
this.destination = pino.destination({
|
||||
dest: getLogFilePath(),
|
||||
mkdir: true,
|
||||
sync: false,
|
||||
})
|
||||
this.currentDate = today
|
||||
this.logger = this.createLogger()
|
||||
try {
|
||||
const today: string = getCurrentDate()
|
||||
if (today !== this.currentDate) {
|
||||
this.currentDate = today
|
||||
this.logger = this.createLogger()
|
||||
}
|
||||
this.logger.info(obj, msg)
|
||||
} catch (error) {
|
||||
console.error(`[DailyLogger] Failed to write log: ${error}`)
|
||||
}
|
||||
|
||||
this.logger.info(obj, msg)
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,13 +92,26 @@ const dailyLogger = new DailyLogger()
|
||||
|
||||
/* API 로그 기록 함수 */
|
||||
export const writeApiLog = async (request: NextRequest, responseStatus: number): Promise<void> => {
|
||||
if (!isProduction) return
|
||||
|
||||
let bodyString: string | undefined
|
||||
if (
|
||||
request.method === 'POST' &&
|
||||
(request.headers.get('content-type') === 'multipart/form-data' || request.headers.get('content-type') === 'application/x-www-form-urlencoded')
|
||||
) {
|
||||
const formData = await request.formData()
|
||||
bodyString = JSON.stringify(Object.fromEntries(formData))
|
||||
} else {
|
||||
bodyString = await request.text()
|
||||
}
|
||||
|
||||
const logData: ApiLogData = {
|
||||
responseStatus: responseStatus,
|
||||
method: request.method,
|
||||
url: request.url,
|
||||
// headers: Object.fromEntries(request.headers),
|
||||
query: Object.fromEntries(new URL(request.url).searchParams),
|
||||
body: request.body ? await request.text() : undefined,
|
||||
body: bodyString,
|
||||
}
|
||||
dailyLogger.info(logData, 'API Request')
|
||||
}
|
||||
|
||||
@ -301,3 +301,25 @@ export type SubmitTargetResponse = {
|
||||
/* 권한 */
|
||||
auth: string
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 조사매물 검색 파라미터 타입
|
||||
*/
|
||||
export type SurveySearchParams = {
|
||||
/** 검색 키워드 */
|
||||
keyword?: string | null
|
||||
/** 검색 옵션 */
|
||||
searchOption?: string | null
|
||||
/** 내 조사매물 여부 */
|
||||
isMySurvey?: string | null
|
||||
/** 정렬 옵션 */
|
||||
sort?: string | null
|
||||
/** 페이지 번호 */
|
||||
offset?: string | null
|
||||
/** 권한 */
|
||||
role?: string | null
|
||||
/** 판매점 ID */
|
||||
storeId?: string | null
|
||||
/** 시공점 ID */
|
||||
builderId?: string | null
|
||||
}
|
||||
@ -155,7 +155,7 @@ export const unescapeString = (str) => {
|
||||
*/
|
||||
|
||||
while (regex.test(str)) {
|
||||
str = str.replace(regex, (matched) => chars[matched] || matched);
|
||||
str = str.replace(regex, (matched) => chars[matched] || matched)
|
||||
}
|
||||
return str
|
||||
}
|
||||
@ -186,15 +186,14 @@ function isObject(value) {
|
||||
return value !== null && typeof value === 'object'
|
||||
}
|
||||
|
||||
|
||||
// 카멜케이스를 스네이크케이스로 변환하는 함수
|
||||
export const toSnakeCase = (str) => {
|
||||
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
|
||||
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
|
||||
}
|
||||
|
||||
// 객체의 키를 스네이크케이스로 변환하는 함수
|
||||
export const convertToSnakeCase = (obj) => {
|
||||
if (obj === null || obj === undefined) return obj;
|
||||
if (obj === null || obj === undefined) return obj
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map((item) => convertToSnakeCase(item))
|
||||
@ -202,11 +201,11 @@ export const convertToSnakeCase = (obj) => {
|
||||
|
||||
if (typeof obj === 'object') {
|
||||
return Object.keys(obj).reduce((acc, key) => {
|
||||
const snakeKey = toSnakeCase(key).toUpperCase();
|
||||
acc[snakeKey] = convertToSnakeCase(obj[key]);
|
||||
return acc;
|
||||
}, {});
|
||||
const snakeKey = toSnakeCase(key).toUpperCase()
|
||||
acc[snakeKey] = convertToSnakeCase(obj[key])
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
return obj;
|
||||
return obj
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user