Handle data reset with cloud sync
The prompt requests clarification on whether data reset should also delete cloud data when the user is logged in.
This commit is contained in:
67
src/utils/sync/clearCloudData.ts
Normal file
67
src/utils/sync/clearCloudData.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
console.log('클라우드 데이터 초기화 완료');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('클라우드 데이터 초기화 중 오류 발생:', error);
|
||||
toast({
|
||||
title: "클라우드 데이터 초기화 실패",
|
||||
description: "서버에서 데이터를 삭제하는 중 문제가 발생했습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
import { isSyncEnabled, setSyncEnabled, getLastSyncTime, setLastSyncTime, initSyncSettings } from './sync/syncSettings';
|
||||
import { uploadTransactions, downloadTransactions, deleteTransactionFromServer } from './sync/transactionSync';
|
||||
import { uploadBudgets, downloadBudgets } from './sync/budget';
|
||||
import { clearCloudData } from './sync/clearCloudData';
|
||||
|
||||
// Export all utility functions to maintain the same public API
|
||||
export {
|
||||
@@ -14,7 +14,8 @@ export {
|
||||
downloadBudgets,
|
||||
getLastSyncTime,
|
||||
setLastSyncTime,
|
||||
initSyncSettings
|
||||
initSyncSettings,
|
||||
clearCloudData
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user