Fix budget synchronization issue
Ensure budget data is synchronized correctly after local data deletion.
This commit is contained in:
@@ -55,10 +55,18 @@ const SyncSettings = () => {
|
||||
const result = await trySyncAllData(user.id);
|
||||
|
||||
if (result.success) {
|
||||
toast({
|
||||
title: "동기화 설정 완료",
|
||||
description: "모든 데이터가 클라우드에 동기화되었습니다.",
|
||||
});
|
||||
if (result.partial) {
|
||||
toast({
|
||||
title: "동기화 일부 완료",
|
||||
description: "일부 데이터만 동기화되었습니다. 다시 시도해보세요.",
|
||||
variant: "warning"
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "동기화 설정 완료",
|
||||
description: "모든 데이터가 클라우드에 동기화되었습니다.",
|
||||
});
|
||||
}
|
||||
setLastSync(getLastSyncTime());
|
||||
} else {
|
||||
toast({
|
||||
@@ -98,19 +106,34 @@ const SyncSettings = () => {
|
||||
const result = await trySyncAllData(user.id);
|
||||
|
||||
if (result.success) {
|
||||
toast({
|
||||
title: "동기화 완료",
|
||||
description: "모든 데이터가 클라우드에 동기화되었습니다.",
|
||||
});
|
||||
if (result.downloadSuccess && result.uploadSuccess) {
|
||||
toast({
|
||||
title: "동기화 완료",
|
||||
description: "모든 데이터가 클라우드에 동기화되었습니다.",
|
||||
});
|
||||
} else if (result.downloadSuccess) {
|
||||
toast({
|
||||
title: "다운로드만 성공",
|
||||
description: "서버 데이터를 가져왔지만, 업로드에 실패했습니다.",
|
||||
variant: "warning"
|
||||
});
|
||||
} else if (result.uploadSuccess) {
|
||||
toast({
|
||||
title: "업로드만 성공",
|
||||
description: "로컬 데이터를 업로드했지만, 다운로드에 실패했습니다.",
|
||||
variant: "warning"
|
||||
});
|
||||
}
|
||||
setLastSync(getLastSyncTime());
|
||||
} else {
|
||||
toast({
|
||||
title: "일부 동기화 실패",
|
||||
description: "일부 데이터 동기화 중 문제가 발생했습니다.",
|
||||
description: "일부 데이터 동기화 중 문제가 발생했습니다. 다시 시도해주세요.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('동기화 오류:', error);
|
||||
toast({
|
||||
title: "동기화 오류",
|
||||
description: "동기화 중 문제가 발생했습니다. 다시 시도해주세요.",
|
||||
@@ -124,6 +147,8 @@ const SyncSettings = () => {
|
||||
const formatLastSyncTime = () => {
|
||||
if (!lastSync) return "아직 동기화된 적 없음";
|
||||
|
||||
if (lastSync === '부분-다운로드') return "부분 동기화 (다운로드만)";
|
||||
|
||||
const date = new Date(lastSync);
|
||||
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
|
||||
};
|
||||
|
||||
@@ -20,11 +20,15 @@ export const downloadBudgets = async (userId: string): Promise<void> => {
|
||||
// 예산 데이터 처리
|
||||
if (budgetData) {
|
||||
await processBudgetData(budgetData);
|
||||
} else {
|
||||
console.log('서버에서 예산 데이터를 찾을 수 없음');
|
||||
}
|
||||
|
||||
// 카테고리 예산 데이터 처리
|
||||
if (categoryData && categoryData.length > 0) {
|
||||
await processCategoryBudgetData(categoryData);
|
||||
} else {
|
||||
console.log('서버에서 카테고리 예산 데이터를 찾을 수 없음');
|
||||
}
|
||||
|
||||
console.log('예산 데이터 다운로드 완료');
|
||||
@@ -42,14 +46,15 @@ async function fetchBudgetData(userId: string) {
|
||||
.from('budgets')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.maybeSingle(); // 사용자당 하나의 예산 데이터만 존재
|
||||
.order('created_at', { ascending: false })
|
||||
.limit(1); // 가장 최근 데이터
|
||||
|
||||
if (error && error.code !== 'PGRST116') {
|
||||
if (error) {
|
||||
console.error('예산 데이터 조회 실패:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data;
|
||||
return data && data.length > 0 ? data[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,25 +91,25 @@ async function processBudgetData(budgetData: any) {
|
||||
// 서버 데이터로 업데이트 (지출 금액은 유지)
|
||||
const updatedBudgetData = {
|
||||
daily: {
|
||||
targetAmount: budgetData.daily_target,
|
||||
targetAmount: Math.round(budgetData.total_budget / 30),
|
||||
spentAmount: localBudgetData.daily.spentAmount,
|
||||
remainingAmount: budgetData.daily_target - localBudgetData.daily.spentAmount
|
||||
remainingAmount: Math.round(budgetData.total_budget / 30) - localBudgetData.daily.spentAmount
|
||||
},
|
||||
weekly: {
|
||||
targetAmount: budgetData.weekly_target,
|
||||
targetAmount: Math.round(budgetData.total_budget / 4.3),
|
||||
spentAmount: localBudgetData.weekly.spentAmount,
|
||||
remainingAmount: budgetData.weekly_target - localBudgetData.weekly.spentAmount
|
||||
remainingAmount: Math.round(budgetData.total_budget / 4.3) - localBudgetData.weekly.spentAmount
|
||||
},
|
||||
monthly: {
|
||||
targetAmount: budgetData.monthly_target,
|
||||
targetAmount: budgetData.total_budget,
|
||||
spentAmount: localBudgetData.monthly.spentAmount,
|
||||
remainingAmount: budgetData.monthly_target - localBudgetData.monthly.spentAmount
|
||||
remainingAmount: budgetData.total_budget - localBudgetData.monthly.spentAmount
|
||||
}
|
||||
};
|
||||
|
||||
// 로컬 스토리지에 저장
|
||||
localStorage.setItem('budgetData', JSON.stringify(updatedBudgetData));
|
||||
console.log('예산 데이터 로컬 저장 완료');
|
||||
console.log('예산 데이터 로컬 저장 완료', updatedBudgetData);
|
||||
|
||||
// 이벤트 발생시켜 UI 업데이트
|
||||
window.dispatchEvent(new Event('budgetDataUpdated'));
|
||||
@@ -124,7 +129,7 @@ async function processCategoryBudgetData(categoryData: any[]) {
|
||||
|
||||
// 로컬 스토리지에 저장
|
||||
localStorage.setItem('categoryBudgets', JSON.stringify(localCategoryBudgets));
|
||||
console.log('카테고리 예산 로컬 저장 완료');
|
||||
console.log('카테고리 예산 로컬 저장 완료', localCategoryBudgets);
|
||||
|
||||
// 이벤트 발생시켜 UI 업데이트
|
||||
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
|
||||
|
||||
@@ -9,19 +9,25 @@ export const uploadBudgets = async (userId: string): Promise<void> => {
|
||||
if (!isSyncEnabled()) return;
|
||||
|
||||
try {
|
||||
const budgetData = localStorage.getItem('budgetData');
|
||||
const categoryBudgets = localStorage.getItem('categoryBudgets');
|
||||
const budgetDataStr = localStorage.getItem('budgetData');
|
||||
const categoryBudgetsStr = localStorage.getItem('categoryBudgets');
|
||||
|
||||
console.log('예산 데이터 업로드 시작');
|
||||
|
||||
// 예산 데이터 업로드
|
||||
if (budgetData) {
|
||||
await uploadBudgetData(userId, JSON.parse(budgetData));
|
||||
if (budgetDataStr) {
|
||||
const budgetData = JSON.parse(budgetDataStr);
|
||||
await uploadBudgetData(userId, budgetData);
|
||||
} else {
|
||||
console.log('업로드할 예산 데이터가 없음');
|
||||
}
|
||||
|
||||
// 카테고리 예산 업로드
|
||||
if (categoryBudgets) {
|
||||
await uploadCategoryBudgets(userId, JSON.parse(categoryBudgets));
|
||||
if (categoryBudgetsStr) {
|
||||
const categoryBudgets = JSON.parse(categoryBudgetsStr);
|
||||
await uploadCategoryBudgets(userId, categoryBudgets);
|
||||
} else {
|
||||
console.log('업로드할 카테고리 예산이 없음');
|
||||
}
|
||||
|
||||
console.log('예산 데이터 업로드 완료');
|
||||
@@ -35,48 +41,63 @@ export const uploadBudgets = async (userId: string): Promise<void> => {
|
||||
* 일반 예산 데이터 업로드
|
||||
*/
|
||||
async function uploadBudgetData(userId: string, parsedBudgetData: any): Promise<void> {
|
||||
console.log('예산 데이터 업로드:', parsedBudgetData);
|
||||
|
||||
// 현재 월/년도 가져오기
|
||||
const now = new Date();
|
||||
const currentMonth = now.getMonth() + 1; // 0-11 -> 1-12
|
||||
const currentYear = now.getFullYear();
|
||||
|
||||
// 기존 예산 데이터 확인
|
||||
const { data: existingBudgets, error: fetchError } = await supabase
|
||||
.from('budgets')
|
||||
.select('*')
|
||||
.eq('user_id', userId);
|
||||
.eq('user_id', userId)
|
||||
.eq('month', currentMonth)
|
||||
.eq('year', currentYear);
|
||||
|
||||
if (fetchError) {
|
||||
console.error('기존 예산 데이터 조회 실패:', fetchError);
|
||||
throw fetchError;
|
||||
}
|
||||
|
||||
// 월간 타겟 금액 가져오기
|
||||
const monthlyTarget = parsedBudgetData.monthly.targetAmount;
|
||||
|
||||
// 업데이트 또는 삽입 결정
|
||||
if (existingBudgets && existingBudgets.length > 0) {
|
||||
// 기존 데이터 업데이트
|
||||
const { error } = await supabase
|
||||
.from('budgets')
|
||||
.update({
|
||||
daily_target: parsedBudgetData.daily.targetAmount,
|
||||
weekly_target: parsedBudgetData.weekly.targetAmount,
|
||||
monthly_target: parsedBudgetData.monthly.targetAmount,
|
||||
total_budget: monthlyTarget,
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.eq('user_id', userId);
|
||||
.eq('id', existingBudgets[0].id);
|
||||
|
||||
if (error) {
|
||||
console.error('예산 데이터 업데이트 실패:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log('예산 데이터 업데이트 성공');
|
||||
} else {
|
||||
// 새 데이터 삽입
|
||||
const { error } = await supabase
|
||||
.from('budgets')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
daily_target: parsedBudgetData.daily.targetAmount,
|
||||
weekly_target: parsedBudgetData.weekly.targetAmount,
|
||||
monthly_target: parsedBudgetData.monthly.targetAmount
|
||||
month: currentMonth,
|
||||
year: currentYear,
|
||||
total_budget: monthlyTarget
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error('예산 데이터 삽입 실패:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log('예산 데이터 삽입 성공');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,6 +105,8 @@ async function uploadBudgetData(userId: string, parsedBudgetData: any): Promise<
|
||||
* 카테고리 예산 데이터 업로드
|
||||
*/
|
||||
async function uploadCategoryBudgets(userId: string, parsedCategoryBudgets: Record<string, number>): Promise<void> {
|
||||
console.log('카테고리 예산 업로드:', parsedCategoryBudgets);
|
||||
|
||||
// 기존 카테고리 예산 삭제
|
||||
const { error: deleteError } = await supabase
|
||||
.from('category_budgets')
|
||||
@@ -92,16 +115,17 @@ async function uploadCategoryBudgets(userId: string, parsedCategoryBudgets: Reco
|
||||
|
||||
if (deleteError) {
|
||||
console.error('기존 카테고리 예산 삭제 실패:', deleteError);
|
||||
// 오류가 나도 계속 진행 (중요 데이터가 아니기 때문)
|
||||
}
|
||||
|
||||
// 카테고리별 예산 데이터 변환 및 삽입
|
||||
const categoryEntries = Object.entries(parsedCategoryBudgets).map(
|
||||
([category, amount]) => ({
|
||||
const categoryEntries = Object.entries(parsedCategoryBudgets)
|
||||
.filter(([_, amount]) => amount > 0) // 금액이 0보다 큰 것만 저장
|
||||
.map(([category, amount]) => ({
|
||||
user_id: userId,
|
||||
category,
|
||||
amount
|
||||
})
|
||||
);
|
||||
}));
|
||||
|
||||
if (categoryEntries.length > 0) {
|
||||
const { error } = await supabase
|
||||
@@ -112,5 +136,9 @@ async function uploadCategoryBudgets(userId: string, parsedCategoryBudgets: Reco
|
||||
console.error('카테고리 예산 삽입 실패:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log('카테고리 예산 삽입 성공:', categoryEntries.length, '개');
|
||||
} else {
|
||||
console.log('저장할 카테고리 예산이 없음');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user