diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-06-01 13:52:21 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-06-01 13:52:21 +0000 |
| commit | bac0228d21b7195065e9cddcc327ae33659c7bcc (patch) | |
| tree | 8f3016ae4533c8706d0c00a605d9b1d41968c2bc /lib/forms/services.ts | |
| parent | 2fdce8d7a57c792bba0ac36fa554dca9c9cc31e3 (diff) | |
(대표님) 20250601까지 작업사항
Diffstat (limited to 'lib/forms/services.ts')
| -rw-r--r-- | lib/forms/services.ts | 235 |
1 files changed, 208 insertions, 27 deletions
diff --git a/lib/forms/services.ts b/lib/forms/services.ts index 0fbe68a6..b6e479a2 100644 --- a/lib/forms/services.ts +++ b/lib/forms/services.ts @@ -45,8 +45,8 @@ export async function getFormsByContractItemId( const cacheKey = `forms-${contractItemId}-${mode}`; try { - return unstable_cache( - async () => { + // return unstable_cache( + // async () => { console.log( `[Forms Service] Fetching forms for contractItemId: ${contractItemId}, mode: ${mode}` ); @@ -86,14 +86,14 @@ export async function getFormsByContractItemId( ); throw error; // 캐시 함수에서 에러를 던져 캐싱이 발생하지 않도록 함 } - }, - [cacheKey], - { - // 캐시 시간 단축 - revalidate: 60, // 1분으로 줄임 - tags: [cacheKey], - } - )(); + // }, + // [cacheKey], + // { + // // 캐시 시간 단축 + // revalidate: 60, // 1분으로 줄임 + // tags: [cacheKey], + // } + // )(); } catch (error) { getErrorMessage( `Cache operation failed for contractItemId ${contractItemId}, mode: ${mode}: ${error}` @@ -231,8 +231,6 @@ export async function getFormData(formCode: string, contractItemId: number) { console.log(cacheKey, "getFormData") try { - const result = await unstable_cache( - async () => { // 기존 로직으로 projectId, columns, data 가져오기 const contractItemResult = await db .select({ @@ -297,6 +295,14 @@ export async function getFormData(formCode: string, contractItemId: number) { } }); + // status 컬럼 추가 + columns.push({ + key: "status", + label: "status", + displayLabel: "Status", + type: "STRING" + }); + let data: Array<Record<string, any>> = []; if (entry) { if (Array.isArray(entry.data)) { @@ -310,15 +316,8 @@ export async function getFormData(formCode: string, contractItemId: number) { const editableFieldsMap = await getEditableFieldsByTag(contractItemId, projectId); return { columns, data, editableFieldsMap }; - }, - [cacheKey], - { - revalidate: 60, - tags: [cacheKey], - } - )(); + - return result; } catch (cacheError) { console.error(`[getFormData] Cache operation failed:`, cacheError); @@ -681,12 +680,13 @@ export async function updateFormDataInDB( }; } - // 5) 병합 + // 5) 병합 (status 필드 추가) const oldItem = dataArray[idx]; const updatedItem = { ...oldItem, ...newData, TAG_NO: oldItem.TAG_NO, // TAG_NO 변경 불가 시 유지 + status: "Imported from EXCEL" // Excel에서 가져온 데이터임을 표시 }; const updatedArray = [...dataArray]; @@ -750,7 +750,6 @@ export async function updateFormDataInDB( }; } } - // FormColumn Type (동일) export interface FormColumn { key: string; @@ -1305,7 +1304,8 @@ export async function sendDataToSEDP( */ export async function sendFormDataToSEDP( formCode: string, - projectId: number, + projectId: number, + contractItemId: number, // contractItemId 파라미터 추가 formData: GenericData[], columns: DataTableColumnJSON[] ): Promise<{ success: boolean; message: string; data?: any }> { @@ -1347,18 +1347,74 @@ export async function sendFormDataToSEDP( console.warn(`No mapping found for formCode ${formCode} in project ${projectId}, using default object code`); } - // 4. Transform data to SEDP format + // 3. Transform data to SEDP format const sedpData = await transformFormDataToSEDP( - formData, - columns, + formData, + columns, formCode, objectCode, projectCode ); - // 5. Send to SEDP API + // 4. Send to SEDP API const result = await sendDataToSEDP(projectCode, sedpData); + // 5. SEDP 전송 성공 후 formEntries에 status 업데이트 + try { + // Get the current formEntries data + const entries = await db + .select() + .from(formEntries) + .where( + and( + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) + ) + ) + .limit(1); + + if (entries && entries.length > 0) { + const entry = entries[0]; + const dataArray = entry.data as Array<Record<string, any>>; + + if (Array.isArray(dataArray)) { + // Extract TAG_NO list from formData + const sentTagNumbers = new Set( + formData + .map(item => item.TAG_NO) + .filter(tagNo => tagNo) // Remove null/undefined values + ); + + // Update status for sent tags + const updatedDataArray = dataArray.map(item => { + if (item.TAG_NO && sentTagNumbers.has(item.TAG_NO)) { + return { + ...item, + status: "Sent to S-EDP" // SEDP로 전송된 데이터임을 표시 + }; + } + return item; + }); + + // Update the database + await db + .update(formEntries) + .set({ + data: updatedDataArray, + updatedAt: new Date() + }) + .where(eq(formEntries.id, entry.id)); + + console.log(`Updated status for ${sentTagNumbers.size} tags to "Sent to S-EDP"`); + } + } else { + console.warn(`No formEntries found for formCode: ${formCode}, contractItemId: ${contractItemId}`); + } + } catch (statusUpdateError) { + // Status 업데이트 실패는 경고로만 처리 (SEDP 전송은 성공했으므로) + console.warn("Failed to update status after SEDP send:", statusUpdateError); + } + return { success: true, message: "Data successfully sent to SEDP", @@ -1371,4 +1427,129 @@ export async function sendFormDataToSEDP( message: error.message || "Failed to send data to SEDP" }; } +} + + +export async function deleteFormDataByTags({ + formCode, + contractItemId, + tagNos, +}: { + formCode: string + contractItemId: number + tagNos: string[] +}): Promise<{ + error?: string + success?: boolean + deletedCount?: number + deletedTagsCount?: number +}> { + try { + // 입력 검증 + if (!formCode || !contractItemId || !Array.isArray(tagNos) || tagNos.length === 0) { + return { + error: "Missing required parameters: formCode, contractItemId, tagNos", + } + } + + console.log(`[DELETE ACTION] Deleting tags for formCode: ${formCode}, contractItemId: ${contractItemId}, tagNos:`, tagNos) + + // 트랜잭션으로 안전하게 처리 + const result = await db.transaction(async (tx) => { + // 1. 현재 formEntry 데이터 가져오기 + const currentEntryResult = await tx + .select() + .from(formEntries) + .where( + and( + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) + ) + ) + .orderBy(desc(formEntries.updatedAt)) + .limit(1) + + if (currentEntryResult.length === 0) { + throw new Error("Form entry not found") + } + + const currentEntry = currentEntryResult[0] + let currentData = Array.isArray(currentEntry.data) ? currentEntry.data : [] + + console.log(`[DELETE ACTION] Current data count: ${currentData.length}`) + + // 2. 삭제할 항목들 필터링 (formEntries에서) + const updatedData = currentData.filter((item: any) => + !tagNos.includes(item.TAG_NO) + ) + + const deletedFromFormEntries = currentData.length - updatedData.length + + console.log(`[DELETE ACTION] Updated data count: ${updatedData.length}`) + console.log(`[DELETE ACTION] Deleted ${deletedFromFormEntries} items from formEntries`) + + if (deletedFromFormEntries === 0) { + throw new Error("No items were found to delete in formEntries") + } + + // 3. tags 테이블에서 해당 태그들 삭제 + const deletedTagsResult = await tx + .delete(tags) + .where( + and( + eq(tags.contractItemId, contractItemId), + inArray(tags.tagNo, tagNos) + ) + ) + .returning({ tagNo: tags.tagNo }) + + const deletedTagsCount = deletedTagsResult.length + + console.log(`[DELETE ACTION] Deleted ${deletedTagsCount} items from tags table`) + console.log(`[DELETE ACTION] Deleted tag numbers:`, deletedTagsResult.map(t => t.tagNo)) + + // 4. formEntries 데이터 업데이트 + await tx + .update(formEntries) + .set({ + data: updatedData, + updatedAt: new Date(), + }) + .where( + and( + eq(formEntries.formCode, formCode), + eq(formEntries.contractItemId, contractItemId) + ) + ) + + return { + deletedFromFormEntries, + deletedTagsCount, + deletedTagNumbers: deletedTagsResult.map(t => t.tagNo) + } + }) + + // 5. 캐시 무효화 + const cacheKey = `form-data-${formCode}-${contractItemId}` + revalidateTag(cacheKey) + revalidateTag(`tags-${contractItemId}`) + + // 페이지 재검증 (필요한 경우) + + console.log(`[DELETE ACTION] Transaction completed successfully`) + console.log(`[DELETE ACTION] FormEntries deleted: ${result.deletedFromFormEntries}`) + console.log(`[DELETE ACTION] Tags deleted: ${result.deletedTagsCount}`) + + return { + success: true, + deletedCount: result.deletedFromFormEntries, + deletedTagsCount: result.deletedTagsCount, + } + + } catch (error) { + console.error("[DELETE ACTION] Error deleting form data:", error) + return { + error: error instanceof Error ? error.message : "An unexpected error occurred", + } + } }
\ No newline at end of file |
