From 593affe7253c5114c09119a24e88f7bfbf33f9bf Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Wed, 3 Dec 2025 12:58:13 +0900 Subject: (대표님) working-dujin 경로 작업사항 [김준회] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/cron/tags-plant/start/route.ts | 38 +- lib/forms-plant/services.ts | 161 ++--- lib/sedp/get-tags-plant.ts | 1013 +++++++++++++---------------- lib/tags-plant/queries.ts | 2 + lib/tags-plant/service.ts | 158 ++--- lib/tags-plant/table/add-tag-dialog.tsx | 2 +- lib/tags-plant/table/tag-table-column.tsx | 19 +- lib/tags-plant/table/tag-table.tsx | 3 + 8 files changed, 682 insertions(+), 714 deletions(-) diff --git a/app/api/cron/tags-plant/start/route.ts b/app/api/cron/tags-plant/start/route.ts index 83e06935..17a96ed7 100644 --- a/app/api/cron/tags-plant/start/route.ts +++ b/app/api/cron/tags-plant/start/route.ts @@ -88,8 +88,6 @@ async function processTagImport(syncId: string) { const jobInfo = syncJobs.get(syncId)!; const projectCode = jobInfo.projectCode; const packageCode = jobInfo.packageCode; - const mode = jobInfo.mode; // 모드 정보 추출 - // 상태 업데이트: 처리 중 syncJobs.set(syncId, { @@ -102,23 +100,40 @@ async function processTagImport(syncId: string) { throw new Error('Package is required'); } - // 여기서 실제 태그 가져오기 로직 import const { importTagsFromSEDP } = await import('@/lib/sedp/get-tags-plant'); - // 진행 상황 업데이트를 위한 콜백 함수 - const updateProgress = (progress: number) => { + // ENG 모드 실행 (0~50%) + const updateProgressENG = (progress: number) => { + syncJobs.set(syncId, { + ...syncJobs.get(syncId)!, + progress: Math.floor(progress * 0.5) + }); + }; + + const engResult = await importTagsFromSEDP(projectCode, packageCode, updateProgressENG, 'ENG'); + + // IM 모드 실행 (50~100%) + const updateProgressIM = (progress: number) => { syncJobs.set(syncId, { ...syncJobs.get(syncId)!, - progress + progress: 50 + Math.floor(progress * 0.5) }); }; - // 실제 태그 가져오기 실행 - const result = await importTagsFromSEDP(projectCode, packageCode,updateProgress, mode); + const imResult = await importTagsFromSEDP(projectCode, packageCode, updateProgressIM, 'IM'); - // 명시적으로 캐시 무효화 + // 캐시 무효화 revalidateTag(`tags-${packageCode}`); - revalidateTag(`forms-${packageCode}-${mode}`); + revalidateTag(`forms-${packageCode}-ENG`); + revalidateTag(`forms-${packageCode}-IM`); + + // 결과 합산 + const result = { + processedCount: engResult.processedCount + imResult.processedCount, + excludedCount: engResult.excludedCount + imResult.excludedCount, + totalEntries: engResult.totalEntries + imResult.totalEntries, + errors: [...(engResult.errors || []), ...(imResult.errors || [])].filter(Boolean) + }; // 상태 업데이트: 완료 syncJobs.set(syncId, { @@ -131,7 +146,6 @@ async function processTagImport(syncId: string) { return result; } catch (error: any) { - // 에러 발생 시 상태 업데이트 syncJobs.set(syncId, { ...syncJobs.get(syncId)!, status: 'failed', @@ -139,7 +153,7 @@ async function processTagImport(syncId: string) { error: error.message || 'Unknown error occurred', }); - throw error; // 에러 다시 던지기 + throw error; } } diff --git a/lib/forms-plant/services.ts b/lib/forms-plant/services.ts index 3f50bd47..64d353de 100644 --- a/lib/forms-plant/services.ts +++ b/lib/forms-plant/services.ts @@ -21,7 +21,7 @@ import { VendorDataReportTempsPlant, } from "@/db/schema/vendorData"; import { eq, and, desc, sql, DrizzleError, inArray, or, type SQL, type InferSelectModel } from "drizzle-orm"; -import { unstable_cache } from "next/cache"; +import { unstable_cache ,unstable_noStore } from "next/cache"; import { revalidateTag } from "next/cache"; import { getErrorMessage } from "../handle-error"; import { DataTableColumnJSON } from "@/components/form-data/form-data-table-columns"; @@ -234,9 +234,10 @@ export async function getEditableFieldsByTag( * 그리고 이 로직 전체를 unstable_cache로 감싸 캐싱. */ export async function getFormData(formCode: string, projectCode: string, packageCode:string) { + unstable_noStore(); try { - console.log(formCode,projectCode, packageCode) + // console.log(formCode,projectCode, packageCode) const project = await db.query.projects.findFirst({ where: eq(projects.code, projectCode), @@ -329,83 +330,84 @@ export async function getFormData(formCode: string, projectCode: string, package console.error(`[getFormData] Cache operation failed:`, cacheError); // Fallback logic (기존과 동일하게 editableFieldsMap 추가) - try { - console.log(`[getFormData] Fallback DB query for (${formCode}, ${packageCode})`); - - const project = await db.query.projects.findFirst({ - where: eq(projects.code, projectCode), - columns: { - id: true - } - }); - - const projectId = project.id; - - const metaRows = await db - .select() - .from(formMetas) - .where( - and( - eq(formMetas.formCode, formCode), - eq(formMetas.projectId, projectId) - ) - ) - .orderBy(desc(formMetas.updatedAt)) - .limit(1); - - const meta = metaRows[0] ?? null; - if (!meta) { - console.warn(`[getFormData] Fallback: No form meta found for formCode: ${formCode} and projectId: ${projectId}`); - return { columns: null, data: [], editableFieldsMap: new Map() }; - } - - const entryRows = await db - .select() - .from(formEntriesPlant) - .where( - and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode) - ) - ) - .orderBy(desc(formEntriesPlant.updatedAt)) - .limit(1); - - const entry = entryRows[0] ?? null; - - let columns = meta.columns as DataTableColumnJSON[]; - const excludeKeys = ['BF_TAG_NO', 'TAG_TYPE_ID', 'PIC_NO']; - columns = columns.filter(col => !excludeKeys.includes(col.key)); - - columns.forEach((col) => { - if (!col.displayLabel) { - if (col.uom) { - col.displayLabel = `${col.label} (${col.uom})`; - } else { - col.displayLabel = col.label; - } - } - }); - - let data: Array> = []; - if (entry) { - if (Array.isArray(entry.data)) { - data = entry.data; - } else { - console.warn("formEntries data was not an array. Using empty array (fallback)."); - } - } - - // Fallback에서도 편집 가능 필드 정보 계산 - const editableFieldsMap = await getEditableFieldsByTag(projectCode, packageCode, projectId); - - return { columns, data, projectId, editableFieldsMap }; - } catch (dbError) { - console.error(`[getFormData] Fallback DB query failed:`, dbError); - return { columns: null, data: [], editableFieldsMap: new Map() }; - } - } + // try { + // console.log(`[getFormData] Fallback DB query for (${formCode}, ${packageCode})`); + + // const project = await db.query.projects.findFirst({ + // where: eq(projects.code, projectCode), + // columns: { + // id: true + // } + // }); + + // const projectId = project.id; + + // const metaRows = await db + // .select() + // .from(formMetas) + // .where( + // and( + // eq(formMetas.formCode, formCode), + // eq(formMetas.projectId, projectId) + // ) + // ) + // .orderBy(desc(formMetas.updatedAt)) + // .limit(1); + + // const meta = metaRows[0] ?? null; + // if (!meta) { + // console.warn(`[getFormData] Fallback: No form meta found for formCode: ${formCode} and projectId: ${projectId}`); + // return { columns: null, data: [], editableFieldsMap: new Map() }; + // } + + // const entryRows = await db + // .select() + // .from(formEntriesPlant) + // .where( + // and( + // eq(formEntriesPlant.formCode, formCode), + // eq(formEntriesPlant.projectCode, projectCode), + // eq(formEntriesPlant.packageCode, packageCode) + // ) + // ) + // .orderBy(desc(formEntriesPlant.updatedAt)) + // .limit(1); + + // const entry = entryRows[0] ?? null; + + // let columns = meta.columns as DataTableColumnJSON[]; + // const excludeKeys = ['BF_TAG_NO', 'TAG_TYPE_ID', 'PIC_NO']; + // columns = columns.filter(col => !excludeKeys.includes(col.key)); + + // columns.forEach((col) => { + // if (!col.displayLabel) { + // if (col.uom) { + // col.displayLabel = `${col.label} (${col.uom})`; + // } else { + // col.displayLabel = col.label; + // } + // } + // }); + + // let data: Array> = []; + // if (entry) { + // if (Array.isArray(entry.data)) { + // data = entry.data; + // } else { + // console.warn("formEntries data was not an array. Using empty array (fallback)."); + // } + // } + + // // Fallback에서도 편집 가능 필드 정보 계산 + // const editableFieldsMap = await getEditableFieldsByTag(projectCode, packageCode, projectId); + + // return { columns, data, projectId, editableFieldsMap }; + // } catch (dbError) { + // console.error(`[getFormData] Fallback DB query failed:`, dbError); + // return { columns: null, data: [], editableFieldsMap: new Map() }; + // } + // } +} } /** * contractId와 formCode(itemCode)를 사용하여 contractItemId를 찾는 서버 액션 @@ -1052,6 +1054,7 @@ type GetReportFileList = ( }>; export const getFormId: GetReportFileList = async (projectCode, packageCode, formCode, mode) => { + unstable_noStore(); const result: { formId: number } = { formId: 0, }; diff --git a/lib/sedp/get-tags-plant.ts b/lib/sedp/get-tags-plant.ts index be0e398b..f804ebe9 100644 --- a/lib/sedp/get-tags-plant.ts +++ b/lib/sedp/get-tags-plant.ts @@ -3,651 +3,578 @@ import { tagsPlant, formsPlant, formEntriesPlant, - items, - tagTypeClassFormMappings, projects, tagTypes, tagClasses, } from "@/db/schema"; -import { eq, and, like, inArray } from "drizzle-orm"; -import { revalidateTag } from "next/cache"; // 추가 +import { eq, and } from "drizzle-orm"; +import { revalidatePath } from "next/cache"; import { getSEDPToken } from "./sedp-token"; -/** - * 태그 가져오기 서비스 함수 - * contractItemId(packageId)를 기반으로 외부 시스템에서 태그 데이터를 가져와 DB에 저장 - * TAG_IDX를 기준으로 태그를 식별합니다. - * - * @param projectCode 계약 아이템 ID (contractItemId) - * @param packageCode 계약 아이템 ID (contractItemId) - * @param progressCallback 진행 상황을 보고하기 위한 콜백 함수 - * @returns 처리 결과 정보 (처리된 태그 수, 오류 목록 등) - */ +const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api'; + +// ============ 타입 정의 ============ +interface newRegister { + PROJ_NO: string; + MAP_ID: string; + EP_ID: string; + DESC: string; + CATEGORY: string; + BYPASS: boolean; + REG_TYPE_ID: string; + TOOL_ID: string; + TOOL_TYPE: string; + SCOPES: string[]; + MAP_CLS: { + TOOL_ATT_NAME: string; + ITEMS: any[]; + }; + MAP_ATT: any[]; + MAP_TMPLS: string[]; + CRTER_NO: string; + CRTE_DTM: string; + CHGER_NO: string; + _id: string; +} + +interface Register { + PROJ_NO: string; + TYPE_ID: string; + EP_ID: string; + DESC: string; + REMARK: string | null; + NEW_TAG_YN: boolean; + ALL_TAG_YN: boolean; + VND_YN: boolean; + SEQ: number; + CMPLX_YN: boolean; + CMPL_SETT: any | null; + MAP_ATT: any[]; + MAP_CLS_ID: string[]; + MAP_OPER: any | null; + LNK_ATT: any[]; + JOIN_TABLS: any[]; + DELETED: boolean; + CRTER_NO: string; + CRTE_DTM: string; + CHGER_NO: string | null; + CHGE_DTM: string | null; + _id: string; +} + +interface FormInfo { + formCode: string; + formName: string; + im: boolean; + eng: boolean; +} + +// ============ API 호출 함수들 ============ + +async function getNewRegisters(projectCode: string): Promise { + const apiKey = await getSEDPToken(); + + const response = await fetch( + `${SEDP_API_BASE_URL}/AdapterDataMapping/GetByToolID`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'accept': '*/*', + 'ApiKey': apiKey, + 'ProjectNo': projectCode + }, + body: JSON.stringify({ + ProjectNo: projectCode, + TOOL_ID: "eVCP" + }) + } + ); + + if (!response.ok) { + throw new Error(`새 레지스터 요청 실패: ${response.status} ${response.statusText}`); + } + + const data = await response.json(); + const registers: newRegister[] = Array.isArray(data) ? data : [data]; + + console.log(`[getNewRegisters] 프로젝트 ${projectCode}에서 ${registers.length}개의 레지스터를 가져왔습니다.`); + return registers; +} + +async function getRegisters(projectCode: string): Promise { + const apiKey = await getSEDPToken(); + + const response = await fetch( + `${SEDP_API_BASE_URL}/Register/Get`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'accept': '*/*', + 'ApiKey': apiKey, + 'ProjectNo': projectCode + }, + body: JSON.stringify({ + ProjectNo: projectCode, + ContainDeleted: false + }) + } + ); + + if (!response.ok) { + throw new Error(`레지스터 요청 실패: ${response.status} ${response.statusText}`); + } + + const data = await response.json(); + const registers: Register[] = Array.isArray(data) ? data : [data]; + + console.log(`[getRegisters] 프로젝트 ${projectCode}에서 ${registers.length}개의 레지스터를 가져왔습니다.`); + return registers; +} + +async function fetchTagDataFromSEDP(projectCode: string, formCode: string): Promise { + const apiKey = await getSEDPToken(); + + const response = await fetch( + `${SEDP_API_BASE_URL}/Data/GetPubData`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'accept': '*/*', + 'ApiKey': apiKey, + 'ProjectNo': projectCode + }, + body: JSON.stringify({ + ProjectNo: projectCode, + REG_TYPE_ID: formCode, + ContainDeleted: false + }) + } + ); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error(`SEDP API 요청 실패: ${response.status} ${response.statusText} - ${errorText}`); + } + + return await response.json(); +} + +async function getRegisterDetail(projectCode: string, formCode: string): Promise { + const apiKey = await getSEDPToken(); + + const response = await fetch( + `${SEDP_API_BASE_URL}/Register/GetByID`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'accept': '*/*', + 'ApiKey': apiKey, + 'ProjectNo': projectCode + }, + body: JSON.stringify({ + ProjectNo: projectCode, + TYPE_ID: formCode, + ContainDeleted: false + }) + } + ); + + if (!response.ok) { + console.error(`Register detail 요청 실패: ${formCode}`); + return null; + } + + return await response.json(); +} + +// ============ 메인 함수 ============ + export async function importTagsFromSEDP( projectCode: string, packageCode: string, - progressCallback?: (progress: number) => void, - mode?: string + progressCallback?: (progress: number) => void ): Promise<{ processedCount: number; excludedCount: number; totalEntries: number; errors?: string[]; }> { + const allErrors: string[] = []; + let totalProcessedCount = 0; + let totalExcludedCount = 0; + let totalEntriesCount = 0; + try { - // 진행 상황 보고 if (progressCallback) progressCallback(5); + // Step 1: 프로젝트 ID 조회 const project = await db.query.projects.findFirst({ where: eq(projects.code, projectCode), - columns: { - id: true - } + columns: { id: true } }); + if (!project) { + throw new Error(`Project not found: ${projectCode}`); + } + const projectId = project.id; + + if (progressCallback) progressCallback(10); - // 프로젝트 ID 획득 - const projectId = project?.id; + // Step 2: 두 API 동시 호출 + const [newRegisters, registers] = await Promise.all([ + getNewRegisters(projectCode), + getRegisters(projectCode) + ]); - // Step 1-2: Get the item using itemId from contractItem - const item = await db.query.items.findFirst({ - where: and(eq(items.ProjectNo, projectCode), eq(items.packageCode, packageCode)) + if (progressCallback) progressCallback(20); + + // ======== 서브클래스 매핑을 위한 태그 클래스 로드 ======== + const allTagClasses = await db.query.tagClasses.findMany({ + where: eq(tagClasses.projectId, projectId) }); - if (!item) { - throw new Error(`Item with ID ${item?.id} not found`); + // 클래스 코드로 빠른 조회를 위한 Map + const tagClassByCode = new Map(allTagClasses.map(tc => [tc.code, tc])); + + // 서브클래스 코드로 부모 클래스 찾기 위한 Map + const parentBySubclassCode = new Map(); + for (const tc of allTagClasses) { + if (tc.subclasses && Array.isArray(tc.subclasses)) { + for (const sub of tc.subclasses as { id: string; desc: string }[]) { + parentBySubclassCode.set(sub.id, tc); + } + } } - const itemCode = item.itemCode; + console.log(`[importTagsFromSEDP] 태그 클래스 ${allTagClasses.length}개 로드, 서브클래스 매핑 ${parentBySubclassCode.size}개 생성`); + // ======== 서브클래스 매핑 준비 완료 ======== - // 진행 상황 보고 - if (progressCallback) progressCallback(10); + // Step 3: packageCode에 해당하는 폼 정보 추출 + const formsToProcess: FormInfo[] = []; - // 기본 매핑 검색 - 모든 모드에서 사용 - const baseMappings = await db.query.tagTypeClassFormMappings.findMany({ - where: and( - like(tagTypeClassFormMappings.remark, `%${itemCode}%`), - eq(tagTypeClassFormMappings.projectId, projectId) - ) - }); - - if (baseMappings.length === 0) { - throw new Error(`No mapping found for item code ${itemCode}`); + // Register 정보를 Map으로 변환 (TYPE_ID로 빠른 조회) + const registerMap = new Map(); + for (const reg of registers) { + registerMap.set(reg.TYPE_ID, reg); } - // Step 2: Find the mapping entries - 모드에 따라 다른 조건 적용 - let mappings = []; - - if (mode === 'IM') { - // IM 모드일 때는 먼저 SEDP에서 태그 데이터를 가져와 TAG_TYPE_ID 리스트 확보 - - // 프로젝트 코드 가져오기 - const project = await db.query.projects.findFirst({ - where: eq(projects.id, projectId) - }); - - if (!project) { - throw new Error(`Project with ID ${projectId} not found`); - } - - // 각 매핑의 formCode에 대해 태그 데이터 조회 - const tagTypeIds = new Set(); - - for (const mapping of baseMappings) { - try { - // SEDP에서 태그 데이터 가져오기 - const tagData = await fetchTagDataFromSEDP(project.code, mapping.formCode); - - // 첫 번째 키를 테이블 이름으로 사용 - const tableName = Object.keys(tagData)[0]; - const tagEntries = tagData[tableName]; - - if (Array.isArray(tagEntries)) { - // 모든 태그에서 TAG_TYPE_ID 수집 - for (const entry of tagEntries) { - if (entry.TAG_TYPE_ID && entry.TAG_TYPE_ID !== "") { - tagTypeIds.add(entry.TAG_TYPE_ID); - } - } - } - } catch (error) { - console.error(`Error fetching tag data for formCode ${mapping.formCode}:`, error); - } - } - - if (tagTypeIds.size === 0) { - throw new Error('No valid TAG_TYPE_ID found in SEDP tag data'); - } - - // 수집된 TAG_TYPE_ID로 tagTypes에서 정보 조회 - const tagTypeInfo = await db.query.tagTypes.findMany({ - where: and( - inArray(tagTypes.code, Array.from(tagTypeIds)), - eq(tagTypes.projectId, projectId) - ) - }); - - if (tagTypeInfo.length === 0) { - throw new Error('No matching tag types found for the collected TAG_TYPE_IDs'); - } - - // 태그 타입 설명 수집 - const tagLabels = tagTypeInfo.map(tt => tt.description); - - // IM 모드에 맞는 매핑 조회 - ep가 "IMEP"인 항목만 - mappings = await db.query.tagTypeClassFormMappings.findMany({ - where: and( - inArray(tagTypeClassFormMappings.tagTypeLabel, tagLabels), - eq(tagTypeClassFormMappings.projectId, projectId), - eq(tagTypeClassFormMappings.ep, "IMEP") - ) - }); - - } else { - // ENG 모드 또는 기본 모드일 때 - 기본 매핑 사용 - mappings = [...baseMappings]; - - // ENG 모드에서는 ep 필드가 "IMEP"가 아닌 매핑만 필터링 - if (mode === 'ENG') { - mappings = mappings.filter(mapping => mapping.ep !== "IMEP"); + // newRegisters에서 packageCode가 SCOPES에 포함된 것 필터링 + for (const newReg of newRegisters) { + if (newReg.SCOPES && newReg.SCOPES.includes(packageCode)) { + const formCode = newReg.REG_TYPE_ID; + const formName = newReg.DESC; + + // Register에서 EP_ID 확인하여 im/eng 결정 + const register = registerMap.get(formCode); + const isIM = register?.EP_ID === "IMEP"; + + formsToProcess.push({ + formCode, + formName, + im: isIM, + eng: !isIM + }); } } - // 매핑이 없는 경우 모드에 따라 다른 오류 메시지 사용 - if (mappings.length === 0) { - if (mode === 'IM') { - throw new Error('No suitable mappings found for IM mode'); - } else { - throw new Error(`No mapping found for item code ${itemCode}`); - } + if (formsToProcess.length === 0) { + throw new Error(`No forms found for packageCode: ${packageCode}`); } - - // 진행 상황 보고 - if (progressCallback) progressCallback(15); - - // 결과 누적을 위한 변수들 초기화 - let totalProcessedCount = 0; - let totalExcludedCount = 0; - let totalEntriesCount = 0; - const allErrors: string[] = []; - - // 각 매핑에 대해 처리 - for (let mappingIndex = 0; mappingIndex < mappings.length; mappingIndex++) { - const mapping = mappings[mappingIndex]; - - // Step 3: Get the project code - const project = await db.query.projects.findFirst({ - where: eq(projects.id, mapping.projectId) - }); - - if (!project) { - allErrors.push(`Project with ID ${mapping.projectId} not found`); - continue; // 다음 매핑으로 진행 - } - // IM 모드에서는 baseMappings에서 같은 formCode를 가진 매핑을 찾음 - let formCode = mapping.formCode; - if (mode === 'IM') { - // baseMapping에서 동일한 formCode를 가진 매핑 찾기 - const originalMapping = baseMappings.find( - baseMapping => baseMapping.formCode === mapping.formCode - ); - - // 찾았으면 해당 formCode 사용, 못 찾았으면 현재 매핑의 formCode 유지 - if (originalMapping) { - formCode = originalMapping.formCode; - } - } + console.log(`[importTagsFromSEDP] ${formsToProcess.length}개의 폼을 처리합니다.`); - // 진행 상황 보고 - 매핑별 진행률 조정 - if (progressCallback) { - const baseProgress = 15; - const mappingProgress = Math.floor(15 * (mappingIndex + 1) / mappings.length); - progressCallback(baseProgress + mappingProgress); - } + if (progressCallback) progressCallback(25); - // Step 4: Find the form ID - const form = await db.query.formsPlant.findFirst({ - where: and( - eq(formsPlant.projectCode, projectCode), - eq(formsPlant.formCode, formCode), - eq(formsPlant.packageCode, packageCode) - ) - }); - - let formId; - - // If form doesn't exist, create it - if (!form) { - // 폼이 없는 경우 새로 생성 - 모드에 따른 필드 설정 - const insertValues: any = { - projectCode, - packageCode, - formCode: formCode, - formName: mapping.formName - }; - - // 모드 정보가 있으면 해당 필드 설정 - if (mode) { - if (mode === "ENG") { - insertValues.eng = true; - } else if (mode === "IM") { - insertValues.im = true; - if (mapping.remark && mapping.remark.includes("VD_")) { - insertValues.eng = true; - } - } - } + // Step 4: 각 폼에 대해 처리 + for (let i = 0; i < formsToProcess.length; i++) { + const formInfo = formsToProcess[i]; + const { formCode, formName, im, eng } = formInfo; - const insertResult = await db.insert(formsPlant) - .values(insertValues) - .onConflictDoUpdate({ - target: [formsPlant.projectCode, formsPlant.formCode], - set: { - packageCode: insertValues.packageCode, - formName: insertValues.formName, - eng: insertValues.eng ?? false, - im: insertValues.im ?? false, - updatedAt: new Date() - } - }) - .returning({ id: formsPlant.id }); + try { + // 진행률 계산 + const baseProgress = 25; + const progressPerForm = 70 / formsToProcess.length; - if (insertResult.length === 0) { - allErrors.push(`Failed to create form record for formCode ${formCode}`); - continue; // 다음 매핑으로 진행 - } - - formId = insertResult[0].id; - } else { - // 폼이 이미 존재하는 경우 - 필요시 모드 필드 업데이트 - formId = form.id; - - if (mode) { - let shouldUpdate = false; - const updateValues: any = {}; - - if (mode === "ENG" && form.eng !== true) { - updateValues.eng = true; - shouldUpdate = true; - } else if (mode === "IM" && form.im !== true) { - updateValues.im = true; - shouldUpdate = true; - } - - if (shouldUpdate) { - await db.update(formsPlant) - .set({ - ...updateValues, - updatedAt: new Date() - }) - .where(eq(formsPlant.id, formId)); + // Step 4-1: formsPlant upsert + const existingForm = await db.query.formsPlant.findFirst({ + where: and( + eq(formsPlant.projectCode, projectCode), + eq(formsPlant.packageCode, packageCode), + eq(formsPlant.formCode, formCode) + ) + }); + + let formId: number; + + if (existingForm) { + // 기존 폼 업데이트 + await db.update(formsPlant) + .set({ + formName, + im, + eng, + updatedAt: new Date() + }) + .where(eq(formsPlant.id, existingForm.id)); + + formId = existingForm.id; + console.log(`[formsPlant] Updated form: ${formCode}`); + } else { + // 새 폼 생성 + const insertResult = await db.insert(formsPlant) + .values({ + projectCode, + packageCode, + formCode, + formName, + im, + eng + }) + .returning({ id: formsPlant.id }); - console.log(`Updated form ${formId} with ${mode} mode enabled`); - } + formId = insertResult[0].id; + console.log(`[formsPlant] Created form: ${formCode}`); } - } - - // 진행 상황 보고 - 매핑별 진행률 조정 - if (progressCallback) { - const baseProgress = 30; - const mappingProgress = Math.floor(20 * (mappingIndex + 1) / mappings.length); - progressCallback(baseProgress + mappingProgress); - } - try { - // Step 5: Call the external API to get tag data - const tagData = await fetchTagDataFromSEDP(projectCode, baseMappings[0].formCode); - - // 진행 상황 보고 if (progressCallback) { - const baseProgress = 50; - const mappingProgress = Math.floor(10 * (mappingIndex + 1) / mappings.length); - progressCallback(baseProgress + mappingProgress); + progressCallback(baseProgress + progressPerForm * (i + 0.2)); } - // Step 6: Process the data and insert into the tags table - let processedCount = 0; - let excludedCount = 0; - - // Get the first key from the response as the table name + // Step 4-2: SEDP에서 태그 데이터 가져오기 + const tagData = await fetchTagDataFromSEDP(projectCode, formCode); const tableName = Object.keys(tagData)[0]; const tagEntries = tagData[tableName]; if (!Array.isArray(tagEntries) || tagEntries.length === 0) { - allErrors.push(`No tag data found in the API response for formCode ${baseMappings[0].formCode}`); - continue; // 다음 매핑으로 진행 + console.log(`[importTagsFromSEDP] No tag data for formCode: ${formCode}`); + continue; } - const entriesCount = tagEntries.length; - totalEntriesCount += entriesCount; - - // formEntries를 위한 데이터 수집 - const newTagsForFormEntry: Array<{ - TAG_IDX: string; // 변경: TAG_NO → TAG_IDX - TAG_NO?: string; // TAG_NO도 함께 저장 (편집 가능한 필드) - TAG_DESC: string | null; - status: string; - [key: string]: any; - }> = []; - const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api'; - const apiKey = await getSEDPToken(); - - const registerResponse = await fetch( - `${SEDP_API_BASE_URL}/Register/GetByID`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'accept': '*/*', - 'ApiKey': apiKey, - 'ProjectNo': projectCode - }, - body: JSON.stringify({ - ProjectNo: projectCode, - TYPE_ID: baseMappings[0].formCode, // 또는 mapping.formCode - ContainDeleted: false - }) - } - ) - - if (!registerResponse.ok) { - allErrors.push(`Failed to fetch register details for ${baseMappings[0].formCode}`) - continue + totalEntriesCount += tagEntries.length; + + if (progressCallback) { + progressCallback(baseProgress + progressPerForm * (i + 0.4)); } - - const registerDetail: Register = await registerResponse.json() + + // Step 4-3: Register detail에서 허용된 ATT_ID 추출 + const registerDetail = await getRegisterDetail(projectCode, formCode); + const allowedAttIds = new Set(); - // ✅ MAP_ATT에서 허용된 ATT_ID 목록 추출 - const allowedAttIds = new Set() - if (Array.isArray(registerDetail.MAP_ATT)) { + if (registerDetail?.MAP_ATT && Array.isArray(registerDetail.MAP_ATT)) { for (const mapAttr of registerDetail.MAP_ATT) { if (mapAttr.ATT_ID) { - allowedAttIds.add(mapAttr.ATT_ID) + allowedAttIds.add(mapAttr.ATT_ID); } } } - - // Process each tag entry - for (let i = 0; i < tagEntries.length; i++) { - try { - const entry = tagEntries[i]; - - // TAG_IDX가 없는 경우 제외 (변경: TAG_NO → TAG_IDX 체크) - if (!entry.TAG_IDX) { - excludedCount++; - totalExcludedCount++; - - // 주기적으로 진행 상황 보고 (건너뛰어도 진행률은 업데이트) - if (progressCallback && (i % 10 === 0 || i === tagEntries.length - 1)) { - const baseProgress = 60; - const entryProgress = Math.floor(30 * ((mappingIndex * entriesCount + i) / (mappings.length * entriesCount))); - progressCallback(baseProgress + entryProgress); - } + // Step 4-4: 태그 처리 + const newTagsForFormEntry: Array> = []; + let processedCount = 0; + let excludedCount = 0; - continue; // 이 항목은 건너뜀 - } + for (const entry of tagEntries) { + // TAG_IDX 없으면 제외 + if (!entry.TAG_IDX) { + excludedCount++; + continue; + } + + // TAG_TYPE_ID 없으면 제외 + if (!entry.TAG_TYPE_ID || entry.TAG_TYPE_ID === "") { + excludedCount++; + continue; + } - const attributes: Record = {} - if (Array.isArray(entry.ATTRIBUTES)) { - for (const attr of entry.ATTRIBUTES) { - // MAP_ATT에 정의된 ATT_ID만 포함 - if (attr.ATT_ID && allowedAttIds.has(attr.ATT_ID)) { - if (attr.VALUE !== null && attr.VALUE !== undefined) { - attributes[attr.ATT_ID] = String(attr.VALUE) - } + // attributes 추출 (허용된 ATT_ID만) + const attributes: Record = {}; + if (Array.isArray(entry.ATTRIBUTES)) { + for (const attr of entry.ATTRIBUTES) { + if (attr.ATT_ID && allowedAttIds.has(attr.ATT_ID)) { + if (attr.VALUE !== null && attr.VALUE !== undefined) { + attributes[attr.ATT_ID] = String(attr.VALUE); } } } - - - // TAG_TYPE_ID가 null이거나 빈 문자열인 경우 제외 - if (entry.TAG_TYPE_ID === null || entry.TAG_TYPE_ID === "") { - excludedCount++; - totalExcludedCount++; - - // 주기적으로 진행 상황 보고 (건너뛰어도 진행률은 업데이트) - if (progressCallback && (i % 10 === 0 || i === tagEntries.length - 1)) { - const baseProgress = 60; - const entryProgress = Math.floor(30 * ((mappingIndex * entriesCount + i) / (mappings.length * entriesCount))); - progressCallback(baseProgress + entryProgress); - } + } - continue; // 이 항목은 건너뜀 + // tagType 조회 + const tagType = await db.query.tagTypes.findFirst({ + where: and( + eq(tagTypes.code, entry.TAG_TYPE_ID), + eq(tagTypes.projectId, projectId) + ) + }); + + // ======== 클래스 및 서브클래스 결정 로직 ======== + let classLabel: string; + let subclassValue: string | null = null; + let tagClassId: number | null = null; + + // 1. 먼저 CLS_ID로 직접 tagClass 찾기 + const tagClass = tagClassByCode.get(entry.CLS_ID); + + if (tagClass) { + // 직접 찾은 경우 - 이게 메인 클래스 + classLabel = tagClass.label || entry.CLS_ID; + tagClassId = tagClass.id; + } else { + // 2. 서브클래스인지 확인 (부모 클래스의 subclasses 배열에 있는지) + const parentClass = parentBySubclassCode.get(entry.CLS_ID); + + if (parentClass) { + // 서브클래스인 경우 + classLabel = parentClass.label || parentClass.code; + subclassValue = entry.CLS_ID; + tagClassId = parentClass.id; + + console.log(`[importTagsFromSEDP] 서브클래스 발견: ${entry.CLS_ID} -> 부모: ${parentClass.code}`); + } else { + // 어디에도 없는 경우 - 원본 값 사용 + classLabel = entry.CLS_ID; + console.log(`[importTagsFromSEDP] 클래스를 찾을 수 없음: ${entry.CLS_ID}`); } - - // Get tag type description - const tagType = await db.query.tagTypes.findFirst({ - where: and( - eq(tagTypes.code, entry.TAG_TYPE_ID), - eq(tagTypes.projectId, mapping.projectId) - ) - }); - - // Get tag class label - const tagClass = await db.query.tagClasses.findFirst({ - where: and( - eq(tagClasses.code, entry.CLS_ID), - eq(tagClasses.projectId, mapping.projectId) - ) - }); - - // Insert or update the tag - tagIdx 필드 추가 - await db.insert(tagsPlant).values({ - projectCode, - packageCode, - formId: formId, - tagIdx: entry.TAG_IDX, + } + // ======== 클래스/서브클래스 결정 완료 ======== + + // tagsPlant upsert (subclass 필드 추가) + await db.insert(tagsPlant).values({ + projectCode, + packageCode, + formId, + tagIdx: entry.TAG_IDX, + tagNo: entry.TAG_NO || entry.TAG_IDX, + tagType: tagType?.description || entry.TAG_TYPE_ID, + tagClassId: tagClassId, + class: classLabel, + subclass: subclassValue, + description: entry.TAG_DESC, + attributes, + }).onConflictDoUpdate({ + target: [tagsPlant.projectCode, tagsPlant.packageCode, tagsPlant.tagIdx], + set: { + formId, tagNo: entry.TAG_NO || entry.TAG_IDX, tagType: tagType?.description || entry.TAG_TYPE_ID, - tagClassId: tagClass?.id, - class: tagClass?.label || entry.CLS_ID, + tagClassId: tagClassId, + class: classLabel, + subclass: subclassValue, description: entry.TAG_DESC, - attributes: attributes, // JSONB로 저장 - }).onConflictDoUpdate({ - target: [tagsPlant.projectCode, tagsPlant.packageCode, tagsPlant.tagIdx], - set: { - formId: formId, - tagNo: entry.TAG_NO || entry.TAG_IDX, - tagType: tagType?.description || entry.TAG_TYPE_ID, - class: tagClass?.label || entry.CLS_ID, - description: entry.TAG_DESC, - attributes: attributes, // JSONB 업데이트 - updatedAt: new Date() - } - }) - // formEntries용 데이터 수집 - const tagDataForFormEntry = { - TAG_IDX: entry.TAG_IDX, // 변경: TAG_NO → TAG_IDX - TAG_NO: entry.TAG_NO || entry.TAG_IDX, // TAG_NO도 함께 저장 - TAG_DESC: entry.TAG_DESC || null, - status: "From S-EDP", // SEDP에서 가져온 데이터임을 표시 - source: "S-EDP" // 태그 출처 (불변) - S-EDP에서 가져옴 - }; - - // ATTRIBUTES가 있으면 추가 (SHI 필드들) - if (Array.isArray(entry.ATTRIBUTES)) { - for (const attr of entry.ATTRIBUTES) { - if (attr.ATT_ID && attr.VALUE !== null && attr.VALUE !== undefined) { - tagDataForFormEntry[attr.ATT_ID] = attr.VALUE; - } + attributes, + updatedAt: new Date() + } + }); + + // formEntriesPlant용 데이터 준비 + const tagDataForFormEntry: Record = { + TAG_IDX: entry.TAG_IDX, + TAG_NO: entry.TAG_NO || entry.TAG_IDX, + TAG_DESC: entry.TAG_DESC || null, + status: "From S-EDP", + source: "S-EDP" + }; + + // ATTRIBUTES 추가 + if (Array.isArray(entry.ATTRIBUTES)) { + for (const attr of entry.ATTRIBUTES) { + if (attr.ATT_ID && attr.VALUE !== null && attr.VALUE !== undefined) { + tagDataForFormEntry[attr.ATT_ID] = attr.VALUE; } } + } - newTagsForFormEntry.push(tagDataForFormEntry); + newTagsForFormEntry.push(tagDataForFormEntry); + processedCount++; + } - processedCount++; - totalProcessedCount++; + totalProcessedCount += processedCount; + totalExcludedCount += excludedCount; - // 주기적으로 진행 상황 보고 - if (progressCallback && (i % 10 === 0 || i === tagEntries.length - 1)) { - const baseProgress = 60; - const entryProgress = Math.floor(30 * ((mappingIndex * entriesCount + i) / (mappings.length * entriesCount))); - progressCallback(baseProgress + entryProgress); - } - } catch (error: any) { - console.error(`Error processing tag entry:`, error); - allErrors.push(error.message || 'Unknown error'); - } + if (progressCallback) { + progressCallback(baseProgress + progressPerForm * (i + 0.8)); } - // Step 7: formEntries 업데이트 - TAG_IDX 기준으로 변경 + // Step 4-5: formEntriesPlant upsert if (newTagsForFormEntry.length > 0) { - try { - // 기존 formEntry 가져오기 - const existingEntry = await db.query.formEntriesPlant.findFirst({ - where: and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode) - ) - }); - - if (existingEntry && existingEntry.id) { - // 기존 formEntry가 있는 경우 - let existingData: Array<{ - TAG_IDX?: string; // 추가: TAG_IDX 필드 - TAG_NO?: string; - TAG_DESC?: string; - status?: string; - [key: string]: any; - }> = []; - - if (Array.isArray(existingEntry.data)) { - existingData = existingEntry.data; - } + const existingEntry = await db.query.formEntriesPlant.findFirst({ + where: and( + eq(formEntriesPlant.formCode, formCode), + eq(formEntriesPlant.projectCode, projectCode), + eq(formEntriesPlant.packageCode, packageCode) + ) + }); + + if (existingEntry) { + // 기존 데이터 병합 + let existingData: Array> = []; + if (Array.isArray(existingEntry.data)) { + existingData = existingEntry.data; + } - // 기존 TAG_IDX들 추출 (변경: TAG_NO → TAG_IDX) - const existingTagIdxs = new Set( - existingData - .map(item => item.TAG_IDX) - .filter(tagIdx => tagIdx !== undefined && tagIdx !== null) - ); + const existingTagIdxs = new Set( + existingData.map(item => item.TAG_IDX).filter(Boolean) + ); - // 중복되지 않은 새 태그들만 필터링 (변경: TAG_NO → TAG_IDX) - const newUniqueTagsData = newTagsForFormEntry.filter( - tagData => !existingTagIdxs.has(tagData.TAG_IDX) + // 기존 데이터 업데이트 + 새 데이터 추가 + const updatedData = existingData.map(existingItem => { + const newData = newTagsForFormEntry.find( + n => n.TAG_IDX === existingItem.TAG_IDX ); + return newData ? { ...existingItem, ...newData } : existingItem; + }); - // 기존 태그들의 status와 ATTRIBUTES 업데이트 (변경: TAG_NO → TAG_IDX) - const updatedExistingData = existingData.map(existingItem => { - const newTagData = newTagsForFormEntry.find( - newItem => newItem.TAG_IDX === existingItem.TAG_IDX - ); - - if (newTagData) { - // 기존 태그가 있으면 SEDP 데이터로 업데이트 - return { - ...existingItem, - ...newTagData, - TAG_IDX: existingItem.TAG_IDX // TAG_IDX는 유지 - }; - } - - return existingItem; - }); - - const finalData = [...updatedExistingData, ...newUniqueTagsData]; + const newUniqueData = newTagsForFormEntry.filter( + n => !existingTagIdxs.has(n.TAG_IDX) + ); - await db - .update(formEntriesPlant) - .set({ - data: finalData, - updatedAt: new Date() - }) - .where(eq(formEntriesPlant.id, existingEntry.id)); + await db.update(formEntriesPlant) + .set({ + data: [...updatedData, ...newUniqueData], + updatedAt: new Date() + }) + .where(eq(formEntriesPlant.id, existingEntry.id)); - console.log(`[IMPORT SEDP] Updated formEntry with ${newUniqueTagsData.length} new tags, updated ${updatedExistingData.length - newUniqueTagsData.length} existing tags for form ${formCode}`); - } else { - // formEntry가 없는 경우 새로 생성 - await db.insert(formEntriesPlant).values({ - formCode: formCode, - projectCode, - packageCode, - data: newTagsForFormEntry, - createdAt: new Date(), - updatedAt: new Date(), - }); - - console.log(`[IMPORT SEDP] Created new formEntry with ${newTagsForFormEntry.length} tags for form ${formCode}`); - } + console.log(`[formEntriesPlant] Updated: ${formCode} (${newUniqueData.length} new, ${updatedData.length} updated)`); + } else { + // 새로 생성 + await db.insert(formEntriesPlant).values({ + formCode, + projectCode, + packageCode, + data: newTagsForFormEntry, + createdAt: new Date(), + updatedAt: new Date() + }); - // 캐시 무효화 - // revalidateTag(`form-data-${formCode}-${packageId}`); - } catch (formEntryError) { - console.error(`[IMPORT SEDP] Error updating formEntry for form ${formCode}:`, formEntryError); - allErrors.push(`Error updating formEntry for form ${formCode}: ${formEntryError}`); + console.log(`[formEntriesPlant] Created: ${formCode} (${newTagsForFormEntry.length} tags)`); } } + if (progressCallback) { + progressCallback(baseProgress + progressPerForm * (i + 1)); + } + } catch (error: any) { - console.error(`Error processing mapping for formCode ${formCode}:`, error); - allErrors.push(`Error with formCode ${formCode}: ${error.message || 'Unknown error'}`); + console.error(`Error processing form ${formCode}:`, error); + allErrors.push(`Form ${formCode}: ${error.message}`); } } - // 모든 매핑 처리 완료 - 진행률 100% - if (progressCallback) { - progressCallback(100); - } + if (progressCallback) progressCallback(100); - // 최종 결과 반환 return { processedCount: totalProcessedCount, excludedCount: totalExcludedCount, totalEntries: totalEntriesCount, errors: allErrors.length > 0 ? allErrors : undefined }; + } catch (error: any) { console.error("Tag import error:", error); throw error; } -} - -/** - * SEDP API에서 태그 데이터 가져오기 - * - * @param projectCode 프로젝트 코드 - * @param formCode 양식 코드 - * @returns API 응답 데이터 - */ -async function fetchTagDataFromSEDP(projectCode: string, formCode: string): Promise { - try { - // Get the token - const apiKey = await getSEDPToken(); - - // Define the API base URL - const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api'; - - // Make the API call - const response = await fetch( - `${SEDP_API_BASE_URL}/Data/GetPubData`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'accept': '*/*', - 'ApiKey': apiKey, - 'ProjectNo': projectCode - }, - body: JSON.stringify({ - ProjectNo: projectCode, - REG_TYPE_ID: formCode, - ContainDeleted: false - }) - } - ); - - if (!response.ok) { - const errorText = await response.text(); - throw new Error(`SEDP API request failed: ${response.status} ${response.statusText} - ${errorText}`); - } - - const data = await response.json(); - return data; - } catch (error: any) { - console.error('Error calling SEDP API:', error); - throw new Error(`Failed to fetch data from SEDP API: ${error.message || 'Unknown error'}`); - } } \ No newline at end of file diff --git a/lib/tags-plant/queries.ts b/lib/tags-plant/queries.ts index a0d28b1e..c7ad43e0 100644 --- a/lib/tags-plant/queries.ts +++ b/lib/tags-plant/queries.ts @@ -5,6 +5,7 @@ import db from "@/db/db" import { tagsPlant } from "@/db/schema/vendorData" import { eq, and } from "drizzle-orm" +import { revalidateTag, unstable_noStore } from "next/cache"; /** * 모든 태그 가져오기 (클라이언트 렌더링용) @@ -13,6 +14,7 @@ export async function getAllTagsPlant( projectCode: string, packageCode: string ) { + unstable_noStore(); try { const tags = await db .select() diff --git a/lib/tags-plant/service.ts b/lib/tags-plant/service.ts index 9e9d9ebf..27cc207b 100644 --- a/lib/tags-plant/service.ts +++ b/lib/tags-plant/service.ts @@ -25,6 +25,14 @@ interface CreatedOrExistingForm { isNewlyCreated: boolean; } +interface FormInfo { + formCode: string; + formName: string; + im: boolean; + eng: boolean; +} + + /** * 16진수 24자리 고유 식별자 생성 * @returns 24자리 16진수 문자열 (예: "a1b2c3d4e5f6789012345678") @@ -280,6 +288,7 @@ export async function createTag( tagIdx: generatedTagIdx, // 🆕 생성된 16진수 24자리 추가 tagNo: validated.data.tagNo, class: validated.data.class, + subclass: validated.data.subclass, tagType: validated.data.tagType, description: validated.data.description ?? null, }) @@ -1790,13 +1799,11 @@ export async function getIMForms( return existingForms } - // 2. DB에 없으면 SEDP API에서 가져오기 + // 2. DB에 없으면 두 API 동시 호출 const apiKey = await getSEDPToken() - // 2-1. GetByToolID로 레지스터 매핑 정보 가져오기 - const mappingResponse = await fetch( - `${SEDP_API_BASE_URL}/AdapterDataMapping/GetByToolID`, - { + const [newRegistersResponse, registersResponse] = await Promise.all([ + fetch(`${SEDP_API_BASE_URL}/AdapterDataMapping/GetByToolID`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -1808,95 +1815,94 @@ export async function getIMForms( ProjectNo: projectCode, TOOL_ID: "eVCP" }) - } - ) + }), + fetch(`${SEDP_API_BASE_URL}/Register/Get`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'accept': '*/*', + 'ApiKey': apiKey, + 'ProjectNo': projectCode + }, + body: JSON.stringify({ + ProjectNo: projectCode, + ContainDeleted: false + }) + }) + ]) - if (!mappingResponse.ok) { - throw new Error( - `레지스터 매핑 요청 실패: ${mappingResponse.status} ${mappingResponse.statusText}` - ) + if (!newRegistersResponse.ok) { + throw new Error(`새 레지스터 요청 실패: ${newRegistersResponse.status}`) } - const mappingData = await mappingResponse.json() - const registers: NewRegister[] = Array.isArray(mappingData) - ? mappingData - : [mappingData] + if (!registersResponse.ok) { + throw new Error(`레지스터 요청 실패: ${registersResponse.status}`) + } - // 2-2. packageCode가 SCOPES에 포함된 레지스터 필터링 - const matchingRegisters = registers.filter(register => - register.SCOPES.includes(packageCode) - ) + const newRegistersData = await newRegistersResponse.json() + const registersData = await registersResponse.json() - if (matchingRegisters.length === 0) { - console.log(`패키지 ${packageCode}에 해당하는 레지스터가 없습니다.`) - return [] + const newRegisters: NewRegister[] = Array.isArray(newRegistersData) + ? newRegistersData + : [newRegistersData] + + const registers: RegisterDetail[] = Array.isArray(registersData) + ? registersData + : [registersData] + + // 3. Register를 Map으로 변환 (TYPE_ID로 빠른 조회) + const registerMap = new Map() + for (const reg of registers) { + registerMap.set(reg.TYPE_ID, reg) } - // 2-3. 각 레지스터의 상세 정보 가져오기 + // 4. packageCode가 SCOPES에 포함되고, EP_ID가 "IMEP"인 것만 필터링 const formInfos: FormInfo[] = [] const formsToInsert: typeof formsPlant.$inferInsert[] = [] - for (const register of matchingRegisters) { - try { - const detailResponse = await fetch( - `${SEDP_API_BASE_URL}/Register/GetByID`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'accept': '*/*', - 'ApiKey': apiKey, - 'ProjectNo': projectCode - }, - body: JSON.stringify({ - ProjectNo: projectCode, - TYPE_ID: register.REG_TYPE_ID, - ContainDeleted: false - }) - } - ) - - if (!detailResponse.ok) { - console.error( - `레지스터 상세 정보 요청 실패 (${register.REG_TYPE_ID}): ${detailResponse.status}` - ) - continue - } - - const detail: RegisterDetail = await detailResponse.json() + for (const newReg of newRegisters) { + // packageCode가 SCOPES에 없으면 스킵 + if (!newReg.SCOPES || !newReg.SCOPES.includes(packageCode)) { + continue + } - // DELETED가 true이거나 DESC가 없으면 스킵 - if (detail.DELETED || !detail.DESC) { - continue - } + const formCode = newReg.REG_TYPE_ID + const register = registerMap.get(formCode) - formInfos.push({ - formCode: detail.TYPE_ID, - formName: detail.DESC - }) + // Register에서 EP_ID가 "IMEP"가 아니면 스킵 (IM 폼만 처리) + if (!register || register.EP_ID !== "IMEP") { + continue + } - // DB 삽입용 데이터 준비 - formsToInsert.push({ - projectCode: projectCode, - packageCode: packageCode, - formCode: detail.TYPE_ID, - formName: detail.DESC, - eng: false, - im: true - }) - } catch (error) { - console.error( - `레지스터 ${register.REG_TYPE_ID} 상세 정보 가져오기 실패:`, - error - ) + // DELETED면 스킵 + if (register.DELETED) { continue } + + const formName = newReg.DESC || register.DESC || formCode + + formInfos.push({ + formCode, + formName + }) + + formsToInsert.push({ + projectCode, + packageCode, + formCode, + formName, + eng: false, + im: true + }) } - // 2-4. DB에 저장 + // 5. DB에 저장 if (formsToInsert.length > 0) { - await db.insert(formsPlant).values(formsToInsert).onConflictDoNothing() - console.log(`${formsToInsert.length}개의 IM 폼을 DB에 저장했습니다.`) + await db.insert(formsPlant) + .values(formsToInsert) + .onConflictDoNothing() + + console.log(`[getIMForms] ${formsToInsert.length}개의 IM 폼을 DB에 저장했습니다.`) } return formInfos diff --git a/lib/tags-plant/table/add-tag-dialog.tsx b/lib/tags-plant/table/add-tag-dialog.tsx index de5d2bf8..1bfb0703 100644 --- a/lib/tags-plant/table/add-tag-dialog.tsx +++ b/lib/tags-plant/table/add-tag-dialog.tsx @@ -329,7 +329,7 @@ export function AddTagDialog({ projectCode, packageCode }: AddTagDialogProps) { const tagData: CreateTagSchema = { tagType: data.tagType, class: data.class, - // subclass: data.subclass, // 서브클래스 정보 추가 + subclass: data.subclass, // 서브클래스 정보 추가 tagNo: row.tagNo, description: row.description, ...Object.fromEntries( diff --git a/lib/tags-plant/table/tag-table-column.tsx b/lib/tags-plant/table/tag-table-column.tsx index 80c25464..30bdacc3 100644 --- a/lib/tags-plant/table/tag-table-column.tsx +++ b/lib/tags-plant/table/tag-table-column.tsx @@ -82,14 +82,27 @@ export function getColumns({ minSize: 150, size: 240, }, - { + { accessorKey: "class", header: ({ column }) => ( - + ), cell: ({ row }) =>
{row.getValue("class")}
, meta: { - excelHeader: "Tag Class" + excelHeader: "Class" + }, + enableResizing: true, + minSize: 100, + size: 150, + }, + { + accessorKey: "subclass", + header: ({ column }) => ( + + ), + cell: ({ row }) =>
{row.getValue("subclass")}
, + meta: { + excelHeader: "Item Class" }, enableResizing: true, minSize: 100, diff --git a/lib/tags-plant/table/tag-table.tsx b/lib/tags-plant/table/tag-table.tsx index 2fdcd5fc..70bfc4e4 100644 --- a/lib/tags-plant/table/tag-table.tsx +++ b/lib/tags-plant/table/tag-table.tsx @@ -78,6 +78,9 @@ export function TagsTable({ const [isLoading, setIsLoading] = React.useState(true) const [rowAction, setRowAction] = React.useState | null>(null) + + console.log(tableData,"tableData") + // 선택된 행 관리 const [selectedRowsData, setSelectedRowsData] = React.useState([]) const [clearSelection, setClearSelection] = React.useState(false) -- cgit v1.2.3 From cff5682ee787c02564c25c30dd44a99e6cad7839 Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Wed, 3 Dec 2025 12:58:52 +0900 Subject: (김준회) 옥창명 프로 요청: 조선에서 EDP와 비교 버튼 비활성화 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/form-data/form-data-table.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/form-data/form-data-table.tsx b/components/form-data/form-data-table.tsx index 70e93a68..d9915b66 100644 --- a/components/form-data/form-data-table.tsx +++ b/components/form-data/form-data-table.tsx @@ -1097,7 +1097,8 @@ export default function DynamicTable({ {/* COMPARE WITH SEDP 버튼 */} - + */} {/* SEDP 전송 버튼 */} + + e.preventDefault()} + onInteractOutside={(e) => e.preventDefault()} + > +
e.stopPropagation()} + onMouseDown={(e) => e.stopPropagation()} + > + { + handleCalendarSelect(date, e as unknown as React.MouseEvent) + }} + month={month} + onMonthChange={setMonth} + disabled={isDateDisabled} + locale={localeProp === "ko" ? ko : undefined} + showOutsideDays + className="p-3" + classNames={{ + months: "flex flex-col sm:flex-row space-y-4 sm:space-x-4 sm:space-y-0", + month: "space-y-4", + caption: "flex justify-center pt-1 relative items-center", + caption_label: "text-sm font-medium", + nav: "space-x-1 flex items-center", + nav_button: cn( + buttonVariants({ variant: "outline" }), + "h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100" + ), + nav_button_previous: "absolute left-1", + nav_button_next: "absolute right-1", + table: "w-full border-collapse space-y-1", + head_row: "flex", + head_cell: "text-muted-foreground rounded-md w-8 font-normal text-[0.8rem]", + row: "flex w-full mt-2", + cell: "relative p-0 text-center text-sm focus-within:relative focus-within:z-20 [&:has([aria-selected])]:bg-accent [&:has([aria-selected])]:rounded-md", + day: cn( + buttonVariants({ variant: "ghost" }), + "h-8 w-8 p-0 font-normal aria-selected:opacity-100" + ), + day_selected: "bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground focus:bg-primary focus:text-primary-foreground", + day_today: "bg-accent text-accent-foreground", + day_outside: "text-muted-foreground opacity-50", + day_disabled: "text-muted-foreground opacity-50", + day_hidden: "invisible", + }} + components={{ + IconLeft: () => , + IconRight: () => , + }} + /> +
+
+ + + {/* 에러 메시지 표시 */} + {hasError && errorMessage && ( +

{errorMessage}

+ )} + + ) +} + diff --git a/components/common/date-picker/index.ts b/components/common/date-picker/index.ts new file mode 100644 index 00000000..85c0c259 --- /dev/null +++ b/components/common/date-picker/index.ts @@ -0,0 +1,3 @@ +// 공용 날짜 선택기 컴포넌트 +export { DatePickerWithInput, type DatePickerWithInputProps } from './date-picker-with-input' + -- cgit v1.2.3 From 04c667f1a3a2ed8daf84a04a593f1c9ffc36f267 Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Thu, 4 Dec 2025 11:51:39 +0900 Subject: (김준회) i18n 적용 오류 수정 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/[lng]/evcp/(evcp)/(procurement)/vendor-investigation/page.tsx | 5 +++-- app/[lng]/evcp/(evcp)/edp-progress/page.tsx | 4 ++-- i18n/locales/en/menu.json | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/[lng]/evcp/(evcp)/(procurement)/vendor-investigation/page.tsx b/app/[lng]/evcp/(evcp)/(procurement)/vendor-investigation/page.tsx index e7109dcb..799c3b5a 100644 --- a/app/[lng]/evcp/(evcp)/(procurement)/vendor-investigation/page.tsx +++ b/app/[lng]/evcp/(evcp)/(procurement)/vendor-investigation/page.tsx @@ -13,13 +13,14 @@ import { InformationButton } from "@/components/information/information-button" import { useTranslation } from "@/i18n" interface IndexPageProps { searchParams: Promise + params: Promise<{ lng: string }> } export default async function IndexPage(props: IndexPageProps) { const searchParams = await props.searchParams const search = searchParamsInvestigationCache.parse(searchParams) - const {lng} = await props.params - const {t} = await useTranslation(lng, 'menu') + const { lng } = await props.params + const { t } = await useTranslation(lng, 'menu') const validFilters = getValidFilters(search.filters) diff --git a/app/[lng]/evcp/(evcp)/edp-progress/page.tsx b/app/[lng]/evcp/(evcp)/edp-progress/page.tsx index 9464c037..b205dd77 100644 --- a/app/[lng]/evcp/(evcp)/edp-progress/page.tsx +++ b/app/[lng]/evcp/(evcp)/edp-progress/page.tsx @@ -10,8 +10,8 @@ interface edpProgressPageProps { params: Promise<{ lng: string }> } -export default async function IndexPage({ params: edpProgressPageProps }) { - const { lng } = await params +export default async function IndexPage(props: edpProgressPageProps) { + const { lng } = await props.params const { t } = await useTranslation(lng, 'menu') return ( diff --git a/i18n/locales/en/menu.json b/i18n/locales/en/menu.json index bee0a946..e4835c37 100644 --- a/i18n/locales/en/menu.json +++ b/i18n/locales/en/menu.json @@ -94,8 +94,9 @@ "tbe": "TBE", "tbe_desc": "Technical Bid Evaluation", "itb": "RFQ Creation", - "itb_desc": "Create RFQ before PR Issue" - + "itb_desc": "Create RFQ before PR Issue", + "vendor_progress": "Vendor Progress", + "vendor_progress_desc": "Vendor EDP input progress" }, "vendor_management": { "title": "Vendor", -- cgit v1.2.3 From 5699e866201566366981ae8399a835fc7fa9fa47 Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Thu, 4 Dec 2025 12:19:53 +0900 Subject: (김준회) SWP 중복 문서 생성 불가 처리 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plant/document-stage-dialogs.tsx | 87 +++++++++++- .../plant/document-stages-service.ts | 158 ++++++++++++++++++++- 2 files changed, 241 insertions(+), 4 deletions(-) diff --git a/lib/vendor-document-list/plant/document-stage-dialogs.tsx b/lib/vendor-document-list/plant/document-stage-dialogs.tsx index d4e0ff33..b6cf6d7a 100644 --- a/lib/vendor-document-list/plant/document-stage-dialogs.tsx +++ b/lib/vendor-document-list/plant/document-stage-dialogs.tsx @@ -44,7 +44,8 @@ import { updateDocument, deleteDocuments, updateStage, - getDocumentClassOptionsByContract + getDocumentClassOptionsByContract, + checkDuplicateDocuments } from "./document-stages-service" import { type Row } from "@tanstack/react-table" @@ -127,6 +128,14 @@ export function AddDocumentDialog({ const [cpyTypeConfigs, setCpyTypeConfigs] = React.useState([]) const [cpyComboBoxOptions, setCpyComboBoxOptions] = React.useState>({}) + // Duplicate check states + const [duplicateWarning, setDuplicateWarning] = React.useState<{ + isDuplicate: boolean + type?: 'SHI_DOC_NO' | 'OWN_DOC_NO' | 'BOTH' + message?: string + }>({ isDuplicate: false }) + const [isCheckingDuplicate, setIsCheckingDuplicate] = React.useState(false) + // Initialize react-hook-form const form = useForm({ resolver: zodResolver(documentFormSchema), @@ -167,6 +176,7 @@ export function AddDocumentDialog({ setShiComboBoxOptions({}) setCpyComboBoxOptions({}) setDocumentClassOptions([]) + setDuplicateWarning({ isDuplicate: false }) } }, [open]) @@ -359,6 +369,59 @@ export function AddDocumentDialog({ return preview && preview !== '' && !preview.includes('[value]') } + // Real-time duplicate check with debounce + const checkDuplicateDebounced = React.useMemo(() => { + let timeoutId: NodeJS.Timeout | null = null + + return (shiDocNo: string, cpyDocNo: string) => { + if (timeoutId) { + clearTimeout(timeoutId) + } + + timeoutId = setTimeout(async () => { + // Skip if both are empty or incomplete + if ((!shiDocNo || shiDocNo.includes('[value]')) && + (!cpyDocNo || cpyDocNo.includes('[value]'))) { + setDuplicateWarning({ isDuplicate: false }) + return + } + + setIsCheckingDuplicate(true) + try { + const result = await checkDuplicateDocuments( + contractId, + shiDocNo && !shiDocNo.includes('[value]') ? shiDocNo : undefined, + cpyDocNo && !cpyDocNo.includes('[value]') ? cpyDocNo : undefined + ) + + if (result.isDuplicate) { + setDuplicateWarning({ + isDuplicate: true, + type: result.duplicateType, + message: result.message + }) + } else { + setDuplicateWarning({ isDuplicate: false }) + } + } catch (error) { + console.error('Duplicate check error:', error) + } finally { + setIsCheckingDuplicate(false) + } + }, 500) // 500ms debounce + } + }, [contractId]) + + // Trigger duplicate check when document numbers change + React.useEffect(() => { + const shiPreview = generateShiPreview() + const cpyPreview = generateCpyPreview() + + if (shiPreview || cpyPreview) { + checkDuplicateDebounced(shiPreview, cpyPreview) + } + }, [shiFieldValues, cpyFieldValues]) + const onSubmit = async (data: DocumentFormValues) => { // Validate that at least one document number is configured and complete if (shiType && !isShiComplete()) { @@ -520,6 +583,24 @@ export function AddDocumentDialog({
+ {/* Duplicate Warning Alert */} + {duplicateWarning.isDuplicate && ( + + + + {duplicateWarning.message} + + + )} + + {/* Checking Duplicate Indicator */} + {isCheckingDuplicate && ( +
+ + Checking for duplicates... +
+ )} + {/* SHI Document Number Card */} {shiType && ( @@ -719,7 +800,9 @@ export function AddDocumentDialog({ form.formState.isSubmitting || !hasAvailableTypes || (shiType && !isShiComplete()) || - (cpyType && !isCpyComplete()) + (cpyType && !isCpyComplete()) || + duplicateWarning.isDuplicate || + isCheckingDuplicate } > {form.formState.isSubmitting ? ( diff --git a/lib/vendor-document-list/plant/document-stages-service.ts b/lib/vendor-document-list/plant/document-stages-service.ts index ed4099b3..cf19eb41 100644 --- a/lib/vendor-document-list/plant/document-stages-service.ts +++ b/lib/vendor-document-list/plant/document-stages-service.ts @@ -878,6 +878,127 @@ interface CreateDocumentData { vendorDocNumber?: string } +// ═══════════════════════════════════════════════════════════════════════════════ +// 문서번호 중복 체크 함수 (SHI_DOC_NO / OWN_DOC_NO 각각 중복 방지) +// ═══════════════════════════════════════════════════════════════════════════════ +interface CheckDuplicateResult { + isDuplicate: boolean + duplicateType?: 'SHI_DOC_NO' | 'OWN_DOC_NO' | 'BOTH' + existingDocNumbers?: { + shiDocNo?: string + ownDocNo?: string + } + message?: string +} + +/** + * 프로젝트 내에서 SHI_DOC_NO (docNumber)와 OWN_DOC_NO (vendorDocNumber) 중복 체크 + * @param contractId 계약 ID (프로젝트 ID를 가져오기 위함) + * @param shiDocNo SHI 문서번호 (docNumber) + * @param ownDocNo CPY 문서번호 (vendorDocNumber) + * @param excludeDocumentId 수정 시 제외할 문서 ID (선택) + */ +export async function checkDuplicateDocuments( + contractId: number, + shiDocNo?: string, + ownDocNo?: string, + excludeDocumentId?: number +): Promise { + try { + // 1. 계약에서 프로젝트 ID 가져오기 + const contract = await db.query.contracts.findFirst({ + where: eq(contracts.id, contractId), + columns: { projectId: true }, + }) + + if (!contract) { + return { isDuplicate: false, message: "유효하지 않은 계약입니다." } + } + + const { projectId } = contract + let shiDuplicate = false + let ownDuplicate = false + const existingDocNumbers: { shiDocNo?: string; ownDocNo?: string } = {} + + // 2. SHI_DOC_NO 중복 체크 (docNumber) + if (shiDocNo && shiDocNo.trim() !== '') { + const shiConditions = [ + eq(stageDocuments.projectId, projectId), + eq(stageDocuments.docNumber, shiDocNo.trim()), + eq(stageDocuments.status, "ACTIVE"), + ] + + if (excludeDocumentId) { + shiConditions.push(ne(stageDocuments.id, excludeDocumentId)) + } + + const existingShiDoc = await db + .select({ id: stageDocuments.id, docNumber: stageDocuments.docNumber }) + .from(stageDocuments) + .where(and(...shiConditions)) + .limit(1) + + if (existingShiDoc.length > 0) { + shiDuplicate = true + existingDocNumbers.shiDocNo = existingShiDoc[0].docNumber + } + } + + // 3. OWN_DOC_NO 중복 체크 (vendorDocNumber) + if (ownDocNo && ownDocNo.trim() !== '') { + const ownConditions = [ + eq(stageDocuments.projectId, projectId), + eq(stageDocuments.vendorDocNumber, ownDocNo.trim()), + eq(stageDocuments.status, "ACTIVE"), + ] + + if (excludeDocumentId) { + ownConditions.push(ne(stageDocuments.id, excludeDocumentId)) + } + + const existingOwnDoc = await db + .select({ id: stageDocuments.id, vendorDocNumber: stageDocuments.vendorDocNumber }) + .from(stageDocuments) + .where(and(...ownConditions)) + .limit(1) + + if (existingOwnDoc.length > 0) { + ownDuplicate = true + existingDocNumbers.ownDocNo = existingOwnDoc[0].vendorDocNumber || undefined + } + } + + // 4. 결과 반환 + if (shiDuplicate && ownDuplicate) { + return { + isDuplicate: true, + duplicateType: 'BOTH', + existingDocNumbers, + message: `SHI Document Number '${shiDocNo}' and CPY Document Number '${ownDocNo}' already exist in this project.` + } + } else if (shiDuplicate) { + return { + isDuplicate: true, + duplicateType: 'SHI_DOC_NO', + existingDocNumbers, + message: `SHI Document Number '${shiDocNo}' already exists in this project.` + } + } else if (ownDuplicate) { + return { + isDuplicate: true, + duplicateType: 'OWN_DOC_NO', + existingDocNumbers, + message: `CPY Document Number '${ownDocNo}' already exists in this project.` + } + } + + return { isDuplicate: false } + } catch (error) { + console.error("중복 체크 실패:", error) + return { isDuplicate: false, message: "중복 체크 중 오류가 발생했습니다." } + } +} + // 문서 생성 export async function createDocument(data: CreateDocumentData) { try { @@ -907,6 +1028,20 @@ export async function createDocument(data: CreateDocumentData) { return { success: false, error: configsResult.error } } + /* ──────────────────────────────── 2. 중복 체크 (SHI_DOC_NO & OWN_DOC_NO) ─────────────────────────────── */ + const duplicateCheck = await checkDuplicateDocuments( + data.contractId, + data.docNumber, + data.vendorDocNumber + ) + + if (duplicateCheck.isDuplicate) { + return { + success: false, + error: duplicateCheck.message || "Document number already exists in this project.", + duplicateType: duplicateCheck.duplicateType, + } + } /* ──────────────────────────────── 3. 문서 레코드 삽입 ─────────────────────────────── */ const insertData = { @@ -1403,7 +1538,7 @@ export async function uploadImportData(data: UploadData) { try { // 개별 트랜잭션으로 각 문서 처리 const result = await db.transaction(async (tx) => { - // 먼저 문서가 이미 존재하는지 확인 + // 먼저 SHI_DOC_NO (docNumber)가 이미 존재하는지 확인 const [existingDoc] = await tx .select({ id: stageDocuments.id }) .from(stageDocuments) @@ -1417,7 +1552,26 @@ export async function uploadImportData(data: UploadData) { .limit(1) if (existingDoc) { - throw new Error(`문서번호 "${doc.docNumber}"가 이미 존재합니다`) + throw new Error(`SHI Document Number "${doc.docNumber}" already exists in this project`) + } + + // OWN_DOC_NO (vendorDocNumber) 중복 체크 + if (doc.vendorDocNumber && doc.vendorDocNumber.trim() !== '') { + const [existingVendorDoc] = await tx + .select({ id: stageDocuments.id, vendorDocNumber: stageDocuments.vendorDocNumber }) + .from(stageDocuments) + .where( + and( + eq(stageDocuments.projectId, contract.projectId), + eq(stageDocuments.vendorDocNumber, doc.vendorDocNumber.trim()), + eq(stageDocuments.status, "ACTIVE") + ) + ) + .limit(1) + + if (existingVendorDoc) { + throw new Error(`CPY Document Number "${doc.vendorDocNumber}" already exists in this project`) + } } // 3-1. 문서 생성 -- cgit v1.2.3 From 25749225689c3934bc10ad1e8285e13020b61282 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Thu, 4 Dec 2025 09:04:09 +0000 Subject: (최겸)구매 입찰, 계약 수정 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bidding/manage/bidding-companies-editor.tsx | 262 ++++++++++- components/bidding/manage/bidding-items-editor.tsx | 181 +++++++- .../bidding/manage/create-pre-quote-rfq-dialog.tsx | 38 +- .../procurement-item-selector-dialog-single.tsx | 4 +- db/schema/bidding.ts | 2 +- lib/bidding/actions.ts | 8 + lib/bidding/approval-actions.ts | 2 + lib/bidding/detail/service.ts | 151 +++---- .../detail/table/bidding-detail-vendor-table.tsx | 5 +- .../bidding-detail-vendor-toolbar-actions.tsx | 195 +++------ lib/bidding/handlers.ts | 12 +- .../list/biddings-table-toolbar-actions.tsx | 54 ++- lib/bidding/list/export-biddings-to-excel.ts | 212 +++++++++ .../manage/export-bidding-items-to-excel.ts | 161 +++++++ .../manage/import-bidding-items-from-excel.ts | 271 ++++++++++++ lib/bidding/manage/project-utils.ts | 87 ++++ lib/bidding/selection/actions.ts | 69 +++ lib/bidding/selection/bidding-info-card.tsx | 2 +- lib/bidding/selection/bidding-item-table.tsx | 192 +++++++++ .../selection/bidding-selection-detail-content.tsx | 11 +- lib/bidding/selection/biddings-selection-table.tsx | 6 +- lib/bidding/selection/selection-result-form.tsx | 213 +++++++-- lib/bidding/selection/vendor-selection-table.tsx | 4 +- lib/bidding/service.ts | 133 +++++- .../vendor/components/pr-items-pricing-table.tsx | 18 +- .../vendor/export-partners-biddings-to-excel.ts | 278 ++++++++++++ .../vendor/partners-bidding-list-columns.tsx | 48 +-- .../vendor/partners-bidding-toolbar-actions.tsx | 34 +- .../detail/general-contract-basic-info.tsx | 478 +++++++++++++++------ .../detail/general-contract-items-table.tsx | 43 +- lib/general-contracts/service.ts | 11 +- lib/procurement-items/service.ts | 15 +- lib/soap/ecc/mapper/bidding-and-pr-mapper.ts | 15 + 33 files changed, 2728 insertions(+), 487 deletions(-) create mode 100644 lib/bidding/list/export-biddings-to-excel.ts create mode 100644 lib/bidding/manage/export-bidding-items-to-excel.ts create mode 100644 lib/bidding/manage/import-bidding-items-from-excel.ts create mode 100644 lib/bidding/manage/project-utils.ts create mode 100644 lib/bidding/selection/bidding-item-table.tsx create mode 100644 lib/bidding/vendor/export-partners-biddings-to-excel.ts diff --git a/components/bidding/manage/bidding-companies-editor.tsx b/components/bidding/manage/bidding-companies-editor.tsx index 6634f528..4c3e6bbc 100644 --- a/components/bidding/manage/bidding-companies-editor.tsx +++ b/components/bidding/manage/bidding-companies-editor.tsx @@ -1,7 +1,7 @@ 'use client' import * as React from 'react' -import { Building, User, Plus, Trash2 } from 'lucide-react' +import { Building, User, Plus, Trash2, Users } from 'lucide-react' import { toast } from 'sonner' import { Button } from '@/components/ui/button' @@ -11,7 +11,9 @@ import { createBiddingCompanyContact, deleteBiddingCompanyContact, getVendorContactsByVendorId, - updateBiddingCompanyPriceAdjustmentQuestion + updateBiddingCompanyPriceAdjustmentQuestion, + getBiddingCompaniesByBidPicId, + addBiddingCompanyFromOtherBidding } from '@/lib/bidding/service' import { deleteBiddingCompany } from '@/lib/bidding/pre-quote/service' import { BiddingDetailVendorCreateDialog } from './bidding-detail-vendor-create-dialog' @@ -36,6 +38,7 @@ import { } from '@/components/ui/table' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' +import { PurchaseGroupCodeSelector, PurchaseGroupCodeWithUser } from '@/components/common/selectors/purchase-group-code/purchase-group-code-selector' interface QuotationVendor { id: number // biddingCompanies.id @@ -102,6 +105,26 @@ export function BiddingCompaniesEditor({ biddingId, readonly = false }: BiddingC const [isLoadingVendorContacts, setIsLoadingVendorContacts] = React.useState(false) const [selectedContactFromVendor, setSelectedContactFromVendor] = React.useState(null) + // 협력사 멀티 선택 다이얼로그 + const [multiSelectDialogOpen, setMultiSelectDialogOpen] = React.useState(false) + const [selectedBidPic, setSelectedBidPic] = React.useState(undefined) + const [biddingCompaniesList, setBiddingCompaniesList] = React.useState>([]) + const [isLoadingBiddingCompanies, setIsLoadingBiddingCompanies] = React.useState(false) + const [selectedBiddingCompany, setSelectedBiddingCompany] = React.useState<{ + biddingId: number + companyId: number + } | null>(null) + const [selectedBiddingCompanyContacts, setSelectedBiddingCompanyContacts] = React.useState([]) + const [isLoadingCompanyContacts, setIsLoadingCompanyContacts] = React.useState(false) + // 업체 목록 다시 로딩 함수 const reloadVendors = React.useCallback(async () => { try { @@ -494,10 +517,16 @@ export function BiddingCompaniesEditor({ biddingId, readonly = false }: BiddingC

{!readonly && ( - +
+ + +
)} @@ -740,6 +769,227 @@ export function BiddingCompaniesEditor({ biddingId, readonly = false }: BiddingC + {/* 협력사 멀티 선택 다이얼로그 */} + + + + 참여협력사 선택 + + 입찰담당자를 선택하여 해당 담당자의 입찰 업체를 조회하고 선택할 수 있습니다. + + + +
+ {/* 입찰담당자 선택 */} +
+ + { + setSelectedBidPic(code) + if (code.user?.id) { + setIsLoadingBiddingCompanies(true) + try { + const result = await getBiddingCompaniesByBidPicId(code.user.id) + if (result.success && result.data) { + setBiddingCompaniesList(result.data) + } else { + toast.error(result.error || '입찰 업체 조회에 실패했습니다.') + setBiddingCompaniesList([]) + } + } catch (error) { + console.error('Failed to load bidding companies:', error) + toast.error('입찰 업체 조회에 실패했습니다.') + setBiddingCompaniesList([]) + } finally { + setIsLoadingBiddingCompanies(false) + } + } + }} + placeholder="입찰담당자 선택" + disabled={readonly} + /> +
+ + {/* 입찰 업체 목록 */} + {isLoadingBiddingCompanies ? ( +
+ + 입찰 업체를 불러오는 중... +
+ ) : biddingCompaniesList.length === 0 && selectedBidPic ? ( +
+ 해당 입찰담당자의 입찰 업체가 없습니다. +
+ ) : biddingCompaniesList.length > 0 ? ( +
+ + + + 선택 + 입찰번호 + 입찰명 + 협력사코드 + 협력사명 + 입찰 업데이트일 + + + + {biddingCompaniesList.map((company) => { + const isSelected = selectedBiddingCompany?.biddingId === company.biddingId && + selectedBiddingCompany?.companyId === company.companyId + return ( + { + if (isSelected) { + setSelectedBiddingCompany(null) + setSelectedBiddingCompanyContacts([]) + return + } + setSelectedBiddingCompany({ + biddingId: company.biddingId, + companyId: company.companyId + }) + setIsLoadingCompanyContacts(true) + try { + const contactsResult = await getBiddingCompanyContacts(company.biddingId, company.companyId) + if (contactsResult.success && contactsResult.data) { + setSelectedBiddingCompanyContacts(contactsResult.data) + } else { + setSelectedBiddingCompanyContacts([]) + } + } catch (error) { + console.error('Failed to load company contacts:', error) + setSelectedBiddingCompanyContacts([]) + } finally { + setIsLoadingCompanyContacts(false) + } + }} + > + e.stopPropagation()}> + { + // 클릭 이벤트는 TableRow의 onClick에서 처리 + }} + disabled={readonly} + /> + + {company.biddingNumber} + {company.biddingTitle} + {company.vendorCode} + {company.vendorName} + + {company.updatedAt ? new Date(company.updatedAt).toLocaleDateString('ko-KR') : '-'} + + + ) + })} + +
+ + {/* 선택한 입찰 업체의 담당자 정보 */} + {selectedBiddingCompany !== null && ( +
+

담당자 정보

+ {isLoadingCompanyContacts ? ( +
+ + 담당자 정보를 불러오는 중... +
+ ) : selectedBiddingCompanyContacts.length === 0 ? ( +
등록된 담당자가 없습니다.
+ ) : ( +
+ {selectedBiddingCompanyContacts.map((contact) => ( +
+ {contact.contactName} + {contact.contactEmail} + {contact.contactNumber && ( + {contact.contactNumber} + )} +
+ ))} +
+ )} +
+ )} +
+ ) : null} +
+ + + + + +
+
+ {/* 벤더 담당자에서 추가 다이얼로그 */} diff --git a/components/bidding/manage/bidding-items-editor.tsx b/components/bidding/manage/bidding-items-editor.tsx index 90e512d2..452cdc3c 100644 --- a/components/bidding/manage/bidding-items-editor.tsx +++ b/components/bidding/manage/bidding-items-editor.tsx @@ -1,7 +1,7 @@ 'use client' import * as React from 'react' -import { Package, Plus, Trash2, Save, RefreshCw, FileText } from 'lucide-react' +import { Package, Plus, Trash2, Save, RefreshCw, FileText, FileSpreadsheet, Upload } from 'lucide-react' import { getPRItemsForBidding } from '@/lib/bidding/detail/service' import { updatePrItem } from '@/lib/bidding/detail/service' import { toast } from 'sonner' @@ -26,7 +26,7 @@ import { CostCenterSingleSelector } from '@/components/common/selectors/cost-cen import { GlAccountSingleSelector } from '@/components/common/selectors/gl-account/gl-account-single-selector' // PR 아이템 정보 타입 (create-bidding-dialog와 동일) -interface PRItemInfo { +export interface PRItemInfo { id: number // 실제 DB ID prNumber?: string | null projectId?: number | null @@ -84,6 +84,16 @@ import { CreatePreQuoteRfqDialog } from './create-pre-quote-rfq-dialog' import { ProcurementItemSelectorDialogSingle } from '@/components/common/selectors/procurement-item/procurement-item-selector-dialog-single' import { Textarea } from '@/components/ui/textarea' import { Label } from '@/components/ui/label' +import { exportBiddingItemsToExcel } from '@/lib/bidding/manage/export-bidding-items-to-excel' +import { importBiddingItemsFromExcel } from '@/lib/bidding/manage/import-bidding-items-from-excel' +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from '@/components/ui/dialog' export function BiddingItemsEditor({ biddingId, readonly = false }: BiddingItemsEditorProps) { const { data: session } = useSession() @@ -114,6 +124,11 @@ export function BiddingItemsEditor({ biddingId, readonly = false }: BiddingItems isPriceAdjustmentApplicable?: boolean | null sparePartOptions?: string | null } | null>(null) + const [importDialogOpen, setImportDialogOpen] = React.useState(false) + const [importFile, setImportFile] = React.useState(null) + const [importErrors, setImportErrors] = React.useState([]) + const [isImporting, setIsImporting] = React.useState(false) + const [isExporting, setIsExporting] = React.useState(false) // 초기 데이터 로딩 - 기존 품목이 있으면 자동으로 로드 React.useEffect(() => { @@ -492,7 +507,7 @@ export function BiddingItemsEditor({ biddingId, readonly = false }: BiddingItems materialGroupInfo: null, materialNumber: null, materialInfo: null, - priceUnit: 1, + priceUnit: '1', purchaseUnit: 'EA', materialWeight: null, wbsCode: null, @@ -644,6 +659,76 @@ export function BiddingItemsEditor({ biddingId, readonly = false }: BiddingItems const totals = calculateTotals() + // Excel 내보내기 핸들러 + const handleExport = React.useCallback(async () => { + if (items.length === 0) { + toast.error('내보낼 품목이 없습니다.') + return + } + + try { + setIsExporting(true) + await exportBiddingItemsToExcel(items, { + filename: `입찰품목목록_${biddingId}`, + }) + toast.success('Excel 파일이 다운로드되었습니다.') + } catch (error) { + console.error('Excel export error:', error) + toast.error('Excel 내보내기 중 오류가 발생했습니다.') + } finally { + setIsExporting(false) + } + }, [items, biddingId]) + + // Excel 가져오기 핸들러 + const handleImportFileSelect = (event: React.ChangeEvent) => { + const file = event.target.files?.[0] + if (file) { + if (!file.name.endsWith('.xlsx') && !file.name.endsWith('.xls')) { + toast.error('Excel 파일(.xlsx, .xls)만 업로드 가능합니다.') + return + } + setImportFile(file) + setImportErrors([]) + } + } + + const handleImport = async () => { + if (!importFile) return + + setIsImporting(true) + setImportErrors([]) + + try { + const result = await importBiddingItemsFromExcel(importFile) + + if (result.errors.length > 0) { + setImportErrors(result.errors) + toast.warning( + `${result.items.length}개의 품목을 파싱했지만 ${result.errors.length}개의 오류가 있습니다.` + ) + return + } + + if (result.items.length === 0) { + toast.error('가져올 품목이 없습니다.') + return + } + + // 기존 아이템에 추가 + setItems((prev) => [...prev, ...result.items]) + setImportDialogOpen(false) + setImportFile(null) + setImportErrors([]) + toast.success(`${result.items.length}개의 품목이 추가되었습니다.`) + } catch (error) { + console.error('Excel import error:', error) + toast.error('Excel 가져오기 중 오류가 발생했습니다.') + } finally { + setIsImporting(false) + } + } + if (isLoading) { return (
@@ -1372,6 +1457,14 @@ export function BiddingItemsEditor({ biddingId, readonly = false }: BiddingItems 사전견적 + + + + + +
) diff --git a/components/bidding/manage/create-pre-quote-rfq-dialog.tsx b/components/bidding/manage/create-pre-quote-rfq-dialog.tsx index de3c19ff..1ab7a40f 100644 --- a/components/bidding/manage/create-pre-quote-rfq-dialog.tsx +++ b/components/bidding/manage/create-pre-quote-rfq-dialog.tsx @@ -465,7 +465,7 @@ export function CreatePreQuoteRfqDialog({ )} > {field.value ? ( - format(field.value, "yyyy-MM-dd") + format(field.value, "yyyy-MM-dd HH:mm") ) : ( 제출마감일을 선택하세요 (선택) )} @@ -477,12 +477,40 @@ export function CreatePreQuoteRfqDialog({ - date < new Date() || date < new Date("1900-01-01") - } + onSelect={(date) => { + if (!date) { + field.onChange(undefined) + return + } + const newDate = new Date(date) + if (field.value) { + newDate.setHours(field.value.getHours(), field.value.getMinutes()) + } else { + newDate.setHours(0, 0, 0, 0) + } + field.onChange(newDate) + }} + disabled={(date) => { + const today = new Date() + today.setHours(0, 0, 0, 0) + return date < today || date < new Date("1900-01-01") + }} initialFocus /> +
+ { + if (field.value) { + const [hours, minutes] = e.target.value.split(':').map(Number) + const newDate = new Date(field.value) + newDate.setHours(hours, minutes) + field.onChange(newDate) + } + }} + /> +
diff --git a/components/common/selectors/procurement-item/procurement-item-selector-dialog-single.tsx b/components/common/selectors/procurement-item/procurement-item-selector-dialog-single.tsx index 84fd85ff..a1b98468 100644 --- a/components/common/selectors/procurement-item/procurement-item-selector-dialog-single.tsx +++ b/components/common/selectors/procurement-item/procurement-item-selector-dialog-single.tsx @@ -23,6 +23,7 @@ export interface ProcurementItemSelectorDialogSingleProps { title?: string; description?: string; showConfirmButtons?: boolean; + disabled?: boolean; } /** @@ -78,6 +79,7 @@ export function ProcurementItemSelectorDialogSingle({ title = "1회성 품목 선택", description = "1회성 품목을 검색하고 선택해주세요.", showConfirmButtons = false, + disabled = false, }: ProcurementItemSelectorDialogSingleProps) { const [open, setOpen] = useState(false); const [tempSelectedProcurementItem, setTempSelectedProcurementItem] = @@ -128,7 +130,7 @@ export function ProcurementItemSelectorDialogSingle({ return ( - - )} - - {/* 발주비율 산정: single select 시에만 활성화 */} - {(bidding.status === 'evaluation_of_bidding') && ( - - )} - - {/* 유찰/낙찰: 입찰공고 또는 입찰평가중 상태에서만 */} - {(bidding.status === 'bidding_opened' || bidding.status === 'evaluation_of_bidding') && ( + {/* 상태별 액션 버튼 - 읽기 전용이 아닐 때만 표시 */} + {!readOnly && ( <> - - + {/* 차수증가: 입찰공고 또는 입찰평가중 상태에서만 */} + {(bidding.status === 'evaluation_of_bidding' || bidding.status === 'bidding_opened') && ( + + )} + + {/* 발주비율 산정: single select 시에만 활성화 */} + {(bidding.status === 'evaluation_of_bidding') && ( + + )} + + {/* 유찰/낙찰: 입찰공고 또는 입찰평가중 상태에서만 */} + {(bidding.status === 'bidding_opened' || bidding.status === 'evaluation_of_bidding') && ( + <> + + + + )} + + {/* 발주비율 취소: 100% 낙찰된 벤더가 있는 경우 */} + {winnerVendor && ( + + )} )} - {/* 발주비율 취소: 100% 낙찰된 벤더가 있는 경우 */} - {winnerVendor && ( - - )} {/* 구분선 */} {(bidding.status === 'bidding_generated' || bidding.status === 'bidding_disposal') && ( diff --git a/lib/bidding/handlers.ts b/lib/bidding/handlers.ts index 11955a39..c64d9527 100644 --- a/lib/bidding/handlers.ts +++ b/lib/bidding/handlers.ts @@ -127,6 +127,7 @@ export async function mapBiddingInvitationToTemplateVariables(payload: { biddingNumber: string; projectName?: string; itemName?: string; + awardCount: string; biddingType: string; bidPicName?: string; supplyPicName?: string; @@ -181,7 +182,7 @@ export async function mapBiddingInvitationToTemplateVariables(payload: { const { bidding, biddingItems, vendors, message, specificationMeeting, requestedAt } = payload; // 제목 - const title = bidding.title || '입찰'; + const title = bidding.title || ''; // 입찰명 const biddingTitle = bidding.title || ''; @@ -190,7 +191,7 @@ export async function mapBiddingInvitationToTemplateVariables(payload: { const biddingNumber = bidding.biddingNumber || ''; // 낙찰업체수 - const winnerCount = '1'; // 기본값, 실제로는 bidding 설정에서 가져와야 함 + const awardCount = bidding.awardCount || ''; // 계약구분 const contractType = bidding.biddingType || ''; @@ -199,7 +200,7 @@ export async function mapBiddingInvitationToTemplateVariables(payload: { const prNumber = ''; // 예산 - const budget = bidding.targetPrice ? bidding.targetPrice.toLocaleString() : ''; + const budget = bidding.budget ? bidding.budget.toLocaleString() : ''; // 내정가 const targetPrice = bidding.targetPrice ? bidding.targetPrice.toLocaleString() : ''; @@ -272,7 +273,7 @@ export async function mapBiddingInvitationToTemplateVariables(payload: { 제목: title, 입찰명: biddingTitle, 입찰번호: biddingNumber, - 낙찰업체수: winnerCount, + 낙찰업체수: awardCount, 계약구분: contractType, 'P/R번호': prNumber, 예산: budget, @@ -637,6 +638,7 @@ export async function mapBiddingAwardToTemplateVariables(payload: { biddingType: biddings.biddingType, bidPicName: biddings.bidPicName, supplyPicName: biddings.supplyPicName, + budget: biddings.budget, targetPrice: biddings.targetPrice, awardCount: biddings.awardCount, }) @@ -684,7 +686,7 @@ export async function mapBiddingAwardToTemplateVariables(payload: { const biddingNumber = bidding.biddingNumber || ''; const winnerCount = (bidding.awardCount === 'single' ? 1 : bidding.awardCount === 'multiple' ? 2 : 1).toString(); const contractType = bidding.biddingType || ''; - const budget = bidding.targetPrice ? bidding.targetPrice.toLocaleString() : ''; + const budget = bidding.budget ? bidding.budget.toLocaleString() : ''; const targetPrice = bidding.targetPrice ? bidding.targetPrice.toLocaleString() : ''; const biddingManager = bidding.bidPicName || bidding.supplyPicName || ''; const biddingOverview = bidding.itemName || ''; diff --git a/lib/bidding/list/biddings-table-toolbar-actions.tsx b/lib/bidding/list/biddings-table-toolbar-actions.tsx index 33368218..b0007c8c 100644 --- a/lib/bidding/list/biddings-table-toolbar-actions.tsx +++ b/lib/bidding/list/biddings-table-toolbar-actions.tsx @@ -7,7 +7,7 @@ import { } from "lucide-react" import { toast } from "sonner" import { useSession } from "next-auth/react" -import { exportTableToExcel } from "@/lib/export" +import { exportBiddingsToExcel } from "./export-biddings-to-excel" import { Button } from "@/components/ui/button" import { DropdownMenu, @@ -92,6 +92,23 @@ export function BiddingsTableToolbarActions({ table }: BiddingsTableToolbarActio return selectedBiddings.length === 1 && selectedBiddings[0].status === 'bidding_generated' }, [selectedBiddings]) + // Excel 내보내기 핸들러 + const handleExport = React.useCallback(async () => { + try { + setIsExporting(true) + await exportBiddingsToExcel(table, { + filename: "입찰목록", + onlySelected: false, + }) + toast.success("Excel 파일이 다운로드되었습니다.") + } catch (error) { + console.error("Excel export error:", error) + toast.error("Excel 내보내기 중 오류가 발생했습니다.") + } finally { + setIsExporting(false) + } + }, [table]) + return ( <>
@@ -100,6 +117,17 @@ export function BiddingsTableToolbarActions({ table }: BiddingsTableToolbarActio // 성공 시 테이블 새로고침 등 추가 작업 // window.location.reload() }} /> + {/* Excel 내보내기 버튼 */} + {/* 전송하기 (업체선정 완료된 입찰만) */} {/* 삭제 버튼 */} - - - - - +
{/* 전송 다이얼로그 */} diff --git a/lib/bidding/list/export-biddings-to-excel.ts b/lib/bidding/list/export-biddings-to-excel.ts new file mode 100644 index 00000000..8b13e38d --- /dev/null +++ b/lib/bidding/list/export-biddings-to-excel.ts @@ -0,0 +1,212 @@ +import { type Table } from "@tanstack/react-table" +import ExcelJS from "exceljs" +import { BiddingListItem } from "@/db/schema" +import { + biddingStatusLabels, + contractTypeLabels, + biddingTypeLabels, +} from "@/db/schema" +import { formatDate } from "@/lib/utils" + +// BiddingListItem 확장 타입 (manager 정보 포함) +type BiddingListItemWithManagerCode = BiddingListItem & { + bidPicName?: string | null + supplyPicName?: string | null +} + +/** + * 입찰 목록을 Excel로 내보내기 + * - 계약구분, 진행상태, 입찰유형은 라벨(명칭)로 변환 + * - 입찰서 제출기간은 submissionStartDate, submissionEndDate 기준 + * - 등록일시는 년, 월, 일 형식 + */ +export async function exportBiddingsToExcel( + table: Table, + { + filename = "입찰목록", + onlySelected = false, + }: { + filename?: string + onlySelected?: boolean + } = {} +): Promise { + // 테이블에서 실제 사용 중인 leaf columns 가져오기 + const allColumns = table.getAllLeafColumns() + + // select, actions 컬럼 제외 + const columns = allColumns.filter( + (col) => !["select", "actions"].includes(col.id) + ) + + // 헤더 행 생성 (excelHeader 사용) + const headerRow = columns.map((col) => { + const excelHeader = (col.columnDef.meta as any)?.excelHeader + return typeof excelHeader === "string" ? excelHeader : col.id + }) + + // 데이터 행 생성 + const rowModel = onlySelected + ? table.getFilteredSelectedRowModel() + : table.getRowModel() + + const dataRows = rowModel.rows.map((row) => { + const original = row.original + return columns.map((col) => { + const colId = col.id + let value: any + + // 특별 처리 필요한 컬럼들 + switch (colId) { + case "contractType": + // 계약구분: 라벨로 변환 + value = contractTypeLabels[original.contractType as keyof typeof contractTypeLabels] || original.contractType + break + + case "status": + // 진행상태: 라벨로 변환 + value = biddingStatusLabels[original.status as keyof typeof biddingStatusLabels] || original.status + break + + case "biddingType": + // 입찰유형: 라벨로 변환 + value = biddingTypeLabels[original.biddingType as keyof typeof biddingTypeLabels] || original.biddingType + break + + case "submissionPeriod": + // 입찰서 제출기간: submissionStartDate, submissionEndDate 기준 + const startDate = original.submissionStartDate + const endDate = original.submissionEndDate + + if (!startDate || !endDate) { + value = "-" + } else { + const startObj = new Date(startDate) + const endObj = new Date(endDate) + + // KST 변환 (UTC+9) + const formatKst = (d: Date) => { + const kstDate = new Date(d.getTime() + 9 * 60 * 60 * 1000) + return kstDate.toISOString().slice(0, 16).replace('T', ' ') + } + + value = `${formatKst(startObj)} ~ ${formatKst(endObj)}` + } + break + + case "updatedAt": + // 등록일시: 년, 월, 일 형식만 + if (original.updatedAt) { + value = formatDate(original.updatedAt, "KR") + } else { + value = "-" + } + break + + case "biddingRegistrationDate": + // 입찰등록일: 년, 월, 일 형식만 + if (original.biddingRegistrationDate) { + value = formatDate(original.biddingRegistrationDate, "KR") + } else { + value = "-" + } + break + + case "projectName": + // 프로젝트: 코드와 이름 조합 + const code = original.projectCode + const name = original.projectName + value = code && name ? `${code} (${name})` : (code || name || "-") + break + + case "hasSpecificationMeeting": + // 사양설명회: Yes/No + value = original.hasSpecificationMeeting ? "Yes" : "No" + break + + default: + // 기본값: row.getValue 사용 + value = row.getValue(colId) + + // null/undefined 처리 + if (value == null) { + value = "" + } + + // 객체인 경우 JSON 문자열로 변환 + if (typeof value === "object") { + value = JSON.stringify(value) + } + break + } + + return value + }) + }) + + // 최종 sheetData + const sheetData = [headerRow, ...dataRows] + + // ExcelJS로 파일 생성 및 다운로드 + await createAndDownloadExcel(sheetData, columns.length, filename) +} + +/** + * Excel 파일 생성 및 다운로드 + */ +async function createAndDownloadExcel( + sheetData: any[][], + columnCount: number, + filename: string +): Promise { + // ExcelJS 워크북/시트 생성 + const workbook = new ExcelJS.Workbook() + const worksheet = workbook.addWorksheet("Sheet1") + + // 칼럼별 최대 길이 추적 + const maxColumnLengths = Array(columnCount).fill(0) + sheetData.forEach((row) => { + row.forEach((cellValue, colIdx) => { + const cellText = cellValue?.toString() ?? "" + if (cellText.length > maxColumnLengths[colIdx]) { + maxColumnLengths[colIdx] = cellText.length + } + }) + }) + + // 시트에 데이터 추가 + 헤더 스타일 + sheetData.forEach((arr, idx) => { + const row = worksheet.addRow(arr) + + // 헤더 스타일 적용 (첫 번째 행) + if (idx === 0) { + row.font = { bold: true } + row.alignment = { horizontal: "center" } + row.eachCell((cell) => { + cell.fill = { + type: "pattern", + pattern: "solid", + fgColor: { argb: "FFCCCCCC" }, + } + }) + } + }) + + // 칼럼 너비 자동 조정 + maxColumnLengths.forEach((len, idx) => { + // 최소 너비 10, +2 여백 + worksheet.getColumn(idx + 1).width = Math.max(len + 2, 10) + }) + + // 최종 파일 다운로드 + const buffer = await workbook.xlsx.writeBuffer() + const blob = new Blob([buffer], { + type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + }) + const url = URL.createObjectURL(blob) + const link = document.createElement("a") + link.href = url + link.download = `${filename}.xlsx` + link.click() + URL.revokeObjectURL(url) +} + diff --git a/lib/bidding/manage/export-bidding-items-to-excel.ts b/lib/bidding/manage/export-bidding-items-to-excel.ts new file mode 100644 index 00000000..814648a7 --- /dev/null +++ b/lib/bidding/manage/export-bidding-items-to-excel.ts @@ -0,0 +1,161 @@ +import ExcelJS from "exceljs" +import { PRItemInfo } from "@/components/bidding/manage/bidding-items-editor" +import { getProjectCodesByIds } from "./project-utils" + +/** + * 입찰품목 목록을 Excel로 내보내기 + */ +export async function exportBiddingItemsToExcel( + items: PRItemInfo[], + { + filename = "입찰품목목록", + }: { + filename?: string + } = {} +): Promise { + // 프로젝트 ID 목록 수집 + const projectIds = items + .map((item) => item.projectId) + .filter((id): id is number => id != null && id > 0) + + // 프로젝트 코드 맵 조회 + const projectCodeMap = await getProjectCodesByIds(projectIds) + + // 헤더 정의 + const headers = [ + "프로젝트코드", + "프로젝트명", + "자재그룹코드", + "자재그룹명", + "자재코드", + "자재명", + "수량", + "수량단위", + "중량", + "중량단위", + "납품요청일", + "가격단위", + "구매단위", + "자재순중량", + "내정단가", + "내정금액", + "내정통화", + "예산금액", + "예산통화", + "실적금액", + "실적통화", + "WBS코드", + "WBS명", + "코스트센터코드", + "코스트센터명", + "GL계정코드", + "GL계정명", + "PR번호", + ] + + // 데이터 행 생성 + const dataRows = items.map((item) => { + // 프로젝트 코드 조회 + const projectCode = item.projectId + ? projectCodeMap.get(item.projectId) || "" + : "" + + return [ + projectCode, + item.projectInfo || "", + item.materialGroupNumber || "", + item.materialGroupInfo || "", + item.materialNumber || "", + item.materialInfo || "", + item.quantity || "", + item.quantityUnit || "", + item.totalWeight || "", + item.weightUnit || "", + item.requestedDeliveryDate || "", + item.priceUnit || "", + item.purchaseUnit || "", + item.materialWeight || "", + item.targetUnitPrice || "", + item.targetAmount || "", + item.targetCurrency || "KRW", + item.budgetAmount || "", + item.budgetCurrency || "KRW", + item.actualAmount || "", + item.actualCurrency || "KRW", + item.wbsCode || "", + item.wbsName || "", + item.costCenterCode || "", + item.costCenterName || "", + item.glAccountCode || "", + item.glAccountName || "", + item.prNumber || "", + ] + }) + + // 최종 sheetData + const sheetData = [headers, ...dataRows] + + // ExcelJS로 파일 생성 및 다운로드 + await createAndDownloadExcel(sheetData, headers.length, filename) +} + +/** + * Excel 파일 생성 및 다운로드 + */ +async function createAndDownloadExcel( + sheetData: any[][], + columnCount: number, + filename: string +): Promise { + // ExcelJS 워크북/시트 생성 + const workbook = new ExcelJS.Workbook() + const worksheet = workbook.addWorksheet("Sheet1") + + // 칼럼별 최대 길이 추적 + const maxColumnLengths = Array(columnCount).fill(0) + sheetData.forEach((row) => { + row.forEach((cellValue, colIdx) => { + const cellText = cellValue?.toString() ?? "" + if (cellText.length > maxColumnLengths[colIdx]) { + maxColumnLengths[colIdx] = cellText.length + } + }) + }) + + // 시트에 데이터 추가 + 헤더 스타일 + sheetData.forEach((arr, idx) => { + const row = worksheet.addRow(arr) + + // 헤더 스타일 적용 (첫 번째 행) + if (idx === 0) { + row.font = { bold: true } + row.alignment = { horizontal: "center" } + row.eachCell((cell) => { + cell.fill = { + type: "pattern", + pattern: "solid", + fgColor: { argb: "FFCCCCCC" }, + } + }) + } + }) + + // 칼럼 너비 자동 조정 + maxColumnLengths.forEach((len, idx) => { + // 최소 너비 10, +2 여백 + worksheet.getColumn(idx + 1).width = Math.max(len + 2, 10) + }) + + // 최종 파일 다운로드 + const buffer = await workbook.xlsx.writeBuffer() + const blob = new Blob([buffer], { + type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + }) + const url = URL.createObjectURL(blob) + const link = document.createElement("a") + link.href = url + link.download = `${filename}.xlsx` + link.click() + URL.revokeObjectURL(url) +} + diff --git a/lib/bidding/manage/import-bidding-items-from-excel.ts b/lib/bidding/manage/import-bidding-items-from-excel.ts new file mode 100644 index 00000000..2e0dfe33 --- /dev/null +++ b/lib/bidding/manage/import-bidding-items-from-excel.ts @@ -0,0 +1,271 @@ +import ExcelJS from "exceljs" +import { PRItemInfo } from "@/components/bidding/manage/bidding-items-editor" +import { getProjectIdByCodeAndName } from "./project-utils" + +export interface ImportBiddingItemsResult { + success: boolean + items: PRItemInfo[] + errors: string[] +} + +/** + * Excel 파일에서 입찰품목 데이터 파싱 + */ +export async function importBiddingItemsFromExcel( + file: File +): Promise { + const errors: string[] = [] + const items: PRItemInfo[] = [] + + try { + const workbook = new ExcelJS.Workbook() + const arrayBuffer = await file.arrayBuffer() + await workbook.xlsx.load(arrayBuffer) + + const worksheet = workbook.worksheets[0] + if (!worksheet) { + return { + success: false, + items: [], + errors: ["Excel 파일에 시트가 없습니다."], + } + } + + // 헤더 행 읽기 (첫 번째 행) + const headerRow = worksheet.getRow(1) + const headerValues = headerRow.values as ExcelJS.CellValue[] + + // 헤더 매핑 생성 + const headerMap: Record = {} + const expectedHeaders = [ + "프로젝트코드", + "프로젝트명", + "자재그룹코드", + "자재그룹명", + "자재코드", + "자재명", + "수량", + "수량단위", + "중량", + "중량단위", + "납품요청일", + "가격단위", + "구매단위", + "자재순중량", + "내정단가", + "내정금액", + "내정통화", + "예산금액", + "예산통화", + "실적금액", + "실적통화", + "WBS코드", + "WBS명", + "코스트센터코드", + "코스트센터명", + "GL계정코드", + "GL계정명", + "PR번호", + ] + + // 헤더 인덱스 매핑 + for (let i = 1; i < headerValues.length; i++) { + const headerValue = String(headerValues[i] || "").trim() + if (headerValue && expectedHeaders.includes(headerValue)) { + headerMap[headerValue] = i + } + } + + // 필수 헤더 확인 + const requiredHeaders = ["자재그룹코드", "자재그룹명"] + const missingHeaders = requiredHeaders.filter( + (h) => !headerMap[h] + ) + if (missingHeaders.length > 0) { + errors.push( + `필수 컬럼이 없습니다: ${missingHeaders.join(", ")}` + ) + } + + // 데이터 행 읽기 (2번째 행부터) + for (let rowIndex = 2; rowIndex <= worksheet.rowCount; rowIndex++) { + const row = worksheet.getRow(rowIndex) + const rowValues = row.values as ExcelJS.CellValue[] + + // 빈 행 건너뛰기 + if (rowValues.every((val) => !val || String(val).trim() === "")) { + continue + } + + // 셀 값 추출 헬퍼 + const getCellValue = (headerName: string): string => { + const colIndex = headerMap[headerName] + if (!colIndex) return "" + const value = rowValues[colIndex] + if (value == null) return "" + + // ExcelJS 객체 처리 + if (typeof value === "object" && "text" in value) { + return String((value as any).text || "") + } + + // 날짜 처리 + if (value instanceof Date) { + return value.toISOString().split("T")[0] + } + + return String(value).trim() + } + + // 필수값 검증 + const materialGroupNumber = getCellValue("자재그룹코드") + const materialGroupInfo = getCellValue("자재그룹명") + + if (!materialGroupNumber || !materialGroupInfo) { + errors.push( + `${rowIndex}번 행: 자재그룹코드와 자재그룹명은 필수입니다.` + ) + continue + } + + // 수량 또는 중량 검증 + const quantity = getCellValue("수량") + const totalWeight = getCellValue("중량") + const quantityUnit = getCellValue("수량단위") + const weightUnit = getCellValue("중량단위") + + if (!quantity && !totalWeight) { + errors.push( + `${rowIndex}번 행: 수량 또는 중량 중 하나는 필수입니다.` + ) + continue + } + + if (quantity && !quantityUnit) { + errors.push( + `${rowIndex}번 행: 수량이 있으면 수량단위가 필수입니다.` + ) + continue + } + + if (totalWeight && !weightUnit) { + errors.push( + `${rowIndex}번 행: 중량이 있으면 중량단위가 필수입니다.` + ) + continue + } + + // 납품요청일 검증 + const requestedDeliveryDate = getCellValue("납품요청일") + if (!requestedDeliveryDate) { + errors.push( + `${rowIndex}번 행: 납품요청일은 필수입니다.` + ) + continue + } + + // 날짜 형식 검증 + const dateRegex = /^\d{4}-\d{2}-\d{2}$/ + if (requestedDeliveryDate && !dateRegex.test(requestedDeliveryDate)) { + errors.push( + `${rowIndex}번 행: 납품요청일 형식이 올바르지 않습니다. (YYYY-MM-DD 형식)` + ) + continue + } + + // 내정단가 검증 (필수) + const targetUnitPrice = getCellValue("내정단가") + if (!targetUnitPrice || parseFloat(targetUnitPrice.replace(/,/g, "")) <= 0) { + errors.push( + `${rowIndex}번 행: 내정단가는 필수이며 0보다 커야 합니다.` + ) + continue + } + + // 숫자 값 정리 (콤마 제거) + const cleanNumber = (value: string): string => { + return value.replace(/,/g, "").trim() + } + + // 프로젝트 ID 조회 (프로젝트코드와 프로젝트명으로) + const projectCode = getCellValue("프로젝트코드") + const projectName = getCellValue("프로젝트명") + let projectId: number | null = null + + if (projectCode && projectName) { + projectId = await getProjectIdByCodeAndName(projectCode, projectName) + if (!projectId) { + errors.push( + `${rowIndex}번 행: 프로젝트코드 "${projectCode}"와 프로젝트명 "${projectName}"에 해당하는 프로젝트를 찾을 수 없습니다.` + ) + // 프로젝트를 찾지 못해도 계속 진행 (경고만 표시) + } + } + + // PRItemInfo 객체 생성 + const item: PRItemInfo = { + id: -(rowIndex - 1), // 임시 ID (음수) + prNumber: getCellValue("PR번호") || null, + projectId: projectId, + projectInfo: projectName || null, + shi: null, + quantity: quantity ? cleanNumber(quantity) : null, + quantityUnit: quantityUnit || null, + totalWeight: totalWeight ? cleanNumber(totalWeight) : null, + weightUnit: weightUnit || null, + materialDescription: null, + hasSpecDocument: false, + requestedDeliveryDate: requestedDeliveryDate || null, + isRepresentative: false, + annualUnitPrice: null, + currency: "KRW", + materialGroupNumber: materialGroupNumber || null, + materialGroupInfo: materialGroupInfo || null, + materialNumber: getCellValue("자재코드") || null, + materialInfo: getCellValue("자재명") || null, + priceUnit: getCellValue("가격단위") || "1", + purchaseUnit: getCellValue("구매단위") || "EA", + materialWeight: getCellValue("자재순중량") || null, + wbsCode: getCellValue("WBS코드") || null, + wbsName: getCellValue("WBS명") || null, + costCenterCode: getCellValue("코스트센터코드") || null, + costCenterName: getCellValue("코스트센터명") || null, + glAccountCode: getCellValue("GL계정코드") || null, + glAccountName: getCellValue("GL계정명") || null, + targetUnitPrice: cleanNumber(targetUnitPrice) || null, + targetAmount: getCellValue("내정금액") + ? cleanNumber(getCellValue("내정금액")) + : null, + targetCurrency: getCellValue("내정통화") || "KRW", + budgetAmount: getCellValue("예산금액") + ? cleanNumber(getCellValue("예산금액")) + : null, + budgetCurrency: getCellValue("예산통화") || "KRW", + actualAmount: getCellValue("실적금액") + ? cleanNumber(getCellValue("실적금액")) + : null, + actualCurrency: getCellValue("실적통화") || "KRW", + } + + items.push(item) + } + + return { + success: errors.length === 0, + items, + errors, + } + } catch (error) { + console.error("Excel import error:", error) + return { + success: false, + items: [], + errors: [ + error instanceof Error + ? error.message + : "Excel 파일 파싱 중 오류가 발생했습니다.", + ], + } + } +} + diff --git a/lib/bidding/manage/project-utils.ts b/lib/bidding/manage/project-utils.ts new file mode 100644 index 00000000..92744695 --- /dev/null +++ b/lib/bidding/manage/project-utils.ts @@ -0,0 +1,87 @@ +'use server' + +import db from '@/db/db' +import { projects } from '@/db/schema' +import { eq, and, inArray } from 'drizzle-orm' + +/** + * 프로젝트 ID로 프로젝트 코드 조회 + */ +export async function getProjectCodeById(projectId: number): Promise { + try { + const result = await db + .select({ code: projects.code }) + .from(projects) + .where(eq(projects.id, projectId)) + .limit(1) + + return result[0]?.code || null + } catch (error) { + console.error('Failed to get project code by id:', error) + return null + } +} + +/** + * 프로젝트 코드와 이름으로 프로젝트 ID 조회 + */ +export async function getProjectIdByCodeAndName( + projectCode: string, + projectName: string +): Promise { + try { + if (!projectCode || !projectName) { + return null + } + + const result = await db + .select({ id: projects.id }) + .from(projects) + .where( + and( + eq(projects.code, projectCode.trim()), + eq(projects.name, projectName.trim()) + ) + ) + .limit(1) + + return result[0]?.id || null + } catch (error) { + console.error('Failed to get project id by code and name:', error) + return null + } +} + +/** + * 여러 프로젝트 ID로 프로젝트 코드 맵 조회 (성능 최적화) + */ +export async function getProjectCodesByIds( + projectIds: number[] +): Promise> { + try { + if (projectIds.length === 0) { + return new Map() + } + + const uniqueIds = [...new Set(projectIds.filter(id => id != null))] + if (uniqueIds.length === 0) { + return new Map() + } + + const result = await db + .select({ id: projects.id, code: projects.code }) + .from(projects) + .where(inArray(projects.id, uniqueIds)) + + const map = new Map() + result.forEach((project) => { + map.set(project.id, project.code) + }) + + return map + } catch (error) { + console.error('Failed to get project codes by ids:', error) + return new Map() + } +} + diff --git a/lib/bidding/selection/actions.ts b/lib/bidding/selection/actions.ts index f19fbe6d..91550960 100644 --- a/lib/bidding/selection/actions.ts +++ b/lib/bidding/selection/actions.ts @@ -131,6 +131,75 @@ export async function saveSelectionResult(data: SaveSelectionResultData) { } } +// 선정결과 조회 +export async function getSelectionResult(biddingId: number) { + try { + // 선정결과 조회 (selectedCompanyId가 null인 레코드) + const allResults = await db + .select() + .from(vendorSelectionResults) + .where(eq(vendorSelectionResults.biddingId, biddingId)) + + // @ts-ignore + const existingResult = allResults.filter((result: any) => result.selectedCompanyId === null).slice(0, 1) + + if (existingResult.length === 0) { + return { + success: true, + data: { + summary: '', + attachments: [] + } + } + } + + const result = existingResult[0] + + // 첨부파일 조회 + const documents = await db + .select({ + id: biddingDocuments.id, + fileName: biddingDocuments.fileName, + originalFileName: biddingDocuments.originalFileName, + fileSize: biddingDocuments.fileSize, + mimeType: biddingDocuments.mimeType, + filePath: biddingDocuments.filePath, + uploadedAt: biddingDocuments.uploadedAt + }) + .from(biddingDocuments) + .where(and( + eq(biddingDocuments.biddingId, biddingId), + eq(biddingDocuments.documentType, 'selection_result') + )) + + return { + success: true, + data: { + summary: result.evaluationSummary || '', + attachments: documents.map(doc => ({ + id: doc.id, + fileName: doc.fileName || doc.originalFileName || '', + originalFileName: doc.originalFileName || '', + fileSize: doc.fileSize || 0, + mimeType: doc.mimeType || '', + filePath: doc.filePath || '', + uploadedAt: doc.uploadedAt + })) + } + } + } catch (error) { + console.error('Failed to get selection result:', error) + return { + success: false, + error: '선정결과 조회 중 오류가 발생했습니다.', + data: { + summary: '', + attachments: [] + } + } + } +} + // 견적 히스토리 조회 export async function getQuotationHistory(biddingId: number, vendorId: number) { try { diff --git a/lib/bidding/selection/bidding-info-card.tsx b/lib/bidding/selection/bidding-info-card.tsx index 8864e7db..b363f538 100644 --- a/lib/bidding/selection/bidding-info-card.tsx +++ b/lib/bidding/selection/bidding-info-card.tsx @@ -56,7 +56,7 @@ export function BiddingInfoCard({ bidding }: BiddingInfoCardProps) { 입찰유형
- {bidding.isPublic ? '공개입찰' : '비공개입찰'} + {bidding.biddingType}
diff --git a/lib/bidding/selection/bidding-item-table.tsx b/lib/bidding/selection/bidding-item-table.tsx new file mode 100644 index 00000000..c101f7e7 --- /dev/null +++ b/lib/bidding/selection/bidding-item-table.tsx @@ -0,0 +1,192 @@ +'use client' + +import * as React from 'react' +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' +import { + getPRItemsForBidding, + getVendorPricesForBidding +} from '@/lib/bidding/detail/service' +import { formatNumber } from '@/lib/utils' +import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area' + +interface BiddingItemTableProps { + biddingId: number +} + +export function BiddingItemTable({ biddingId }: BiddingItemTableProps) { + const [data, setData] = React.useState<{ + prItems: any[] + vendorPrices: any[] + }>({ prItems: [], vendorPrices: [] }) + const [loading, setLoading] = React.useState(true) + + React.useEffect(() => { + const loadData = async () => { + try { + setLoading(true) + const [prItems, vendorPrices] = await Promise.all([ + getPRItemsForBidding(biddingId), + getVendorPricesForBidding(biddingId) + ]) + console.log('prItems', prItems) + console.log('vendorPrices', vendorPrices) + setData({ prItems, vendorPrices }) + } catch (error) { + console.error('Failed to load bidding items:', error) + } finally { + setLoading(false) + } + } + + loadData() + }, [biddingId]) + + if (loading) { + return ( + + + 응찰품목 + + +
+
로딩 중...
+
+
+
+ ) + } + + const { prItems, vendorPrices } = data + + // Calculate Totals + const totalQuantity = prItems.reduce((sum, item) => sum + Number(item.quantity || 0), 0) + const totalWeight = prItems.reduce((sum, item) => sum + Number(item.totalWeight || 0), 0) + const totalTargetAmount = prItems.reduce((sum, item) => sum + Number(item.targetAmount || 0), 0) + + // Calculate Vendor Totals + const vendorTotals = vendorPrices.map(vendor => { + const total = vendor.itemPrices.reduce((sum: number, item: any) => sum + Number(item.amount || 0), 0) + return { + companyId: vendor.companyId, + totalAmount: total + } + }) + + return ( + + + 응찰품목 + + + +
+ + + {/* Header Row 1: Base Info + Vendor Groups */} + + + + + + + + + + + + + + {vendorPrices.map((vendor) => ( + + ))} + + {/* Header Row 2: Vendor Sub-columns */} + + {vendorPrices.map((vendor) => ( + + + + + + + ))} + + + + {/* Summary Row */} + + + + + + + + + + + {vendorPrices.map((vendor) => { + const vTotal = vendorTotals.find(t => t.companyId === vendor.companyId)?.totalAmount || 0 + const ratio = totalTargetAmount > 0 ? (vTotal / totalTargetAmount) * 100 : 0 + return ( + + + + + + + ) + })} + + + {/* Data Rows */} + {prItems.map((item) => ( + + + + + + + + + + + + + + {vendorPrices.map((vendor) => { + const bidItem = vendor.itemPrices.find((p: any) => p.prItemId === item.id) + const bidAmount = bidItem ? bidItem.amount : 0 + const targetAmt = Number(item.targetAmount || 0) + const ratio = targetAmt > 0 && bidAmount > 0 ? (bidAmount / targetAmt) * 100 : 0 + + return ( + + + + + + + ) + })} + + ))} + +
자재번호자재내역자재내역상세구매단위수량단위총중량중량단위내정단가내정액통화 + {vendor.companyName} +
단가총액통화내정액(%)
합계{formatNumber(totalQuantity)}-{formatNumber(totalWeight)}--{formatNumber(totalTargetAmount)}KRW-{formatNumber(vTotal)}{vendor.currency}{formatNumber(ratio, 0)}%
{item.materialNumber}{item.materialInfo}{item.specification}{item.purchaseUnit}{formatNumber(item.quantity)}{item.quantityUnit}{formatNumber(item.totalWeight)}{item.weightUnit}{formatNumber(item.targetUnitPrice)}{formatNumber(item.targetAmount)}{item.currency} + {bidItem ? formatNumber(bidItem.unitPrice) : '-'} + + {bidItem ? formatNumber(bidItem.amount) : '-'} + + {vendor.currency} + + {bidItem && ratio > 0 ? `${formatNumber(ratio, 0)}%` : '-'} +
+
+ +
+
+
+ ) +} + diff --git a/lib/bidding/selection/bidding-selection-detail-content.tsx b/lib/bidding/selection/bidding-selection-detail-content.tsx index 45d5d402..887498dc 100644 --- a/lib/bidding/selection/bidding-selection-detail-content.tsx +++ b/lib/bidding/selection/bidding-selection-detail-content.tsx @@ -5,6 +5,7 @@ import { Bidding } from '@/db/schema' import { BiddingInfoCard } from './bidding-info-card' import { SelectionResultForm } from './selection-result-form' import { VendorSelectionTable } from './vendor-selection-table' +import { BiddingItemTable } from './bidding-item-table' interface BiddingSelectionDetailContentProps { biddingId: number @@ -17,6 +18,9 @@ export function BiddingSelectionDetailContent({ }: BiddingSelectionDetailContentProps) { const [refreshKey, setRefreshKey] = React.useState(0) + // 입찰평가중 상태가 아니면 읽기 전용 + const isReadOnly = bidding.status !== 'evaluation_of_bidding' + const handleRefresh = React.useCallback(() => { setRefreshKey(prev => prev + 1) }, []) @@ -27,7 +31,7 @@ export function BiddingSelectionDetailContent({ {/* 선정결과 폼 */} - + {/* 업체선정 테이블 */} + + {/* 응찰품목 테이블 */} + + ) } diff --git a/lib/bidding/selection/biddings-selection-table.tsx b/lib/bidding/selection/biddings-selection-table.tsx index c3990e7b..41225531 100644 --- a/lib/bidding/selection/biddings-selection-table.tsx +++ b/lib/bidding/selection/biddings-selection-table.tsx @@ -84,13 +84,13 @@ export function BiddingsSelectionTable({ promises }: BiddingsSelectionTableProps switch (rowAction.type) { case "view": // 상세 페이지로 이동 - // 입찰평가중일때만 상세보기 가능 - if (rowAction.row.original.status === 'evaluation_of_bidding') { + // 입찰평가중, 업체선정, 차수증가, 재입찰 상태일 때 상세보기 가능 + if (['evaluation_of_bidding', 'vendor_selected', 'round_increase', 'rebidding'].includes(rowAction.row.original.status)) { router.push(`/evcp/bid-selection/${rowAction.row.original.id}/detail`) } else { toast({ title: '접근 제한', - description: '입찰평가중이 아닙니다.', + description: '상세보기가 불가능한 상태입니다.', variant: 'destructive', }) } diff --git a/lib/bidding/selection/selection-result-form.tsx b/lib/bidding/selection/selection-result-form.tsx index 54687cc9..af6b8d43 100644 --- a/lib/bidding/selection/selection-result-form.tsx +++ b/lib/bidding/selection/selection-result-form.tsx @@ -9,8 +9,8 @@ import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form' import { useToast } from '@/hooks/use-toast' -import { saveSelectionResult } from './actions' -import { Loader2, Save, FileText } from 'lucide-react' +import { saveSelectionResult, getSelectionResult } from './actions' +import { Loader2, Save, FileText, Download, X } from 'lucide-react' import { Dropzone, DropzoneZone, DropzoneUploadIcon, DropzoneTitle, DropzoneDescription, DropzoneInput } from '@/components/ui/dropzone' const selectionResultSchema = z.object({ @@ -22,12 +22,25 @@ type SelectionResultFormData = z.infer interface SelectionResultFormProps { biddingId: number onSuccess: () => void + readOnly?: boolean } -export function SelectionResultForm({ biddingId, onSuccess }: SelectionResultFormProps) { +interface AttachmentInfo { + id: number + fileName: string + originalFileName: string + fileSize: number + mimeType: string + filePath: string + uploadedAt: Date | null +} + +export function SelectionResultForm({ biddingId, onSuccess, readOnly = false }: SelectionResultFormProps) { const { toast } = useToast() const [isSubmitting, setIsSubmitting] = React.useState(false) + const [isLoading, setIsLoading] = React.useState(true) const [attachmentFiles, setAttachmentFiles] = React.useState([]) + const [existingAttachments, setExistingAttachments] = React.useState([]) const form = useForm({ resolver: zodResolver(selectionResultSchema), @@ -36,10 +49,53 @@ export function SelectionResultForm({ biddingId, onSuccess }: SelectionResultFor }, }) + // 기존 선정결과 로드 + React.useEffect(() => { + const loadSelectionResult = async () => { + setIsLoading(true) + try { + const result = await getSelectionResult(biddingId) + if (result.success && result.data) { + form.reset({ + summary: result.data.summary || '', + }) + if (result.data.attachments) { + setExistingAttachments(result.data.attachments) + } + } + } catch (error) { + console.error('Failed to load selection result:', error) + toast({ + title: '로드 실패', + description: '선정결과를 불러오는데 실패했습니다.', + variant: 'destructive', + }) + } finally { + setIsLoading(false) + } + } + + loadSelectionResult() + }, [biddingId, form, toast]) + const removeAttachmentFile = (index: number) => { setAttachmentFiles(prev => prev.filter((_, i) => i !== index)) } + const removeExistingAttachment = (id: number) => { + setExistingAttachments(prev => prev.filter(att => att.id !== id)) + } + + const downloadAttachment = (filePath: string, fileName: string) => { + // 파일 다운로드 (filePath가 절대 경로인 경우) + if (filePath.startsWith('http') || filePath.startsWith('/')) { + window.open(filePath, '_blank') + } else { + // 상대 경로인 경우 + window.open(`/api/files/${filePath}`, '_blank') + } + } + const onSubmit = async (data: SelectionResultFormData) => { setIsSubmitting(true) try { @@ -74,6 +130,22 @@ export function SelectionResultForm({ biddingId, onSuccess }: SelectionResultFor } } + if (isLoading) { + return ( + + + 선정결과 + + +
+ + 로딩 중... +
+
+
+ ) + } + return ( @@ -94,6 +166,7 @@ export function SelectionResultForm({ biddingId, onSuccess }: SelectionResultFor placeholder="선정결과에 대한 요약을 입력해주세요..." className="min-h-[120px]" {...field} + disabled={readOnly} /> @@ -104,35 +177,83 @@ export function SelectionResultForm({ biddingId, onSuccess }: SelectionResultFor {/* 첨부파일 */}
첨부파일 - { - const newFiles = Array.from(files) - setAttachmentFiles(prev => [...prev, ...newFiles]) - }} - onDropRejected={() => { - toast({ - title: "파일 업로드 거부", - description: "파일 크기 및 형식을 확인해주세요.", - variant: "destructive", - }) - }} - > - - - - 파일을 드래그하거나 클릭하여 업로드 - - - PDF, Word, Excel, 이미지 파일 (최대 10MB) - - - - + + {/* 기존 첨부파일 */} + {existingAttachments.length > 0 && ( +
+

기존 첨부파일

+
+ {existingAttachments.map((attachment) => ( +
+
+ +
+

{attachment.originalFileName || attachment.fileName}

+

+ {(attachment.fileSize / 1024 / 1024).toFixed(2)} MB +

+
+
+
+ + {!readOnly && ( + + )} +
+
+ ))} +
+
+ )} + + {!readOnly && ( + { + const newFiles = Array.from(files) + setAttachmentFiles(prev => [...prev, ...newFiles]) + }} + onDropRejected={() => { + toast({ + title: "파일 업로드 거부", + description: "파일 크기 및 형식을 확인해주세요.", + variant: "destructive", + }) + }} + > + + + + 파일을 드래그하거나 클릭하여 업로드 + + + PDF, Word, Excel, 이미지 파일 (최대 10MB) + + + + + )} {attachmentFiles.length > 0 && (
-

업로드된 파일

+

새로 추가할 파일

{attachmentFiles.map((file, index) => (
- + {!readOnly && ( + + )}
))}
@@ -164,13 +287,15 @@ export function SelectionResultForm({ biddingId, onSuccess }: SelectionResultFor {/* 저장 버튼 */} -
- -
+ {!readOnly && ( +
+ +
+ )}
diff --git a/lib/bidding/selection/vendor-selection-table.tsx b/lib/bidding/selection/vendor-selection-table.tsx index 8570b5b6..40f13ec1 100644 --- a/lib/bidding/selection/vendor-selection-table.tsx +++ b/lib/bidding/selection/vendor-selection-table.tsx @@ -10,9 +10,10 @@ interface VendorSelectionTableProps { biddingId: number bidding: Bidding onRefresh: () => void + readOnly?: boolean } -export function VendorSelectionTable({ biddingId, bidding, onRefresh }: VendorSelectionTableProps) { +export function VendorSelectionTable({ biddingId, bidding, onRefresh, readOnly = false }: VendorSelectionTableProps) { const [vendors, setVendors] = React.useState([]) const [loading, setLoading] = React.useState(true) @@ -59,6 +60,7 @@ export function VendorSelectionTable({ biddingId, bidding, onRefresh }: VendorSe vendors={vendors} onRefresh={onRefresh} onOpenSelectionReasonDialog={() => {}} + readOnly={readOnly} /> diff --git a/lib/bidding/service.ts b/lib/bidding/service.ts index a658ee6a..27dae87d 100644 --- a/lib/bidding/service.ts +++ b/lib/bidding/service.ts @@ -18,6 +18,7 @@ import { vendorContacts, vendors } from '@/db/schema' +import { companyConditionResponses } from '@/db/schema/bidding' import { eq, desc, @@ -2196,7 +2197,7 @@ export async function updateBiddingProjectInfo(biddingId: number) { } // 입찰의 PR 아이템 금액 합산하여 bidding 업데이트 -async function updateBiddingAmounts(biddingId: number) { +export async function updateBiddingAmounts(biddingId: number) { try { // 해당 bidding의 모든 PR 아이템들의 금액 합계 계산 const amounts = await db @@ -2214,9 +2215,9 @@ async function updateBiddingAmounts(biddingId: number) { await db .update(biddings) .set({ - targetPrice: totalTargetAmount, - budget: totalBudgetAmount, - finalBidPrice: totalActualAmount, + targetPrice: String(totalTargetAmount), + budget: String(totalBudgetAmount), + finalBidPrice: String(totalActualAmount), updatedAt: new Date() }) .where(eq(biddings.id, biddingId)) @@ -2511,6 +2512,119 @@ export async function deleteBiddingCompanyContact(contactId: number) { } } +// 입찰담당자별 입찰 업체 조회 +export async function getBiddingCompaniesByBidPicId(bidPicId: number) { + try { + const companies = await db + .select({ + biddingId: biddings.id, + biddingNumber: biddings.biddingNumber, + biddingTitle: biddings.title, + companyId: biddingCompanies.companyId, + vendorCode: vendors.vendorCode, + vendorName: vendors.vendorName, + updatedAt: biddings.updatedAt, + }) + .from(biddings) + .innerJoin(biddingCompanies, eq(biddings.id, biddingCompanies.biddingId)) + .innerJoin(vendors, eq(biddingCompanies.companyId, vendors.id)) + .where(eq(biddings.bidPicId, bidPicId)) + .orderBy(desc(biddings.updatedAt)) + + return { + success: true, + data: companies + } + } catch (error) { + console.error('Failed to get bidding companies by bidPicId:', error) + return { + success: false, + error: '입찰 업체 조회에 실패했습니다.', + data: [] + } + } +} + +// 입찰 업체를 현재 입찰에 추가 (담당자 정보 포함) +export async function addBiddingCompanyFromOtherBidding( + targetBiddingId: number, + sourceBiddingId: number, + companyId: number, + contacts?: Array<{ + contactName: string + contactEmail: string + contactNumber?: string + }> +) { + try { + return await db.transaction(async (tx) => { + // 중복 체크 + const existingCompany = await tx + .select() + .from(biddingCompanies) + .where( + and( + eq(biddingCompanies.biddingId, targetBiddingId), + eq(biddingCompanies.companyId, companyId) + ) + ) + .limit(1) + + if (existingCompany.length > 0) { + return { + success: false, + error: '이미 등록된 업체입니다.' + } + } + + // 1. biddingCompanies 레코드 생성 + const [biddingCompanyResult] = await tx + .insert(biddingCompanies) + .values({ + biddingId: targetBiddingId, + companyId: companyId, + invitationStatus: 'pending', + invitedAt: new Date(), + }) + .returning({ id: biddingCompanies.id }) + + if (!biddingCompanyResult) { + throw new Error('업체 추가에 실패했습니다.') + } + + // 2. 담당자 정보 추가 + if (contacts && contacts.length > 0) { + await tx.insert(biddingCompaniesContacts).values( + contacts.map(contact => ({ + biddingId: targetBiddingId, + vendorId: companyId, + contactName: contact.contactName, + contactEmail: contact.contactEmail, + contactNumber: contact.contactNumber || null, + })) + ) + } + + // 3. company_condition_responses 레코드 생성 + await tx.insert(companyConditionResponses).values({ + biddingCompanyId: biddingCompanyResult.id, + }) + + return { + success: true, + message: '업체가 성공적으로 추가되었습니다.', + data: { id: biddingCompanyResult.id } + } + }) + } catch (error) { + console.error('Failed to add bidding company from other bidding:', error) + return { + success: false, + error: error instanceof Error ? error.message : '업체 추가에 실패했습니다.' + } + } +} + export async function updateBiddingConditions( biddingId: number, updates: { @@ -3145,9 +3259,9 @@ export async function increaseRoundOrRebid(biddingId: number, userId: string | u } } - revalidatePath('/bidding') - revalidatePath(`/bidding/${biddingId}`) // 기존 입찰 페이지도 갱신 - revalidatePath(`/bidding/${newBidding.id}`) + revalidatePath('/bid-receive') + revalidatePath(`/bid-receive/${biddingId}`) // 기존 입찰 페이지도 갱신 + revalidatePath(`/bid-receive/${newBidding.id}`) return { success: true, @@ -3436,9 +3550,10 @@ export async function getBiddingsForSelection(input: GetBiddingsSchema) { // 'bidding_opened', 'bidding_closed', 'evaluation_of_bidding', 'vendor_selected' 상태만 조회 basicConditions.push( or( - eq(biddings.status, 'bidding_closed'), eq(biddings.status, 'evaluation_of_bidding'), - eq(biddings.status, 'vendor_selected') + eq(biddings.status, 'vendor_selected'), + eq(biddings.status, 'round_increase'), + eq(biddings.status, 'rebidding'), )! ) diff --git a/lib/bidding/vendor/components/pr-items-pricing-table.tsx b/lib/bidding/vendor/components/pr-items-pricing-table.tsx index 7dd8384e..5afb2b67 100644 --- a/lib/bidding/vendor/components/pr-items-pricing-table.tsx +++ b/lib/bidding/vendor/components/pr-items-pricing-table.tsx @@ -382,18 +382,14 @@ export function PrItemsPricingTable({ ) : ( { - let value = e.target.value - if (/^0[0-9]+/.test(value)) { - value = value.replace(/^0+/, '') - if (value === '') value = '0' - } - const numericValue = parseFloat(value) + // 콤마 제거 및 숫자만 허용 + const value = e.target.value.replace(/,/g, '').replace(/[^0-9]/g, '') + const numericValue = Number(value) + updateQuotation( item.id, 'bidUnitPrice', diff --git a/lib/bidding/vendor/export-partners-biddings-to-excel.ts b/lib/bidding/vendor/export-partners-biddings-to-excel.ts new file mode 100644 index 00000000..9e99eeec --- /dev/null +++ b/lib/bidding/vendor/export-partners-biddings-to-excel.ts @@ -0,0 +1,278 @@ +import { type Table } from "@tanstack/react-table" +import ExcelJS from "exceljs" +import { PartnersBiddingListItem } from '../detail/service' +import { + biddingStatusLabels, + contractTypeLabels, +} from "@/db/schema" +import { formatDate } from "@/lib/utils" + +/** + * Partners 입찰 목록을 Excel로 내보내기 + * - 계약구분, 진행상태는 라벨(명칭)로 변환 + * - 입찰기간은 submissionStartDate, submissionEndDate 기준 + * - 날짜는 적절한 형식으로 변환 + */ +export async function exportPartnersBiddingsToExcel( + table: Table, + { + filename = "협력업체입찰목록", + onlySelected = false, + }: { + filename?: string + onlySelected?: boolean + } = {} +): Promise { + // 테이블에서 실제 사용 중인 leaf columns 가져오기 + const allColumns = table.getAllLeafColumns() + + // select, actions, attachments 컬럼 제외 + const columns = allColumns.filter( + (col) => !["select", "actions", "attachments"].includes(col.id) + ) + + // 헤더 매핑 (컬럼 id -> Excel 헤더명) + const headerMap: Record = { + biddingNumber: "입찰 No.", + status: "입찰상태", + isUrgent: "긴급여부", + title: "입찰명", + isAttendingMeeting: "사양설명회", + isBiddingParticipated: "입찰 참여의사", + biddingSubmissionStatus: "입찰 제출여부", + contractType: "계약구분", + submissionStartDate: "입찰기간", + contractStartDate: "계약기간", + bidPicName: "입찰담당자", + supplyPicName: "조달담당자", + updatedAt: "최종수정일", + } + + // 헤더 행 생성 + const headerRow = columns.map((col) => { + return headerMap[col.id] || col.id + }) + + // 데이터 행 생성 + const rowModel = onlySelected + ? table.getFilteredSelectedRowModel() + : table.getRowModel() + + const dataRows = rowModel.rows.map((row) => { + const original = row.original + return columns.map((col) => { + const colId = col.id + let value: any + + // 특별 처리 필요한 컬럼들 + switch (colId) { + case "contractType": + // 계약구분: 라벨로 변환 + value = contractTypeLabels[original.contractType as keyof typeof contractTypeLabels] || original.contractType + break + + case "status": + // 입찰상태: 라벨로 변환 + value = biddingStatusLabels[original.status as keyof typeof biddingStatusLabels] || original.status + break + + case "isUrgent": + // 긴급여부: Yes/No + value = original.isUrgent ? "긴급" : "일반" + break + + case "isAttendingMeeting": + // 사양설명회: 참석/불참/미결정 + if (original.isAttendingMeeting === null) { + value = "해당없음" + } else { + value = original.isAttendingMeeting ? "참석" : "불참" + } + break + + case "isBiddingParticipated": + // 입찰 참여의사: 참여/불참/미결정 + if (original.isBiddingParticipated === null) { + value = "미결정" + } else { + value = original.isBiddingParticipated ? "참여" : "불참" + } + break + + case "biddingSubmissionStatus": + // 입찰 제출여부: 최종제출/제출/미제출 + const finalQuoteAmount = original.finalQuoteAmount + const isFinalSubmission = original.isFinalSubmission + + if (!finalQuoteAmount) { + value = "미제출" + } else if (isFinalSubmission) { + value = "최종제출" + } else { + value = "제출" + } + break + + case "submissionStartDate": + // 입찰기간: submissionStartDate, submissionEndDate 기준 + const startDate = original.submissionStartDate + const endDate = original.submissionEndDate + + if (!startDate || !endDate) { + value = "-" + } else { + const startObj = new Date(startDate) + const endObj = new Date(endDate) + + // KST 변환 (UTC+9) + const formatKst = (d: Date) => { + const kstDate = new Date(d.getTime() + 9 * 60 * 60 * 1000) + return kstDate.toISOString().slice(0, 16).replace('T', ' ') + } + + value = `${formatKst(startObj)} ~ ${formatKst(endObj)}` + } + break + + // case "preQuoteDeadline": + // // 사전견적 마감일: 날짜 형식 + // if (!original.preQuoteDeadline) { + // value = "-" + // } else { + // const deadline = new Date(original.preQuoteDeadline) + // value = deadline.toISOString().slice(0, 16).replace('T', ' ') + // } + // break + + case "contractStartDate": + // 계약기간: contractStartDate, contractEndDate 기준 + const contractStart = original.contractStartDate + const contractEnd = original.contractEndDate + + if (!contractStart || !contractEnd) { + value = "-" + } else { + const startObj = new Date(contractStart) + const endObj = new Date(contractEnd) + value = `${formatDate(startObj, "KR")} ~ ${formatDate(endObj, "KR")}` + } + break + + case "bidPicName": + // 입찰담당자: bidPicName + value = original.bidPicName || "-" + break + + case "supplyPicName": + // 조달담당자: supplyPicName + value = original.supplyPicName || "-" + break + + case "updatedAt": + // 최종수정일: 날짜 시간 형식 + if (original.updatedAt) { + const updated = new Date(original.updatedAt) + value = updated.toISOString().slice(0, 16).replace('T', ' ') + } else { + value = "-" + } + break + + case "biddingNumber": + // 입찰번호: 원입찰번호 포함 + const biddingNumber = original.biddingNumber + const originalBiddingNumber = original.originalBiddingNumber + if (originalBiddingNumber) { + value = `${biddingNumber} (원: ${originalBiddingNumber})` + } else { + value = biddingNumber + } + break + + default: + // 기본값: row.getValue 사용 + value = row.getValue(colId) + + // null/undefined 처리 + if (value == null) { + value = "" + } + + // 객체인 경우 JSON 문자열로 변환 + if (typeof value === "object") { + value = JSON.stringify(value) + } + break + } + + return value + }) + }) + + // 최종 sheetData + const sheetData = [headerRow, ...dataRows] + + // ExcelJS로 파일 생성 및 다운로드 + await createAndDownloadExcel(sheetData, columns.length, filename) +} + +/** + * Excel 파일 생성 및 다운로드 + */ +async function createAndDownloadExcel( + sheetData: any[][], + columnCount: number, + filename: string +): Promise { + // ExcelJS 워크북/시트 생성 + const workbook = new ExcelJS.Workbook() + const worksheet = workbook.addWorksheet("Sheet1") + + // 칼럼별 최대 길이 추적 + const maxColumnLengths = Array(columnCount).fill(0) + sheetData.forEach((row) => { + row.forEach((cellValue, colIdx) => { + const cellText = cellValue?.toString() ?? "" + if (cellText.length > maxColumnLengths[colIdx]) { + maxColumnLengths[colIdx] = cellText.length + } + }) + }) + + // 시트에 데이터 추가 + 헤더 스타일 + sheetData.forEach((arr, idx) => { + const row = worksheet.addRow(arr) + + // 헤더 스타일 적용 (첫 번째 행) + if (idx === 0) { + row.font = { bold: true } + row.alignment = { horizontal: "center" } + row.eachCell((cell) => { + cell.fill = { + type: "pattern", + pattern: "solid", + fgColor: { argb: "FFCCCCCC" }, + } + }) + } + }) + + // 칼럼 너비 자동 조정 + maxColumnLengths.forEach((len, idx) => { + // 최소 너비 10, +2 여백 + worksheet.getColumn(idx + 1).width = Math.max(len + 2, 10) + }) + + // 최종 파일 다운로드 + const buffer = await workbook.xlsx.writeBuffer() + const blob = new Blob([buffer], { + type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + }) + const url = URL.createObjectURL(blob) + const link = document.createElement("a") + link.href = url + link.download = `${filename}.xlsx` + link.click() + URL.revokeObjectURL(url) +} + diff --git a/lib/bidding/vendor/partners-bidding-list-columns.tsx b/lib/bidding/vendor/partners-bidding-list-columns.tsx index a122e87b..6276d1b7 100644 --- a/lib/bidding/vendor/partners-bidding-list-columns.tsx +++ b/lib/bidding/vendor/partners-bidding-list-columns.tsx @@ -285,7 +285,7 @@ export function getPartnersBiddingListColumns({ setRowAction }: PartnersBiddingL cell: ({ row }) => { const isAttending = row.original.isAttendingMeeting if (isAttending === null) { - return
-
+ return
해당없음
} return isAttending ? ( @@ -366,31 +366,31 @@ export function getPartnersBiddingListColumns({ setRowAction }: PartnersBiddingL }), // 사전견적 마감일 - columnHelper.accessor('preQuoteDeadline', { - header: '사전견적 마감일', - cell: ({ row }) => { - const deadline = row.original.preQuoteDeadline - if (!deadline) { - return
-
- } + // columnHelper.accessor('preQuoteDeadline', { + // header: '사전견적 마감일', + // cell: ({ row }) => { + // const deadline = row.original.preQuoteDeadline + // if (!deadline) { + // return
-
+ // } - const now = new Date() - const deadlineDate = new Date(deadline) - const isExpired = deadlineDate < now + // const now = new Date() + // const deadlineDate = new Date(deadline) + // const isExpired = deadlineDate < now - return ( -
- - {format(new Date(deadline), "yyyy-MM-dd HH:mm")} - {isExpired && ( - - 마감 - - )} -
- ) - }, - }), + // return ( + //
+ // + // {format(new Date(deadline), "yyyy-MM-dd HH:mm")} + // {isExpired && ( + // + // 마감 + // + // )} + //
+ // ) + // }, + // }), // 계약기간 columnHelper.accessor('contractStartDate', { diff --git a/lib/bidding/vendor/partners-bidding-toolbar-actions.tsx b/lib/bidding/vendor/partners-bidding-toolbar-actions.tsx index 87b1367e..9a2f026c 100644 --- a/lib/bidding/vendor/partners-bidding-toolbar-actions.tsx +++ b/lib/bidding/vendor/partners-bidding-toolbar-actions.tsx @@ -2,10 +2,12 @@ import * as React from "react" import { type Table } from "@tanstack/react-table" -import { Users} from "lucide-react" +import { Users, FileSpreadsheet } from "lucide-react" +import { toast } from "sonner" import { Button } from "@/components/ui/button" import { PartnersBiddingListItem } from '../detail/service' +import { exportPartnersBiddingsToExcel } from './export-partners-biddings-to-excel' interface PartnersBiddingToolbarActionsProps { table: Table @@ -20,6 +22,8 @@ export function PartnersBiddingToolbarActions({ const selectedRows = table.getFilteredSelectedRowModel().rows const selectedBidding = selectedRows.length === 1 ? selectedRows[0].original : null + const [isExporting, setIsExporting] = React.useState(false) + const handleSpecificationMeetingClick = () => { if (selectedBidding && setRowAction) { setRowAction({ @@ -29,8 +33,36 @@ export function PartnersBiddingToolbarActions({ } } + // Excel 내보내기 핸들러 + const handleExport = React.useCallback(async () => { + try { + setIsExporting(true) + await exportPartnersBiddingsToExcel(table, { + filename: "협력업체입찰목록", + onlySelected: false, + }) + toast.success("Excel 파일이 다운로드되었습니다.") + } catch (error) { + console.error("Excel export error:", error) + toast.error("Excel 내보내기 중 오류가 발생했습니다.") + } finally { + setIsExporting(false) + } + }, [table]) + return (
+ {/* Excel 내보내기 버튼 */} + + + + + + + 검색 결과가 없습니다. + + {paymentTermsOptions.map((option) => ( + { + setFormData(prev => ({ ...prev, paymentDelivery: option.code })) + }} + > + + {option.code} {option.description && `(${option.description})`} + + ))} + { + setFormData(prev => ({ ...prev, paymentDelivery: '납품완료일로부터 60일 이내 지급' })) + }} + > + + 60일 이내 + + { + setFormData(prev => ({ ...prev, paymentDelivery: '추가조건' })) + }} + > + + 추가조건 + + + + + + {formData.paymentDelivery === '추가조건' && (
- {/* 지불조건 -> 세금조건 (지불조건 삭제됨) */} + {/*세금조건*/}
- {/* 지불조건 필드 삭제됨 -
- - -
- */}
- + + + + + + + + + 검색 결과가 없습니다. + + {TAX_CONDITIONS.map((condition) => ( + { + setFormData(prev => ({ ...prev, taxType: condition.code })) + }} + > + + {condition.name} + + ))} + + + + +
@@ -1266,79 +1393,178 @@ export function ContractBasicInfo({ contractId }: ContractBasicInfoProps) { {/* 인도조건 */}
- + + + + + + + + + 검색 결과가 없습니다. + + {incotermsOptions.length > 0 ? ( + incotermsOptions.map((option) => ( + { + setFormData(prev => ({ ...prev, deliveryTerm: option.code })) + }} + > + + {option.code} {option.description && `(${option.description})`} + + )) + ) : ( + + 로딩중... + + )} + + + + +
{/* 선적지 */}
- + + + + + + + + + 검색 결과가 없습니다. + + {shippingPlaces.length > 0 ? ( + shippingPlaces.map((place) => ( + { + setFormData(prev => ({ ...prev, shippingLocation: place.code })) + }} + > + + {place.code} {place.description && `(${place.description})`} + + )) + ) : ( + + 로딩중... + + )} + + + + +
{/* 하역지 */}
- + + + + + + + + + 검색 결과가 없습니다. + + {destinationPlaces.length > 0 ? ( + destinationPlaces.map((place) => ( + { + setFormData(prev => ({ ...prev, dischargeLocation: place.code })) + }} + > + + {place.code} {place.description && `(${place.description})`} + + )) + ) : ( + + 로딩중... + + )} + + + + +
{/* 계약납기일 */} diff --git a/lib/general-contracts/detail/general-contract-items-table.tsx b/lib/general-contracts/detail/general-contract-items-table.tsx index 15e5c926..e5fc6cf2 100644 --- a/lib/general-contracts/detail/general-contract-items-table.tsx +++ b/lib/general-contracts/detail/general-contract-items-table.tsx @@ -30,6 +30,8 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from import { ProjectSelector } from '@/components/ProjectSelector' import { MaterialGroupSelectorDialogSingle } from '@/components/common/material/material-group-selector-dialog-single' import { MaterialSearchItem } from '@/lib/material/material-group-service' +import { ProcurementItemSelectorDialogSingle } from '@/components/common/selectors/procurement-item/procurement-item-selector-dialog-single' +import { ProcurementSearchItem } from '@/components/common/selectors/procurement-item/procurement-item-service' interface ContractItem { id?: number @@ -174,7 +176,7 @@ export function ContractItemsTable({ const errors: string[] = [] for (let index = 0; index < localItems.length; index++) { const item = localItems[index] - if (!item.itemCode) errors.push(`${index + 1}번째 품목의 품목코드`) + // if (!item.itemCode) errors.push(`${index + 1}번째 품목의 품목코드`) if (!item.itemInfo) errors.push(`${index + 1}번째 품목의 Item 정보`) if (!item.quantity || item.quantity <= 0) errors.push(`${index + 1}번째 품목의 수량`) if (!item.contractUnitPrice || item.contractUnitPrice <= 0) errors.push(`${index + 1}번째 품목의 단가`) @@ -271,6 +273,34 @@ export function ContractItemsTable({ onItemsChange(updatedItems) } + // 1회성 품목 선택 시 행 추가 + const handleOneTimeItemSelect = (item: ProcurementSearchItem | null) => { + if (!item) return + + const newItem: ContractItem = { + projectId: null, + itemCode: item.itemCode, + itemInfo: item.itemName, + materialGroupCode: '', + materialGroupDescription: '', + specification: item.specification || '', + quantity: 0, + quantityUnit: item.unit || 'EA', + totalWeight: 0, + weightUnit: 'KG', + contractDeliveryDate: '', + contractUnitPrice: 0, + contractAmount: 0, + contractCurrency: 'KRW', + isSelected: false + } + + const updatedItems = [...localItems, newItem] + setLocalItems(updatedItems) + onItemsChange(updatedItems) + toast.success('1회성 품목이 추가되었습니다.') + } + // 일괄입력 적용 const applyBatchInput = () => { if (localItems.length === 0) { @@ -382,6 +412,17 @@ export function ContractItemsTable({ 행 추가 + + + + + 준법문의 요청 데이터 + + 준법문의 요청 데이터를 조회합니다. + {data.length > 0 && ` (${data.length}건, ${selectedRows.length}개 선택됨)`} + + + +
+ {isLoading ? ( +
+ + 데이터 로딩 중... +
+ ) : error ? ( +
+ 오류: {error} +
+ ) : data.length === 0 ? ( +
+ 데이터가 없습니다. +
+ ) : ( +
+ {/* 테이블 영역 - 스크롤 가능 */} + + + + {table.getHeaderGroups().map((headerGroup) => ( + + {headerGroup.headers.map((header) => ( + + {header.isPlaceholder + ? null + : flexRender( + header.column.columnDef.header, + header.getContext() + )} + + ))} + + ))} + + + {table.getRowModel().rows?.length ? ( + table.getRowModel().rows.map((row) => ( + + {row.getVisibleCells().map((cell) => ( + + {flexRender( + cell.column.columnDef.cell, + cell.getContext() + )} + + ))} + + )) + ) : ( + + + 데이터가 없습니다. + + + )} + +
+ +
+ + {/* 페이지네이션 컨트롤 - 고정 영역 */} +
+
+ {table.getFilteredSelectedRowModel().rows.length}개 행 선택됨 +
+
+
+

페이지당 행 수

+ +
+
+ {table.getState().pagination.pageIndex + 1} /{" "} + {table.getPageCount()} +
+
+ + + + +
+
+
+
+ )} +
+ + + + + + +
+
+ ) +} + diff --git a/db/schema/basicContractDocumnet.ts b/db/schema/basicContractDocumnet.ts index 944c4b2c..e571c7e0 100644 --- a/db/schema/basicContractDocumnet.ts +++ b/db/schema/basicContractDocumnet.ts @@ -67,6 +67,12 @@ export const basicContract = pgTable('basic_contract', { legalReviewRegNo: varchar('legal_review_reg_no', { length: 100 }), // 법무 시스템 REG_NO legalReviewProgressStatus: varchar('legal_review_progress_status', { length: 255 }), // PRGS_STAT_DSC 값 + // 준법문의 관련 필드 + complianceReviewRequestedAt: timestamp('compliance_review_requested_at'), // 준법문의 요청일 + complianceReviewCompletedAt: timestamp('compliance_review_completed_at'), // 준법문의 완료일 + complianceReviewRegNo: varchar('compliance_review_reg_no', { length: 100 }), // 준법문의 시스템 REG_NO + complianceReviewProgressStatus: varchar('compliance_review_progress_status', { length: 255 }), // 준법문의 PRGS_STAT_DSC 값 + createdAt: timestamp('created_at').defaultNow(), updatedAt: timestamp('updated_at').defaultNow(), completedAt: timestamp('completed_at'), // 계약 체결 완료 날짜 @@ -99,6 +105,12 @@ export const basicContractView = pgView('basic_contract_view').as((qb) => { legalReviewRegNo: sql`${basicContract.legalReviewRegNo}`.as('legal_review_reg_no'), legalReviewProgressStatus: sql`${basicContract.legalReviewProgressStatus}`.as('legal_review_progress_status'), + // 준법문의 관련 필드 + complianceReviewRequestedAt: sql`${basicContract.complianceReviewRequestedAt}`.as('compliance_review_requested_at'), + complianceReviewCompletedAt: sql`${basicContract.complianceReviewCompletedAt}`.as('compliance_review_completed_at'), + complianceReviewRegNo: sql`${basicContract.complianceReviewRegNo}`.as('compliance_review_reg_no'), + complianceReviewProgressStatus: sql`${basicContract.complianceReviewProgressStatus}`.as('compliance_review_progress_status'), + createdAt: sql`${basicContract.createdAt}`.as('created_at'), updatedAt: sql`${basicContract.updatedAt}`.as('updated_at'), completedAt: sql`${basicContract.completedAt}`.as('completed_at'), @@ -121,6 +133,9 @@ export const basicContractView = pgView('basic_contract_view').as((qb) => { // 법무검토 상태 (PRGS_STAT_DSC 동기화 값) legalReviewStatus: sql`${basicContract.legalReviewProgressStatus}`.as('legal_review_status'), + + // 준법문의 상태 (PRGS_STAT_DSC 동기화 값) + complianceReviewStatus: sql`${basicContract.complianceReviewProgressStatus}`.as('compliance_review_status'), // 템플릿 파일 정보 templateFilePath: sql`${basicContractTemplates.filePath}`.as('template_file_path'), diff --git a/lib/basic-contract/cpvw-service.ts b/lib/basic-contract/cpvw-service.ts new file mode 100644 index 00000000..6d249002 --- /dev/null +++ b/lib/basic-contract/cpvw-service.ts @@ -0,0 +1,236 @@ +"use server" + +import { oracleKnex } from '@/lib/oracle-db/db' + +// CPVW_WAB_QUST_LIST_VIEW 테이블 데이터 타입 (실제 테이블 구조에 맞게 조정 필요) +export interface CPVWWabQustListView { + [key: string]: string | number | Date | null | undefined +} + +// 테스트 환경용 폴백 데이터 (실제 CPVW_WAB_QUST_LIST_VIEW 테이블 구조에 맞춤) +const FALLBACK_TEST_DATA: CPVWWabQustListView[] = [ + { + REG_NO: '1030', + INQ_TP: 'OC', + INQ_TP_DSC: '해외계약', + TIT: 'Contrack of Sale', + REQ_DGR: '2', + REQR_NM: '김원식', + REQ_DT: '20130829', + REVIEW_TERM_DT: '20130902', + RVWR_NM: '김미정', + CNFMR_NM: '안한진', + APPR_NM: '염정훈', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '검토중', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' + }, + { + REG_NO: '1076', + INQ_TP: 'IC', + INQ_TP_DSC: '국내계약', + TIT: 'CAISSON PIPE 복관 계약서 검토 요청件', + REQ_DGR: '1', + REQR_NM: '서권환', + REQ_DT: '20130821', + REVIEW_TERM_DT: '20130826', + RVWR_NM: '이택준', + CNFMR_NM: '이택준', + APPR_NM: '전상용', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '완료', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' + }, + { + REG_NO: '1100', + INQ_TP: 'IC', + INQ_TP_DSC: '국내계약', + TIT: '(7102) HVAC 작업계약', + REQ_DGR: '1', + REQR_NM: '신동동', + REQ_DT: '20130826', + REVIEW_TERM_DT: '20130829', + RVWR_NM: '이두리', + CNFMR_NM: '이두리', + APPR_NM: '전상용', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '완료', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' + }, + { + REG_NO: '1105', + INQ_TP: 'IC', + INQ_TP_DSC: '국내계약', + TIT: 'Plate 가공계약서 검토 요청건', + REQ_DGR: '1', + REQR_NM: '서권환', + REQ_DT: '20130826', + REVIEW_TERM_DT: '20130829', + RVWR_NM: '백영국', + CNFMR_NM: '백영국', + APPR_NM: '전상용', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '완료', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' + }, + { + REG_NO: '1106', + INQ_TP: 'IC', + INQ_TP_DSC: '국내계약', + TIT: 'SHELL FLNG, V-BRACE 제작 계약서 검토件', + REQ_DGR: '1', + REQR_NM: '성기승', + REQ_DT: '20130826', + REVIEW_TERM_DT: '20130830', + RVWR_NM: '이두리', + CNFMR_NM: '이두리', + APPR_NM: '전상용', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '완료', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' + } +] + +const normalizeOracleRows = (rows: Array>): CPVWWabQustListView[] => { + return rows.map((item) => { + const convertedItem: CPVWWabQustListView = {} + for (const [key, value] of Object.entries(item)) { + if (value instanceof Date) { + convertedItem[key] = value + } else if (value === null) { + convertedItem[key] = null + } else { + convertedItem[key] = String(value) + } + } + return convertedItem + }) +} + +/** + * CPVW_WAB_QUST_LIST_VIEW 테이블 전체 조회 + * @returns 테이블 데이터 배열 + */ +export async function getCPVWWabQustListViewData(): Promise<{ + success: boolean + data: CPVWWabQustListView[] + error?: string + isUsingFallback?: boolean +}> { + try { + console.log('📋 [getCPVWWabQustListViewData] CPVW_WAB_QUST_LIST_VIEW 테이블 조회 시작...') + + const result = await oracleKnex.raw(` + SELECT * + FROM CPVW_WAB_QUST_LIST_VIEW + WHERE ROWNUM < 100 + ORDER BY 1 + `) + + // Oracle raw query의 결과는 rows 배열에 들어있음 + const rows = (result.rows || result) as Array> + + console.log(`✅ [getCPVWWabQustListViewData] 조회 성공 - ${rows.length}건`) + + // 데이터 타입 변환 (필요에 따라 조정) + const cleanedResult = normalizeOracleRows(rows) + + return { + success: true, + data: cleanedResult, + isUsingFallback: false + } + } catch (error) { + console.error('❌ [getCPVWWabQustListViewData] 오류:', error) + console.log('🔄 [getCPVWWabQustListViewData] 폴백 테스트 데이터 사용') + return { + success: true, + data: FALLBACK_TEST_DATA, + isUsingFallback: true + } + } +} + +export async function getCPVWWabQustListViewByRegNo(regNo: string): Promise<{ + success: boolean + data?: CPVWWabQustListView + error?: string + isUsingFallback?: boolean +}> { + if (!regNo) { + return { + success: false, + error: 'REG_NO는 필수입니다.' + } + } + + try { + console.log(`[getCPVWWabQustListViewByRegNo] REG_NO=${regNo} 조회`) + const result = await oracleKnex.raw( + ` + SELECT * + FROM CPVW_WAB_QUST_LIST_VIEW + WHERE REG_NO = :regNo + `, + { regNo } + ) + + const rows = (result.rows || result) as Array> + const cleanedResult = normalizeOracleRows(rows) + + if (cleanedResult.length === 0) { + // 데이터가 없을 때 폴백 테스트 데이터에서 찾기 + console.log(`[getCPVWWabQustListViewByRegNo] 데이터 없음, 폴백 테스트 데이터에서 검색: REG_NO=${regNo}`) + const fallbackData = FALLBACK_TEST_DATA.find(item => + String(item.REG_NO) === String(regNo) + ) + + if (fallbackData) { + console.log(`[getCPVWWabQustListViewByRegNo] 폴백 테스트 데이터에서 찾음: REG_NO=${regNo}`) + return { + success: true, + data: fallbackData, + isUsingFallback: true + } + } + + return { + success: false, + error: '해당 REG_NO에 대한 데이터가 없습니다.' + } + } + + return { + success: true, + data: cleanedResult[0], + isUsingFallback: false + } + } catch (error) { + console.error('[getCPVWWabQustListViewByRegNo] 오류:', error) + console.log(`[getCPVWWabQustListViewByRegNo] 폴백 테스트 데이터에서 검색: REG_NO=${regNo}`) + + // 오류 발생 시 폴백 테스트 데이터에서 찾기 + const fallbackData = FALLBACK_TEST_DATA.find(item => + String(item.REG_NO) === String(regNo) + ) + + if (fallbackData) { + console.log(`[getCPVWWabQustListViewByRegNo] 폴백 테스트 데이터에서 찾음: REG_NO=${regNo}`) + return { + success: true, + data: fallbackData, + isUsingFallback: true + } + } + + return { + success: false, + error: error instanceof Error ? error.message : 'REG_NO 조회 중 오류가 발생했습니다.' + } + } +} diff --git a/lib/basic-contract/service.ts b/lib/basic-contract/service.ts index 6f4e5d53..12278c54 100644 --- a/lib/basic-contract/service.ts +++ b/lib/basic-contract/service.ts @@ -2862,6 +2862,10 @@ export async function requestLegalReviewAction( } } +// ⚠️ SSLVW(법무관리시스템) PRGS_STAT_DSC 문자열을 그대로 저장하는 함수입니다. +// - 상태 텍스트 및 완료 여부는 외부 시스템에 의존하므로 신뢰도가 100%는 아니고, +// - 여기에서 관리하는 값들은 UI 표시/참고용으로만 사용해야 합니다. +// - 최종 승인 차단 등 핵심 비즈니스 로직에서는 SSLVW 쪽 완료 시간을 직접 신뢰하지 않습니다. const persistLegalReviewStatus = async ({ contractId, regNo, @@ -2903,6 +2907,121 @@ const persistLegalReviewStatus = async ({ revalidateTag("basic-contracts") } +/** + * 준법문의 요청 서버 액션 + */ +export async function requestComplianceInquiryAction( + contractIds: number[] +): Promise<{ success: boolean; message: string }> { + const session = await getServerSession(authOptions) + + if (!session?.user) { + return { + success: false, + message: "로그인이 필요합니다." + } + } + + // 계약서 정보 조회 + const contracts = await db + .select({ + id: basicContractView.id, + complianceReviewRequestedAt: basicContractView.complianceReviewRequestedAt, + }) + .from(basicContractView) + .where(inArray(basicContractView.id, contractIds)) + + if (contracts.length === 0) { + return { + success: false, + message: "선택된 계약서를 찾을 수 없습니다." + } + } + + // 준법문의 요청 가능한 계약서 필터링 (이미 요청되지 않은 것만) + const eligibleContracts = contracts.filter(contract => + !contract.complianceReviewRequestedAt + ) + + if (eligibleContracts.length === 0) { + return { + success: false, + message: "준법문의 요청 가능한 계약서가 없습니다." + } + } + + const currentDate = new Date() + + // 트랜잭션으로 처리 + await db.transaction(async (tx) => { + for (const contract of eligibleContracts) { + await tx + .update(basicContract) + .set({ + complianceReviewRequestedAt: currentDate, + updatedAt: currentDate, + }) + .where(eq(basicContract.id, contract.id)) + } + }) + + revalidateTag("basic-contracts") + + return { + success: true, + message: `${eligibleContracts.length}건의 준법문의 요청이 완료되었습니다.` + } +} + +/** + * 준법문의 상태 저장 (준법문의 전용 필드 사용) + */ +const persistComplianceReviewStatus = async ({ + contractId, + regNo, + progressStatus, +}: { + contractId: number + regNo: string + progressStatus: string +}) => { + const now = new Date() + + // 완료 상태 확인 (법무검토와 동일한 패턴) + // ⚠️ CPVW PRGS_STAT_DSC 문자열을 기반으로 한 best-effort 휴리스틱입니다. + // - 외부 시스템의 상태 텍스트에 의존하므로 신뢰도가 100%는 아니고, + // - 여기에서 설정하는 완료 시간(complianceReviewCompletedAt)은 UI 표시용으로만 사용해야 합니다. + // - 버튼 활성화, 서버 액션 차단, 필터 조건 등 핵심 비즈니스 로직에서는 + // 이 값을 신뢰하지 않도록 합니다. + // 완료 상태 확인 (법무검토와 동일한 패턴) + const isCompleted = progressStatus && ( + progressStatus.includes('완료') || + progressStatus.includes('승인') || + progressStatus.includes('종료') + ) + + await db.transaction(async (tx) => { + // 준법문의 상태 업데이트 (준법문의 전용 필드 사용) + const updateData: any = { + complianceReviewRegNo: regNo, + complianceReviewProgressStatus: progressStatus, + updatedAt: now, + } + + // 완료 상태인 경우 완료일 설정 + if (isCompleted) { + updateData.complianceReviewCompletedAt = now + } + + await tx + .update(basicContract) + .set(updateData) + .where(eq(basicContract.id, contractId)) + }) + + revalidateTag("basic-contracts") +} + /** * SSLVW 데이터로부터 법무검토 상태 업데이트 * @param sslvwData 선택된 SSLVW 데이터 배열 @@ -3033,6 +3152,137 @@ export async function updateLegalReviewStatusFromSSLVW( } } +/** + * CPVW 데이터로부터 준법문의 상태 업데이트 + * @param cpvwData 선택된 CPVW 데이터 배열 + * @param selectedContractIds 선택된 계약서 ID 배열 + * @returns 성공 여부 및 메시지 + */ +export async function updateComplianceReviewStatusFromCPVW( + cpvwData: Array<{ REG_NO?: string; reg_no?: string; PRGS_STAT_DSC?: string; prgs_stat_dsc?: string; [key: string]: any }>, + selectedContractIds: number[] +): Promise<{ success: boolean; message: string; updatedCount: number; errors: string[] }> { + try { + console.log(`[updateComplianceReviewStatusFromCPVW] CPVW 데이터로부터 준법문의 상태 업데이트 시작`) + + if (!cpvwData || cpvwData.length === 0) { + return { + success: false, + message: 'CPVW 데이터가 없습니다.', + updatedCount: 0, + errors: [] + } + } + + if (!selectedContractIds || selectedContractIds.length === 0) { + return { + success: false, + message: '선택된 계약서가 없습니다.', + updatedCount: 0, + errors: [] + } + } + + if (selectedContractIds.length !== 1) { + return { + success: false, + message: '한 개의 계약서만 선택해 주세요.', + updatedCount: 0, + errors: [] + } + } + + if (cpvwData.length !== 1) { + return { + success: false, + message: '준법문의 시스템 데이터도 한 건만 선택해 주세요.', + updatedCount: 0, + errors: [] + } + } + + const contractId = selectedContractIds[0] + const cpvwItem = cpvwData[0] + const regNo = String( + cpvwItem.REG_NO ?? + cpvwItem.reg_no ?? + cpvwItem.RegNo ?? + '' + ).trim() + const progressStatus = String( + cpvwItem.PRGS_STAT_DSC ?? + cpvwItem.prgs_stat_dsc ?? + cpvwItem.PrgsStatDsc ?? + '' + ).trim() + + if (!regNo) { + return { + success: false, + message: 'REG_NO 값을 찾을 수 없습니다.', + updatedCount: 0, + errors: [] + } + } + + if (!progressStatus) { + return { + success: false, + message: 'PRGS_STAT_DSC 값을 찾을 수 없습니다.', + updatedCount: 0, + errors: [] + } + } + + const contract = await db + .select({ + id: basicContract.id, + complianceReviewRegNo: basicContract.complianceReviewRegNo, + }) + .from(basicContract) + .where(eq(basicContract.id, contractId)) + .limit(1) + + if (!contract[0]) { + return { + success: false, + message: `계약서(${contractId})를 찾을 수 없습니다.`, + updatedCount: 0, + errors: [] + } + } + + if (contract[0].complianceReviewRegNo && contract[0].complianceReviewRegNo !== regNo) { + console.warn(`[updateComplianceReviewStatusFromCPVW] REG_NO가 변경됩니다: ${contract[0].complianceReviewRegNo} -> ${regNo}`) + } + + // 준법문의 상태 업데이트 + await persistComplianceReviewStatus({ + contractId, + regNo, + progressStatus, + }) + + console.log(`[updateComplianceReviewStatusFromCPVW] 완료: 계약서 ${contractId}, REG_NO ${regNo}, 상태 ${progressStatus}`) + + return { + success: true, + message: '준법문의 상태가 업데이트되었습니다.', + updatedCount: 1, + errors: [] + } + + } catch (error) { + console.error('[updateComplianceReviewStatusFromCPVW] 오류:', error) + return { + success: false, + message: '준법문의 상태 업데이트 중 오류가 발생했습니다.', + updatedCount: 0, + errors: [error instanceof Error ? error.message : '알 수 없는 오류'] + } + } +} + export async function refreshLegalReviewStatusFromOracle(contractId: number): Promise<{ success: boolean message: string @@ -3274,12 +3524,9 @@ export async function processBuyerSignatureAction( } } - if (contractData.legalReviewRequestedAt && !contractData.legalReviewCompletedAt) { - return { - success: false, - message: "법무검토가 완료되지 않았습니다." - } - } + // ⚠️ 법무검토 완료 여부는 SSLVW 상태/시간에 의존하므로 + // 여기서는 legalReviewCompletedAt 기반으로 최종승인을 막지 않습니다. + // (법무 상태는 UI에서 참고 정보로만 사용) // 파일 저장 로직 (기존 파일 덮어쓰기) const saveResult = await saveBuffer({ @@ -3373,9 +3620,9 @@ export async function prepareFinalApprovalAction( if (contract.completedAt !== null || !contract.signedFilePath) { return false } - if (contract.legalReviewRequestedAt && !contract.legalReviewCompletedAt) { - return false - } + // ⚠️ 법무검토 완료 여부는 SSLVW 상태/시간에 의존하므로 + // 여기서는 legalReviewCompletedAt 기반으로 필터링하지 않습니다. + // (법무 상태는 UI에서 참고 정보로만 사용) return true }) @@ -3949,6 +4196,8 @@ export async function saveGtcDocumentAction({ buyerSignedAt: null, legalReviewRequestedAt: null, legalReviewCompletedAt: null, + complianceReviewRequestedAt: null, + complianceReviewCompletedAt: null, updatedAt: new Date() }) .where(eq(basicContract.id, documentId)) diff --git a/lib/basic-contract/sslvw-service.ts b/lib/basic-contract/sslvw-service.ts index 38ecb67d..08b43f82 100644 --- a/lib/basic-contract/sslvw-service.ts +++ b/lib/basic-contract/sslvw-service.ts @@ -10,18 +10,89 @@ export interface SSLVWPurInqReq { // 테스트 환경용 폴백 데이터 const FALLBACK_TEST_DATA: SSLVWPurInqReq[] = [ { - id: 1, - request_number: 'REQ001', - status: 'PENDING', - created_date: new Date('2025-01-01'), - description: '테스트 요청 1' + REG_NO: '1030', + INQ_TP: 'OC', + INQ_TP_DSC: '해외계약', + TIT: 'Contrack of Sale', + REQ_DGR: '2', + REQR_NM: '김원식', + REQ_DT: '20130829', + REVIEW_TERM_DT: '20130902', + RVWR_NM: '김미정', + CNFMR_NM: '안한진', + APPR_NM: '염정훈', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '검토중이라고', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' }, { - id: 2, - request_number: 'REQ002', - status: 'APPROVED', - created_date: new Date('2025-01-02'), - description: '테스트 요청 2' + REG_NO: '1076', + INQ_TP: 'IC', + INQ_TP_DSC: '국내계약', + TIT: 'CAISSON PIPE 복관 계약서 검토 요청件', + REQ_DGR: '1', + REQR_NM: '서권환', + REQ_DT: '20130821', + REVIEW_TERM_DT: '20130826', + RVWR_NM: '이택준', + CNFMR_NM: '이택준', + APPR_NM: '전상용', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '완료', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' + }, + { + REG_NO: '1100', + INQ_TP: 'IC', + INQ_TP_DSC: '국내계약', + TIT: '(7102) HVAC 작업계약', + REQ_DGR: '1', + REQR_NM: '신동동', + REQ_DT: '20130826', + REVIEW_TERM_DT: '20130829', + RVWR_NM: '이두리', + CNFMR_NM: '이두리', + APPR_NM: '전상용', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '완료', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' + }, + { + REG_NO: '1105', + INQ_TP: 'IC', + INQ_TP_DSC: '국내계약', + TIT: 'Plate 가공계약서 검토 요청건', + REQ_DGR: '1', + REQR_NM: '서권환', + REQ_DT: '20130826', + REVIEW_TERM_DT: '20130829', + RVWR_NM: '백영국', + CNFMR_NM: '백영국', + APPR_NM: '전상용', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '완료', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' + }, + { + REG_NO: '1106', + INQ_TP: 'IC', + INQ_TP_DSC: '국내계약', + TIT: 'SHELL FLNG, V-BRACE 제작 계약서 검토件', + REQ_DGR: '1', + REQR_NM: '성기승', + REQ_DT: '20130826', + REVIEW_TERM_DT: '20130830', + RVWR_NM: '이두리', + CNFMR_NM: '이두리', + APPR_NM: '전상용', + PRGS_STAT: 'E', + PRGS_STAT_DSC: '완료', + REGR_DPTCD: 'D602058000', + REGR_DEPTNM: '구매1팀(사외계약)' } ] @@ -89,6 +160,7 @@ export async function getSSLVWPurInqReqByRegNo(regNo: string): Promise<{ success: boolean data?: SSLVWPurInqReq error?: string + isUsingFallback?: boolean }> { if (!regNo) { return { @@ -112,6 +184,21 @@ export async function getSSLVWPurInqReqByRegNo(regNo: string): Promise<{ const cleanedResult = normalizeOracleRows(rows) if (cleanedResult.length === 0) { + // 데이터가 없을 때 폴백 테스트 데이터에서 찾기 + console.log(`[getSSLVWPurInqReqByRegNo] 데이터 없음, 폴백 테스트 데이터에서 검색: REG_NO=${regNo}`) + const fallbackData = FALLBACK_TEST_DATA.find(item => + String(item.REG_NO) === String(regNo) + ) + + if (fallbackData) { + console.log(`[getSSLVWPurInqReqByRegNo] 폴백 테스트 데이터에서 찾음: REG_NO=${regNo}`) + return { + success: true, + data: fallbackData, + isUsingFallback: true + } + } + return { success: false, error: '해당 REG_NO에 대한 데이터가 없습니다.' @@ -120,10 +207,27 @@ export async function getSSLVWPurInqReqByRegNo(regNo: string): Promise<{ return { success: true, - data: cleanedResult[0] + data: cleanedResult[0], + isUsingFallback: false } } catch (error) { console.error('[getSSLVWPurInqReqByRegNo] 오류:', error) + console.log(`[getSSLVWPurInqReqByRegNo] 폴백 테스트 데이터에서 검색: REG_NO=${regNo}`) + + // 오류 발생 시 폴백 테스트 데이터에서 찾기 + const fallbackData = FALLBACK_TEST_DATA.find(item => + String(item.REG_NO) === String(regNo) + ) + + if (fallbackData) { + console.log(`[getSSLVWPurInqReqByRegNo] 폴백 테스트 데이터에서 찾음: REG_NO=${regNo}`) + return { + success: true, + data: fallbackData, + isUsingFallback: true + } + } + return { success: false, error: error instanceof Error ? error.message : 'REG_NO 조회 중 오류가 발생했습니다.' diff --git a/lib/basic-contract/status-detail/basic-contract-detail-table-toolbar-actions.tsx b/lib/basic-contract/status-detail/basic-contract-detail-table-toolbar-actions.tsx index 575582cf..3e7caee1 100644 --- a/lib/basic-contract/status-detail/basic-contract-detail-table-toolbar-actions.tsx +++ b/lib/basic-contract/status-detail/basic-contract-detail-table-toolbar-actions.tsx @@ -18,9 +18,10 @@ import { DialogTitle, } from "@/components/ui/dialog" import { Badge } from "@/components/ui/badge" -import { prepareFinalApprovalAction, quickFinalApprovalAction, resendContractsAction, updateLegalReviewStatusFromSSLVW } from "../service" +import { prepareFinalApprovalAction, quickFinalApprovalAction, resendContractsAction, updateLegalReviewStatusFromSSLVW, updateComplianceReviewStatusFromCPVW, requestComplianceInquiryAction } from "../service" import { BasicContractSignDialog } from "../vendor-table/basic-contract-sign-dialog" import { SSLVWPurInqReqDialog } from "@/components/common/legal/sslvw-pur-inq-req-dialog" +import { CPVWWabQustListViewDialog } from "@/components/common/legal/cpvw-wab-qust-list-view-dialog" import { prepareRedFlagResolutionApproval, requestRedFlagResolution } from "@/lib/compliance/red-flag-resolution" import { useRouter } from "next/navigation" import { useSession } from "next-auth/react" @@ -81,24 +82,26 @@ export function BasicContractDetailTableToolbarActions({ if (contract.completedAt !== null || !contract.signedFilePath) { return false; } - if (contract.legalReviewRequestedAt && !contract.legalReviewCompletedAt) { - return false; - } + // ⚠️ 법무/준법문의 완료 여부는 SSLVW/CPVW 상태 및 완료 시간에 의존하므로, + // 여기서는 legalReviewCompletedAt / complianceReviewCompletedAt 기반으로 + // 최종 승인 버튼을 막지 않습니다. (상태/시간은 UI 참고용으로만 사용) return true; }); - // 법무검토 요청 가능 여부 - // 1. 협의 완료됨 (negotiationCompletedAt 있음) OR - // 2. 협의 없음 (코멘트 없음, hasComments: false) + // 법무검토 요청 가능 여부 (준법서약 템플릿이 아닐 때만) + // 1. 협력업체 서명 완료 (vendorSignedAt 있음) + // 2. 협의 완료됨 (negotiationCompletedAt 있음) OR + // 3. 협의 없음 (코멘트 없음, hasComments: false) // 협의 중 (negotiationCompletedAt 없고 코멘트 있음)은 불가 - const canRequestLegalReview = hasSelectedRows && selectedRows.some(row => { + const canRequestLegalReview = !isComplianceTemplate && hasSelectedRows && selectedRows.some(row => { const contract = row.original; - // 이미 법무검토 요청된 계약서는 제외 - if (contract.legalReviewRequestedAt) { - return false; - } - // 이미 최종승인 완료된 계약서는 제외 - if (contract.completedAt) { + + // 필수 조건 확인: 최종승인 미완료, 법무검토 미요청, 협력업체 서명 완료 + if ( + contract.legalReviewRequestedAt || + contract.completedAt || + !contract.vendorSignedAt + ) { return false; } @@ -123,6 +126,35 @@ export function BasicContractDetailTableToolbarActions({ return false; }); + // 준법문의 버튼 활성화 가능 여부 + // 1. 협력업체 서명 완료 (vendorSignedAt 있음) + // 2. 협의 완료 (negotiationCompletedAt 있음) + // 3. 레드플래그 해소됨 (redFlagResolutionData에서 resolved 상태) + // 4. 이미 준법문의 요청되지 않음 (complianceReviewRequestedAt 없음) + const canRequestComplianceInquiry = hasSelectedRows && selectedRows.some(row => { + const contract = row.original; + + // 필수 조건 확인: 준법서약 템플릿, 최종승인 미완료, 협력업체 서명 완료, 협의 완료, 준법문의 미요청 + if ( + !isComplianceTemplate || + contract.completedAt || + !contract.vendorSignedAt || + !contract.negotiationCompletedAt || + contract.complianceReviewRequestedAt + ) { + return false; + } + + // 레드플래그 해소 확인 + const resolution = redFlagResolutionData[contract.id]; + // 레드플래그가 있는 경우, 해소되어야 함 + if (redFlagData[contract.id] === true && !resolution?.resolved) { + return false; + } + + return true; + }); + // 필터링된 계약서들 계산 const resendContracts = selectedRows.map(row => row.original) @@ -394,6 +426,47 @@ export function BasicContractDetailTableToolbarActions({ } } + // CPVW 데이터 선택 확인 핸들러 + const handleCPVWConfirm = async (selectedCPVWData: any[]) => { + if (!selectedCPVWData || selectedCPVWData.length === 0) { + toast.error("선택된 데이터가 없습니다.") + return + } + + if (selectedRows.length !== 1) { + toast.error("계약서 한 건을 선택해주세요.") + return + } + + try { + setLoading(true) + + // 선택된 계약서 ID들 추출 + const selectedContractIds = selectedRows.map(row => row.original.id) + + // 서버 액션 호출 + const result = await updateComplianceReviewStatusFromCPVW(selectedCPVWData, selectedContractIds) + + if (result.success) { + toast.success(result.message) + router.refresh() + table.toggleAllPageRowsSelected(false) + } else { + toast.error(result.message) + } + + if (result.errors && result.errors.length > 0) { + toast.warning(`일부 처리 실패: ${result.errors.join(', ')}`) + } + + } catch (error) { + console.error('CPVW 확인 처리 실패:', error) + toast.error('준법문의 상태 업데이트 중 오류가 발생했습니다.') + } finally { + setLoading(false) + } + } + // 빠른 승인 (서명 없이) const confirmQuickApproval = async () => { setLoading(true) @@ -541,9 +614,26 @@ export function BasicContractDetailTableToolbarActions({ const complianceInquiryUrl = 'http://60.101.207.55/Inquiry/Write/InquiryWrite.aspx' // 법무검토 요청 / 준법문의 - const handleRequestLegalReview = () => { + const handleRequestLegalReview = async () => { if (isComplianceTemplate) { - window.open(complianceInquiryUrl, '_blank', 'noopener,noreferrer') + // 준법문의: 요청일 기록 후 외부 URL 열기 + const selectedContractIds = selectedRows.map(row => row.original.id) + try { + setLoading(true) + const result = await requestComplianceInquiryAction(selectedContractIds) + if (result.success) { + toast.success(result.message) + router.refresh() + window.open(complianceInquiryUrl, '_blank', 'noopener,noreferrer') + } else { + toast.error(result.message) + } + } catch (error) { + console.error('준법문의 요청 처리 실패:', error) + toast.error('준법문의 요청 중 오류가 발생했습니다.') + } finally { + setLoading(false) + } return } setLegalReviewDialog(true) @@ -617,31 +707,72 @@ export function BasicContractDetailTableToolbarActions({ - {/* 법무검토 버튼 (SSLVW 데이터 조회) */} - + {/* 법무검토 버튼 (SSLVW 데이터 조회) - 준법서약 템플릿이 아닐 때만 표시 */} + {!isComplianceTemplate && ( + + )} + + {/* 준법문의 요청 데이터 조회 버튼 (준법서약 템플릿만) */} + {isComplianceTemplate && ( + + )} {/* 법무검토 요청 / 준법문의 버튼 */} - + {isComplianceTemplate ? ( + + ) : ( + + )} {/* 최종승인 버튼 */} + + {/* 로고 영역 */} +
+ + EVCP Logo + + {t(brandNameKey)} + + +
+ + {/* 네비게이션 메뉴 */} +
+ {isLoading ? ( +
+ +
+ ) : ( + +
+ + {tree.map((node) => { + // 드롭다운 메뉴 (menu_group with children) + if (isDropdownMenu(node)) { + return ( + + { + e.preventDefault(); + e.stopPropagation(); + toggleMenu(String(node.id)); + }} + onPointerEnter={(e) => e.preventDefault()} + onPointerMove={(e) => e.preventDefault()} + onPointerLeave={(e) => e.preventDefault()} + > + {getTitle(node)} + + + e.preventDefault()} + onPointerLeave={(e) => e.preventDefault()} + forceMount={ + openMenuKey === String(node.id) + ? true + : undefined + } + > + setOpenMenuKey("")} + /> + + + ); + } + + // 단일 링크 메뉴 (최상위 menu) + if (node.nodeType === 'menu' && node.menuPath) { + return ( + + + e.preventDefault()} + onPointerLeave={(e) => e.preventDefault()} + > + {getTitle(node)} + + + + ); + } + + return null; + })} + +
+
+ )} +
+ + {/* 우측 영역 */} +
+ {/* 데스크탑에서는 CommandMenu, 모바일에서는 검색 아이콘만 */} +
+ +
+ + + {/* 알림 버튼 */} + + + {/* 사용자 메뉴 */} + + + + + {initials || "?"} + + + + {t("user.my_account")} + + + {t("user.settings")} + + + + customSignOut({ + callbackUrl: `${window.location.origin}${basePath}`, + }) + } + > + {t("user.logout")} + + + +
+ + + + {/* 모바일 메뉴 */} + {isMobileMenuOpen && ( + + )} + + + ); +} diff --git a/components/layout/MobileMenuV2.tsx b/components/layout/MobileMenuV2.tsx new file mode 100644 index 00000000..c83ba779 --- /dev/null +++ b/components/layout/MobileMenuV2.tsx @@ -0,0 +1,160 @@ +"use client"; + +import * as React from "react"; +import Link from "next/link"; +import { cn } from "@/lib/utils"; +import { Button } from "@/components/ui/button"; +import { ScrollArea } from "@/components/ui/scroll-area"; +import { X, ChevronDown, ChevronRight } from "lucide-react"; +import type { MenuTreeNode } from "@/lib/menu-v2/types"; + +interface MobileMenuV2Props { + lng: string; + onClose: () => void; + tree: MenuTreeNode[]; + getTitle: (node: MenuTreeNode) => string; + getDescription: (node: MenuTreeNode) => string | null; +} + +export function MobileMenuV2({ + lng, + onClose, + tree, + getTitle, + getDescription, +}: MobileMenuV2Props) { + const [expandedGroups, setExpandedGroups] = React.useState>( + new Set() + ); + + const toggleGroup = (groupId: number) => { + setExpandedGroups((prev) => { + const next = new Set(prev); + if (next.has(groupId)) { + next.delete(groupId); + } else { + next.add(groupId); + } + return next; + }); + }; + + // 드롭다운 메뉴인지 판단 + const isDropdownMenu = (node: MenuTreeNode) => + node.nodeType === 'menu_group' && node.children && node.children.length > 0; + + return ( +
+ {/* 헤더 */} +
+ 메뉴 + +
+ + {/* 스크롤 영역 */} + +
+ {tree.map((node) => { + // 드롭다운 메뉴 (menu_group with children) + if (isDropdownMenu(node)) { + return ( +
+ {/* 메뉴그룹 헤더 */} + + + {/* 하위 메뉴 */} + {expandedGroups.has(node.id) && ( +
+ {node.children?.map((item) => { + if (item.nodeType === "group") { + // 그룹인 경우 + return ( +
+
+ {getTitle(item)} +
+
+ {item.children?.map((menu) => ( + + ))} +
+
+ ); + } else if (item.nodeType === "menu") { + // 직접 메뉴인 경우 + return ( + + ); + } + return null; + })} +
+ )} +
+ ); + } + + // 단일 링크 메뉴 (최상위 menu) + if (node.nodeType === 'menu' && node.menuPath) { + return ( + + ); + } + + return null; + })} +
+
+
+ ); +} + +interface MobileMenuLinkProps { + href: string; + title: string; + onClick: () => void; +} + +function MobileMenuLink({ href, title, onClick }: MobileMenuLinkProps) { + return ( + + {title} + + ); +} diff --git a/db/schema/index.ts b/db/schema/index.ts index 6463e0ec..022431cc 100644 --- a/db/schema/index.ts +++ b/db/schema/index.ts @@ -29,7 +29,11 @@ export * from './evaluation'; export * from './evaluationTarget'; export * from './evaluationCriteria'; export * from './projectGtc'; +// 기존 menu 스키마 (deprecated - menu-v2로 대체됨) export * from './menu'; + +// 새로운 메뉴 트리 스키마 (v2) +export * from './menu-v2'; export * from './information'; export * from './qna'; export * from './notice'; diff --git a/db/schema/menu-v2.ts b/db/schema/menu-v2.ts new file mode 100644 index 00000000..2d0282fa --- /dev/null +++ b/db/schema/menu-v2.ts @@ -0,0 +1,88 @@ +// db/schema/menu-v2.ts +import { pgTable, pgEnum, integer, varchar, text, timestamp, boolean, index, uniqueIndex } from "drizzle-orm/pg-core"; +import { relations } from "drizzle-orm"; +import { users } from "./users"; + +export const menuTreeNodeTypeEnum = pgEnum('menu_tree_node_type', [ + 'menu_group', // 메뉴그룹 (1단계) - 헤더에 표시되는 드롭다운 트리거 + 'group', // 그룹 (2단계) - 드롭다운 내 구분 영역 + 'menu', // 메뉴 (3단계) - 드롭다운 내 링크 + 'additional' // 추가 메뉴 - 최상위 단일 링크 (Dashboard, QNA, FAQ 등) +]); + +export const menuDomainEnum = pgEnum('menu_domain', [ + 'evcp', // 내부 사용자용 + 'partners' // 협력업체용 +]); + +export const menuTreeNodes = pgTable("menu_tree_nodes", { + id: integer("id").primaryKey().generatedAlwaysAsIdentity(), + + // 도메인 구분 + domain: menuDomainEnum("domain").notNull(), + + // 트리 구조 + parentId: integer("parent_id").references((): any => menuTreeNodes.id, { onDelete: "cascade" }), + nodeType: menuTreeNodeTypeEnum("node_type").notNull(), + sortOrder: integer("sort_order").notNull().default(0), + + // 다국어 텍스트 (DB 직접 관리) + titleKo: varchar("title_ko", { length: 255 }).notNull(), + titleEn: varchar("title_en", { length: 255 }), + descriptionKo: text("description_ko"), + descriptionEn: text("description_en"), + + // 메뉴 전용 필드 (nodeType === 'menu' 또는 'additional'일 때) + menuPath: varchar("menu_path", { length: 255 }), // href 값 (예: /evcp/projects) + icon: varchar("icon", { length: 100 }), + + // 권한 연동 + // evcp: Oracle DB SCR_ID 참조 + // partners: 자체 권한 시스템 (TODO) + scrId: varchar("scr_id", { length: 100 }), + + // 상태 + isActive: boolean("is_active").default(true).notNull(), + + // 담당자 (evcp 전용) + manager1Id: integer("manager1_id").references(() => users.id, { onDelete: "set null" }), + manager2Id: integer("manager2_id").references(() => users.id, { onDelete: "set null" }), + + createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(), + updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(), +}, (table) => ({ + domainIdx: index("menu_tree_domain_idx").on(table.domain), + parentIdx: index("menu_tree_parent_idx").on(table.parentId), + sortOrderIdx: index("menu_tree_sort_order_idx").on(table.sortOrder), + menuPathUnique: uniqueIndex("menu_tree_path_unique_idx").on(table.menuPath), + scrIdIdx: index("menu_tree_scr_id_idx").on(table.scrId), +})); + +// Relations 정의 +export const menuTreeNodesRelations = relations(menuTreeNodes, ({ one, many }) => ({ + parent: one(menuTreeNodes, { + fields: [menuTreeNodes.parentId], + references: [menuTreeNodes.id], + relationName: "parentChild", + }), + children: many(menuTreeNodes, { + relationName: "parentChild", + }), + manager1: one(users, { + fields: [menuTreeNodes.manager1Id], + references: [users.id], + relationName: "menuManager1", + }), + manager2: one(users, { + fields: [menuTreeNodes.manager2Id], + references: [users.id], + relationName: "menuManager2", + }), +})); + +// Type exports +export type MenuTreeNode = typeof menuTreeNodes.$inferSelect; +export type NewMenuTreeNode = typeof menuTreeNodes.$inferInsert; +export type NodeType = (typeof menuTreeNodeTypeEnum.enumValues)[number]; +export type MenuDomain = (typeof menuDomainEnum.enumValues)[number]; + diff --git a/db/seeds/menu-v2-seed.js b/db/seeds/menu-v2-seed.js new file mode 100644 index 00000000..e332f044 --- /dev/null +++ b/db/seeds/menu-v2-seed.js @@ -0,0 +1,231 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); + return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (g && (g = 0, op[0] && (_ = 0)), _) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.seedMenuTree = seedMenuTree; +// db/seeds/menu-v2-seed.ts +var menuConfig_1 = require("@/config/menuConfig"); +var menu_json_1 = require("@/i18n/locales/ko/menu.json"); +var menu_json_2 = require("@/i18n/locales/en/menu.json"); +var db_1 = require("@/db/db"); +var menu_v2_1 = require("@/db/schema/menu-v2"); +// 중첩 키로 번역 값 가져오기 +function getTranslation(key, locale) { + var translations = locale === 'ko' ? menu_json_1.default : menu_json_2.default; + var keys = key.split('.'); + var value = translations; + for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { + var k = keys_1[_i]; + if (typeof value === 'object' && value !== null) { + value = value[k]; + } + else { + return key; + } + if (value === undefined) + return key; + } + return typeof value === 'string' ? value : key; +} +function seedMenuTree() { + return __awaiter(this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + console.log('🌱 Starting menu tree seeding...'); + // 기존 데이터 삭제 + return [4 /*yield*/, db_1.default.delete(menu_v2_1.menuTreeNodes)]; + case 1: + // 기존 데이터 삭제 + _a.sent(); + console.log('✅ Cleared existing menu tree data'); + // evcp 도메인 seed + return [4 /*yield*/, seedDomainMenus('evcp', menuConfig_1.mainNav, menuConfig_1.additionalNav)]; + case 2: + // evcp 도메인 seed + _a.sent(); + console.log('✅ Seeded evcp menu tree'); + // partners 도메인 seed + return [4 /*yield*/, seedDomainMenus('partners', menuConfig_1.mainNavVendor, menuConfig_1.additionalNavVendor)]; + case 3: + // partners 도메인 seed + _a.sent(); + console.log('✅ Seeded partners menu tree'); + console.log('🎉 Menu tree seeding completed!'); + return [2 /*return*/]; + } + }); + }); +} +function seedDomainMenus(domain, navConfig, additionalConfig) { + return __awaiter(this, void 0, void 0, function () { + var globalSortOrder, _loop_1, _i, navConfig_1, section, additionalSortOrder, _a, additionalConfig_1, item; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + globalSortOrder = 0; + _loop_1 = function (section) { + var menuGroup, groupedItems, groupSortOrder, _c, groupedItems_1, _d, groupKey, items, parentId, group, menuSortOrder, _e, items_1, item; + return __generator(this, function (_f) { + switch (_f.label) { + case 0: return [4 /*yield*/, db_1.default.insert(menu_v2_1.menuTreeNodes).values({ + domain: domain, + parentId: null, + nodeType: 'menu_group', + titleKo: getTranslation(section.titleKey, 'ko'), + titleEn: getTranslation(section.titleKey, 'en'), + sortOrder: globalSortOrder++, + isActive: true, + }).returning()]; + case 1: + menuGroup = (_f.sent())[0]; + groupedItems = new Map(); + section.items.forEach(function (item) { + var groupKey = item.groupKey || '__default__'; + if (!groupedItems.has(groupKey)) { + groupedItems.set(groupKey, []); + } + groupedItems.get(groupKey).push(item); + }); + groupSortOrder = 0; + _c = 0, groupedItems_1 = groupedItems; + _f.label = 2; + case 2: + if (!(_c < groupedItems_1.length)) return [3 /*break*/, 9]; + _d = groupedItems_1[_c], groupKey = _d[0], items = _d[1]; + parentId = menuGroup.id; + if (!(groupKey !== '__default__')) return [3 /*break*/, 4]; + return [4 /*yield*/, db_1.default.insert(menu_v2_1.menuTreeNodes).values({ + domain: domain, + parentId: menuGroup.id, + nodeType: 'group', + titleKo: getTranslation(groupKey, 'ko'), + titleEn: getTranslation(groupKey, 'en'), + sortOrder: groupSortOrder++, + isActive: true, + }).returning()]; + case 3: + group = (_f.sent())[0]; + parentId = group.id; + _f.label = 4; + case 4: + menuSortOrder = 0; + _e = 0, items_1 = items; + _f.label = 5; + case 5: + if (!(_e < items_1.length)) return [3 /*break*/, 8]; + item = items_1[_e]; + return [4 /*yield*/, db_1.default.insert(menu_v2_1.menuTreeNodes).values({ + domain: domain, + parentId: parentId, + nodeType: 'menu', + titleKo: getTranslation(item.titleKey, 'ko'), + titleEn: getTranslation(item.titleKey, 'en'), + descriptionKo: item.descriptionKey ? getTranslation(item.descriptionKey, 'ko') : null, + descriptionEn: item.descriptionKey ? getTranslation(item.descriptionKey, 'en') : null, + menuPath: item.href, + icon: item.icon || null, + sortOrder: menuSortOrder++, + isActive: true, + })]; + case 6: + _f.sent(); + _f.label = 7; + case 7: + _e++; + return [3 /*break*/, 5]; + case 8: + _c++; + return [3 /*break*/, 2]; + case 9: return [2 /*return*/]; + } + }); + }; + _i = 0, navConfig_1 = navConfig; + _b.label = 1; + case 1: + if (!(_i < navConfig_1.length)) return [3 /*break*/, 4]; + section = navConfig_1[_i]; + return [5 /*yield**/, _loop_1(section)]; + case 2: + _b.sent(); + _b.label = 3; + case 3: + _i++; + return [3 /*break*/, 1]; + case 4: + additionalSortOrder = 0; + _a = 0, additionalConfig_1 = additionalConfig; + _b.label = 5; + case 5: + if (!(_a < additionalConfig_1.length)) return [3 /*break*/, 8]; + item = additionalConfig_1[_a]; + return [4 /*yield*/, db_1.default.insert(menu_v2_1.menuTreeNodes).values({ + domain: domain, + parentId: null, + nodeType: 'additional', + titleKo: getTranslation(item.titleKey, 'ko'), + titleEn: getTranslation(item.titleKey, 'en'), + descriptionKo: item.descriptionKey ? getTranslation(item.descriptionKey, 'ko') : null, + descriptionEn: item.descriptionKey ? getTranslation(item.descriptionKey, 'en') : null, + menuPath: item.href, + sortOrder: additionalSortOrder++, + isActive: true, + })]; + case 6: + _b.sent(); + _b.label = 7; + case 7: + _a++; + return [3 /*break*/, 5]; + case 8: return [2 /*return*/]; + } + }); + }); +} +// CLI에서 직접 실행 가능하도록 +if (require.main === module) { + seedMenuTree() + .then(function () { + console.log('Seed completed successfully'); + process.exit(0); + }) + .catch(function (error) { + console.error('Seed failed:', error); + process.exit(1); + }); +} diff --git a/db/seeds/menu-v2-seed.ts b/db/seeds/menu-v2-seed.ts new file mode 100644 index 00000000..0c6b310d --- /dev/null +++ b/db/seeds/menu-v2-seed.ts @@ -0,0 +1,145 @@ +// db/seeds/menu-v2-seed.ts +import { mainNav, additionalNav, mainNavVendor, additionalNavVendor, MenuSection, MenuItem } from "@/config/menuConfig"; +import koMenu from '@/i18n/locales/ko/menu.json'; +import enMenu from '@/i18n/locales/en/menu.json'; +import db from "@/db/db"; +import { menuTreeNodes } from "@/db/schema/menu-v2"; +import type { MenuDomain } from "@/lib/menu-v2/types"; + +type TranslationObject = { [key: string]: string | TranslationObject }; + +// 중첩 키로 번역 값 가져오기 +function getTranslation(key: string, locale: 'ko' | 'en'): string { + const translations: TranslationObject = locale === 'ko' ? koMenu : enMenu; + const keys = key.split('.'); + let value: string | TranslationObject | undefined = translations; + + for (const k of keys) { + if (typeof value === 'object' && value !== null) { + value = value[k]; + } else { + return key; + } + if (value === undefined) return key; + } + + return typeof value === 'string' ? value : key; +} + +export async function seedMenuTree() { + console.log('🌱 Starting menu tree seeding...'); + + // 기존 데이터 삭제 + await db.delete(menuTreeNodes); + console.log('✅ Cleared existing menu tree data'); + + // evcp 도메인 seed + await seedDomainMenus('evcp', mainNav, additionalNav); + console.log('✅ Seeded evcp menu tree'); + + // partners 도메인 seed + await seedDomainMenus('partners', mainNavVendor, additionalNavVendor); + console.log('✅ Seeded partners menu tree'); + + console.log('🎉 Menu tree seeding completed!'); +} + +async function seedDomainMenus( + domain: MenuDomain, + navConfig: MenuSection[], + additionalConfig: MenuItem[] +) { + // 최상위 sortOrder (메뉴그룹과 최상위 메뉴 모두 같은 레벨에서 정렬) + let topLevelSortOrder = 0; + + // 메인 네비게이션 (메뉴그룹 → 그룹 → 메뉴) + for (const section of navConfig) { + // 1단계: 메뉴그룹 생성 + const [menuGroup] = await db.insert(menuTreeNodes).values({ + domain, + parentId: null, + nodeType: 'menu_group', + titleKo: getTranslation(section.titleKey, 'ko'), + titleEn: getTranslation(section.titleKey, 'en'), + sortOrder: topLevelSortOrder++, + isActive: true, + }).returning(); + + // groupKey별로 그룹화 + const groupedItems = new Map(); + section.items.forEach(item => { + const groupKey = item.groupKey || '__default__'; + if (!groupedItems.has(groupKey)) { + groupedItems.set(groupKey, []); + } + groupedItems.get(groupKey)!.push(item); + }); + + let groupSortOrder = 0; + for (const [groupKey, items] of groupedItems) { + let parentId = menuGroup.id; + + // groupKey가 있으면 2단계 그룹 생성 + if (groupKey !== '__default__') { + const [group] = await db.insert(menuTreeNodes).values({ + domain, + parentId: menuGroup.id, + nodeType: 'group', + titleKo: getTranslation(groupKey, 'ko'), + titleEn: getTranslation(groupKey, 'en'), + sortOrder: groupSortOrder++, + isActive: true, + }).returning(); + parentId = group.id; + } + + // 3단계: 메뉴 생성 + let menuSortOrder = 0; + for (const item of items) { + await db.insert(menuTreeNodes).values({ + domain, + parentId, + nodeType: 'menu', + titleKo: getTranslation(item.titleKey, 'ko'), + titleEn: getTranslation(item.titleKey, 'en'), + descriptionKo: item.descriptionKey ? getTranslation(item.descriptionKey, 'ko') : null, + descriptionEn: item.descriptionKey ? getTranslation(item.descriptionKey, 'en') : null, + menuPath: item.href, + icon: item.icon || null, + sortOrder: menuSortOrder++, + isActive: true, + }); + } + } + } + + // 최상위 단일 링크 메뉴 (기존 additional) + // nodeType을 'menu'로 설정하고 parentId를 null로 유지 + for (const item of additionalConfig) { + await db.insert(menuTreeNodes).values({ + domain, + parentId: null, + nodeType: 'menu', // 'additional' 대신 'menu' 사용 + titleKo: getTranslation(item.titleKey, 'ko'), + titleEn: getTranslation(item.titleKey, 'en'), + descriptionKo: item.descriptionKey ? getTranslation(item.descriptionKey, 'ko') : null, + descriptionEn: item.descriptionKey ? getTranslation(item.descriptionKey, 'en') : null, + menuPath: item.href, + sortOrder: topLevelSortOrder++, // 메뉴그룹 다음 순서 + isActive: true, + }); + } +} + +// CLI에서 직접 실행 가능하도록 +if (require.main === module) { + seedMenuTree() + .then(() => { + console.log('Seed completed successfully'); + process.exit(0); + }) + .catch((error) => { + console.error('Seed failed:', error); + process.exit(1); + }); +} diff --git a/hooks/use-visible-menu-tree.ts b/hooks/use-visible-menu-tree.ts new file mode 100644 index 00000000..bc7f1f73 --- /dev/null +++ b/hooks/use-visible-menu-tree.ts @@ -0,0 +1,49 @@ +"use client"; + +import { useState, useEffect, useCallback } from "react"; +import { getVisibleMenuTree } from "@/lib/menu-v2/permission-service"; +import type { MenuDomain, MenuTreeNode, MenuTreeActiveResult } from "@/lib/menu-v2/types"; + +interface UseVisibleMenuTreeResult extends MenuTreeActiveResult { + isLoading: boolean; + error: Error | null; + refetch: () => Promise; +} + +/** + * Hook to fetch user's visible menu tree (filtered by permissions) + * Tree contains both menu groups (dropdowns) and top-level menus (single links) + */ +export function useVisibleMenuTree(domain: MenuDomain): UseVisibleMenuTreeResult { + const [tree, setTree] = useState([]); + const [isLoading, setIsLoading] = useState(true); + const [error, setError] = useState(null); + + const fetchMenuTree = useCallback(async () => { + setIsLoading(true); + setError(null); + + try { + // Call server action directly + const result = await getVisibleMenuTree(domain); + setTree(result.tree); + } catch (err) { + console.error("Error fetching visible menu tree:", err); + setError(err instanceof Error ? err : new Error("Unknown error")); + setTree([]); + } finally { + setIsLoading(false); + } + }, [domain]); + + useEffect(() => { + fetchMenuTree(); + }, [fetchMenuTree]); + + return { + tree, + isLoading, + error, + refetch: fetchMenuTree, + }; +} diff --git a/lib/information/service.ts b/lib/information/service.ts index 02efe616..39e810e4 100644 --- a/lib/information/service.ts +++ b/lib/information/service.ts @@ -3,7 +3,7 @@ import { getErrorMessage } from "@/lib/handle-error" import { desc, or, eq } from "drizzle-orm" import db from "@/db/db" -import { pageInformation, menuAssignments, users } from "@/db/schema" +import { pageInformation, menuTreeNodes, users } from "@/db/schema" import { saveDRMFile } from "@/lib/file-stroage" import { decryptWithServerAction } from "@/components/drm/drmUtils" @@ -144,27 +144,27 @@ export async function checkInformationEditPermission(pagePath: string, userId: s pagePath // 원본 경로 정확한 매칭 ] - // menu_assignments에서 해당 pagePath와 매칭되는 메뉴 찾기 - const menuAssignment = await db + // menu_tree_nodes에서 해당 pagePath와 매칭되는 메뉴 찾기 + const menuNode = await db .select() - .from(menuAssignments) + .from(menuTreeNodes) .where( or( - ...menuPathQueries.map(path => eq(menuAssignments.menuPath, path)) + ...menuPathQueries.map(path => eq(menuTreeNodes.menuPath, path)) ) ) .limit(1) - if (menuAssignment.length === 0) { + if (menuNode.length === 0) { // 매칭되는 메뉴가 없으면 권한 없음 return false } - const assignment = menuAssignment[0] + const node = menuNode[0] const userIdNumber = parseInt(userId) // 현재 사용자가 manager1 또는 manager2인지 확인 - return assignment.manager1Id === userIdNumber || assignment.manager2Id === userIdNumber + return node.manager1Id === userIdNumber || node.manager2Id === userIdNumber } catch (error) { console.error("Failed to check information edit permission:", error) return false @@ -176,17 +176,21 @@ export async function getEditPermissionDirect(pagePath: string, userId: string) return await checkInformationEditPermission(pagePath, userId) } -// menu_assignments 기반으로 page_information 동기화 +// menu_tree_nodes 기반으로 page_information 동기화 export async function syncInformationFromMenuAssignments() { try { - // menu_assignments에서 모든 메뉴 가져오기 - const menuItems = await db.select().from(menuAssignments); + // menu_tree_nodes에서 메뉴 타입 노드만 가져오기 (menuPath가 있는 것) + const menuItems = await db.select() + .from(menuTreeNodes) + .where(eq(menuTreeNodes.nodeType, 'menu')); let processedCount = 0; // upsert를 사용하여 각 메뉴 항목 처리 for (const menu of menuItems) { try { + if (!menu.menuPath) continue; + // 맨 앞의 / 제거하여 pagePath 정규화 const normalizedPagePath = menu.menuPath.startsWith('/') ? menu.menuPath.slice(1) @@ -195,14 +199,14 @@ export async function syncInformationFromMenuAssignments() { await db.insert(pageInformation) .values({ pagePath: normalizedPagePath, - pageName: menu.menuTitle, + pageName: menu.titleKo, informationContent: "", isActive: true // 기본값으로 활성화 }) .onConflictDoUpdate({ target: pageInformation.pagePath, set: { - pageName: menu.menuTitle, + pageName: menu.titleKo, updatedAt: new Date() } }); @@ -213,8 +217,6 @@ export async function syncInformationFromMenuAssignments() { } } - // 캐시 무효화 제거됨 - return { success: true, message: `페이지 정보 동기화 완료: ${processedCount}개 처리됨` diff --git a/lib/menu-v2/components/add-node-dialog.tsx b/lib/menu-v2/components/add-node-dialog.tsx new file mode 100644 index 00000000..b6762820 --- /dev/null +++ b/lib/menu-v2/components/add-node-dialog.tsx @@ -0,0 +1,186 @@ +"use client"; + +import { useForm } from "react-hook-form"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import type { + MenuDomain, + CreateMenuGroupInput, + CreateGroupInput, + CreateTopLevelMenuInput +} from "../types"; + +type DialogType = "menu_group" | "group" | "top_level_menu"; + +interface AddNodeDialogProps { + open: boolean; + onOpenChange: (open: boolean) => void; + type: DialogType; + domain: MenuDomain; + parentId?: number; // group 생성 시 필요 + onSave: (data: CreateMenuGroupInput | CreateGroupInput | CreateTopLevelMenuInput) => Promise; +} + +interface FormData { + titleKo: string; + titleEn: string; + menuPath: string; +} + +export function AddNodeDialog({ + open, + onOpenChange, + type, + domain, + parentId, + onSave, +}: AddNodeDialogProps) { + const { + register, + handleSubmit, + reset, + formState: { isSubmitting, errors }, + } = useForm({ + defaultValues: { + titleKo: "", + titleEn: "", + menuPath: "", + }, + }); + + const getTitle = () => { + switch (type) { + case "menu_group": + return "Add Menu Group"; + case "group": + return "Add Group"; + case "top_level_menu": + return "Add Top-Level Menu"; + default: + return "Add"; + } + }; + + const getDescription = () => { + switch (type) { + case "menu_group": + return "A dropdown trigger displayed in the header navigation."; + case "group": + return "Groups menus within a menu group."; + case "top_level_menu": + return "A single link displayed in the header navigation."; + default: + return ""; + } + }; + + const onSubmit = async (data: FormData) => { + let saveData: CreateMenuGroupInput | CreateGroupInput | CreateTopLevelMenuInput; + + if (type === "menu_group") { + saveData = { + titleKo: data.titleKo, + titleEn: data.titleEn || undefined, + }; + } else if (type === "group" && parentId) { + saveData = { + parentId, + titleKo: data.titleKo, + titleEn: data.titleEn || undefined, + }; + } else if (type === "top_level_menu") { + saveData = { + titleKo: data.titleKo, + titleEn: data.titleEn || undefined, + menuPath: data.menuPath, + }; + } else { + return; + } + + await onSave(saveData); + reset(); + onOpenChange(false); + }; + + const handleClose = () => { + reset(); + onOpenChange(false); + }; + + return ( + + + + {getTitle()} + {getDescription()} + + +
+
+ {/* Korean Name */} +
+ + + {errors.titleKo && ( +

{errors.titleKo.message}

+ )} +
+ + {/* English Name */} +
+ + +
+ + {/* Menu Path for Top-Level Menu */} + {type === "top_level_menu" && ( +
+ + + {errors.menuPath && ( +

{errors.menuPath.message}

+ )} +

+ e.g., /{domain}/report, /{domain}/faq +

+
+ )} +
+ + + + + +
+
+
+ ); +} diff --git a/lib/menu-v2/components/domain-tabs.tsx b/lib/menu-v2/components/domain-tabs.tsx new file mode 100644 index 00000000..e52fa80b --- /dev/null +++ b/lib/menu-v2/components/domain-tabs.tsx @@ -0,0 +1,25 @@ +"use client"; + +import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import type { MenuDomain } from "../types"; + +interface DomainTabsProps { + value: MenuDomain; + onChange: (domain: MenuDomain) => void; +} + +export function DomainTabs({ value, onChange }: DomainTabsProps) { + return ( + onChange(v as MenuDomain)}> + + + EVCP (Internal) + + + Partners (Vendors) + + + + ); +} + diff --git a/lib/menu-v2/components/edit-node-dialog.tsx b/lib/menu-v2/components/edit-node-dialog.tsx new file mode 100644 index 00000000..9631a611 --- /dev/null +++ b/lib/menu-v2/components/edit-node-dialog.tsx @@ -0,0 +1,215 @@ +"use client"; + +import { useEffect } from "react"; +import { useForm } from "react-hook-form"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Textarea } from "@/components/ui/textarea"; +import { Switch } from "@/components/ui/switch"; +import type { MenuTreeNode, UpdateNodeInput } from "../types"; + +interface EditNodeDialogProps { + open: boolean; + onOpenChange: (open: boolean) => void; + node: MenuTreeNode | null; + onSave: (nodeId: number, data: UpdateNodeInput) => Promise; +} + +interface FormData { + titleKo: string; + titleEn: string; + descriptionKo: string; + descriptionEn: string; + scrId: string; + isActive: boolean; +} + +export function EditNodeDialog({ + open, + onOpenChange, + node, + onSave, +}: EditNodeDialogProps) { + const { + register, + handleSubmit, + reset, + setValue, + watch, + formState: { isSubmitting }, + } = useForm({ + defaultValues: { + titleKo: "", + titleEn: "", + descriptionKo: "", + descriptionEn: "", + scrId: "", + isActive: true, + }, + }); + + const isActive = watch("isActive"); + + useEffect(() => { + if (node) { + reset({ + titleKo: node.titleKo, + titleEn: node.titleEn || "", + descriptionKo: node.descriptionKo || "", + descriptionEn: node.descriptionEn || "", + scrId: node.scrId || "", + isActive: node.isActive, + }); + } + }, [node, reset]); + + const onSubmit = async (data: FormData) => { + if (!node) return; + + await onSave(node.id, { + titleKo: data.titleKo, + titleEn: data.titleEn || undefined, + descriptionKo: data.descriptionKo || undefined, + descriptionEn: data.descriptionEn || undefined, + scrId: data.scrId || undefined, + isActive: data.isActive, + }); + + onOpenChange(false); + }; + + const getTypeLabel = () => { + switch (node?.nodeType) { + case "menu_group": + return "Menu Group"; + case "group": + return "Group"; + case "menu": + return "Menu"; + case "additional": + return "Additional Menu"; + default: + return "Node"; + } + }; + + const showMenuFields = node?.nodeType === "menu" || node?.nodeType === "additional"; + + return ( + + + + Edit {getTypeLabel()} + + {node?.menuPath && ( + {node.menuPath} + )} + + + +
+
+ {/* Korean Name */} +
+ + +
+ + {/* English Name */} +
+ + +
+ + {/* Korean Description */} + {showMenuFields && ( +
+ +