fix: auth/me gives back reports for the user

This commit is contained in:
Angie 2025-08-03 22:22:14 +02:00
parent e429ee806c
commit d620c0e4e7

View File

@ -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 });
});
}
);
}