Clarify data synchronization process

Clarify the data synchronization process and address a scenario where data is lost after logging out, initializing data, and logging back in.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-16 09:58:20 +00:00
parent 3f22e6c484
commit bdf1584095
4 changed files with 303 additions and 71 deletions

View File

@@ -1,6 +1,7 @@
import { supabase } from '@/lib/supabase';
import { isSyncEnabled } from './syncSettings';
import { toast } from '@/hooks/useToast.wrapper';
/**
* Upload budget data from local storage to Supabase
@@ -12,34 +13,70 @@ export const uploadBudgets = async (userId: string): Promise<void> => {
const budgetData = localStorage.getItem('budgetData');
const categoryBudgets = localStorage.getItem('categoryBudgets');
console.log('예산 데이터 업로드 시작');
// 예산 데이터 업로드
if (budgetData) {
const parsedBudgetData = JSON.parse(budgetData);
// 기존 예산 데이터 삭제
await supabase
// 기존 예산 데이터 확인
const { data: existingBudgets, error: fetchError } = await supabase
.from('budgets')
.delete()
.select('*')
.eq('user_id', userId);
if (fetchError) {
console.error('기존 예산 데이터 조회 실패:', fetchError);
}
// 새 예산 데이터 삽입
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
});
if (error) throw error;
// 업데이트 또는 삽입 결정
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,
updated_at: new Date().toISOString()
})
.eq('user_id', userId);
if (error) {
console.error('예산 데이터 업데이트 실패:', error);
throw error;
}
} 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
});
if (error) {
console.error('예산 데이터 삽입 실패:', error);
throw error;
}
}
}
// 카테고리 예산 업로드
if (categoryBudgets) {
const parsedCategoryBudgets = JSON.parse(categoryBudgets);
// 기존 카테고리 예산 삭제
await supabase
const { error: deleteError } = await supabase
.from('category_budgets')
.delete()
.eq('user_id', userId);
if (deleteError) {
console.error('기존 카테고리 예산 삭제 실패:', deleteError);
}
// 카테고리별 예산 데이터 변환 및 삽입
const categoryEntries = Object.entries(parsedCategoryBudgets).map(
@@ -50,16 +87,22 @@ export const uploadBudgets = async (userId: string): Promise<void> => {
})
);
const { error } = await supabase
.from('category_budgets')
.insert(categoryEntries);
if (error) throw error;
if (categoryEntries.length > 0) {
const { error } = await supabase
.from('category_budgets')
.insert(categoryEntries);
if (error) {
console.error('카테고리 예산 삽입 실패:', error);
throw error;
}
}
}
console.log('예산 데이터 업로드 완료');
} catch (error) {
console.error('예산 데이터 업로드 실패:', error);
throw error;
}
};
@@ -70,14 +113,19 @@ export const downloadBudgets = async (userId: string): Promise<void> => {
if (!isSyncEnabled()) return;
try {
console.log('서버에서 예산 데이터 다운로드 시작');
// 예산 데이터 가져오기
const { data: budgetData, error: budgetError } = await supabase
.from('budgets')
.select('*')
.eq('user_id', userId)
.single();
.maybeSingle(); // 사용자당 하나의 예산 데이터만 존재
if (budgetError && budgetError.code !== 'PGRST116') throw budgetError;
if (budgetError && budgetError.code !== 'PGRST116') {
console.error('예산 데이터 조회 실패:', budgetError);
throw budgetError;
}
// 카테고리 예산 가져오기
const { data: categoryData, error: categoryError } = await supabase
@@ -85,43 +133,71 @@ export const downloadBudgets = async (userId: string): Promise<void> => {
.select('*')
.eq('user_id', userId);
if (categoryError) throw categoryError;
if (categoryError) {
console.error('카테고리 예산 조회 실패:', categoryError);
throw categoryError;
}
// 서버에서 받은 예산 데이터가 있으면 로컬에 저장
if (budgetData) {
// 예산 데이터 로컬 형식으로 변환
const localBudgetData = {
console.log('서버에서 예산 데이터 수신:', budgetData);
// 기존 로컬 데이터 가져오기
const localBudgetDataStr = localStorage.getItem('budgetData');
let localBudgetData = localBudgetDataStr ? JSON.parse(localBudgetDataStr) : {
daily: { targetAmount: 0, spentAmount: 0, remainingAmount: 0 },
weekly: { targetAmount: 0, spentAmount: 0, remainingAmount: 0 },
monthly: { targetAmount: 0, spentAmount: 0, remainingAmount: 0 }
};
// 서버 데이터로 업데이트 (지출 금액은 유지)
const updatedBudgetData = {
daily: {
targetAmount: budgetData.daily_target,
spentAmount: 0, // 지출액은 로컬에서 계산
remainingAmount: budgetData.daily_target
spentAmount: localBudgetData.daily.spentAmount,
remainingAmount: budgetData.daily_target - localBudgetData.daily.spentAmount
},
weekly: {
targetAmount: budgetData.weekly_target,
spentAmount: 0,
remainingAmount: budgetData.weekly_target
spentAmount: localBudgetData.weekly.spentAmount,
remainingAmount: budgetData.weekly_target - localBudgetData.weekly.spentAmount
},
monthly: {
targetAmount: budgetData.monthly_target,
spentAmount: 0,
remainingAmount: budgetData.monthly_target
spentAmount: localBudgetData.monthly.spentAmount,
remainingAmount: budgetData.monthly_target - localBudgetData.monthly.spentAmount
}
};
localStorage.setItem('budgetData', JSON.stringify(localBudgetData));
// 로컬 스토리지에 저장
localStorage.setItem('budgetData', JSON.stringify(updatedBudgetData));
console.log('예산 데이터 로컬 저장 완료');
// 이벤트 발생시켜 UI 업데이트
window.dispatchEvent(new Event('budgetDataUpdated'));
}
// 서버에서 받은 카테고리 예산 데이터가 있으면 로컬에 저장
if (categoryData && categoryData.length > 0) {
console.log(`${categoryData.length}개의 카테고리 예산 수신`);
// 카테고리 예산 로컬 형식으로 변환
const localCategoryBudgets = categoryData.reduce((acc, curr) => {
acc[curr.category] = curr.amount;
return acc;
}, {} as Record<string, number>);
// 로컬 스토리지에 저장
localStorage.setItem('categoryBudgets', JSON.stringify(localCategoryBudgets));
console.log('카테고리 예산 로컬 저장 완료');
// 이벤트 발생시켜 UI 업데이트
window.dispatchEvent(new Event('categoryBudgetsUpdated'));
}
console.log('예산 데이터 다운로드 완료');
} catch (error) {
console.error('예산 데이터 다운로드 실패:', error);
throw error;
}
};