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
9 changes: 9 additions & 0 deletions admin/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import Sidebar from "./components/Sidebar";
import BranchRepresentatives from "./pages/BranchRepresentatives";
import CoursesWithoutBR from "./pages/CoursesWithoutBR";
import Students from "./pages/Students";
import Courses from "./pages/Courses";
import CourseLinking from "./pages/CourseLinking";
Expand Down Expand Up @@ -48,6 +49,14 @@ function App() {
</PrivateRoute>
}
/>
<Route
path="/admin/courses-without-br"
element={
<PrivateRoute>
<CoursesWithoutBR />
</PrivateRoute>
}
/>
<Route
path="*"
element={
Expand Down
18 changes: 13 additions & 5 deletions admin/src/apis/br.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ export const fetchBRs = async () => {
try {
const response = await fetch(`${API_BASE_URL}api/br/allBRs`, {
credentials: "include",
headers: {
Authorization: "Bearer admin-coursehub-cc23-golang",
},
});
return await response.json();
} catch (error) {
Expand All @@ -16,6 +13,19 @@ export const fetchBRs = async () => {
}
};

// Fetch all courses that don't have a branch representative
export const fetchCoursesWithoutBR = async () => {
try {
const response = await fetch(`${API_BASE_URL}api/br/coursesWithoutBR`, {
credentials: "include",
});
return await response.json();
} catch (error) {
console.error("Error fetching courses without BR:", error);
throw error;
}
};

// Create a single BR
export const createBR = async (email) => {
try {
Expand All @@ -24,7 +34,6 @@ export const createBR = async (email) => {
credentials: "include",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer admin-coursehub-cc23-golang",
},
body: JSON.stringify({ email }),
});
Expand Down Expand Up @@ -69,7 +78,6 @@ export const uploadBRs = async (file) => {
credentials: "include",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer admin-coursehub-cc23-golang",
},
body: JSON.stringify({ emails }),
});
Expand Down
41 changes: 41 additions & 0 deletions admin/src/components/CoursesWithoutBRTable.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from "react";

const CoursesWithoutBRTable = ({ courses }) => {
return (
<div className="p-6 bg-white rounded-lg shadow-md">
<div className="flex items-center mb-6">
<h2 className="text-2xl font-bold text-gray-800">Courses Without BR</h2>
</div>
{courses.length === 0 ? (
<div className="text-center py-16 text-gray-400 text-sm">No courses found.</div>
) : (
<div className="overflow-x-auto">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-100">
<tr>
<th className="py-3 px-4 text-left text-sm font-semibold text-gray-700">
Code
</th>
<th className="py-3 px-4 text-left text-sm font-semibold text-gray-700">
Name
</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-100">
{courses.map((course) => (
<tr key={course._id || course.code} className="hover:bg-gray-50 transition">
<td className="py-4 px-4 text-sm font-medium text-gray-900">
{course.code}
</td>
<td className="py-4 px-4 text-sm text-gray-600">{course.name}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
);
};

export default CoursesWithoutBRTable;
3 changes: 2 additions & 1 deletion admin/src/components/Sidebar.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from "react";
import { Link, useLocation } from "react-router-dom";
import { FaBook, FaUsers, FaLayerGroup, FaLink, FaUserGraduate } from "react-icons/fa";
import { FaBook, FaUsers, FaLayerGroup, FaLink, FaUserGraduate, FaExclamationTriangle } from "react-icons/fa";
import { adminLogout } from "@/apis/auth";

const navItems = [
{ label: "Branch Representatives", to: "/admin/", icon: FaUsers },
{ label: "Students", to: "/admin/students", icon: FaUserGraduate },
{ label: "Courses", to: "/admin/courses", icon: FaBook },
{ label: "Course Linking", to: "/admin/course-linking", icon: FaLink },
{ label: "Courses Without BR", to: "/admin/courses-without-br", icon: FaExclamationTriangle },
];

const Sidebar = () => {
Expand Down
44 changes: 44 additions & 0 deletions admin/src/pages/CoursesWithoutBR.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useState, useEffect } from "react";
import CoursesWithoutBRTable from "../components/CoursesWithoutBRTable";
import { fetchCoursesWithoutBR } from "@/apis/br";

export default function CoursesWithoutBR() {
const [courses, setCourses] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

const loadCourses = async () => {
try {
setLoading(true);
const response = await fetchCoursesWithoutBR();
setCourses(response.coursesWithoutBR);
setLoading(false);
} catch (err) {
setError(err.message || "An error occurred while fetching courses without BR.");
setLoading(false);
}
};

useEffect(() => {
loadCourses();
}, []);

return (
<div className="p-6 space-y-6">
<div className="bg-white/80 backdrop-blur-sm rounded-2xl shadow-lg border border-gray-200/60 p-6 flex flex-col md:flex-row items-start md:items-center justify-between gap-4">
<div>
<h1 className="text-2xl font-bold text-gray-900">Courses Without BR</h1>
<p className="text-gray-600 mt-1">
Courses that don't have a branch representative assigned yet
</p>
</div>
</div>

<div className="bg-white/80 backdrop-blur-sm rounded-2xl shadow-lg border border-gray-200/60 p-6">
{loading && <p>Loading...</p>}
{error && <p className="text-red-500">{error}</p>}
{!loading && !error && <CoursesWithoutBRTable courses={courses} />}
</div>
</div>
);
}
54 changes: 52 additions & 2 deletions server/modules/br/br.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ import BR from "./br.model.js";
import User from "../user/user.model.js";
import { fetchCoursesForBr } from "../auth/auth.controller.js";
import logger from "../../utils/logger.js";
import CourseModel from "../course/course.model.js";

const normalizeEmail = (email) => email?.toString().trim().toLowerCase();

// Mirrors normalizeCourseCode (utils/course.js) as an aggregation expression: uppercase + strip spaces.
const normalizedCodeExpr = (field) => ({
$replaceAll: {
input: { $toUpper: { $ifNull: [field, ""] } },
find: " ",
replacement: "",
},
});

const findUserByEmailInsensitive = async (email) => {
if (!email) return null;
return User.findOne({ email }).collation({ locale: "en", strength: 2 });
Expand Down Expand Up @@ -142,5 +152,45 @@ const getBRs = async (req, res) => {
res.status(500).json({ error: "Internal Server Error" });
}
};

export { updateBRs, createBR, getAll, deleteBR, getBRs };
const getCoursesWithoutBR = async (req, res) => {
try {
// Normalized course codes covered by any registered BR, computed in the DB via a $lookup join.
const coveredCodesResult = await BR.aggregate([
{ $addFields: { emailLower: { $toLower: "$email" } } },
{
$lookup: {
from: User.collection.name,
let: { emailLower: "$emailLower" },
pipeline: [
{ $match: { $expr: { $eq: [{ $toLower: "$email" }, "$$emailLower"] } } },
{ $project: { courses: 1, _id: 0 } },
],
as: "brUser",
},
},
{ $unwind: "$brUser" },
{ $unwind: { path: "$brUser.courses", preserveNullAndEmptyArrays: false } },
{
$group: {
_id: null,
codes: { $addToSet: normalizedCodeExpr("$brUser.courses.code") },
},
},
]);

const coveredCodes = coveredCodesResult[0]?.codes || [];

// Courses whose normalized code isn't in the covered set, filtered at the DB level.
const coursesWithoutBR = await CourseModel.aggregate([
{ $addFields: { normalizedCode: normalizedCodeExpr("$code") } },
{ $match: { normalizedCode: { $nin: coveredCodes } } },
{ $project: { normalizedCode: 0 } },
]);

res.status(200).json({ coursesWithoutBR });
} catch (error) {
logger.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
};
export { updateBRs, createBR, getAll, deleteBR, getBRs, getCoursesWithoutBR };
3 changes: 2 additions & 1 deletion server/modules/br/br.routes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import express from "express";
import { updateBRs, createBR, getAll, deleteBR, getBRs } from "./br.controller.js";
import { updateBRs, createBR, getAll, deleteBR, getBRs, getCoursesWithoutBR } from "./br.controller.js";

const router = express.Router();

router.post("/updateList", updateBRs);
router.post("/create", createBR);
router.get("/all", getAll);
router.get("/allBRs", getBRs);
router.get("/coursesWithoutBR", getCoursesWithoutBR);
router.delete("/delete", deleteBR);

export default router;