From 58f5bb4b4fbade01db997a325c491d5e14384173 Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Sun, 20 Jul 2025 07:47:25 +0200 Subject: [PATCH] Refactor dashboard route to use mock data instead of database queries, simplifying the code and improving performance. Remove unnecessary database operations and error handling for user data retrieval. --- src/routes/dashboard.js | 69 ++++++----------------------------------- 1 file changed, 10 insertions(+), 59 deletions(-) diff --git a/src/routes/dashboard.js b/src/routes/dashboard.js index 31f5364..b30cd6b 100644 --- a/src/routes/dashboard.js +++ b/src/routes/dashboard.js @@ -1,7 +1,5 @@ const express = require('express'); const { authenticateToken } = require('../middleware/auth'); -const { executeQuery } = require('../config/database'); -const { checkAuthorizedEmail } = require('../middleware/emailAuth'); const router = express.Router(); @@ -18,45 +16,15 @@ router.get('/', authenticateToken, async (req, res) => { recentActivity: [] }; - try { - // Only try database operations if configured - if (process.env.DB_SERVER) { - // Try to fetch real data from database - const userCountResult = await executeQuery('SELECT COUNT(*) as count FROM Users'); - const userCount = userCountResult.recordset[0]?.count || 0; - - // Update stats with real data - dashboardData.stats[0].value = userCount.toString(); - - // Fetch recent activity - const activityResult = await executeQuery(` - SELECT TOP 10 - CONCAT('User ', name, ' logged in') as description, - FORMAT(last_login, 'yyyy-MM-dd HH:mm') as timestamp - FROM Users - WHERE last_login IS NOT NULL - ORDER BY last_login DESC - `); - - dashboardData.recentActivity = activityResult.recordset || []; - console.log('✅ Dashboard data loaded from database'); - } else { - console.log('⚠️ No database configured, using mock dashboard data'); - // Update with mock data - dashboardData.stats[0].value = '1'; - dashboardData.stats[1].value = '$0'; - dashboardData.stats[2].value = '0'; - dashboardData.stats[3].value = '0%'; - dashboardData.recentActivity = [ - { description: 'System started without database', timestamp: new Date().toISOString().slice(0, 16) } - ]; - } - - } catch (dbError) { - console.error('Database query error in dashboard:', dbError.message); - // Keep fallback data - console.log('✅ Using fallback dashboard data'); - } + // Use mock data since we don't store user information in database + dashboardData.stats[0].value = 'N/A'; + dashboardData.stats[1].value = '$0'; + dashboardData.stats[2].value = '0'; + dashboardData.stats[3].value = '0%'; + dashboardData.recentActivity = [ + { description: 'System operational', timestamp: new Date().toISOString().slice(0, 16) } + ]; + console.log('✅ Dashboard loaded with mock data (no user storage)'); res.json(dashboardData); @@ -69,29 +37,12 @@ router.get('/', authenticateToken, async (req, res) => { // Get user-specific data router.get('/user', authenticateToken, async (req, res) => { try { - const userId = req.user.id; - - let userData = { + const userData = { profile: req.user, preferences: {}, activity: [] }; - try { - // Fetch user preferences from database - const prefsResult = await executeQuery( - 'SELECT * FROM UserPreferences WHERE user_id = @userId', - { userId } - ); - - if (prefsResult.recordset.length > 0) { - userData.preferences = prefsResult.recordset[0]; - } - - } catch (dbError) { - console.error('Database query error for user data:', dbError); - } - res.json(userData); } catch (error) {