Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Put, | |
| Delete, | |
| Param, | |
| Body, | |
| UseGuards, | |
| HttpCode, | |
| ParseIntPipe, | |
| } from '@nestjs/common'; | |
| import { | |
| ApiTags, | |
| ApiOperation, | |
| ApiResponse, | |
| ApiBearerAuth, | |
| ApiParam, | |
| ApiBody, | |
| } from '@nestjs/swagger'; | |
| import { DepartmentService } from '../services/department.service'; | |
| import { | |
| CreateDepartmentDto, | |
| UpdateDepartmentDto, | |
| DepartmentDto, | |
| } from '../dtos/department.dto'; | |
| import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; | |
| import { RolesGuard } from '../../auth/guards/roles.guard'; | |
| import { Roles } from '../../auth/roles.decorator'; | |
| import { RoleName } from '../../auth/entities/role.entity'; | |
| ('🏢 Departments') | |
| ('api') | |
| (JwtAuthGuard, RolesGuard) | |
| ('JWT-auth') | |
| export class DepartmentController { | |
| constructor(private readonly departmentService: DepartmentService) {} | |
| ('departments') | |
| ( | |
| RoleName.IT_ADMIN, | |
| RoleName.ADMIN, | |
| RoleName.INSTRUCTOR, | |
| RoleName.TA, | |
| RoleName.STUDENT, | |
| ) | |
| ({ | |
| summary: 'List all departments', | |
| description: ` | |
| ## List All Departments | |
| Retrieves all departments across campuses. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles Required**: All roles (STUDENT, INSTRUCTOR, TA, ADMIN, IT_ADMIN) | |
| `, | |
| }) | |
| ({ status: 200, description: 'List of all departments' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| async findAll(): Promise<DepartmentDto[]> { | |
| return this.departmentService.findAll() as Promise<DepartmentDto[]>; | |
| } | |
| ('campuses/:campusId/departments') | |
| ( | |
| RoleName.IT_ADMIN, | |
| RoleName.ADMIN, | |
| RoleName.INSTRUCTOR, | |
| RoleName.TA, | |
| RoleName.STUDENT, | |
| ) | |
| ({ | |
| summary: 'List departments by campus', | |
| description: ` | |
| ## List Departments in Campus | |
| Retrieves all departments belonging to a specific campus. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles Required**: All roles (STUDENT, INSTRUCTOR, TA, ADMIN, IT_ADMIN) | |
| `, | |
| }) | |
| ({ name: 'campusId', description: 'Campus ID', type: Number }) | |
| ({ status: 200, description: 'List of departments' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 404, description: 'Campus not found' }) | |
| async findByCampusId( | |
| ('campusId', ParseIntPipe) campusId: number, | |
| ): Promise<DepartmentDto[]> { | |
| return this.departmentService.findByCampusId(campusId) as Promise< | |
| DepartmentDto[] | |
| >; | |
| } | |
| ('departments') | |
| (RoleName.IT_ADMIN, RoleName.ADMIN) | |
| (201) | |
| ({ | |
| summary: 'Create new department', | |
| description: ` | |
| ## Create New Department | |
| Creates a new department within a campus. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN only | |
| `, | |
| }) | |
| ({ type: CreateDepartmentDto }) | |
| ({ status: 201, description: 'Department created successfully' }) | |
| ({ status: 400, description: 'Invalid input data' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| async create(() dto: CreateDepartmentDto): Promise<DepartmentDto> { | |
| return this.departmentService.create(dto) as Promise<DepartmentDto>; | |
| } | |
| ('departments/:id') | |
| ( | |
| RoleName.IT_ADMIN, | |
| RoleName.ADMIN, | |
| RoleName.INSTRUCTOR, | |
| RoleName.TA, | |
| RoleName.STUDENT, | |
| ) | |
| ({ | |
| summary: 'Get department by ID', | |
| description: ` | |
| ## Get Department Details | |
| Retrieves details of a specific department. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles Required**: All roles (STUDENT, INSTRUCTOR, TA, ADMIN, IT_ADMIN) | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Department ID', type: Number }) | |
| ({ status: 200, description: 'Department details' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 404, description: 'Department not found' }) | |
| async findById( | |
| ('id', ParseIntPipe) id: number, | |
| ): Promise<DepartmentDto> { | |
| return this.departmentService.findById(id) as Promise<DepartmentDto>; | |
| } | |
| ('departments/:id') | |
| (RoleName.IT_ADMIN, RoleName.ADMIN) | |
| ({ | |
| summary: 'Update department', | |
| description: ` | |
| ## Update Department | |
| Updates an existing department. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN only | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Department ID', type: Number }) | |
| ({ type: UpdateDepartmentDto }) | |
| ({ status: 200, description: 'Department updated successfully' }) | |
| ({ status: 400, description: 'Invalid input data' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'Department not found' }) | |
| async update( | |
| ('id', ParseIntPipe) id: number, | |
| () dto: UpdateDepartmentDto, | |
| ): Promise<DepartmentDto> { | |
| return this.departmentService.update(id, dto) as Promise<DepartmentDto>; | |
| } | |
| ('departments/:id') | |
| (RoleName.IT_ADMIN, RoleName.ADMIN) | |
| (204) | |
| ({ | |
| summary: 'Delete department', | |
| description: ` | |
| ## Delete Department | |
| Deletes a department from the system. | |
| ### Access Control | |
| - **Authentication Required**: ✅ Yes (Bearer Token) | |
| - **Roles Required**: ADMIN, IT_ADMIN only | |
| ### ⚠️ Warning | |
| Departments with programs or courses cannot be deleted. | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Department ID', type: Number }) | |
| ({ status: 204, description: 'Department deleted successfully' }) | |
| ({ status: 400, description: 'Cannot delete department with programs' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| ({ status: 403, description: 'Forbidden - Admin access required' }) | |
| ({ status: 404, description: 'Department not found' }) | |
| async delete(('id', ParseIntPipe) id: number): Promise<void> { | |
| return this.departmentService.delete(id); | |
| } | |
| } | |