파일 업로드 회사별로 보이도록 수정

This commit is contained in:
dohyeons
2025-11-04 17:57:28 +09:00
parent 958aeb2d53
commit acaa3414d2
3 changed files with 154 additions and 87 deletions

View File

@@ -204,24 +204,37 @@ const FileUploadComponent: React.FC<FileUploadComponentProps> = ({
// 템플릿 파일과 데이터 파일을 조회하는 함수
const loadComponentFiles = useCallback(async () => {
if (!component?.id) return;
if (!component?.id) return false;
try {
let screenId =
formData?.screenId ||
(typeof window !== "undefined" && window.location.pathname.includes("/screens/")
? parseInt(window.location.pathname.split("/screens/")[1])
: null);
// 디자인 모드인 경우 기본 화면 ID 사용
if (!screenId && isDesignMode) {
screenId = 40; // 기본 화면 ID
console.log("📂 디자인 모드: 기본 화면 ID 사용 (40)");
// 1. formData에서 screenId 가져오기
let screenId = formData?.screenId;
// 2. URL에서 screenId 추출 (/screens/:id 패턴)
if (!screenId && typeof window !== "undefined") {
const pathname = window.location.pathname;
const screenMatch = pathname.match(/\/screens\/(\d+)/);
if (screenMatch) {
screenId = parseInt(screenMatch[1]);
console.log("📂 URL에서 화면 ID 추출:", screenId);
}
}
// 3. 디자인 모드인 경우 임시 화면 ID 사용
if (!screenId && isDesignMode) {
screenId = 999999; // 디자인 모드 임시 ID
console.log("📂 디자인 모드: 임시 화면 ID 사용 (999999)");
}
// 4. 화면 ID가 없으면 컴포넌트 ID만으로 조회 시도
if (!screenId) {
console.log("📂 화면 ID 없음, 기존 파일 로직 사용");
return false; // 기존 로직 사용
console.warn("⚠️ 화면 ID 없음, 컴포넌트 ID만으로 파일 조회:", {
componentId: component.id,
pathname: window.location.pathname,
formData: formData,
});
// screenId를 0으로 설정하여 컴포넌트 ID로만 조회
screenId = 0;
}
const params = {
@@ -229,7 +242,7 @@ const FileUploadComponent: React.FC<FileUploadComponentProps> = ({
componentId: component.id,
tableName: formData?.tableName || component.tableName,
recordId: formData?.id,
columnName: component.columnName,
columnName: component.columnName || component.id, // 🔑 columnName이 없으면 component.id 사용
};
console.log("📂 컴포넌트 파일 조회:", params);
@@ -319,7 +332,7 @@ const FileUploadComponent: React.FC<FileUploadComponentProps> = ({
return false; // 기존 로직 사용
}, [component.id, component.tableName, component.columnName, formData?.screenId, formData?.tableName, formData?.id]);
// 컴포넌트 파일 동기화
// 컴포넌트 파일 동기화 (DB 우선, localStorage는 보조)
useEffect(() => {
const componentFiles = (component as any)?.uploadedFiles || [];
const lastUpdate = (component as any)?.lastFileUpdate;
@@ -332,15 +345,15 @@ const FileUploadComponent: React.FC<FileUploadComponentProps> = ({
currentUploadedFiles: uploadedFiles.length,
});
// 먼저 새로운 템플릿 파일 조회 시도
loadComponentFiles().then((useNewLogic) => {
if (useNewLogic) {
console.log("✅ 새로운 템플릿 파일 로직 사용");
return; // 새로운 로직이 성공했으면 기존 로직 스킵
// 🔒 항상 DB에서 최신 파일 목록을 조회 (멀티테넌시 격리)
loadComponentFiles().then((dbLoadSuccess) => {
if (dbLoadSuccess) {
console.log("✅ DB에서 파일 로드 성공 (멀티테넌시 적용)");
return; // DB 로드 성공 시 localStorage 무시
}
// 기존 로직 사용
console.log("📂 기존 파일 로직 사용");
// DB 로드 실패 시에만 기존 로직 사용 (하위 호환성)
console.log("📂 DB 로드 실패, 기존 로직 사용");
// 전역 상태에서 최신 파일 정보 가져오기
const globalFileState = typeof window !== "undefined" ? (window as any).globalFileState || {} : {};
@@ -358,34 +371,6 @@ const FileUploadComponent: React.FC<FileUploadComponentProps> = ({
lastUpdate: lastUpdate,
});
// localStorage에서 백업 파일 복원 (새로고침 시 중요!)
try {
const backupKey = `fileUpload_${component.id}`;
const backupFiles = localStorage.getItem(backupKey);
if (backupFiles) {
const parsedFiles = JSON.parse(backupFiles);
if (parsedFiles.length > 0 && currentFiles.length === 0) {
console.log("🔄 localStorage에서 파일 복원:", {
componentId: component.id,
restoredFiles: parsedFiles.length,
files: parsedFiles.map((f: any) => ({ objid: f.objid, name: f.realFileName })),
});
setUploadedFiles(parsedFiles);
// 전역 상태에도 복원
if (typeof window !== "undefined") {
(window as any).globalFileState = {
...(window as any).globalFileState,
[component.id]: parsedFiles,
};
}
return;
}
}
} catch (e) {
console.warn("localStorage 백업 복원 실패:", e);
}
// 최신 파일과 현재 파일 비교
if (JSON.stringify(currentFiles) !== JSON.stringify(uploadedFiles)) {
console.log("🔄 useEffect에서 파일 목록 변경 감지:", {
@@ -535,24 +520,39 @@ const FileUploadComponent: React.FC<FileUploadComponentProps> = ({
// targetObjid 생성 - 템플릿 vs 데이터 파일 구분
const tableName = formData?.tableName || component.tableName || "default_table";
const recordId = formData?.id;
const screenId = formData?.screenId;
const columnName = component.columnName || component.id;
// screenId 추출 (우선순위: formData > URL)
let screenId = formData?.screenId;
if (!screenId && typeof window !== "undefined") {
const pathname = window.location.pathname;
const screenMatch = pathname.match(/\/screens\/(\d+)/);
if (screenMatch) {
screenId = parseInt(screenMatch[1]);
}
}
let targetObjid;
if (recordId && tableName) {
// 실제 데이터 파일
// 우선순위: 1) 실제 데이터 (recordId가 숫자/문자열이고 temp_가 아님) > 2) 템플릿 (screenId) > 3) 기본값
const isRealRecord = recordId && typeof recordId !== 'undefined' && !String(recordId).startsWith('temp_');
if (isRealRecord && tableName) {
// 실제 데이터 파일 (진짜 레코드 ID가 있을 때만)
targetObjid = `${tableName}:${recordId}:${columnName}`;
console.log("📁 실제 데이터 파일 업로드:", targetObjid);
} else if (screenId) {
// 템플릿 파일
targetObjid = `screen_${screenId}:${component.id}`;
console.log("🎨 템플릿 파일 업로드:", targetObjid);
// 🔑 템플릿 파일 (백엔드 조회 형식과 동일하게)
targetObjid = `screen_files:${screenId}:${component.id}:${columnName}`;
console.log("🎨 템플릿 파일 업로드:", { targetObjid, screenId, componentId: component.id, columnName });
} else {
// 기본값 (화면관리에서 사용)
targetObjid = `temp_${component.id}`;
console.log("📝 기본 파일 업로드:", targetObjid);
}
// 🔒 현재 사용자의 회사 코드 가져오기 (멀티테넌시 격리)
const userCompanyCode = (window as any).__user__?.companyCode;
const uploadData = {
// 🎯 formData에서 백엔드 API 설정 가져오기
autoLink: formData?.autoLink || true,
@@ -562,6 +562,7 @@ const FileUploadComponent: React.FC<FileUploadComponentProps> = ({
isVirtualFileColumn: formData?.isVirtualFileColumn || true,
docType: component.fileConfig?.docType || "DOCUMENT",
docTypeName: component.fileConfig?.docTypeName || "일반 문서",
companyCode: userCompanyCode, // 🔒 멀티테넌시: 회사 코드 명시적 전달
// 호환성을 위한 기존 필드들
tableName: tableName,
fieldName: columnName,