feat: update login endpoint to retrieve stores for worker
This commit is contained in:
@@ -50,15 +50,52 @@ router.post(
|
||||
{ expiresIn: "24h" }
|
||||
);
|
||||
|
||||
res.json({
|
||||
token,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
name: user.name,
|
||||
role: user.role,
|
||||
},
|
||||
});
|
||||
// === Return all stores for admin, only assigned for employee/manager ===
|
||||
if (user.role === "admin") {
|
||||
db.all(
|
||||
"SELECT id, name, address FROM stores",
|
||||
[],
|
||||
(err, stores) => {
|
||||
if (err) {
|
||||
return res.status(500).json({ error: "Database error" });
|
||||
}
|
||||
res.json({
|
||||
token,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
fullName: user.fullName,
|
||||
role: user.role,
|
||||
stores: stores,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
} else {
|
||||
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 = ?`,
|
||||
[user.id],
|
||||
(err, stores) => {
|
||||
if (err) {
|
||||
return res.status(500).json({ error: "Database error" });
|
||||
}
|
||||
res.json({
|
||||
token,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
fullName: user.fullName,
|
||||
role: user.role,
|
||||
stores: stores,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
// ===
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
@@ -79,9 +116,34 @@ router.get("/me", verifyToken, (req, res) => {
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: "User not found" });
|
||||
}
|
||||
// Ensure name field exists for frontend (even if null)
|
||||
user.fullName = user.fullName || "";
|
||||
res.json({ user });
|
||||
|
||||
// For admin, return all stores
|
||||
if (user.role === "admin") {
|
||||
db.all("SELECT id, name, address FROM stores", [], (err, stores) => {
|
||||
if (err) {
|
||||
return res.status(500).json({ error: "Database error" });
|
||||
}
|
||||
user.stores = stores;
|
||||
res.json({ user });
|
||||
});
|
||||
} else {
|
||||
// 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 = ?`,
|
||||
[user.id],
|
||||
(err, stores) => {
|
||||
if (err) {
|
||||
return res.status(500).json({ error: "Database error" });
|
||||
}
|
||||
user.stores = stores;
|
||||
res.json({ user });
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user