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

@@ -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'));

View File

@@ -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('저장할 카테고리 예산이 없음');
}
}