diff options
Diffstat (limited to 'lib/forms-plant/services.ts')
| -rw-r--r-- | lib/forms-plant/services.ts | 458 |
1 files changed, 252 insertions, 206 deletions
diff --git a/lib/forms-plant/services.ts b/lib/forms-plant/services.ts index 7e1976e6..219f36e4 100644 --- a/lib/forms-plant/services.ts +++ b/lib/forms-plant/services.ts @@ -7,18 +7,18 @@ import fs from "fs/promises"; import { v4 as uuidv4 } from "uuid"; import db from "@/db/db"; import { - formEntries,formEntriesPlant, - formMetas,formsPlant, + formEntries, + formMetas, forms, tagClassAttributes, tagClasses, - tags,tagsPlant, + tags, tagSubfieldOptions, tagSubfields, tagTypeClassFormMappings, tagTypes, - vendorDataReportTempsPlant, - VendorDataReportTempsPlant, + vendorDataReportTemps, + VendorDataReportTemps, } 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"; @@ -29,7 +29,6 @@ import { contractItems, contracts, items, projects } from "@/db/schema"; import { getSEDPToken } from "../sedp/sedp-token"; import { decryptWithServerAction } from "@/components/drm/drmUtils"; import { deleteFile, saveFile } from "@/lib/file-stroage"; -import { Register } from "@/components/form-data-plant/form-data-table-columns"; export type FormInfo = InferSelectModel<typeof forms>; @@ -165,8 +164,7 @@ export interface EditableFieldsInfo { // TAG별 편집 가능 필드 조회 함수 export async function getEditableFieldsByTag( - projectCode: string, - packageCode: string, + contractItemId: number, projectId: number ): Promise<Map<string, string[]>> { try { @@ -176,11 +174,8 @@ export async function getEditableFieldsByTag( tagNo: tags.tagNo, tagClass: tags.class }) - .from(tagsPlant) - .where( - eq(tagsPlant.projectCode, projectCode), - eq(tagsPlant.packageCode, packageCode), - ); + .from(tags) + .where(eq(tags.contractItemId, contractItemId)); const editableFieldsMap = new Map<string, string[]>(); @@ -233,17 +228,26 @@ export async function getEditableFieldsByTag( * data가 배열이면 그 배열을 반환, * 그리고 이 로직 전체를 unstable_cache로 감싸 캐싱. */ -export async function getFormData(formCode: string, projectCode: string, packageCode:string) { +export async function getFormData(formCode: string, contractItemId: number) { try { - const project = await db.query.projects.findFirst({ - where: eq(projects.code, projectCode), - columns: { - id: true - } - }); + // 기존 로직으로 projectId, columns, data 가져오기 + const contractItemResult = await db + .select({ + projectId: projects.id + }) + .from(contractItems) + .innerJoin(contracts, eq(contractItems.contractId, contracts.id)) + .innerJoin(projects, eq(contracts.projectId, projects.id)) + .where(eq(contractItems.id, contractItemId)) + .limit(1); + + if (contractItemResult.length === 0) { + console.warn(`[getFormData] No contract item found with ID: ${contractItemId}`); + return { columns: null, data: [], editableFieldsMap: new Map() }; + } - const projectId = project.id; + const projectId = contractItemResult[0].projectId; const metaRows = await db .select() @@ -265,15 +269,14 @@ export async function getFormData(formCode: string, projectCode: string, package const entryRows = await db .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode), + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) - .orderBy(desc(formEntriesPlant.updatedAt)) + .orderBy(desc(formEntries.updatedAt)) .limit(1); const entry = entryRows[0] ?? null; @@ -318,7 +321,7 @@ export async function getFormData(formCode: string, projectCode: string, package } // *** 새로 추가: 편집 가능 필드 정보 계산 *** - const editableFieldsMap = await getEditableFieldsByTag(projectCode,packageCode ,projectId); + const editableFieldsMap = await getEditableFieldsByTag(contractItemId, projectId); return { columns, data, editableFieldsMap }; @@ -328,16 +331,24 @@ export async function getFormData(formCode: string, projectCode: string, package // Fallback logic (기존과 동일하게 editableFieldsMap 추가) try { - console.log(`[getFormData] Fallback DB query for (${formCode}, ${packageCode})`); + console.log(`[getFormData] Fallback DB query for (${formCode}, ${contractItemId})`); - const project = await db.query.projects.findFirst({ - where: eq(projects.code, projectCode), - columns: { - id: true - } - }); + const contractItemResult = await db + .select({ + projectId: projects.id + }) + .from(contractItems) + .innerJoin(contracts, eq(contractItems.contractId, contracts.id)) + .innerJoin(projects, eq(contracts.projectId, projects.id)) + .where(eq(contractItems.id, contractItemId)) + .limit(1); + + if (contractItemResult.length === 0) { + console.warn(`[getFormData] Fallback: No contract item found with ID: ${contractItemId}`); + return { columns: null, data: [], editableFieldsMap: new Map() }; + } - const projectId = project.id; + const projectId = contractItemResult[0].projectId; const metaRows = await db .select() @@ -359,15 +370,14 @@ export async function getFormData(formCode: string, projectCode: string, package const entryRows = await db .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode) + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) - .orderBy(desc(formEntriesPlant.updatedAt)) + .orderBy(desc(formEntries.updatedAt)) .limit(1); const entry = entryRows[0] ?? null; @@ -396,7 +406,7 @@ export async function getFormData(formCode: string, projectCode: string, package } // Fallback에서도 편집 가능 필드 정보 계산 - const editableFieldsMap = await getEditableFieldsByTag(projectCode, packageCode, projectId); + const editableFieldsMap = await getEditableFieldsByTag(contractItemId, projectId); return { columns, data, projectId, editableFieldsMap }; } catch (dbError) { @@ -405,7 +415,7 @@ export async function getFormData(formCode: string, projectCode: string, package } } } -/** +/**1 * contractId와 formCode(itemCode)를 사용하여 contractItemId를 찾는 서버 액션 * * @param contractId - 계약 ID @@ -507,26 +517,24 @@ export async function getPackageCodeById(contractItemId: number): Promise<string export async function syncMissingTags( - projectCode: string, - packageCode: string, + contractItemId: number, formCode: string ) { // (1) Ensure there's a row in `forms` matching (contractItemId, formCode). const [formRow] = await db .select() - .from(formsPlant) + .from(forms) .where( and( - eq(formsPlant.projectCode, projectCode), - eq(formsPlant.packageCode, packageCode), - eq(formsPlant.formCode, formCode) + eq(forms.contractItemId, contractItemId), + eq(forms.formCode, formCode) ) ) .limit(1); if (!formRow) { throw new Error( - `Form not found for projectCode=${projectCode}, formCode=${formCode}` + `Form not found for contractItemId=${contractItemId}, formCode=${formCode}` ); } @@ -550,28 +558,26 @@ export async function syncMissingTags( // (3) Fetch all matching `tags` for the contractItemId + any of the (tagType, class) pairs. const tagRows = await db .select() - .from(tagsPlant) - .where(and(eq(tagsPlant.packageCode, packageCode),eq(tagsPlant.projectCode, projectCode), or(...orConditions))); + .from(tags) + .where(and(eq(tags.contractItemId, contractItemId), or(...orConditions))); // (4) Fetch (or create) a single `formEntries` row for (contractItemId, formCode). let [entry] = await db .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( - eq(formEntriesPlant.packageCode, packageCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.formCode, formCode) + eq(formEntries.contractItemId, contractItemId), + eq(formEntries.formCode, formCode) ) ) .limit(1); if (!entry) { const [inserted] = await db - .insert(formEntriesPlant) + .insert(formEntries) .values({ - projectCode, - packageCode, + contractItemId, formCode, data: [], // Initialize with empty array }) @@ -640,13 +646,13 @@ export async function syncMissingTags( // (6) 실제로 추가되거나 수정되거나 삭제된 게 있다면 DB에 반영 if (createdCount > 0 || updatedCount > 0 || deletedCount > 0) { await db - .update(formEntriesPlant) + .update(formEntries) .set({ data: updatedData }) - .where(eq(formEntriesPlant.id, entry.id)); + .where(eq(formEntries.id, entry.id)); } // 캐시 무효화 등 후처리 - // revalidateTag(`form-data-${formCode}-${projectCode}`); + revalidateTag(`form-data-${formCode}-${contractItemId}`); return { createdCount, updatedCount, deletedCount }; } @@ -675,8 +681,7 @@ export interface UpdateResponse { export async function updateFormDataInDB( formCode: string, - projectCode: string, - packageCode: string, + contractItemId: number, newData: Record<string, any> ): Promise<UpdateResponse> { try { @@ -692,12 +697,11 @@ export async function updateFormDataInDB( // 2) row 찾기 (단 하나) const entries = await db .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( eq(formEntries.formCode, formCode), - eq(formEntries.projectCode, projectCode), - eq(formEntries.packageCode, packageCode), + eq(formEntries.contractItemId, contractItemId) ) ) .limit(1); @@ -752,12 +756,12 @@ export async function updateFormDataInDB( // 6) DB UPDATE try { await db - .update(formEntriesPlant) + .update(formEntries) .set({ data: updatedArray, updatedAt: new Date(), // 업데이트 시간도 갱신 }) - .where(eq(formEntriesPlant.id, entry.id)); + .where(eq(formEntries.id, entry.id)); } catch (dbError) { console.error("Database update error:", dbError); @@ -777,7 +781,7 @@ export async function updateFormDataInDB( // 7) Cache 무효화 try { // 캐시 태그를 form-data-${formCode}-${contractItemId} 형태로 가정 - const cacheTag = `form-data-${formCode}-${projectCode}`; + const cacheTag = `form-data-${formCode}-${contractItemId}`; console.log(cacheTag, "update") revalidateTag(cacheTag); } catch (cacheError) { @@ -810,8 +814,7 @@ export async function updateFormDataInDB( export async function updateFormDataBatchInDB( formCode: string, - projectCode: string, - packageCode: string, + contractItemId: number, newDataArray: Record<string, any>[] ): Promise<UpdateResponse> { try { @@ -836,12 +839,11 @@ export async function updateFormDataBatchInDB( // 1) DB에서 현재 데이터 가져오기 const entries = await db .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode), + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) .limit(1); @@ -849,7 +851,7 @@ export async function updateFormDataBatchInDB( if (!entries || entries.length === 0) { return { success: false, - message: `폼 데이터를 찾을 수 없습니다. (formCode=${formCode}, projectCode=${projectCode})`, + message: `폼 데이터를 찾을 수 없습니다. (formCode=${formCode}, contractItemId=${contractItemId})`, }; } @@ -916,12 +918,12 @@ export async function updateFormDataBatchInDB( // 3) DB에 한 번만 저장 try { await db - .update(formEntriesPlant) + .update(formEntries) .set({ data: updatedArray, updatedAt: new Date(), }) - .where(eq(formEntriesPlant.id, entry.id)); + .where(eq(formEntries.id, entry.id)); } catch (dbError) { console.error("Database update error:", dbError); @@ -950,7 +952,7 @@ export async function updateFormDataBatchInDB( // 4) 캐시 무효화 try { - const cacheTag = `form-data-${formCode}-${projectCode}`; + const cacheTag = `form-data-${formCode}-${contractItemId}`; console.log(`Cache invalidated: ${cacheTag}`); revalidateTag(cacheTag); } catch (cacheError) { @@ -1041,37 +1043,26 @@ export async function fetchFormMetadata( } type GetReportFileList = ( - projectCode: string, - packageCode: string, - formCode: string, - mode: string + packageId: string, + formCode: string ) => Promise<{ formId: number; }>; -export const getFormId: GetReportFileList = async (projectCode, packageCode, formCode, mode) => { +export const getFormId: GetReportFileList = async (packageId, formCode) => { const result: { formId: number } = { formId: 0, }; try { - // mode에 따른 조건 배열 생성 - const conditions = [ - eq(formsPlant.formCode, formCode), - eq(formsPlant.projectCode, projectCode), - eq(formsPlant.packageCode, packageCode), - ]; - - // mode에 따라 추가 조건 설정 - if (mode === "IM") { - conditions.push(eq(formsPlant.im, true)); - } else if (mode === "ENG") { - conditions.push(eq(formsPlant.eng, true)); - } - const [targetForm] = await db .select() - .from(formsPlant) - .where(and(...conditions)); + .from(forms) + .where( + and( + eq(forms.formCode, formCode), + eq(forms.contractItemId, Number(packageId)) + ) + ); if (!targetForm) { throw new Error("Not Found Target Form"); @@ -1081,34 +1072,30 @@ export const getFormId: GetReportFileList = async (projectCode, packageCode, for result.formId = formId; } catch (err) { - console.error("Error getting form ID:", err); } finally { return result; } }; type getReportTempList = ( - projectCode: string, - packageCode: string, + packageId: number, formId: number -) => Promise<VendorDataReportTempsPlant[]>; +) => Promise<VendorDataReportTemps[]>; export const getReportTempList: getReportTempList = async ( - projectCode, - packageCode, + packageId, formId ) => { - let result: VendorDataReportTempsPlant[] = []; + let result: VendorDataReportTemps[] = []; try { result = await db .select() - .from(vendorDataReportTempsPlant) + .from(vendorDataReportTemps) .where( and( - eq(vendorDataReportTempsPlant.projectCode, projectCode), - eq(vendorDataReportTempsPlant.packageCode, packageCode), - eq(vendorDataReportTempsPlant.formId, formId) + eq(vendorDataReportTemps.contractItemId, packageId), + eq(vendorDataReportTemps.formId, formId) ) ); } catch (err) { @@ -1118,8 +1105,7 @@ export const getReportTempList: getReportTempList = async ( }; export async function uploadReportTemp( - projectCode: string, - packageCode: string, + packageId: number, formId: number, formData: FormData ) { @@ -1142,10 +1128,9 @@ export async function uploadReportTemp( return db.transaction(async (tx) => { // 파일 정보를 테이블에 저장 await tx - .insert(vendorDataReportTempsPlant) + .insert(vendorDataReportTemps) .values({ - projectCode, - packageCode, + contractItemId: packageId, formId: formId, fileName: customFileName, filePath: saveResult.publicPath!, @@ -1175,16 +1160,16 @@ export const deleteReportTempFile: deleteReportTempFile = async (id) => { return db.transaction(async (tx) => { const [targetTempFile] = await tx .select() - .from(vendorDataReportTempsPlant) - .where(eq(vendorDataReportTempsPlant.id, id)); + .from(vendorDataReportTemps) + .where(eq(vendorDataReportTemps.id, id)); if (!targetTempFile) { throw new Error("해당 Template File을 찾을 수 없습니다."); } await tx - .delete(vendorDataReportTempsPlant) - .where(eq(vendorDataReportTempsPlant.id, id)); + .delete(vendorDataReportTemps) + .where(eq(vendorDataReportTemps.id, id)); const { filePath } = targetTempFile; @@ -1325,8 +1310,7 @@ async function transformDataToSEDPFormat( formCode: string, objectCode: string, projectNo: string, - packageCode: string, - contractItemId: string, + contractItemId: number, // Add contractItemId parameter designerNo: string = "253213" ): Promise<SEDPDataItem[]> { // Create a map for quick column lookup @@ -1347,6 +1331,9 @@ async function transformDataToSEDPFormat( // Cache for UOM factors to avoid duplicate API calls const uomFactorCache = new Map<string, number>(); + // Cache for packageCode to avoid duplicate DB queries for same tag + const packageCodeCache = new Map<string, string>(); + // Cache for tagClass code to avoid duplicate DB queries for same tag const tagClassCodeCache = new Map<string, string>(); @@ -1354,16 +1341,98 @@ async function transformDataToSEDPFormat( const transformedItems = []; for (const row of tableData) { - let tagClassCode = ""; - // Get tagClass code if TAG_NO exists + const cotractItem = await db.query.contractItems.findFirst({ + where: + eq(contractItems.id, contractItemId), + }); + + const item = await db.query.items.findFirst({ + where: + eq(items.id, cotractItem.itemId), + }); + + // Get packageCode for this specific tag + let packageCode = item.packageCode; // fallback to formCode + let tagClassCode = ""; // for CLS_ID + if (row.TAG_NO && contractItemId) { + // Check cache first const cacheKey = `${contractItemId}-${row.TAG_NO}`; - if (tagClassCodeCache.has(cacheKey)) { - tagClassCode = tagClassCodeCache.get(cacheKey)!; + if (packageCodeCache.has(cacheKey)) { + packageCode = packageCodeCache.get(cacheKey)!; } else { try { + // Query to get packageCode for this specific tag + const tagResult = await db.query.tags.findFirst({ + where: and( + eq(tags.contractItemId, contractItemId), + eq(tags.tagNo, row.TAG_NO) + ) + }); + + if (tagResult) { + // Get tagClass code if tagClassId exists + if (tagResult.tagClassId) { + // Check tagClass cache first + if (tagClassCodeCache.has(cacheKey)) { + tagClassCode = tagClassCodeCache.get(cacheKey)!; + } else { + const tagClassResult = await db.query.tagClasses.findFirst({ + where: eq(tagClasses.id, tagResult.tagClassId) + }); + + if (tagClassResult) { + tagClassCode = tagClassResult.code; + console.log(`Found tagClass code for tag ${row.TAG_NO}: ${tagClassCode}`); + } else { + console.warn(`No tagClass found for tagClassId: ${tagResult.tagClassId}`); + } + + // Cache the tagClass code result + tagClassCodeCache.set(cacheKey, tagClassCode); + } + } + + // Get the contract item + const contractItemResult = await db.query.contractItems.findFirst({ + where: eq(contractItems.id, tagResult.contractItemId) + }); + + if (contractItemResult) { + // Get the first item with this itemId + const itemResult = await db.query.items.findFirst({ + where: eq(items.id, contractItemResult.itemId) + }); + + if (itemResult && itemResult.packageCode) { + packageCode = itemResult.packageCode; + console.log(`Found packageCode for tag ${row.TAG_NO}: ${packageCode}`); + } else { + console.warn(`No item found for contractItem.itemId: ${contractItemResult.itemId}, using fallback`); + } + } else { + console.warn(`No contractItem found for tag ${row.TAG_NO}, using fallback`); + } + } else { + console.warn(`No tag found for contractItemId: ${contractItemId}, tagNo: ${row.TAG_NO}, using fallback`); + } + + // Cache the result (even if it's the fallback value) + packageCodeCache.set(cacheKey, packageCode); + } catch (error) { + console.error(`Error fetching packageCode for tag ${row.TAG_NO}:`, error); + // Use fallback value and cache it + packageCodeCache.set(cacheKey, packageCode); + } + } + + // Get tagClass code if not already retrieved above + if (!tagClassCode && tagClassCodeCache.has(cacheKey)) { + tagClassCode = tagClassCodeCache.get(cacheKey)!; + } else if (!tagClassCode) { + try { const tagResult = await db.query.tags.findFirst({ where: and( eq(tags.contractItemId, contractItemId), @@ -1371,20 +1440,22 @@ async function transformDataToSEDPFormat( ) }); - if (tagResult?.tagClassId) { + if (tagResult && tagResult.tagClassId) { const tagClassResult = await db.query.tagClasses.findFirst({ where: eq(tagClasses.id, tagResult.tagClassId) }); - + if (tagClassResult) { tagClassCode = tagClassResult.code; console.log(`Found tagClass code for tag ${row.TAG_NO}: ${tagClassCode}`); } } + // Cache the tagClass code result tagClassCodeCache.set(cacheKey, tagClassCode); } catch (error) { - console.error(`Error fetching tagClass for tag ${row.TAG_NO}:`, error); + console.error(`Error fetching tagClass code for tag ${row.TAG_NO}:`, error); + // Cache empty string as fallback tagClassCodeCache.set(cacheKey, ""); } } @@ -1395,16 +1466,17 @@ async function transformDataToSEDPFormat( TAG_NO: row.TAG_NO || "", TAG_DESC: row.TAG_DESC || "", ATTRIBUTES: [], + // SCOPE: objectCode, SCOPE: packageCode, - TOOLID: "eVCP", + TOOLID: "eVCP", // Changed from VDCS ITM_NO: row.TAG_NO || "", - OP_DELETE: row.status === "Deleted", + OP_DELETE: row.status === "Deleted", // Set OP_DELETE based on status MAIN_YN: true, LAST_REV_YN: true, CRTER_NO: designerNo, CHGER_NO: designerNo, - TYPE: formCode, - CLS_ID: tagClassCode, + TYPE: formCode, // Use packageCode instead of formCode + CLS_ID: tagClassCode, // Add CLS_ID with tagClass code PROJ_NO: projectNo, REV_NO: "00", CRTE_DTM: currentTimestamp, @@ -1450,7 +1522,7 @@ async function transformDataToSEDPFormat( const uomData = await response.json(); if (uomData && uomData.FACTOR !== undefined && uomData.FACTOR !== null) { factor = Number(uomData.FACTOR); - // Store in cache for future use + // Store in cache for future use (type assertion to ensure it's a number) uomFactorCache.set(column.uomId, factor); } } else { @@ -1461,7 +1533,7 @@ async function transformDataToSEDPFormat( } } - // Apply the factor if needed (currently commented out) + // Apply the factor if we got one // if (factor !== undefined && typeof value === 'number') { // value = value * factor; // } @@ -1469,7 +1541,7 @@ async function transformDataToSEDPFormat( const attribute: SEDPAttribute = { NAME: key, - VALUE: String(value), + VALUE: String(value), // 모든 값을 문자열로 변환 UOM: column?.uom || "", CLS_ID: tagClassCode || "", }; @@ -1497,7 +1569,7 @@ export async function transformFormDataToSEDP( formCode: string, objectCode: string, projectNo: string, - packageCode: string, // Add contractItemId parameter + contractItemId: number, // Add contractItemId parameter designerNo: string = "253213" ): Promise<SEDPDataItem[]> { return transformDataToSEDPFormat( @@ -1506,7 +1578,7 @@ export async function transformFormDataToSEDP( formCode, objectCode, projectNo, - packageCode, + contractItemId, // Pass contractItemId designerNo ); } @@ -1527,20 +1599,6 @@ export async function getProjectCodeById(projectId: number): Promise<string> { return projectRecord[0].code; } -export async function getProjectIdByCode(projectCode: string): Promise<number> { - const projectRecord = await db - .select({ id: projects.id }) - .from(projects) - .where(eq(projects.code, projectCode)) - .limit(1); - - if (!projectRecord || projectRecord.length === 0) { - throw new Error(`Project not found with ID: ${projectId}`); - } - - return projectRecord[0].id; -} - export async function getProjectById(projectId: number): Promise<{ code: string; type: string; }> { const projectRecord = await db .select({ code: projects.code , type:projects.type}) @@ -1621,13 +1679,13 @@ export async function sendDataToSEDP( export async function sendFormDataToSEDP( formCode: string, projectId: number, - projectCode: string, // contractItemId 파라미터 추가 - packageCode: string, // contractItemId 파라미터 추가 + contractItemId: number, // contractItemId 파라미터 추가 formData: GenericData[], columns: DataTableColumnJSON[] ): Promise<{ success: boolean; message: string; data?: any }> { try { // 1. Get project code + const projectCode = await getProjectCodeById(projectId); // 2. Get class mapping const mappingsResult = await db.query.tagTypeClassFormMappings.findFirst({ @@ -1670,7 +1728,7 @@ export async function sendFormDataToSEDP( formCode, objectCode, projectCode, - packageCode // Add contractItemId parameter + contractItemId // Add contractItemId parameter ); // 4. Send to SEDP API @@ -1681,12 +1739,11 @@ export async function sendFormDataToSEDP( // Get the current formEntries data const entries = await db .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode), + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) .limit(1); @@ -1721,17 +1778,17 @@ export async function sendFormDataToSEDP( // Update the database await db - .update(formEntriesPlant) + .update(formEntries) .set({ data: updatedDataArray, updatedAt: new Date() }) - .where(eq(formEntriesPlant.id, entry.id)); + .where(eq(formEntries.id, entry.id)); console.log(`Updated status for ${sentTagNumbers.size} tags to "Sent to S-EDP"`); } } else { - console.warn(`No formEntriesPlant found for formCode: ${formCode}`); + console.warn(`No formEntries found for formCode: ${formCode}, contractItemId: ${contractItemId}`); } } catch (statusUpdateError) { // Status 업데이트 실패는 경고로만 처리 (SEDP 전송은 성공했으므로) @@ -1755,14 +1812,12 @@ export async function sendFormDataToSEDP( export async function deleteFormDataByTags({ formCode, - projectCode, - packageCode, + contractItemId, tagIdxs, projectId, }: { formCode: string - projectCode: string - packageCode: string + contractItemId: number tagIdxs: string[] projectId?: number }): Promise<{ @@ -1775,26 +1830,25 @@ export async function deleteFormDataByTags({ }> { try { // 입력 검증 - if (!formCode || !projectCode || !Array.isArray(tagIdxs) || tagIdxs.length === 0) { + if (!formCode || !contractItemId || !Array.isArray(tagIdxs) || tagIdxs.length === 0) { return { error: "Missing required parameters: formCode, contractItemId, tagIdxs", } } - console.log(`[DELETE ACTION] Deleting tags for formCode: ${formCode}, projectCode: ${projectCode}, tagIdxs:`, tagIdxs) + console.log(`[DELETE ACTION] Deleting tags for formCode: ${formCode}, contractItemId: ${contractItemId}, tagIdxs:`, tagIdxs) // 1. 트랜잭션 전에 삭제할 항목들을 미리 조회하여 저장 (S-EDP 전송용) const entryForSedp = await db .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode) + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) - .orderBy(desc(formEntriesPlant.updatedAt)) + .orderBy(desc(formEntries.updatedAt)) .limit(1) let itemsToSendToSedp: Record<string, unknown>[] = [] @@ -1814,15 +1868,14 @@ export async function deleteFormDataByTags({ // 2-1. 현재 formEntry 데이터 가져오기 const currentEntryResult = await tx .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode) + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) - .orderBy(desc(formEntriesPlant.updatedAt)) + .orderBy(desc(formEntries.updatedAt)) .limit(1) if (currentEntryResult.length === 0) { @@ -1850,15 +1903,14 @@ export async function deleteFormDataByTags({ // 2-3. tags 테이블에서 해당 태그들 삭제 const deletedTagsResult = await tx - .delete(tagsPlant) + .delete(tags) .where( and( - eq(tagsPlant.projectCode, projectCode), - eq(tagsPlant.packageCode, packageCode), - inArray(tagsPlant.tagIdx, tagIdxs) + eq(tags.contractItemId, contractItemId), + inArray(tags.tagIdx, tagIdxs) ) ) - .returning({ tagNo: tagsPlant.tagNo }) + .returning({ tagNo: tags.tagNo }) const deletedTagsCount = deletedTagsResult.length @@ -1867,16 +1919,15 @@ export async function deleteFormDataByTags({ // 2-4. formEntries 데이터 업데이트 (삭제된 항목 제외) await tx - .update(formEntriesPlant) + .update(formEntries) .set({ data: updatedData, updatedAt: new Date(), }) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode), + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) @@ -1969,8 +2020,7 @@ export async function deleteFormDataByTags({ const sedpResult = await sendFormDataToSEDP( formCode, projectId, - projectCode, - packageCode, + contractItemId, uniqueDeletedItems as GenericData[], formMetaResult.columns as DataTableColumnJSON[] ) @@ -2029,13 +2079,11 @@ export async function deleteFormDataByTags({ */ export async function excludeFormDataByTags({ formCode, - projectCode, - packageCode, + contractItemId, tagNumbers, }: { formCode: string - projectCode: string - packageCode: string + contractItemId: number tagNumbers: string[] }): Promise<{ error?: string @@ -2044,28 +2092,27 @@ export async function excludeFormDataByTags({ }> { try { // 입력 검증 - if (!formCode || !projectCode || !Array.isArray(tagNumbers) || tagNumbers.length === 0) { + if (!formCode || !contractItemId || !Array.isArray(tagNumbers) || tagNumbers.length === 0) { return { - error: "Missing required parameters: formCode, projectCode, tagNumbers", + error: "Missing required parameters: formCode, contractItemId, tagNumbers", } } - console.log(`[EXCLUDE ACTION] Excluding tags for formCode: ${formCode}, projectCode: ${projectCode}, tagNumbers:`, tagNumbers) + console.log(`[EXCLUDE ACTION] Excluding tags for formCode: ${formCode}, contractItemId: ${contractItemId}, tagNumbers:`, tagNumbers) // 트랜잭션으로 안전하게 처리 const result = await db.transaction(async (tx) => { // 1. 현재 formEntry 데이터 가져오기 const currentEntryResult = await tx .select() - .from(formEntriesPlant) + .from(formEntries) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode) + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) - .orderBy(desc(formEntriesPlant.updatedAt)) + .orderBy(desc(formEntries.updatedAt)) .limit(1) if (currentEntryResult.length === 0) { @@ -2099,16 +2146,15 @@ export async function excludeFormDataByTags({ // 3. formEntries 데이터 업데이트 await tx - .update(formEntriesPlant) + .update(formEntries) .set({ data: updatedData, updatedAt: new Date(), }) .where( and( - eq(formEntriesPlant.formCode, formCode), - eq(formEntriesPlant.projectCode, projectCode), - eq(formEntriesPlant.packageCode, packageCode) + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) ) ) @@ -2119,7 +2165,7 @@ export async function excludeFormDataByTags({ }) // 4. 캐시 무효화 - const cacheKey = `form-data-${formCode}-${packageCode}` + const cacheKey = `form-data-${formCode}-${contractItemId}` revalidateTag(cacheKey) console.log(`[EXCLUDE ACTION] Transaction completed successfully`) |
