PMT-4 Refactor addEmployee
Some checks failed
Quality Check / Validate OAS (push) Successful in 51s
Quality Check / Validate OAS (pull_request) Successful in 1m6s
Quality Check / Linting (push) Failing after 1m41s
Quality Check / Linting (pull_request) Failing after 1m49s
Quality Check / Testing (push) Successful in 2m13s
Quality Check / Static Analysis (push) Successful in 2m18s
Quality Check / Testing (pull_request) Successful in 2m9s
Quality Check / Static Analysis (pull_request) Successful in 2m12s

This commit is contained in:
Rajbir Singh 2024-10-21 10:07:58 +02:00
parent 63ce5526b4
commit 8b0cb91e53

View file

@ -100,8 +100,7 @@ public class ApiController implements DefaultApi {
public ResponseEntity<Void> addEmployee(Long id, AddEmployeeDTO body) { public ResponseEntity<Void> addEmployee(Long id, AddEmployeeDTO body) {
Optional<Project> optionalProject = projectRepository.findById(id); Optional<Project> optionalProject = projectRepository.findById(id);
if (optionalProject.isEmpty()) {
if (projectRepository.findById(id).isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} }
Project project = optionalProject.get(); Project project = optionalProject.get();
@ -115,28 +114,22 @@ public class ApiController implements DefaultApi {
: HttpStatus.INTERNAL_SERVER_ERROR); : HttpStatus.INTERNAL_SERVER_ERROR);
} }
boolean hasQualification = employee.getSkillSet() if (employee.getSkillSet()
.stream() .stream()
.anyMatch(qualification -> qualification.getId().equals(body.getQualificationId())); .anyMatch(qualification -> qualification.getId().equals(body.getQualificationId()))) {
if (!hasQualification) {
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
} }
long start = project.getStart().toEpochSecond(null); long start = project.getStart().toEpochSecond(null);
long plannedEnd = project.getPlannedEnd().toEpochSecond(null); long plannedEnd = project.getPlannedEnd().toEpochSecond(null);
List<Allocation> allocations = allocationRepository.findAllocationsByEmployeeId(body.getEmployeeId()); List<Allocation> allocations = allocationRepository.findAllocationsByEmployeeId(body.getEmployeeId());
if (allocations.stream()
boolean hasOverlap = allocations.stream()
.map(Allocation::getProject) .map(Allocation::getProject)
.anyMatch(allocatedProject -> { .anyMatch(allocatedProject -> {
long allocatedStart = allocatedProject.getStart().toEpochSecond(null); long allocatedStart = allocatedProject.getStart().toEpochSecond(null);
long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(null); long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(null);
return Math.max(start, allocatedStart) <= Math.min(plannedEnd, allocatedPlannedEnd); return Math.max(start, allocatedStart) <= Math.min(plannedEnd, allocatedPlannedEnd);
}); })) {
if (hasOverlap) {
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
} }