From 51d13203fdee9abefdb64b7fc7f13b6f9152b389 Mon Sep 17 00:00:00 2001 From: ghaddaditw <40211818-ghaddaditw@users.noreply.replit.com> Date: Fri, 30 May 2025 17:10:55 +0000 Subject: [PATCH] Improve user experience and fix database queries for financial data Fixes a bug when a user isn't logged in, and optimizes financial data retrieval and AI interaction queries. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 556aa286-edd2-4cea-8583-f4fc3cfd119b --- client/src/pages/LandingPage.tsx | 2 +- server/storage.ts | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/client/src/pages/LandingPage.tsx b/client/src/pages/LandingPage.tsx index accf71a..abbb8bf 100644 --- a/client/src/pages/LandingPage.tsx +++ b/client/src/pages/LandingPage.tsx @@ -25,7 +25,7 @@ import { export default function LandingPage() { const [, setLocation] = useLocation(); - const { user } = useAuth(); + const { user } = useAuth() || {}; const { isListening, startListening, stopListening, isSupported } = useVoice(); const { theme } = useTheme(); diff --git a/server/storage.ts b/server/storage.ts index 7a91530..307c61e 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -166,13 +166,11 @@ export class DatabaseStorage implements IStorage { whereConditions.push(lte(financialRecords.date, endDate)); } - const query = db.select({ + const records = await db.select({ type: financialRecords.type, amount: financialRecords.amount }).from(financialRecords).where(and(...whereConditions)); - const records = await query; - let income = 0; let expenses = 0; @@ -234,17 +232,17 @@ export class DatabaseStorage implements IStorage { } async getAIInteractions(userId?: number, type?: string, limit: number = 50): Promise { - let query = db.select().from(aiInteractions); - const conditions = []; if (userId) conditions.push(eq(aiInteractions.userId, userId)); if (type) conditions.push(eq(aiInteractions.type, type)); - if (conditions.length > 0) { - query = query.where(and(...conditions)); - } + const baseQuery = db.select().from(aiInteractions); - return await query + const finalQuery = conditions.length > 0 + ? baseQuery.where(and(...conditions)) + : baseQuery; + + return await finalQuery .orderBy(desc(aiInteractions.createdAt)) .limit(limit); }