summaryrefslogtreecommitdiff
path: root/lib/pos/index.ts
blob: d889f8c1c3f098c38267fa707df8c548867fab76 (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
/**
 * POS (Purchase Order Specification) 파일 관련 기능 통합 모듈
 * 
 * 주요 기능:
 * - MATNR(자재코드)로 DCMTM_ID 조회
 * - SOAP API를 통한 POS 파일 경로 조회
 * - NFS 네트워크 드라이브에서 파일 다운로드
 * - 온디맨드 방식의 POS 파일 다운로드 (자동 동기화 제거됨)
 * 
 * 주요 변경사항:
 * - syncRfqPosFiles 함수 제거됨 (온디맨드 방식으로 대체)
 * - getDownloadUrlByMaterialCode 등 새로운 온디맨드 함수 추가
 */

import { getEncryptDocumentumFile } from './get-pos';
import { createDownloadUrl } from './download-pos-file';
import { getDcmtmIdByMaterialCode } from './get-dcmtm-id';
import type { PosFileInfo } from './types';

export { 
  getEncryptDocumentumFile
} from './get-pos';

export { 
  downloadPosFile,
  createDownloadUrl
} from './download-pos-file';

export {
  getDcmtmIdByMaterialCode,
  getFirstDcmtmId
} from './get-dcmtm-id';

export {
  getDesignDocumentByMaterialCode,
  getDesignDocumentsForRfqItems,
  getDesignDocumentByMaterialCodeAction,
  getDesignDocumentsForRfqItemsAction
} from './design-document-service';

export {
  getDownloadUrlByMaterialCode,
  getDownloadUrlsForMaterialCodes,
  checkPosFileExists
} from './download-on-demand-action';

// 타입들은 ./types 에서 export
export type * from './types';

/**
 * POS 파일을 가져와서 다운로드 URL을 생성하는 통합 함수
 * 
 * @example
 * ```typescript
 * const result = await getPosFileAndCreateDownloadUrl({
 *   objectID: "0900746983f2e12a"
 * });
 * 
 * if (result.success && result.downloadUrl) {
 *   // 클라이언트에서 다운로드 링크 사용
 *   window.open(result.downloadUrl, '_blank');
 * }
 * ```
 */
export async function getPosFileAndCreateDownloadUrl(params: {
  objectID: string;
  sabun?: string;
  appCode?: string;
  fileCreateMode?: number;
  securityLevel?: string;
  isDesign?: boolean;
}): Promise<{
  success: boolean;
  downloadUrl?: string;
  fileName?: string;
  error?: string;
}> {
  try {
    // 1. POS에서 파일 경로 가져오기
    const posResult = await getEncryptDocumentumFile(params);
    
    if (!posResult.success || !posResult.result) {
      return {
        success: false,
        error: posResult.error || 'POS에서 파일 정보를 가져올 수 없습니다.',
      };
    }

    // 2. 다운로드 URL 생성
    const downloadUrl = await createDownloadUrl(posResult.result);
    
    // 파일명 추출 (경로의 마지막 부분)
    const fileName = posResult.result.split(/[\\\/]/).pop() || 'unknown';

    return {
      success: true,
      downloadUrl,
      fileName,
    };
  } catch (error) {
    return {
      success: false,
      error: error instanceof Error ? error.message : 'Unknown error',
    };
  }
}

/**
 * 자재코드부터 POS 파일 다운로드까지 전체 워크플로우를 처리하는 통합 함수
 * 
 * @example
 * ```typescript
 * const result = await getPosFileByMaterialCode({
 *   materialCode: "SN2693A6410100001"
 * });
 * 
 * if (result.success && result.downloadUrl) {
 *   // 클라이언트에서 다운로드 링크 사용
 *   window.open(result.downloadUrl, '_blank');
 * }
 * ```
 */
export async function getPosFileByMaterialCode(params: {
  materialCode: string;
  /**
   * 여러 파일이 있을 경우 선택할 파일 인덱스 (기본값: 0)
   */
  fileIndex?: number;
}): Promise<{
  success: boolean;
  downloadUrl?: string;
  fileName?: string;
  availableFiles?: PosFileInfo[];
  error?: string;
}> {
  try {
    const { materialCode, fileIndex = 0 } = params;

    // 1. 자재코드로 DCMTM_ID 조회
    const dcmtmResult = await getDcmtmIdByMaterialCode({ materialCode });
    
    if (!dcmtmResult.success || !dcmtmResult.files || dcmtmResult.files.length === 0) {
      return {
        success: false,
        error: dcmtmResult.error || '해당 자재코드에 대한 POS 파일을 찾을 수 없습니다.',
      };
    }

    // 선택된 파일이 범위를 벗어나는지 확인
    if (fileIndex >= dcmtmResult.files.length) {
      return {
        success: false,
        error: `파일 인덱스가 범위를 벗어났습니다. 사용 가능한 파일 수: ${dcmtmResult.files.length}`,
        availableFiles: dcmtmResult.files,
      };
    }

    const selectedFile = dcmtmResult.files[fileIndex];

    // 2. DCMTM_ID로 POS 파일 정보 가져오기
    const posResult = await getPosFileAndCreateDownloadUrl({
      objectID: selectedFile.dcmtmId,
    });

    if (!posResult.success) {
      return {
        success: false,
        error: posResult.error,
        availableFiles: dcmtmResult.files,
      };
    }

    return {
      success: true,
      downloadUrl: posResult.downloadUrl,
      fileName: selectedFile.fileName, // 오라클에서 가져온 파일명 사용
      availableFiles: dcmtmResult.files,
    };
  } catch (error) {
    return {
      success: false,
      error: error instanceof Error ? error.message : 'Unknown error',
    };
  }
}