Skip to content

Commit 3c1ce7b

Browse files
authored
PROPERLY finished the mentee log summary endpoint (backend) - WIT 235 (#166)
PROPERLY done with backend mentee tracking
1 parent 4537f5f commit 3c1ce7b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

backend/database/mentee.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,49 @@ const menteeViewHours = async (req, res) => {
6969
}
7070
};
7171

72+
// function for storing and retrieving mentee hours from the database
73+
// unique identifier for each mentee, full name
74+
// total required hours, sum of logged hours, timestamp of last log update
75+
76+
const menteeLogSummary = async (req, res) => {
77+
const zid = verifyToken(req.headers["authorization"], res);
78+
if (zid instanceof Object) return;
79+
80+
try {
81+
const query = `
82+
SELECT
83+
u.zid,
84+
u.firstname,
85+
u.lastname,
86+
COALESCE(SUM(h.num_hours), 0) AS total_logged_hours,
87+
MAX(h.timestamp) AS last_log_timestamp
88+
FROM users u
89+
LEFT JOIN hours h ON u.zid = h.zid AND h.status = 'approved'
90+
WHERE u.zid = $1
91+
GROUP BY u.zid, u.firstname, u.lastname
92+
`;
93+
94+
const { rows } = await db.query(query, [zid]);
95+
96+
if (rows.length === 0) {
97+
return res.status(404).json({ message: "Mentee not found" });
98+
}
99+
100+
const result = {
101+
...rows[0],
102+
total_required_hours: 20
103+
};
104+
105+
res.status(200).json(result);
106+
} catch (err) {
107+
108+
res.status(500).json({ message: "Internal server error" });
109+
}
110+
};
111+
112+
72113
module.exports = {
73114
requestHours,
74115
menteeViewHours,
116+
menteeLogSummary,
75117
};

backend/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ app.post("/user/forgot-password", auth.forgotPassword);
2626
// -------- Mentee --------//
2727
app.post("/mentee/request-hours", mentee.requestHours);
2828
app.get("/mentee/view-hours", mentee.menteeViewHours);
29+
app.get("/mentee/log-summary", mentee.menteeLogSummary);
2930

3031
// -------- Admin --------//
3132
app.patch("/admin/approve-hours", admin.approveHours);

0 commit comments

Comments
 (0)