summaryrefslogtreecommitdiff
path: root/lib/pos/design-document-service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pos/design-document-service.ts')
-rw-r--r--lib/pos/design-document-service.ts188
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: {},
+ };
+ }
+}