diff options
| author | joonhoekim <26rote@gmail.com> | 2025-09-26 14:13:20 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-09-26 14:13:20 +0900 |
| commit | f8fc02e175f93466cd7693eb6e549c45362e785b (patch) | |
| tree | 1037ec1f9225b0a0142defd6a27c68c3e6a47009 /lib/pos/design-document-service.ts | |
| parent | 11bc8239ad474a8f31c1c73de51f7d0f101594df (diff) | |
(김준회) POS 및 구매 피드백 처리
- 요구사항 28.(0.1) 24번 행 (prItem번호 별도 표기)
- pos nfs 경로에서 가져오도록 수정개발
Diffstat (limited to 'lib/pos/design-document-service.ts')
| -rw-r--r-- | lib/pos/design-document-service.ts | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/lib/pos/design-document-service.ts b/lib/pos/design-document-service.ts new file mode 100644 index 00000000..8f213d57 --- /dev/null +++ b/lib/pos/design-document-service.ts @@ -0,0 +1,188 @@ +'use server'; + +import db from '@/db/db'; +import { rfqLastAttachments, rfqLastAttachmentRevisions } from '@/db/schema/rfqLast'; +import { eq, and } from 'drizzle-orm'; + +/** + * 자재코드별 설계 문서 조회 + */ +export async function getDesignDocumentByMaterialCode( + rfqId: number, + materialCode: string +): Promise<{ + success: boolean; + document?: { + id: number; + attachmentType: string; + serialNo: string; + description: string | null; + latestRevisionId: number | null; + fileName: string; + filePath: string; + fileSize: number | null; + fileType: string | null; + }; + error?: string; +}> { + try { + const result = await db + .select({ + attachmentId: rfqLastAttachments.id, + attachmentType: rfqLastAttachments.attachmentType, + serialNo: rfqLastAttachments.serialNo, + description: rfqLastAttachments.description, + latestRevisionId: rfqLastAttachments.latestRevisionId, + fileName: rfqLastAttachmentRevisions.fileName, + filePath: rfqLastAttachmentRevisions.filePath, + fileSize: rfqLastAttachmentRevisions.fileSize, + fileType: rfqLastAttachmentRevisions.fileType, + }) + .from(rfqLastAttachments) + .leftJoin( + rfqLastAttachmentRevisions, + eq(rfqLastAttachments.latestRevisionId, rfqLastAttachmentRevisions.id) + ) + .where( + and( + eq(rfqLastAttachments.rfqId, rfqId), + eq(rfqLastAttachments.attachmentType, '설계'), + eq(rfqLastAttachments.serialNo, materialCode) + ) + ) + .limit(1); + + if (result.length === 0) { + return { + success: false, + error: '설계 문서를 찾을 수 없습니다.' + }; + } + + const doc = result[0]; + return { + success: true, + document: { + id: doc.attachmentId, + attachmentType: doc.attachmentType, + serialNo: doc.serialNo, + description: doc.description, + latestRevisionId: doc.latestRevisionId, + fileName: doc.fileName || '', + filePath: doc.filePath || '', + fileSize: doc.fileSize, + fileType: doc.fileType, + } + }; + } catch (error) { + console.error('설계 문서 조회 오류:', error); + return { + success: false, + error: error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.' + }; + } +} + +/** + * RFQ의 모든 PR Items에 대한 설계 문서 매핑 조회 + */ +export async function getDesignDocumentsForRfqItems( + rfqId: number +): Promise<{ + success: boolean; + documents?: Record<string, { + id: number; + fileName: string; + filePath: string; + fileSize: number | null; + fileType: string | null; + description: string | null; + }>; + error?: string; +}> { + try { + const result = await db + .select({ + materialCode: rfqLastAttachments.serialNo, + attachmentId: rfqLastAttachments.id, + description: rfqLastAttachments.description, + fileName: rfqLastAttachmentRevisions.fileName, + filePath: rfqLastAttachmentRevisions.filePath, + fileSize: rfqLastAttachmentRevisions.fileSize, + fileType: rfqLastAttachmentRevisions.fileType, + }) + .from(rfqLastAttachments) + .leftJoin( + rfqLastAttachmentRevisions, + eq(rfqLastAttachments.latestRevisionId, rfqLastAttachmentRevisions.id) + ) + .where( + and( + eq(rfqLastAttachments.rfqId, rfqId), + eq(rfqLastAttachments.attachmentType, '설계') + ) + ); + + const documentsMap: Record<string, any> = {}; + + for (const doc of result) { + if (doc.materialCode) { + documentsMap[doc.materialCode] = { + id: doc.attachmentId, + fileName: doc.fileName || '', + filePath: doc.filePath || '', + fileSize: doc.fileSize, + fileType: doc.fileType, + description: doc.description, + }; + } + } + + return { + success: true, + documents: documentsMap + }; + } catch (error) { + console.error('설계 문서 매핑 조회 오류:', error); + return { + success: false, + error: error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.' + }; + } +} + +/** + * 자재코드별 설계 문서 조회 서버 액션 + */ +export async function getDesignDocumentByMaterialCodeAction( + rfqId: number, + materialCode: string +) { + try { + const result = await getDesignDocumentByMaterialCode(rfqId, materialCode); + return result; + } catch (error) { + console.error("Error in getDesignDocumentByMaterialCodeAction:", error); + return { + success: false, + error: "설계 문서를 불러오는데 실패했습니다.", + }; + } +} + +/** + * RFQ Items 설계 문서 매핑 조회 서버 액션 + */ +export async function getDesignDocumentsForRfqItemsAction(rfqId: number) { + try { + const result = await getDesignDocumentsForRfqItems(rfqId); + return result; + } catch (error) { + console.error("Error in getDesignDocumentsForRfqItemsAction:", error); + return { + success: false, + error: "설계 문서 매핑을 불러오는데 실패했습니다.", + documents: {}, + }; + } +} |
