Open MuskanNazim opened 2 weeks ago
The plan to solve the bug involves addressing issues in both the database schema and the data fetching logic. The primary steps include:
amount
fields in the Budgets
and Incomes
tables are of type numeric
to maintain consistency and avoid potential issues with data manipulation.BudgetList
component to ensure that the data fetching logic works correctly and handles edge cases.The bug is likely caused by a combination of issues:
amount
fields in the Budgets
and Incomes
tables are defined as varchar
, while the amount
field in the Expenses
table is numeric
. This inconsistency can lead to issues when performing calculations or comparisons.BudgetList
component relies on the user's primary email address to filter budgets. If the user information is not correctly populated, the query will not return any results.Update the utils/schema.jsx
file to change the amount
fields in the Budgets
and Incomes
tables to numeric
.
// utils/schema.jsx
import { pgTable, serial, varchar, numeric } from "drizzle-orm";
export const Budgets = pgTable("budgets", {
id: serial("id").primaryKey(),
name: varchar("name").notNull(),
amount: numeric("amount").notNull(),
icon: varchar("icon"),
createdBy: varchar("createdBy").notNull(),
});
export const Incomes = pgTable("incomes", {
id: serial("id").primaryKey(),
name: varchar("name").notNull(),
amount: numeric("amount").notNull(),
icon: varchar("icon"),
createdBy: varchar("createdBy").notNull(),
});
Update the BudgetList
component to include error handling and user checks.
// app/(routes)/dashboard/budgets/_components/BudgetList.jsx
import React, { useEffect, useState } from 'react';
import { useUser } from '@clerk/nextjs';
import { db } from '../../../utils/dbConfig';
import BudgetItem from './BudgetItem';
const BudgetList = () => {
const { user } = useUser();
const [budgetList, setBudgetList] = useState([]);
const [error, setError] = useState(null);
useEffect(() => {
const getBudgetList = async () => {
if (!user || !user.primaryEmailAddress) {
setError('User information is not available');
return;
}
try {
const budgets = await db.query(`
SELECT
Budgets.id,
Budgets.name,
Budgets.amount,
Budgets.icon,
SUM(Expenses.amount) AS totalSpend,
COUNT(Expenses.id) AS totalItem
FROM Budgets
LEFT JOIN Expenses ON Budgets.id = Expenses.budgetId
WHERE Budgets.createdBy = $1
GROUP BY Budgets.id
ORDER BY Budgets.id DESC
`, [user.primaryEmailAddress.emailAddress]);
setBudgetList(budgets);
} catch (err) {
setError('Failed to fetch budgets');
console.error(err);
}
};
getBudgetList();
}, [user]);
if (error) {
return <div>Error: {error}</div>;
}
return (
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4'>
{budgetList.length > 0 ? (
budgetList.map(budget => (
<BudgetItem key={budget.id} budget={budget} />
))
) : (
<div>Loading...</div>
)}
</div>
);
};
export default BudgetList;
amount
fields in the Budgets
and Incomes
tables defined as varchar
.Ticket title: error
Ticket Description: no budgets are showing
Click here to create a Pull Request with the proposed solution
Files used for this task:
no budgets are showing