Files
zellyy-finance/src/utils/sync/clearCloudData.ts
gpt-engineer-app[bot] dfb9d8ca69 Fix sync toggle issue
The sync toggle was not working as expected. This commit fixes the issue.
2025-03-18 01:42:00 +00:00

72 lines
2.3 KiB
TypeScript

import { supabase } from '@/lib/supabase';
import { isSyncEnabled } from './syncSettings';
import { toast } from '@/hooks/useToast.wrapper';
/**
* Supabase에 저장된 사용자의 모든 데이터 삭제
*/
export const clearCloudData = async (userId: string): Promise<boolean> => {
if (!userId || !isSyncEnabled()) return false;
try {
console.log('클라우드 데이터 초기화 시작...');
// 트랜잭션 데이터 삭제
const { error: txError } = await supabase
.from('transactions')
.delete()
.eq('user_id', userId);
if (txError) {
console.error('클라우드 트랜잭션 삭제 오류:', txError);
throw txError;
}
// 예산 데이터 삭제 (budgets 테이블이 있는 경우)
try {
const { error: budgetError } = await supabase
.from('budgets')
.delete()
.eq('user_id', userId);
if (budgetError) {
console.error('클라우드 예산 삭제 오류:', budgetError);
// 이 오류는 심각하지 않으므로 진행 계속
}
} catch (e) {
console.log('budgets 테이블이 없거나 삭제 중 오류 발생:', e);
}
// 카테고리 예산 데이터 삭제 (category_budgets 테이블이 있는 경우)
try {
const { error: catBudgetError } = await supabase
.from('category_budgets')
.delete()
.eq('user_id', userId);
if (catBudgetError) {
console.error('클라우드 카테고리 예산 삭제 오류:', catBudgetError);
// 이 오류는 심각하지 않으므로 진행 계속
}
} catch (e) {
console.log('category_budgets 테이블이 없거나 삭제 중 오류 발생:', e);
}
// 동기화 설정 초기화 및 마지막 동기화 시간 초기화
localStorage.removeItem('lastSync');
localStorage.setItem('syncEnabled', 'false'); // 동기화 설정을 OFF로 변경
console.log('클라우드 데이터 초기화 완료 및 동기화 설정 OFF');
return true;
} catch (error) {
console.error('클라우드 데이터 초기화 중 오류 발생:', error);
toast({
title: "클라우드 데이터 초기화 실패",
description: "서버에서 데이터를 삭제하는 중 문제가 발생했습니다.",
variant: "destructive"
});
return false;
}
};