Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/controllers/dashboardController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// controllers/dashboardController.js

const {
Feedback,
Achievement,
Expand Down Expand Up @@ -184,4 +185,23 @@ exports.getDashboardStats = async (req, res) => {
console.error("Error fetching dashboard stats:", error);
res.status(500).send("Server Error");
}
};
exports.getRegisteredEvents = async (req, res) => {
try {
if (!req.user) {
return res.status(401).json({ message: "Unauthorized: Please login first" });
}

const userId = req.user._id;

const events = await Event.find({
participants: userId
});

res.status(200).json(events);

} catch (error) {
console.error("Error fetching registered events:", error);
res.status(500).json({ message: "Server error" });
}
};
20 changes: 20 additions & 0 deletions backend/controllers/eventControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@ exports.getLatestEvents = async (req, res) => {
console.error('Error fetching latest events:', error);
res.status(500).json({ message: 'Server error' });
}
};
exports.getRegisteredEvents = async (req, res) => {
try {

if (!req.user) {
return res.status(401).json({ message: "Unauthorized: Please login first" });
}

const userId = req.user._id;

const events = await Event.find({
participants: userId
});

res.status(200).json(events);

} catch (error) {
console.error("Error fetching registered events:", error);
res.status(500).json({ message: "Server error" });
}
};
Comment on lines +24 to 43
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Dead code: this function is never used.

According to the route definition in backend/routes/events.js (lines 499-503), the /registered-events endpoint is wired to dashboardController.getRegisteredEvents, not this export. This implementation is identical to the one in dashboardController.js and is never called.

Remove this dead code to avoid confusion and maintenance burden.

🗑️ Proposed fix: remove unused function
-exports.getRegisteredEvents = async (req, res) => {
-    try {
-
-        if (!req.user) {
-            return res.status(401).json({ message: "Unauthorized: Please login first" });
-        }
-
-        const userId = req.user._id;
-
-        const events = await Event.find({
-            participants: userId
-        });
-
-        res.status(200).json(events);
-
-    } catch (error) {
-        console.error("Error fetching registered events:", error);
-        res.status(500).json({ message: "Server error" });
-    }
-};
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
exports.getRegisteredEvents = async (req, res) => {
try {
if (!req.user) {
return res.status(401).json({ message: "Unauthorized: Please login first" });
}
const userId = req.user._id;
const events = await Event.find({
participants: userId
});
res.status(200).json(events);
} catch (error) {
console.error("Error fetching registered events:", error);
res.status(500).json({ message: "Server error" });
}
};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/controllers/eventControllers.js` around lines 24 - 43, Remove the
dead export exports.getRegisteredEvents from
backend/controllers/eventControllers.js since the route /registered-events is
wired to dashboardController.getRegisteredEvents; locate the function named
getRegisteredEvents (exports.getRegisteredEvents) in that file and delete the
entire async function block and its export to avoid duplication and confusion,
and run tests/lint to ensure no remaining references to
exports.getRegisteredEvents exist.

2 changes: 2 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 21 additions & 12 deletions backend/routes/events.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const dashboardController = require("../controllers/dashboardController");
const express = require("express");
const router = express.Router();
const { Event, User, OrganizationalUnit } = require("../models/schema");
Expand Down Expand Up @@ -255,14 +256,12 @@ router.post(
event: updatedEvent,
});
} catch (error) {
if (error?.name === "CastError") {
return res.status(400).json({ message: "Invalid event ID format." });
}
console.error("Event registration error:", error);
return res
.status(500)
.json({ message: "Server error during registration." });
}
if (error?.name === "CastError") {
return res.status(400).json({ message: "Invalid event ID format." });
}
console.error("Event registration error:", error);
return res.status(500).json({ message: "Internal server error." });
}
Comment on lines +259 to +264
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Inconsistent indentation in catch block.

The catch block content is not properly indented relative to the function body. This appears to be a formatting error.

🧹 Proposed fix: correct indentation
     } catch (error) {
-  if (error?.name === "CastError") {
-    return res.status(400).json({ message: "Invalid event ID format." });
-  }
-  console.error("Event registration error:", error);
-  return res.status(500).json({ message: "Internal server error." });
-}
+      if (error?.name === "CastError") {
+        return res.status(400).json({ message: "Invalid event ID format." });
+      }
+      console.error("Event registration error:", error);
+      return res.status(500).json({ message: "Internal server error." });
+    }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/routes/events.js` around lines 259 - 264, The catch block in the
events route has inconsistent indentation for the lines handling CastError,
console.error, and the res.status(500) return; fix it by re-indenting the entire
catch body so its statements (the if (error?.name === "CastError") check,
console.error("Event registration error:", error), and return
res.status(500).json(...)) align with the surrounding function block style used
in this file (ensure the same number of spaces/tabs as other blocks in this
handler to maintain consistent formatting).

},
);

Expand Down Expand Up @@ -474,7 +473,6 @@ router.put("/:eventId", isAuthenticated, isEventContact, async (req, res) => {
.json({ message: "Server error", error: err.message });
}
});

// Delete an event (only unit contact)
router.delete(
"/:eventId",
Expand All @@ -484,13 +482,24 @@ router.delete(
try {
const { eventId } = req.params;
const deleted = await Event.findByIdAndDelete(eventId);
if (!deleted) return res.status(404).json({ message: "Event not found" });

if (!deleted) {
return res.status(404).json({ message: "Event not found" });
}

return res.json({ message: "Event deleted" });
} catch (err) {
console.error("delete event error:", err);
return res.status(500).json({ message: "Server error" });
}
},
}
);

// Get registered events
router.get(
"/registered-events",
isAuthenticated,
dashboardController.getRegisteredEvents
);
Comment on lines +498 to 503
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Route will never be reached due to ordering.

The /registered-events route is defined after the /:id route (line 269). Express matches routes in declaration order, so a request to /registered-events will be captured by /:id with id = "registered-events", resulting in a MongoDB CastError or unexpected behavior.

Move this route before all parameterized routes (/:id, /:eventId).

🐛 Proposed fix: move route before parameterized routes

Move the route definition to appear before line 151 (before /:eventId/is-contact), for example after line 12:

 router.get("/latest", eventsController.getLatestEvents);
 
+// Get registered events for authenticated user
+router.get(
+  "/registered-events",
+  isAuthenticated,
+  dashboardController.getRegisteredEvents
+);
+
 // Create a new event (new events can be created by admins only)
 router.post(
   "/create",

And remove lines 498-503 from the end of the file.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/routes/events.js` around lines 498 - 503, The "/registered-events"
route registration using router.get("/registered-events", isAuthenticated,
dashboardController.getRegisteredEvents) is declared after parameterized routes
(e.g., '/:id' and '/:eventId') so it will never be reached; relocate that
router.get(...) declaration to a position before any parameterized routes (for
example before the '/:eventId/is-contact' and '/:id' handlers) and remove the
duplicate declaration at the end of the file so the explicit
"/registered-events" path is matched first.


module.exports = router;
module.exports = router;