Fix type error in uploadBudget

The code was throwing a type error because it was comparing a value of type 'unknown' with a number. This commit fixes the error by ensuring that the value being compared is indeed a number.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-21 12:32:26 +00:00
parent befb29611b
commit e03642a75e

View File

@@ -23,7 +23,7 @@ export const uploadBudgets = async (userId: string): Promise<void> => {
const budgetData = JSON.parse(budgetDataStr);
// 월간 예산이 0보다 클 때만 업로드
if (budgetData.monthly && budgetData.monthly.targetAmount > 0) {
if (budgetData.monthly && typeof budgetData.monthly.targetAmount === 'number' && budgetData.monthly.targetAmount > 0) {
console.log('유효한 월간 예산 발견:', budgetData.monthly.targetAmount);
await uploadBudgetData(userId, budgetData);
// 업로드 성공 후 수정 추적 정보 초기화
@@ -88,7 +88,7 @@ async function uploadBudgetData(userId: string, parsedBudgetData: Record<string,
const monthlyTarget = parsedBudgetData.monthly.targetAmount;
// 예산이 0 이하면 업로드하지 않음
if (monthlyTarget <= 0) {
if (typeof monthlyTarget !== 'number' || monthlyTarget <= 0) {
console.log('월간 예산이 0 이하여서 업로드 건너뜀:', monthlyTarget);
return;
}
@@ -102,7 +102,7 @@ async function uploadBudgetData(userId: string, parsedBudgetData: Record<string,
if (existingBudgets && existingBudgets.length > 0) {
const existingBudget = existingBudgets[0];
// 새 예산이 기존 예산보다 클 때만 업데이트
if (monthlyTarget > existingBudget.total_budget) {
if (typeof existingBudget.total_budget === 'number' && monthlyTarget > existingBudget.total_budget) {
console.log(`새 예산(${monthlyTarget})이 기존 예산(${existingBudget.total_budget})보다 큼, 업데이트 실행`);
// 기존 데이터 업데이트
@@ -165,7 +165,9 @@ async function uploadCategoryBudgets(userId: string, parsedCategoryBudgets: Reco
// 기존 카테고리 예산의 총액 계산
let existingTotal = 0;
if (existingCategoryBudgets && existingCategoryBudgets.length > 0) {
existingTotal = existingCategoryBudgets.reduce((sum, item) => sum + item.amount, 0);
existingTotal = existingCategoryBudgets.reduce((sum, item) => {
return sum + (typeof item.amount === 'number' ? item.amount : 0);
}, 0);
}
// 새 카테고리 예산의 총액 계산
@@ -201,7 +203,7 @@ async function uploadCategoryBudgets(userId: string, parsedCategoryBudgets: Reco
// 카테고리별 예산 데이터 변환 및 삽입
const categoryEntries = Object.entries(parsedCategoryBudgets)
.filter(([_, amount]) => amount > 0) // 금액이 0보다 큰 것만 저장
.filter(([_, amount]) => typeof amount === 'number' && amount > 0) // 금액이 0보다 큰 것만 저장
.map(([category, amount]) => ({
user_id: userId,
category,