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
This commit is contained in:
ghaddaditw
2025-05-30 17:10:55 +00:00
parent de12c6e3df
commit 51d13203fd
2 changed files with 8 additions and 10 deletions
+7 -9
View File
@@ -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<AIInteraction[]> {
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);
}