Execute SQL queries

Run the provided SQL queries to create tables.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-15 16:34:20 +00:00
parent 1136683b57
commit af52ec897f
5 changed files with 247 additions and 55 deletions

View File

@@ -0,0 +1,82 @@
import { supabase } from '@/lib/supabase';
import { isSyncEnabled } from '@/utils/syncUtils';
/**
* 카테고리 예산을 Supabase에서 가져오기
*/
export const loadCategoryBudgetsFromSupabase = async (userId: string): Promise<Record<string, number> | null> => {
if (!userId || !isSyncEnabled()) return null;
try {
const { data, error } = await supabase
.from('category_budgets')
.select('*')
.eq('user_id', userId);
if (error) {
console.error('카테고리 예산 조회 오류:', error);
return null;
}
if (data && data.length > 0) {
// 데이터를 카테고리->금액 형식의 객체로 변환
const categoryBudgets: Record<string, number> = {};
data.forEach(item => {
categoryBudgets[item.category] = item.amount;
});
return categoryBudgets;
}
return null;
} catch (error) {
console.error('카테고리 예산 로드 오류:', error);
return null;
}
};
/**
* 카테고리 예산을 Supabase에 저장
*/
export const saveCategoryBudgetsToSupabase = async (
userId: string,
categoryBudgets: Record<string, number>
): Promise<boolean> => {
if (!userId || !isSyncEnabled()) return false;
try {
// 기존 데이터 삭제 (다시 추가하기 위해)
const { error: deleteError } = await supabase
.from('category_budgets')
.delete()
.eq('user_id', userId);
if (deleteError) {
console.error('카테고리 예산 삭제 오류:', deleteError);
return false;
}
// 새 데이터 추가
const budgetEntries = Object.entries(categoryBudgets).map(([category, amount]) => ({
user_id: userId,
category,
amount
}));
if (budgetEntries.length === 0) return true;
const { error } = await supabase
.from('category_budgets')
.insert(budgetEntries);
if (error) {
console.error('카테고리 예산 추가 오류:', error);
return false;
}
return true;
} catch (error) {
console.error('카테고리 예산 저장 오류:', error);
return false;
}
};

View File

@@ -17,7 +17,10 @@ export const syncTransactionsWithSupabase = async (
.select('*')
.eq('user_id', user.id);
if (error) throw error;
if (error) {
console.error('Supabase 데이터 조회 오류:', error);
return transactions;
}
if (data && data.length > 0) {
// Supabase 데이터 로컬 형식으로 변환
@@ -59,7 +62,7 @@ export const updateTransactionInSupabase = async (
if (!user || !isSyncEnabled()) return;
try {
await supabase.from('transactions')
const { error } = await supabase.from('transactions')
.upsert({
user_id: user.id,
title: transaction.title,
@@ -69,6 +72,10 @@ export const updateTransactionInSupabase = async (
type: transaction.type,
transaction_id: transaction.id
});
if (error) {
console.error('트랜잭션 업데이트 오류:', error);
}
} catch (error) {
console.error('Supabase 업데이트 오류:', error);
}
@@ -82,9 +89,13 @@ export const deleteTransactionFromSupabase = async (
if (!user || !isSyncEnabled()) return;
try {
await supabase.from('transactions')
const { error } = await supabase.from('transactions')
.delete()
.eq('transaction_id', transactionId);
if (error) {
console.error('트랜잭션 삭제 오류:', error);
}
} catch (error) {
console.error('Supabase 삭제 오류:', error);
}