import { supabase } from '@/archive/lib/supabase'; import { Transaction } from '@/components/TransactionCard'; import { isSyncEnabled } from '../syncSettings'; import { normalizeDate } from './dateUtils'; import { getDeletedTransactions, removeFromDeletedTransactions } from './deletedTransactionsTracker'; /** * Upload transaction data from local storage to Supabase * 로컬 데이터를 서버에 업로드 (새로운 또는 수정된 데이터만) */ export const uploadTransactions = async (userId: string): Promise => { if (!isSyncEnabled()) { console.log('[동기화] 업로드: 동기화 비활성화 상태, 작업 건너뜀'); return; } try { console.log('[동기화] 트랜잭션 업로드 시작'); const uploadStartTime = new Date().toISOString(); // 로컬 트랜잭션 데이터 로드 const localTransactions = localStorage.getItem('transactions'); if (!localTransactions) { console.log('[동기화] 로컬 트랜잭션 데이터 없음, 업로드 건너뜀'); return; } // 트랜잭션 파싱 const transactions: Transaction[] = JSON.parse(localTransactions); console.log(`[동기화] 로컬 트랜잭션 ${transactions.length}개 동기화 시작`); if (transactions.length === 0) { console.log('[동기화] 트랜잭션이 없음, 업로드 건너뜀'); return; // 트랜잭션이 없으면 처리하지 않음 } // 삭제된 트랜잭션 처리 const deletedIds = getDeletedTransactions(); if (deletedIds.length > 0) { console.log(`[동기화] 삭제된 트랜잭션 ${deletedIds.length}개 처리 시작`); // 100개씩 나눠서 처리 (대용량 데이터 처리) const batchSize = 100; for (let i = 0; i < deletedIds.length; i += batchSize) { const batch = deletedIds.slice(i, i + batchSize); console.log(`[동기화] 삭제 배치 처리 중: ${i+1}~${Math.min(i+batch.length, deletedIds.length)}/${deletedIds.length}`); // 각 삭제된 ID 처리 (병렬 처리) const deletePromises = batch.map(async (id) => { try { const { error } = await supabase .from('transactions') .delete() .eq('transaction_id', id) .eq('user_id', userId); if (error) { console.error(`[동기화] 트랜잭션 삭제 실패 (ID: ${id}):`, error); return { id, success: false }; } else { console.log(`[동기화] 트랜잭션 삭제 성공: ${id}`); removeFromDeletedTransactions(id); return { id, success: true }; } } catch (err) { console.error(`[동기화] 트랜잭션 삭제 중 오류 (ID: ${id}):`, err); return { id, success: false }; } }); // 병렬 처리 대기 const results = await Promise.all(deletePromises); const successCount = results.filter(r => r.success).length; console.log(`[동기화] 삭제 배치 처리 결과: ${successCount}/${batch.length} 성공`); } } // 먼저 서버에서 현재 트랜잭션 목록 가져오기 const { data: existingData, error: fetchError } = await supabase .from('transactions') .select('transaction_id, updated_at') .eq('user_id', userId); if (fetchError) { console.error('[동기화] 기존 트랜잭션 조회 실패:', fetchError); console.error('[동기화] 오류 상세:', JSON.stringify(fetchError, null, 2)); throw fetchError; } // 서버에 이미 있는 트랜잭션 ID 맵 생성 const existingMap = new Map(); existingData?.forEach(t => { existingMap.set(t.transaction_id, t.updated_at); }); console.log(`[동기화] 서버에 이미 존재하는 트랜잭션: ${existingMap.size}개`); // 삽입할 새 트랜잭션과 업데이트할 기존 트랜잭션 분리 const newTransactions = []; const updateTransactions = []; for (const t of transactions) { try { // 삭제 목록에 있는 트랜잭션은 건너뜀 if (deletedIds.includes(t.id)) { console.log(`[동기화] 삭제된 항목 건너뜀: ${t.id}`); continue; } // 날짜 형식 정규화 const normalizedDate = normalizeDate(t.date); // 현재 시간을 타임스탬프로 사용 const timestamp = t.localTimestamp || uploadStartTime; const transactionData = { user_id: userId, title: t.title || '무제', amount: t.amount || 0, date: normalizedDate, // 정규화된 날짜 사용 category: t.category || '기타', type: t.type || 'expense', transaction_id: t.id, notes: t.notes || null, updated_at: timestamp }; // 서버에 이미 존재하는지 확인 if (existingMap.has(t.id)) { // 서버 타임스탬프와 비교 const serverTimestamp = existingMap.get(t.id); // 로컬 데이터가 더 최신인 경우만 업데이트 if (!serverTimestamp || timestamp > serverTimestamp) { updateTransactions.push(transactionData); console.log(`[동기화] 업데이트 필요: ${t.id} - ${t.title} (로컬: ${timestamp}, 서버: ${serverTimestamp || '없음'})`); } else { console.log(`[동기화] 업데이트 불필요: ${t.id} - ${t.title} (로컬: ${timestamp}, 서버: ${serverTimestamp})`); } } else { newTransactions.push(transactionData); console.log(`[동기화] 새 항목 추가: ${t.id} - ${t.title}`); } } catch (err) { console.error(`[동기화] 트랜잭션 처리 중 오류 (ID: ${t.id}):`, err); // 개별 트랜잭션 오류는 기록하고 계속 진행 } } // 새 트랜잭션 삽입 (있는 경우) - 배치 처리 if (newTransactions.length > 0) { console.log(`[동기화] ${newTransactions.length}개의 새 트랜잭션 업로드`); // 대용량 데이터 처리를 위해 배치 처리 (최대 100개씩) const batchSize = 100; for (let i = 0; i < newTransactions.length; i += batchSize) { const batch = newTransactions.slice(i, i + batchSize); console.log(`[동기화] 새 트랜잭션 배치 업로드 중: ${i+1}~${Math.min(i+batch.length, newTransactions.length)}/${newTransactions.length}`); const { error: insertError } = await supabase .from('transactions') .insert(batch); if (insertError) { console.error(`[동기화] 새 트랜잭션 배치 업로드 실패:`, insertError); console.error('[동기화] 오류 상세:', JSON.stringify(insertError, null, 2)); // 배치 실패해도 다음 배치 계속 시도 } else { console.log(`[동기화] 새 트랜잭션 배치 업로드 성공: ${batch.length}개`); } } } // 기존 트랜잭션 업데이트 (있는 경우) - 배치 처리 if (updateTransactions.length > 0) { console.log(`[동기화] ${updateTransactions.length}개의 기존 트랜잭션 업데이트`); // 대용량 데이터 처리를 위해 배치 처리 (최대 50개씩) const batchSize = 50; for (let i = 0; i < updateTransactions.length; i += batchSize) { const batch = updateTransactions.slice(i, i + batchSize); console.log(`[동기화] 트랜잭션 업데이트 배치 처리 중: ${i+1}~${Math.min(i+batch.length, updateTransactions.length)}/${updateTransactions.length}`); // 배치 내 트랜잭션을 병렬로 업데이트 (Promise.all 사용) const updatePromises = batch.map(transaction => supabase .from('transactions') .update(transaction) .eq('transaction_id', transaction.transaction_id) .eq('user_id', userId) ); try { const results = await Promise.all(updatePromises); // 오류 확인 const errors = results.filter(result => result.error); if (errors.length > 0) { console.error(`[동기화] ${errors.length}개의 트랜잭션 업데이트 실패`); errors.forEach(err => { console.error('[동기화] 업데이트 오류:', err.error); }); } else { console.log(`[동기화] 트랜잭션 업데이트 배치 성공: ${batch.length}개`); } } catch (batchError) { console.error(`[동기화] 트랜잭션 배치 업데이트 실패:`, batchError); // 배치 실패해도 다음 배치 계속 시도 } } } console.log('[동기화] 트랜잭션 업로드 완료'); } catch (error) { console.error('[동기화] 트랜잭션 업로드 실패:', error); console.error('[동기화] 오류 상세:', JSON.stringify(error, null, 2)); throw error; } };