Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

feat: add admin only functions and general optimizations - #10

Merged
DanielvG-IT merged 24 commits into
mainfrom
AdminOnly
Oct 19, 2025
Merged

feat: add admin only functions and general optimizations#10
DanielvG-IT merged 24 commits into
mainfrom
AdminOnly

Conversation

@DanielvG-IT

Copy link
Copy Markdown
Contributor

This pull request introduces significant improvements to how electives and teachers are managed, centralizes password handling, and streamlines user and teacher-related logic. The most important changes are grouped below:

Elective-Teacher Assignment and Data Model

  • Added assignTeacherToElective and unassignTeacherFromElective methods to IElectiveService, with full implementation in ElectiveService, allowing teachers to be assigned or unassigned from electives with proper validation. (apps/backend/src/application/ports/elective.port.ts [1] apps/backend/src/application/services/elective.service.ts [2]
  • Updated the Elective domain model and Mongoose schema to include a teachers field (array of teacher IDs), and removed the modulesGiven field from TeacherUser and the teacher schema, shifting the relationship to be managed on the elective side. (apps/backend/src/domain/elective/elective.ts [1] apps/backend/src/domain/user/user.ts [2] apps/backend/src/infrastructure/mongoose/schemas/elective.schema.ts [3] apps/backend/src/infrastructure/mongoose/schemas/teacher.schema.ts [4]

Repository and Query Enhancements

  • Added findByTeacherId to IElectiveRepository and implemented it in the Mongoose repository, enabling efficient queries for electives by teacher assignment. Also updated create/update logic to handle teacher IDs as ObjectIds. (apps/backend/src/domain/elective/elective.repository.interface.ts [1] apps/backend/src/infrastructure/mongoose/repositories/mongoose-elective.repository.ts [2] [3]
  • Changed teacher-related queries in TeacherService to use the new repository method, simplifying logic when fetching electives taught by a teacher. (apps/backend/src/application/services/teacher.service.ts [1] [2]

User and Authentication Improvements

  • Centralized password hashing and comparison logic into a new PasswordUtil utility, replacing direct bcrypt usage in services and ensuring consistent password handling. (apps/backend/src/application/utils/password.util.ts [1] apps/backend/src/application/services/auth.service.ts [2] [3] apps/backend/src/application/services/user.service.ts [4]
  • Enhanced UserService to prevent duplicate user creation by checking for existing emails, and to restrict deletion of teachers who are assigned to electives using the new assignment model. (apps/backend/src/application/services/user.service.ts [1] [2]

API and Controller Updates

  • Added an endpoint to fetch all users in IUserService and its implementation. (apps/backend/src/application/ports/user.port.ts [1] apps/backend/src/application/services/user.service.ts [2]
  • Updated the electives controller to use both AuthGuard and RolesGuard for enhanced security. (apps/backend/src/interfaces/controllers/elective.controller.ts [1] [2]

These changes collectively modernize the way electives and teachers are related, improve security and maintainability, and streamline service and repository logic.

- Create PasswordUtil utility class with hash() and compare() methods
- Uses bcrypt with 10 salt rounds
- Centralizes password logic for reusability across auth and user services
- Replace direct bcrypt.compare with PasswordUtil.compare
- Remove bcrypt import, add PasswordUtil import
- Improves code consistency and maintainability
- Add getAllUsers() method to IUserService interface and implementation
- Add email uniqueness validation in createUser()
- Implement password hashing using PasswordUtil in createUser()
- Add proper error handling for duplicate users
- Add createUserDTO with firstName, lastName, email, password, and role
- Add updateUserDTO for partial user updates (firstName, lastName, email)
- Refactor naming convention to camelCase (baseUserDTO, studentUserDTO, etc.)
- Improves API contract consistency
- Add GET /users endpoint to retrieve all users (admin only)
- Add POST /users endpoint to create new users (admin only)
- Add PATCH /users/:userId to update user details (admin only)
- Add DELETE /users/:userId to delete users (admin only)
- All endpoints protected with @roles('admin') decorator
- Includes proper validation and error handling
- Add userApi.getAll() to fetch all users
- Add userApi.create() to create new users
- Add userApi.update() to update user details
- Add userApi.delete() to remove users
- Add userApi.separateByRole() helper to split users by role
- All methods include proper error handling and TypeScript typing
- Create Dialog component with accessibility features (Escape key, overlay)
- Add DialogContent, DialogHeader, DialogTitle, DialogDescription subcomponents
- Add DialogFooter for action buttons
- Handles body scroll lock when dialog is open
- Styled with Tailwind CSS and theme-aware
- Add UserCard: displays user details with edit/delete actions
- Add UserList: renders list of users with add button
- Add UserTabs: tabbed interface for students/teachers
- Add UserFormDialog: form for creating/editing users
- Components support both students and teachers
- Shows role-specific data (favorites for students, modules for teachers)
- Complete CRUD functionality for users
- Separate tabs for students and teachers
- Create, edit, and delete user operations
- Real-time state updates after mutations
- Proper loading states and error handling
- Toast notifications for user feedback
- Confirmation dialogs for destructive actions
- Add /users route for UserManagement page (admin only)
- Add /admin route for AdminDashboard page (admin only)
- Both routes protected with ProtectedRoute and RoleProtectedRoute
- Import AdminDashboard and UserManagement components
- Add 'Users' link to header (visible to admins only)
- Add 'Recommendations' link to header (visible to students only)
- Implement role-based filtering in navigation rendering
- Add NavItem interface with optional roles property
- Update JSDoc comment for better clarity
- Specify 'current authenticated user' instead of generic 'user data'
- Minor documentation improvement
Restructure the many-to-many relationship between teachers and electives
by moving ownership from Teacher.modulesGiven[] to Elective.teachers[].

This improves query performance and domain semantics by making electives
the aggregate root that owns the relationship.

Changes:
- Add teachers[] array to Elective model and schema
- Remove modulesGiven[] from Teacher model and schema
- Move assign/unassign logic from TeacherService to ElectiveService
- Add findByTeacherId() method to ElectiveRepository
- Update TeacherService.getElectivesGiven() to query by teacher ID
- Update UserService to check electives when deleting teachers
- Update frontend types and Profile component to fetch electives
- Update UserCard component to remove modulesGiven display

BREAKING CHANGE: Teacher.modulesGiven[] no longer exists. Use
Elective.teachers[] or ElectiveRepository.findByTeacherId() instead.
@DanielvG-IT DanielvG-IT self-assigned this Oct 19, 2025
@DanielvG-IT DanielvG-IT added the enhancement New feature or request label Oct 19, 2025
@DanielvG-IT DanielvG-IT changed the title Admin only feat: add admin only functions and general optimizations Oct 19, 2025
@DanielvG-IT
DanielvG-IT merged commit 09a7c4a into main Oct 19, 2025
4 checks passed
@DanielvG-IT
DanielvG-IT deleted the AdminOnly branch October 19, 2025 16:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant