import { Body,Controller,Delete,Get,NotFoundException,Param,ParseIntPipe,Patch,Post,UseGuards } from '@nestjs/common'; import { Employees, Roles as RoleEnum } from '@prisma/client'; import { EmployeesService } from '../services/employees.service'; import { CreateEmployeeDto } from '../dtos/create-employee.dto'; import { UpdateEmployeeDto } from '../dtos/update-employee.dto'; import { RolesAllowed } from '../../../common/decorators/roles.decorators'; import { ApiBearerAuth, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'; import { EmployeeListItemDto } from '../dtos/list-employee.dto'; import { EmployeeProfileItemDto } from '../dtos/profil-employee.dto'; @ApiTags('Employees') @ApiBearerAuth('access-token') // @UseGuards() @Controller('employees') export class EmployeesController { constructor(private readonly employeesService: EmployeesService) {} @Post() //@RolesAllowed(RoleEnum.ADMIN, RoleEnum.HR, RoleEnum.SUPERVISOR) @ApiOperation({summary: 'Create employee' }) @ApiResponse({ status: 201, description: 'Employee created', type: CreateEmployeeDto }) @ApiResponse({ status: 400, description: 'Incomplete task or invalid data' }) create(@Body() dto: CreateEmployeeDto): Promise { return this.employeesService.create(dto); } @Get() //@RolesAllowed(RoleEnum.ADMIN, RoleEnum.HR, RoleEnum.SUPERVISOR, RoleEnum.ACCOUNTING) @ApiOperation({summary: 'Find all employees' }) @ApiResponse({ status: 200, description: 'List of employees found', type: CreateEmployeeDto, isArray: true }) @ApiResponse({ status: 400, description: 'List of employees not found' }) findAll(): Promise { return this.employeesService.findAll(); } @Get('employee-list') //@RolesAllowed(RoleEnum.ADMIN, RoleEnum.HR, RoleEnum.SUPERVISOR, RoleEnum.ACCOUNTING) @ApiOperation({summary: 'Find all employees with scoped info' }) @ApiResponse({ status: 200, description: 'List of employees with scoped info found', type: EmployeeListItemDto, isArray: true }) @ApiResponse({ status: 400, description: 'List of employees with scoped info not found' }) findListEmployees(): Promise { return this.employeesService.findListEmployees(); } @Get(':email') //@RolesAllowed(RoleEnum.ADMIN, RoleEnum.HR, RoleEnum.SUPERVISOR,RoleEnum.ACCOUNTING ) @ApiOperation({summary: 'Find employee' }) @ApiResponse({ status: 200, description: 'Employee found', type: CreateEmployeeDto }) @ApiResponse({ status: 400, description: 'Employee not found' }) findOne(@Param('email', ParseIntPipe) email: string): Promise { return this.employeesService.findOne(email); } @Get('profile/:email') @ApiOperation({summary: 'Find employee profile' }) @ApiParam({ name: 'email', type: String, description: 'Identifier of the employee' }) @ApiResponse({ status: 200, description: 'Employee profile found', type: EmployeeProfileItemDto }) @ApiResponse({ status: 400, description: 'Employee profile not found' }) findOneProfile(@Param('email') email: string): Promise { return this.employeesService.findOneProfile(email); } @Delete(':email') //@RolesAllowed(RoleEnum.ADMIN, RoleEnum.HR, RoleEnum.SUPERVISOR ) @ApiOperation({summary: 'Delete employee' }) @ApiParam({ name: 'email', type: Number, description: 'Email of the employee to delete' }) @ApiResponse({ status: 204, description: 'Employee deleted' }) @ApiResponse({ status: 404, description: 'Employee not found' }) remove(@Param('email', ParseIntPipe) email: string): Promise { return this.employeesService.remove(email); } @Patch(':email') //@RolesAllowed(RoleEnum.ADMIN, RoleEnum.HR, RoleEnum.SUPERVISOR) @ApiBearerAuth('access-token') @ApiOperation({ summary: 'Update, archive or restore an employee' }) @ApiParam({ name: 'email', type: Number, description: 'Email of the employee' }) @ApiResponse({ status: 200, description: 'Employee updated or restored', type: CreateEmployeeDto }) @ApiResponse({ status: 202, description: 'Employee archived successfully', type: CreateEmployeeDto }) @ApiResponse({ status: 404, description: 'Employee not found in active or archive' }) async updateOrArchiveOrRestore(@Param('email') email: string, @Body() dto: UpdateEmployeeDto,) { // if last_work_day is set => archive the employee // else if employee is archived and first_work_day or last_work_day = null => restore //otherwise => standard update const result = await this.employeesService.patchEmployee(email, dto); if(!result) { throw new NotFoundException(`Employee with email: ${ email } is not found in active or archive.`) } return result; } }