Skip to content

Commit

Permalink
#354 rework validation of breaks and rest time
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausRicharz committed Jul 3, 2024
1 parent bf1550b commit b765211
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/main/java/org/tb/dailyreport/service/TimereportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,15 @@ public List<WorkingDayValidationError> validateForRelease(Long employeeContractI
.filter(timeReport -> timeReport.getOrderType().isRelevantForWorkingTimeValidation())
.collect(Collectors.groupingBy(TimereportDTO::getReferenceday, Collectors.mapping(identity(), Collectors.toList())))
.forEach((date, timeReports) -> {
Workingday workingDay = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date, employeeContractId);
WorkingDayValidationError error = validateBeginOfWorkingDay(workingDay, date);
WorkingDayValidationError error = validateBeginOfWorkingDay(date, employeeContractId);
if (error != null) {
errors.add(error);
}
error = validateBreakTimes(timeReports, workingDay);
error = validateBreakTimes(date, employeeContractId, timeReports);
if (error != null) {
errors.add(error);
}
error = validateRestTime(workingDay, date, employeeContractId);
error = validateRestTime(date, employeeContractId);
if (error != null) {
errors.add(error);
}
Expand Down Expand Up @@ -497,30 +496,30 @@ private void validateTimeReportingBusinessRules(List<Timereport> timereports) {
});
}

private WorkingDayValidationError validateBreakTimes(List<TimereportDTO> timeReports, Workingday workingDay) {
private WorkingDayValidationError validateBreakTimes(LocalDate date, long employeeContractId, List<TimereportDTO> timeReports) {
Workingday workingDay = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date, employeeContractId);
Duration workDurationPerDay = timeReports.stream().map(TimereportDTO::getDuration).reduce(Duration.ZERO, Duration::plus);
boolean notEnoughBreaksAfter9Hours = workingDay != null && workingDay.getBreakLengthInMinutes() < BREAK_MINUTES_AFTER_NINE_HOURS;
boolean notEnoughBreaksAfter6Hours = workingDay != null && workingDay.getBreakLengthInMinutes() < BREAK_MINUTES_AFTER_SIX_HOURS;
if (workDurationPerDay.toMinutes() > NINE_HOURS_IN_MINUTES && notEnoughBreaksAfter9Hours) {
return new WorkingDayValidationError(workingDay.getRefday(), "form.release.error.breaktime.nine.length");
} else if (workDurationPerDay.toMinutes() > SIX_HOURS_IN_MINUTES && notEnoughBreaksAfter6Hours) {
return new WorkingDayValidationError(workingDay.getRefday(), "form.release.error.breaktime.six.length");
boolean notEnoughBreaksAfter9Hours = workingDay == null || workingDay.getBreakLengthInMinutes() < BREAK_MINUTES_AFTER_NINE_HOURS;
boolean notEnoughBreaksAfter6Hours = workingDay == null || workingDay.getBreakLengthInMinutes() < BREAK_MINUTES_AFTER_SIX_HOURS;
if (workDurationPerDay.toMinutes() >= NINE_HOURS_IN_MINUTES && notEnoughBreaksAfter9Hours) {
return new WorkingDayValidationError(date, "form.release.error.breaktime.nine.length");
} else if (workDurationPerDay.toMinutes() >= SIX_HOURS_IN_MINUTES && notEnoughBreaksAfter6Hours) {
return new WorkingDayValidationError(date, "form.release.error.breaktime.six.length");
}
return null;
}

private WorkingDayValidationError validateRestTime(Workingday workingDay,
LocalDate releaseDate,
long employeeContractId) {
Workingday theDayBefore = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(releaseDate.minusDays(1), employeeContractId);
private WorkingDayValidationError validateRestTime(LocalDate date, long employeeContractId) {
Workingday workingDay = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date, employeeContractId);
Workingday theDayBefore = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date.minusDays(1), employeeContractId);
if (theDayBefore == null) {
return null;
}
Duration workDurationPerDay = timereportDAO.getOpenTimereportsByEmployeeContractIdBeforeDate(employeeContractId, releaseDate.minusDays(1)).stream()
Duration workDurationSum = timereportDAO.getTimereportsByDateAndEmployeeContractId(employeeContractId, theDayBefore.getRefday()).stream()
.filter(timeReport -> timeReport.getOrderType().isRelevantForWorkingTimeValidation())
.map(TimereportDTO::getDuration)
.reduce(Duration.ZERO, Duration::plus);
LocalDateTime endOfWorkingDay = theDayBefore.getStartOfWorkingDay().plus(theDayBefore.getBreakLength()).plus(workDurationPerDay);
LocalDateTime endOfWorkingDay = theDayBefore.getStartOfWorkingDay().plus(theDayBefore.getBreakLength()).plus(workDurationSum);
LocalDateTime startOfWorkingDay = workingDay.getStartOfWorkingDay();
Duration restTime = Duration.between(endOfWorkingDay, startOfWorkingDay);
if (restTime.toMinutes() < REST_PERIOD_IN_MINUTES) {
Expand All @@ -529,7 +528,8 @@ private WorkingDayValidationError validateRestTime(Workingday workingDay,
return null;
}

private WorkingDayValidationError validateBeginOfWorkingDay(Workingday workingDay, LocalDate date) {
private WorkingDayValidationError validateBeginOfWorkingDay(LocalDate date, long employeeContractId) {
Workingday workingDay = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date, employeeContractId);
if (workingDay == null || workingDay.getStartOfWorkingDay() == null) {
return new WorkingDayValidationError(date, "form.release.error.beginofworkingday.required");
}
Expand Down

0 comments on commit b765211

Please sign in to comment.