summaryrefslogtreecommitdiff
path: root/lib/pos/design-document-service.ts
blob: 8f213d577118b95a18ea2dad2105b120cf0dbafa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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: {},
    };
  }
}