From 5b8d0a0e4505bd15858eab32f51d5736c41a33f2 Mon Sep 17 00:00:00 2001 From: Otto Date: Tue, 31 Mar 2026 07:42:06 -0400 Subject: [PATCH] Fix entries filter by local date --- server/index.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/server/index.js b/server/index.js index 5cb1c30..00ff678 100644 --- a/server/index.js +++ b/server/index.js @@ -17,16 +17,35 @@ const users = { 1: { id: 1, username: 'admin', password: 'admin123', isAdmin: tr function getUserId(req) { return req.headers['x-user-id'] || req.body.user_id || 1; } -app.get('/api/entries', (req, res) => res.json(db.prepare('SELECT * FROM entries WHERE user_id = ? ORDER BY created_at DESC').all(getUserId(req)))); +function getLocalDate() { + const now = new Date(); + const offset = now.getTimezoneOffset() * 60000; + return new Date(now.getTime() - offset).toISOString().split('T')[0]; +} + +app.get('/api/entries', (req, res) => { + const u = getUserId(req); + const date = req.query.date || getLocalDate(); + // Use LIKE to match date at start (handles UTC vs local) + const dateLike = date + '%'; + const entries = db.prepare('SELECT * FROM entries WHERE user_id = ? AND created_at LIKE ? ORDER BY created_at DESC').all(u, dateLike); + res.json(entries); +}); -// Single entry route - MUST come before wildcard app.get('/api/entries/:id', (req, res) => { const entry = db.prepare('SELECT * FROM entries WHERE id = ? AND user_id = ?').get(req.params.id, getUserId(req)); if (!entry) return res.status(404).json({error: 'Entry not found'}); res.json(entry); }); -app.get('/api/summary/daily', (req, res) => { const u = getUserId(req); const s = db.prepare('SELECT COALESCE(SUM(calories),0) as total_calories, COALESCE(SUM(protein),0) as total_protein, COALESCE(SUM(carbs),0) as total_carbs, COALESCE(SUM(fat),0) as total_fat FROM entries WHERE user_id = ? AND date(created_at) = ?').get(u, new Date().toISOString().split('T')[0]); res.json(s); }); +app.get('/api/summary/daily', (req, res) => { + const u = getUserId(req); + const date = req.query.date || getLocalDate(); + const dateLike = date + '%'; + const s = db.prepare('SELECT COALESCE(SUM(calories),0) as total_calories, COALESCE(SUM(protein),0) as total_protein, COALESCE(SUM(carbs),0) as total_carbs, COALESCE(SUM(fat),0) as total_fat FROM entries WHERE user_id = ? AND created_at LIKE ?').get(u, dateLike); + res.json(s); +}); + app.get('/api/favorites', (req, res) => res.json(db.prepare('SELECT * FROM entries WHERE favorite = 1 AND user_id = ?').all(getUserId(req)))); app.get('/api/goals', (req, res) => res.json({calories: 2000, protein: 150, carbs: 250, fat: 65})); app.get('/api/streak', (req, res) => res.json({currentStreak: 0, longestStreak: 0})); @@ -53,6 +72,5 @@ app.delete('/api/admin/users/:id', (req, res) => res.json({success:true})); app.post('/api/admin/users/:id/reset-password', (req, res) => { if(users[req.params.id]) { users[req.params.id].password = req.body.password; res.json({success:true}); } else res.status(404).json({error:'Not found'}); }); app.post('/api/admin/users', (req, res) => res.json({id:3, username:req.body.username, isAdmin:false})); -// SPA fallback - MUST be last app.get('*', (req, res) => res.sendFile(path.join(process.cwd(), 'public', 'index.html'))); app.listen(PORT, '0.0.0.0', () => console.log('Meal Tracker running on port ' + PORT));