Refactor syncUtils module

Refactor the syncUtils module to improve code organization and maintainability by breaking it down into smaller, more focused utility functions.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-15 05:12:43 +00:00
parent 8783a607fa
commit d74acdbbb8
4 changed files with 233 additions and 199 deletions

View File

@@ -0,0 +1,76 @@
import { supabase } from '@/lib/supabase';
import { Transaction } from '@/components/TransactionCard';
import { isSyncEnabled } from './syncSettings';
/**
* Upload transaction data from local storage to Supabase
*/
export const uploadTransactions = async (userId: string): Promise<void> => {
if (!isSyncEnabled()) return;
try {
const localTransactions = localStorage.getItem('transactions');
if (!localTransactions) return;
const transactions: Transaction[] = JSON.parse(localTransactions);
// 기존 데이터 삭제 후 새로 업로드
await supabase
.from('transactions')
.delete()
.eq('user_id', userId);
// 트랜잭션 배치 처리
const { error } = await supabase.from('transactions').insert(
transactions.map(t => ({
user_id: userId,
title: t.title,
amount: t.amount,
date: t.date,
category: t.category,
type: t.type,
transaction_id: t.id // 로컬 ID 보존
}))
);
if (error) throw error;
console.log('트랜잭션 업로드 완료');
} catch (error) {
console.error('트랜잭션 업로드 실패:', error);
}
};
/**
* Download transaction data from Supabase to local storage
*/
export const downloadTransactions = async (userId: string): Promise<void> => {
if (!isSyncEnabled()) return;
try {
const { data, error } = await supabase
.from('transactions')
.select('*')
.eq('user_id', userId);
if (error) throw error;
if (data && data.length > 0) {
// Supabase 형식에서 로컬 형식으로 변환
const transactions = data.map(t => ({
id: t.transaction_id || t.id,
title: t.title,
amount: t.amount,
date: t.date,
category: t.category,
type: t.type
}));
localStorage.setItem('transactions', JSON.stringify(transactions));
console.log('트랜잭션 다운로드 완료');
}
} catch (error) {
console.error('트랜잭션 다운로드 실패:', error);
}
};