diff --git a/backend/routes/auth.js b/backend/routes/auth.js index f34976e..c54333a 100644 --- a/backend/routes/auth.js +++ b/backend/routes/auth.js @@ -8,6 +8,18 @@ const verifyToken = require("../middleware/auth"); const router = express.Router(); const JWT_SECRET = process.env.JWT_SECRET || "your-secret-key"; +//helper with today's reports +function fetchTodaysReports(userId, cb) { + const today = new Date().toISOString().split("T")[0]; + db.all( + `SELECT * FROM reports WHERE userId = ? AND reportDate = ?`, + [userId, today], + (err, reports) => { + cb(err, reports || []); + } + ); +} + // Login endpoint // LOGIN endpoint with today's reports for non-admin router.post( @@ -139,16 +151,22 @@ router.get("/me", verifyToken, (req, res) => { // For employees, only assigned stores db.all( `SELECT stores.id, stores.name, stores.address - FROM stores - JOIN user_store_access ON stores.id = user_store_access.storeId - WHERE user_store_access.userId = ?`, + FROM stores + JOIN user_store_access ON stores.id = user_store_access.storeId + WHERE user_store_access.userId = ?`, [user.id], (err, stores) => { if (err) { return res.status(500).json({ error: "Database error" }); } user.stores = stores; - res.json({ user }); + // ⬇️ Fetch and attach todaysReports! + fetchTodaysReports(user.id, (err, todaysReports) => { + if (err) { + return res.status(500).json({ error: "Database error" }); + } + res.json({ user, todaysReports }); + }); } ); }