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,43 +100,36 @@ public class ApiController implements DefaultApi {
public ResponseEntity<Void> addEmployee(Long id, AddEmployeeDTO body) {
Optional<Project> optionalProject = projectRepository.findById(id);
if (projectRepository.findById(id).isEmpty()) {
if (optionalProject.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
Project project = optionalProject.get();
EmployeeResponseDTO employee;
try {
employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId());
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);
}
boolean hasQualification = employee.getSkillSet()
if (employee.getSkillSet()
.stream()
.anyMatch(qualification -> qualification.getId().equals(body.getQualificationId()));
if (!hasQualification) {
.anyMatch(qualification -> qualification.getId().equals(body.getQualificationId()))) {
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()
if (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);
}