Open MuskanNazim opened 2 weeks ago
The plan to solve the bug involves addressing potential issues in the database schema, query logic, and user object handling. The primary steps include:
amount
field in the Budgets
table is of a numeric type to avoid issues with numerical operations.getBudgetList
function to capture any issues during database query execution.user
object is correctly populated before calling getBudgetList
.The bug is likely caused by a combination of factors:
amount
field in the Budgets
table is defined as varchar
, which can cause issues when performing numerical operations.useEffect
hook depends on the user
object. If user
is null
or undefined initially, the getBudgetList
function will not be called.getBudgetList
function makes it difficult to diagnose issues during database query execution.Change the data type of the amount
field in the Budgets
table from varchar
to numeric
.
// utils/schema.jsx
export const Budgets = {
id: 'serial',
name: 'varchar',
amount: 'numeric', // Changed from 'varchar' to 'numeric'
icon: 'varchar',
createdBy: 'varchar'
};
export const Expenses = {
id: 'serial',
name: 'varchar',
amount: 'numeric',
budgetId: 'integer',
createdAt: 'varchar'
};
Add error handling and logging in the getBudgetList
function.
// app/(routes)/dashboard/budgets/_components/BudgetList.jsx
const getBudgetList = async () => {
try {
const result = await db.select({
...getTableColumns(Budgets),
totalSpend: sql `sum(${Expenses.amount})`.mapWith(Number),
totalItem: sql `count(${Expenses.id})`.mapWith(Number)
}).from(Budgets)
.leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId))
.where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress))
.groupBy(Budgets.id)
.orderBy(desc(Budgets.id));
setBudgetList(result);
} catch (error) {
console.error("Error fetching budget list:", error);
}
}
Ensure the user
object is correctly populated before calling getBudgetList
.
// app/(routes)/dashboard/budgets/_components/BudgetList.jsx
useEffect(() => {
if (user && user.primaryEmailAddress && user.primaryEmailAddress.emailAddress) {
getBudgetList();
}
}, [user]);
amount
field in the Budgets
table defined as varchar
.useUser
hook is triggered.getBudgetList
function.amount
field is not correctly handled or the user
object is not populated, errors will occur in the console or the budget list will not be displayed correctly.By following these steps, the bug can be replicated and subsequently fixed by implementing the proposed solution.
Click here to create a Pull Request with the proposed solution
Files used for this task:
"use client" import React, { useEffect, useState } from 'react' import CreateBudget from './CreateBudget' import { db } from '@/utils/dbConfig' import { desc, eq, getTableColumns, sql } from 'drizzle-orm' import { Budgets, Expenses } from '@/utils/schema' import { useUser } from '@clerk/nextjs' import BudgetItem from './BudgetItem'
function BudgetList() { const [budgetList, setBudgetList] = useState([]); const { user } = useUser(); useEffect(() => { user && getBudgetList(); }, [user]);
/**
used to get budget List */ const getBudgetList = async () => { const result = await db.select({ ...getTableColumns(Budgets), totalSpend: sql
sum(${Expenses.amount})
.mapWith(Number), totalItem: sqlcount(${Expenses.id})
.mapWith(Number) }).from(Budgets) .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId)) .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress)) .groupBy(Budgets.id) .orderBy(desc(Budgets.id));setBudgetList(result); }
return (
); }
export default BudgetList