diff --git a/src/main/java/io/redlink/more/data/repository/StudyRepository.java b/src/main/java/io/redlink/more/data/repository/StudyRepository.java index 7e5c415..dc085c6 100644 --- a/src/main/java/io/redlink/more/data/repository/StudyRepository.java +++ b/src/main/java/io/redlink/more/data/repository/StudyRepository.java @@ -260,7 +260,7 @@ private void updateParticipantStatus(long studyId, int groupId, int participantI if ("active".equals(newStatus)) { start = Timestamp.from( - SchedulerUtils.shiftStartIfObservationAlreadyStarted(Instant.now(), listObservations(studyId, groupId, participantId, true)) + SchedulerUtils.shiftStartIfObservationAlreadyEnded(Instant.now(), listObservations(studyId, groupId, participantId, true)) ); } diff --git a/src/main/java/io/redlink/more/data/schedule/SchedulerUtils.java b/src/main/java/io/redlink/more/data/schedule/SchedulerUtils.java index f040233..dbdad23 100644 --- a/src/main/java/io/redlink/more/data/schedule/SchedulerUtils.java +++ b/src/main/java/io/redlink/more/data/schedule/SchedulerUtils.java @@ -35,8 +35,6 @@ public static List> parseToObservationSchedulesForRelativ List> events = new ArrayList<>(); - start = shiftStartIfNecessary(start); - Pair currentEvt = Pair.of(toInstant(event.getDtstart(), start), toInstant(event.getDtend(), start)); if(event.getRrrule() != null) { @@ -57,11 +55,6 @@ public static List> parseToObservationSchedulesForRelativ return events; } - private static Instant shiftStartIfNecessary(Instant start) { - // TODO - return start; - } - private static Instant toInstant(RelativeDate date, Instant start) { return ZonedDateTime.ofInstant(start.plus(date.getOffset().getValue() - 1L, date.getOffset().getUnit().toTemporalUnit()), date.getZoneId()) .withHour(date.getHours()) @@ -98,7 +91,7 @@ public static List> parseToObservationSchedules(ScheduleE } } - public static Instant shiftStartIfObservationAlreadyStarted(Instant start, List observations) { + public static Instant shiftStartIfObservationAlreadyEnded(Instant start, List observations) { // returns start date, if now event ends before, otherwise start date + 1 day return observations.stream() .map(Observation::observationSchedule) @@ -107,7 +100,7 @@ public static Instant shiftStartIfObservationAlreadyStarted(Instant start, List< .filter(relativeDate -> relativeDate.getOffset().getValue() == 1) .map(relativeDate -> start.atZone(relativeDate.getZoneId()).withHour(relativeDate.getHours()).withMinute(relativeDate.getMinutes()).withSecond(0).withNano(0).toInstant()) .filter(instant -> { - return instant.isBefore(start.plus(1, ChronoUnit.HOURS)); + return instant.isBefore(start); }) .map(instant -> start.atZone(ZoneId.systemDefault()).withHour(0).withMinute(0).plusDays(1).toInstant()) .findFirst() diff --git a/src/main/java/io/redlink/more/data/service/CalendarService.java b/src/main/java/io/redlink/more/data/service/CalendarService.java index 27741d1..3409014 100644 --- a/src/main/java/io/redlink/more/data/service/CalendarService.java +++ b/src/main/java/io/redlink/more/data/service/CalendarService.java @@ -17,7 +17,7 @@ import java.util.Optional; import java.util.TimeZone; -import static io.redlink.more.data.schedule.SchedulerUtils.shiftStartIfObservationAlreadyStarted; +import static io.redlink.more.data.schedule.SchedulerUtils.shiftStartIfObservationAlreadyEnded; @Service public class CalendarService { @@ -39,7 +39,7 @@ public Optional getICalendarString(Long studyId) { iCalEvent.setDateEnd(Date.from(study.endDate().atStartOfDay(TimeZone.getDefault().toZoneId()).toInstant()), false); ical.addEvent(iCalEvent); - final Instant start = shiftStartIfObservationAlreadyStarted( + final Instant start = shiftStartIfObservationAlreadyEnded( study.plannedStartDate().atStartOfDay(ZoneId.systemDefault()).toInstant(), study.observations() ); diff --git a/src/test/java/io/redlink/more/data/schedule/SchedulerUtilsTest.java b/src/test/java/io/redlink/more/data/schedule/SchedulerUtilsTest.java index 9103e86..b740f87 100644 --- a/src/test/java/io/redlink/more/data/schedule/SchedulerUtilsTest.java +++ b/src/test/java/io/redlink/more/data/schedule/SchedulerUtilsTest.java @@ -25,7 +25,7 @@ import java.util.Arrays; import java.util.List; -import static io.redlink.more.data.schedule.SchedulerUtils.shiftStartIfObservationAlreadyStarted; +import static io.redlink.more.data.schedule.SchedulerUtils.shiftStartIfObservationAlreadyEnded; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -392,13 +392,13 @@ void testRelativeEventShift() { when(observationDay1At13.observationSchedule()).thenReturn(day1At13); when(observationDay2At10.observationSchedule()).thenReturn(day2At10); - Instant s1 = shiftStartIfObservationAlreadyStarted(start, List.of(observationDay1At10, observationDay1At12, observationDay2At10)); + Instant s1 = shiftStartIfObservationAlreadyEnded(start, List.of(observationDay1At10, observationDay1At12, observationDay2At10)); Assertions.assertNotEquals(s1.toEpochMilli(), start.toEpochMilli()); - Instant s2 = shiftStartIfObservationAlreadyStarted(start, List.of(observationDay1At12, observationDay2At10)); - Assertions.assertNotEquals(s2.toEpochMilli(), start.toEpochMilli()); + Instant s2 = shiftStartIfObservationAlreadyEnded(start, List.of(observationDay1At12, observationDay2At10)); + Assertions.assertEquals(s2.toEpochMilli(), start.toEpochMilli()); - Instant s3 = shiftStartIfObservationAlreadyStarted(start, List.of(observationDay1At13, observationDay2At10)); + Instant s3 = shiftStartIfObservationAlreadyEnded(start, List.of(observationDay1At13, observationDay2At10)); Assertions.assertEquals(s3.toEpochMilli(), start.toEpochMilli()); }