Fix budget synchronization issue

Ensure budget data is synchronized correctly after local data deletion.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-16 10:11:09 +00:00
parent c3ddd3b88f
commit b0177ba5cd
4 changed files with 136 additions and 49 deletions

View File

@@ -1,7 +1,7 @@
import { isSyncEnabled, setSyncEnabled, getLastSyncTime, setLastSyncTime, initSyncSettings } from './sync/syncSettings';
import { uploadTransactions, downloadTransactions, deleteTransactionFromServer } from './sync/transactionSync';
import { uploadBudgets, downloadBudgets } from './sync/budgetSync';
import { uploadBudgets, downloadBudgets } from './sync/budget';
// Export all utility functions to maintain the same public API
export {
@@ -26,13 +26,16 @@ export const syncAllData = async (userId: string): Promise<void> => {
try {
console.log('데이터 동기화 시작...');
// 기 동기화 순서 변경: 서버에서 먼저 다운로드 후, 로컬 데이터 업로드
// 이렇게 하면 서버에 저장된 데이터를 먼저 가져온 후, 로컬 변경사항을 반영
// 기 동기화 순서: 서버에서 먼저 다운로드 후, 로컬 데이터 업로드
// 이 순서를 유지하여 서버에 저장된 데이터를 먼저 가져온 후, 로컬 변경사항을 반영
// 1. 서버에서 데이터 다운로드 (기존 데이터 불러오기)
await downloadTransactions(userId);
await downloadBudgets(userId);
// 약간의 딜레이를 추가하여 다운로드된 데이터가 처리될 시간을 줌
await new Promise(resolve => setTimeout(resolve, 500));
// 2. 로컬 데이터를 서버에 업로드 (변경사항 반영)
await uploadTransactions(userId);
await uploadBudgets(userId);
@@ -46,40 +49,66 @@ export const syncAllData = async (userId: string): Promise<void> => {
}
};
// 안전하게 동기화 시도하는 함수 추가
// 안전하게 동기화 시도하는 함수 개선
export const trySyncAllData = async (userId: string): Promise<{ success: boolean, error?: any }> => {
if (!userId || !isSyncEnabled()) return { success: true };
let downloadSuccess = false;
let uploadSuccess = false;
try {
// 각 단계별로 안전하게 동기화 시도
console.log('안전한 데이터 동기화 시도...');
try {
// 1단계: 서버에서 데이터 다운로드
await downloadTransactions(userId);
await downloadBudgets(userId);
// 각 단계가 성공적으로 완료되면 동기화 시간 부분 업데이트
setLastSyncTime();
console.log('서버 데이터 다운로드 성공');
downloadSuccess = true;
// 다운로드 단계가 성공적으로 완료되면 부분 동기화 마킹
setLastSyncTime('부분-다운로드');
} catch (downloadError) {
console.error('다운로드 동기화 오류:', downloadError);
// 다운로드 실패해도 업로드는 시도 - 부분 동기화
}
// 다운로드 후 약간의 지연을 추가하여 로컬 상태가 업데이트될 시간을 줌
await new Promise(resolve => setTimeout(resolve, 500));
try {
// 2단계: 로컬 데이터를 서버에 업로드
await uploadTransactions(userId);
await uploadBudgets(userId);
console.log('로컬 데이터 업로드 성공');
uploadSuccess = true;
// 업로드까지 성공적으로 완료되면 동기화 시간 업데이트
setLastSyncTime();
} catch (uploadError) {
console.error('업로드 동기화 오류:', uploadError);
// 업로드 실패해도 부분 동기화는 성공한 것으로 간주
// 업로드 실패해도 다운로드는 성공했을 수 있으므로 부분 성공으로 간주
}
// 모든 단계가 시도되었으므로 성공으로 간주
return { success: true };
// 하나라도 성공했으면 부분 성공으로 간주
const result = {
success: downloadSuccess || uploadSuccess,
partial: (downloadSuccess && !uploadSuccess) || (!downloadSuccess && uploadSuccess),
downloadSuccess,
uploadSuccess
};
console.log('동기화 결과:', result);
return result;
} catch (error) {
console.error('전체 동기화 시도 중 오류:', error);
return { success: false, error };
return {
success: false,
error,
downloadSuccess,
uploadSuccess
};
}
};