diff options
Diffstat (limited to 'lib/avl/history-service.ts')
| -rw-r--r-- | lib/avl/history-service.ts | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/lib/avl/history-service.ts b/lib/avl/history-service.ts new file mode 100644 index 00000000..fdad0ab9 --- /dev/null +++ b/lib/avl/history-service.ts @@ -0,0 +1,163 @@ +'use server'; + +import db from '@/db/db'; +import { avlList } from '@/db/schema/avl/avl'; +import { eq, and, desc } from 'drizzle-orm'; +import { debugLog, debugError } from '@/lib/debug-utils'; +import type { + AvlHistoryRecord, + VendorSnapshot, +} from './components/avl-history-modal'; + +/** + * AVL 히스토리 조회 서비스 + * 프로젝트 AVL과 표준 AVL의 모든 리비전 히스토리를 조회합니다. + */ + +/** + * 프로젝트 AVL 히스토리 조회 + * @param projectCode 프로젝트 코드 + * @returns 모든 리비전의 히스토리 데이터 + */ +export async function getProjectAvlHistory( + projectCode: string +): Promise<AvlHistoryRecord[]> { + try { + debugLog('프로젝트 AVL 히스토리 조회 시작', { projectCode }); + + // 동일 프로젝트 코드의 모든 AVL 리스트 조회 (isTemplate = false) + const allRevisions = await db.query.avlList.findMany({ + where: and( + eq(avlList.projectCode, projectCode), + eq(avlList.isTemplate, false) + ), + orderBy: [desc(avlList.rev)], + }); + + debugLog('프로젝트 AVL 히스토리 조회 결과', { + projectCode, + revisionCount: allRevisions.length, + }); + + // 히스토리 레코드로 변환 + const historyData: AvlHistoryRecord[] = allRevisions.map((rev) => ({ + id: rev.id, + rev: rev.rev || 1, + createdAt: rev.createdAt?.toISOString() || new Date().toISOString(), + createdBy: rev.createdBy || 'system', + vendorInfoSnapshot: (rev.vendorInfoSnapshot as VendorSnapshot[]) || [], + changeDescription: `리비전 ${ + rev.rev + } - ${rev.createdAt?.toLocaleDateString('ko-KR')}`, + })); + + return historyData; + } catch (error) { + debugError('프로젝트 AVL 히스토리 조회 실패', { projectCode, error }); + throw new Error( + `프로젝트 AVL 히스토리 조회 중 오류가 발생했습니다: ${ + error instanceof Error ? error.message : 'Unknown error' + }` + ); + } +} + +/** + * 표준 AVL 히스토리 조회 + * @param constructionSector 공사부문 + * @param shipType 선종 + * @param avlKind AVL 종류 + * @param htDivision H/T 구분 + * @returns 모든 리비전의 히스토리 데이터 + */ +export async function getStandardAvlHistory( + constructionSector: string, + shipType: string, + avlKind: string, + htDivision: string +): Promise<AvlHistoryRecord[]> { + try { + debugLog('표준 AVL 히스토리 조회 시작', { + constructionSector, + shipType, + avlKind, + htDivision, + }); + + // 동일 조건의 모든 표준 AVL 리스트 조회 (isTemplate = true) + const allRevisions = await db.query.avlList.findMany({ + where: and( + eq(avlList.constructionSector, constructionSector), + eq(avlList.shipType, shipType), + eq(avlList.avlKind, avlKind), + eq(avlList.htDivision, htDivision), + eq(avlList.isTemplate, true) + ), + orderBy: [desc(avlList.rev)], + }); + + debugLog('표준 AVL 히스토리 조회 결과', { + constructionSector, + shipType, + avlKind, + htDivision, + revisionCount: allRevisions.length, + }); + + // 히스토리 레코드로 변환 + const historyData: AvlHistoryRecord[] = allRevisions.map((rev) => ({ + id: rev.id, + rev: rev.rev || 1, + createdAt: rev.createdAt?.toISOString() || new Date().toISOString(), + createdBy: rev.createdBy || 'system', + vendorInfoSnapshot: (rev.vendorInfoSnapshot as VendorSnapshot[]) || [], + changeDescription: `리비전 ${ + rev.rev + } - ${rev.createdAt?.toLocaleDateString('ko-KR')}`, + })); + + return historyData; + } catch (error) { + debugError('표준 AVL 히스토리 조회 실패', { + constructionSector, + shipType, + avlKind, + htDivision, + error, + }); + throw new Error( + `표준 AVL 히스토리 조회 중 오류가 발생했습니다: ${ + error instanceof Error ? error.message : 'Unknown error' + }` + ); + } +} + +/** + * AVL 아이템에 따른 히스토리 조회 (통합 함수) + * @param avlItem AVL 리스트 아이템 + * @returns 해당 AVL의 모든 리비전 히스토리 + */ +export async function getAvlHistory(avlItem: any): Promise<AvlHistoryRecord[]> { + try { + if (avlItem.isTemplate) { + // 표준 AVL인 경우 + return await getStandardAvlHistory( + avlItem.constructionSector, + avlItem.shipType, + avlItem.avlKind, + avlItem.htDivision + ); + } else { + // 프로젝트 AVL인 경우 + return await getProjectAvlHistory(avlItem.projectCode); + } + } catch (error) { + debugError('AVL 히스토리 조회 실패', { avlItem, error }); + throw new Error( + `AVL 히스토리 조회 중 오류가 발생했습니다: ${ + error instanceof Error ? error.message : 'Unknown error' + }` + ); + } +} |
