fix: auth/me gives back reports for the user
This commit is contained in:
parent
e429ee806c
commit
d620c0e4e7
@ -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 });
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user