Fix sync error after login

Addresses an issue where a sync error occurs after the user logs in.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-16 10:02:27 +00:00
parent bdf1584095
commit 67fc6be649
3 changed files with 101 additions and 18 deletions

View File

@@ -3,6 +3,42 @@ import { supabase } from '@/lib/supabase';
import { Transaction } from '@/components/TransactionCard';
import { isSyncEnabled } from './syncSettings';
import { toast } from '@/hooks/useToast.wrapper';
import { formatISO } from 'date-fns';
/**
* 날짜 문자열을 ISO 형식으로 변환하는 함수
* "오늘, 19:00 PM"과 같은 형식을 처리하기 위한 함수
*/
const normalizeDate = (dateStr: string): string => {
// 이미 ISO 형식인 경우 그대로 반환
if (dateStr.match(/^\d{4}-\d{2}-\d{2}T/)) {
return dateStr;
}
try {
// "오늘"라는 표현이 있으면 현재 날짜로 변환
if (dateStr.includes('오늘')) {
const today = new Date();
// 시간 추출 시도
const timeMatch = dateStr.match(/(\d{1,2}):(\d{2})/);
if (timeMatch) {
const hours = parseInt(timeMatch[1], 10);
const minutes = parseInt(timeMatch[2], 10);
today.setHours(hours, minutes, 0, 0);
}
return formatISO(today);
}
// 일반 날짜 문자열은 그대로 Date 객체로 변환 시도
return formatISO(new Date(dateStr));
} catch (error) {
console.warn(`날짜 변환 오류: "${dateStr}"를 ISO 형식으로 변환할 수 없습니다.`, error);
// 오류 발생 시 현재 시간 반환 (데이터 손실 방지)
return formatISO(new Date());
}
};
/**
* Upload transaction data from local storage to Supabase
@@ -40,11 +76,14 @@ export const uploadTransactions = async (userId: string): Promise<void> => {
const updateTransactions = [];
for (const t of transactions) {
// 날짜 형식 정규화
const normalizedDate = normalizeDate(t.date);
const transactionData = {
user_id: userId,
title: t.title,
amount: t.amount,
date: t.date,
date: normalizedDate, // 정규화된 날짜 사용
category: t.category,
type: t.type,
transaction_id: t.id
@@ -156,7 +195,6 @@ export const downloadTransactions = async (userId: string): Promise<void> => {
// 이벤트 발생시켜 UI 업데이트
window.dispatchEvent(new Event('transactionUpdated'));
} catch (error) {
console.error('트랜잭션 다운로드 중 오류:', error);
throw error;