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