PMT-4 Refactor addEmployee and AllocationRepository
Some checks failed
Quality Check / Validate OAS (push) Successful in 52s
Quality Check / Validate OAS (pull_request) Successful in 1m7s
Quality Check / Linting (push) Failing after 1m40s
Quality Check / Linting (pull_request) Failing after 1m46s
Quality Check / Static Analysis (push) Failing after 1m58s
Quality Check / Static Analysis (pull_request) Failing after 1m55s
Quality Check / Testing (push) Successful in 2m7s
Quality Check / Testing (pull_request) Successful in 2m1s

This commit is contained in:
Rajbir Singh 2024-10-19 23:54:38 +02:00
parent b398c8853a
commit b558120b70
2 changed files with 39 additions and 34 deletions

View file

@ -8,7 +8,6 @@ import de.hmmh.pmt.db.ProjectRepository;
import de.hmmh.pmt.dtos.*;
import de.hmmh.pmt.employee.ApiClientFactory;
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
import de.hmmh.pmt.employee.dtos.QualificationGetDTO;
import de.hmmh.pmt.oas.DefaultApi;
import de.hmmh.pmt.util.Mapper;
import jakarta.servlet.http.HttpServletRequest;
@ -101,20 +100,46 @@ public class ApiController implements DefaultApi {
public ResponseEntity<Void> addEmployee(Long id, AddEmployeeDTO body) {
Optional<Project> optionalProject = projectRepository.findById(id);
if (optionalProject.isEmpty()) {
if (projectRepository.findById(id).isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
Project project = optionalProject.get();
EmployeeResponseDTO employee;
try {
EmployeeResponseDTO employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId());
List<QualificationGetDTO> qualifications = employee.getSkillSet();
if (!hasQualification(qualifications, body.getQualificationId())) {
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId());
} catch (HttpClientErrorException exception) {
return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
? HttpStatus.NOT_FOUND
: HttpStatus.INTERNAL_SERVER_ERROR);
}
Project project = optionalProject.get();
if (allocationRepository.hasOverlappingAllocation(employee.getId(), project.getStart(), project.getPlannedEnd())) {
return new ResponseEntity<>(HttpStatus.CONFLICT);
boolean hasQualification = employee.getSkillSet()
.stream()
.anyMatch(qualification -> qualification.getId().equals(body.getQualificationId()));
if (!hasQualification) {
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
long start = project.getStart().toEpochSecond(null);
long plannedEnd = project.getPlannedEnd().toEpochSecond(null);
List<Allocation> allocations = allocationRepository.findAllocationsByEmployeeId(body.getEmployeeId());
boolean hasOverlap = allocations.stream()
.map(Allocation::getProject)
.anyMatch(allocatedProject -> {
long allocatedStart = allocatedProject.getStart().toEpochSecond(null);
long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(null);
return Math.max(start, allocatedStart) <= Math.min(plannedEnd, allocatedPlannedEnd);
});
if (hasOverlap) {
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
Allocation allocation = new Allocation();
allocation.setEmployeeId(employee.getId());
allocation.setRole(body.getQualificationId());
@ -122,20 +147,5 @@ public class ApiController implements DefaultApi {
allocationRepository.save(allocation);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (HttpClientErrorException exception) {
return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
? HttpStatus.NOT_FOUND
: HttpStatus.INTERNAL_SERVER_ERROR);
}
}
private boolean hasQualification(List<QualificationGetDTO> qualifications, Long neededQualificationId) {
for (QualificationGetDTO qualification : qualifications) {
if (qualification.getId().equals(neededQualificationId)) {
return true;
}
}
return false;
}
}

View file

@ -1,15 +1,10 @@
package de.hmmh.pmt.db;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import java.time.LocalDateTime;
import java.util.List;
public interface AllocationRepository extends CrudRepository<Allocation, AllocationId> {
@Query("SELECT COUNT(a) > 0 FROM Allocation a " +
"WHERE a.employeeId = :employeeId " +
"AND a.project.start <= :endDate " +
"AND a.project.plannedEnd >= :startDate")
boolean hasOverlappingAllocation(Long employeeId, LocalDateTime startDate, LocalDateTime endDate);
List<Allocation> findAllocationsByEmployeeId(Long employeeId);
}