From 6bb8e2bfc07ab56b0b3661c36ee472e1d23a3069 Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Thu, 29 Feb 2024 17:03:40 +0100 Subject: [PATCH] fix(appointments): Decide rounding by increment OR length Signed-off-by: Anna Larch --- .../Appointments/AvailabilityGenerator.php | 9 +- .../AvailabilityGeneratorTest.php | 255 +++++++++++++++++- 2 files changed, 256 insertions(+), 8 deletions(-) diff --git a/lib/Service/Appointments/AvailabilityGenerator.php b/lib/Service/Appointments/AvailabilityGenerator.php index 9adabeac3..61d4d3994 100644 --- a/lib/Service/Appointments/AvailabilityGenerator.php +++ b/lib/Service/Appointments/AvailabilityGenerator.php @@ -63,12 +63,13 @@ public function generate(AppointmentConfig $config, $now + $bufferBeforeStart, ($config->getStart() ?? $now) + $bufferBeforeStart ); - // Always round to "beautiful" slot starts according to slot length + + // Always round to "beautiful" slot starts // E.g. 5m slots should only be available at 10:20 and 10:25, not at 10:17 // when the user opens the page at 10:17. // But only do this when the time isn't already a "pretty" time - if ($earliestStart % $config->getLength() !== 0) { - $roundTo = (int)round(($config->getLength()) / 300) * 300; + if ($earliestStart % $config->getIncrement() !== 0) { + $roundTo = (int)round(($config->getIncrement()) / 300) * 300; $earliestStart = (int)ceil($earliestStart / $roundTo) * $roundTo; } @@ -94,7 +95,7 @@ public function generate(AppointmentConfig $config, $timeZone = $availabilityRule['timezoneId']; $slots = $availabilityRule['slots']; - $applicableSlots = $this->filterDates($start, $slots, $timeZone, $config->getLength()); + $applicableSlots = $this->filterDates($start, $slots, $timeZone, $config->getIncrement()); $intervals = []; foreach ($applicableSlots as $slot) { if ($slot->getEnd() <= $earliestStart || $slot->getStart() >= $latestEnd) { diff --git a/tests/php/unit/Service/Appointments/AvailabilityGeneratorTest.php b/tests/php/unit/Service/Appointments/AvailabilityGeneratorTest.php index 6b846ab31..044682c8d 100644 --- a/tests/php/unit/Service/Appointments/AvailabilityGeneratorTest.php +++ b/tests/php/unit/Service/Appointments/AvailabilityGeneratorTest.php @@ -80,7 +80,7 @@ public function testNoAvailabilitySetRoundToFive(): void { self::assertCount(1, $slots); self::assertEquals(0, $slots[0]->getStart() % 900); - self::assertEquals(5400, $slots[0]->getStart()); + self::assertEquals(4500, $slots[0]->getStart()); } public function testNoAvailabilitySetRoundWithSpecificTimes(): void { @@ -93,9 +93,69 @@ public function testNoAvailabilitySetRoundWithSpecificTimes(): void { self::assertCount(1, $slots); self::assertEquals(0, $slots[0]->getStart() % 900); - self::assertEquals(2700, ($slots[0]->getEnd() - $slots[0]->getStart())); + self::assertEquals(3600, ($slots[0]->getEnd() - $slots[0]->getStart())); self::assertEquals( - [new Interval(1637838000, 1637840700)], + [new Interval(1637837100, 1637840700)], + $slots, + ); + } + + public function testNoAvailabilitySetRoundWithRealLifeTimes(): void { + $config = new AppointmentConfig(); + $config->setLength(900); + $config->setIncrement(3600); + $config->setAvailability(null); + + $sdate = new \DateTime('2024-03-04 10:00'); + $edate = new \DateTime('2024-03-04 10:15'); + + $slots = $this->generator->generate($config, $sdate->getTimestamp(), $edate->getTimestamp()); + + self::assertCount(1, $slots); + self::assertEquals(0, $slots[0]->getStart() % 3600); + self::assertEquals(900, ($slots[0]->getEnd() - $slots[0]->getStart())); + self::assertEquals( + [new Interval(1709546400, 1709547300)], + $slots, + ); + } + + public function testNoAvailabilitySetRoundWithRealLifeTimesUgly(): void { + $config = new AppointmentConfig(); + $config->setLength(900); + $config->setIncrement(3600); + $config->setAvailability(null); + + $sdate = new \DateTime('2024-03-04 09:50'); + $edate = new \DateTime('2024-03-04 10:15'); + + $slots = $this->generator->generate($config, $sdate->getTimestamp(), $edate->getTimestamp()); + + self::assertCount(1, $slots); + self::assertEquals(0, $slots[0]->getStart() % 3600); + self::assertEquals(900, ($slots[0]->getEnd() - $slots[0]->getStart())); + self::assertEquals( + [new Interval(1709546400, 1709547300)], + $slots, + ); + } + + public function testNoAvailabilitySetRoundWithRealLifeTimesUglyTwo(): void { + $config = new AppointmentConfig(); + $config->setLength(900); + $config->setIncrement(3600); + $config->setAvailability(null); + + $sdate = new \DateTime('2024-03-04 09:01'); + $edate = new \DateTime('2024-03-04 10:15'); + + $slots = $this->generator->generate($config, $sdate->getTimestamp(), $edate->getTimestamp()); + + self::assertCount(1, $slots); + self::assertEquals(0, $slots[0]->getStart() % 3600); + self::assertEquals(900, ($slots[0]->getEnd() - $slots[0]->getStart())); + self::assertEquals( + [new Interval(1709546400, 1709547300)], $slots, ); } @@ -113,6 +173,19 @@ public function testNoAvailabilitySetRoundWithIncrementForHalfHour(): void { self::assertEquals(3600, $slots[0]->getStart()); } + public function testNoAvailabilitySetRoundWithIncrementForTwoHours(): void { + $config = new AppointmentConfig(); + $config->setLength(3600); + $config->setIncrement(7200); + $config->setAvailability(null); + + $slots = $this->generator->generate($config, 1 * 1800, 3 * 3600); + + self::assertCount(1, $slots); + self::assertEquals(0, $slots[0]->getStart() % 3600); + self::assertEquals(7200, $slots[0]->getStart()); + } + public function testNoAvailabilitySetRoundWithIncrementForFullHour(): void { $config = new AppointmentConfig(); $config->setLength(3600); @@ -162,7 +235,7 @@ public function testNoAvailabilitySetRoundWithFourtyMinutesNotPretty(): void { self::assertCount(1, $slots); self::assertEquals(0, $slots[0]->getStart() % 300); - self::assertEquals(4800, $slots[0]->getStart()); + self::assertEquals(2700, $slots[0]->getStart()); } public function testNoAvailabilityButEndDate(): void { @@ -253,6 +326,180 @@ public function testSimpleRule(): void { self::assertCount(1, $slots); } + public function testSimpleRuleUgly(): void { + $dateTime = new DateTimeImmutable(); + $tz = new DateTimeZone('Europe/Vienna'); + $startTimestamp = $dateTime + ->setTimezone($tz) + ->setDate(2021, 11, 22) + ->setTime(8, 10)->getTimestamp(); + $endTimestamp = $dateTime + ->setTimezone($tz) + ->setTime(17, 0)->getTimestamp(); + $config = new AppointmentConfig(); + $config->setLength(900); + $config->setIncrement(3600); + $config->setAvailability(json_encode([ + 'timezoneId' => $tz->getName(), + 'slots' => [ + 'MO' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'TU' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'WE' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'TH' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'FR' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'SA' => [], + 'SU' => [] + ] + ], JSON_THROW_ON_ERROR)); + $start = (new DateTimeImmutable())->setDate(2021, 11, 3)->setTime(9, 0); + $end = $start->modify('+15 minutes'); + + $slots = $this->generator->generate($config, $start->getTimestamp(), $end->getTimestamp()); + + self::assertCount(1, $slots); + } + + public function testSimpleRuleUglyTwo(): void { + $dateTime = new DateTimeImmutable(); + $tz = new DateTimeZone('Europe/Vienna'); + $startTimestamp = $dateTime + ->setTimezone($tz) + ->setDate(2021, 11, 22) + ->setTime(8, 10)->getTimestamp(); + $endTimestamp = $dateTime + ->setTimezone($tz) + ->setTime(17, 0)->getTimestamp(); + $config = new AppointmentConfig(); + $config->setLength(3600); + $config->setIncrement(900); + $config->setAvailability(json_encode([ + 'timezoneId' => $tz->getName(), + 'slots' => [ + 'MO' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'TU' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'WE' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'TH' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'FR' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'SA' => [], + 'SU' => [] + ] + ], JSON_THROW_ON_ERROR)); + $start = (new DateTimeImmutable())->setDate(2021, 11, 3)->setTime(9, 15); + $end = $start->modify('+1 hour'); + + $slots = $this->generator->generate($config, $start->getTimestamp(), $end->getTimestamp()); + + self::assertCount(1, $slots); + } + + public function testSimpleRuleUglyEqual(): void { + $dateTime = new DateTimeImmutable(); + $tz = new DateTimeZone('Europe/Vienna'); + $startTimestamp = $dateTime + ->setTimezone($tz) + ->setDate(2021, 11, 22) + ->setTime(8, 10)->getTimestamp(); + $endTimestamp = $dateTime + ->setTimezone($tz) + ->setTime(17, 0)->getTimestamp(); + $config = new AppointmentConfig(); + $config->setLength(900); + $config->setIncrement(900); + $config->setAvailability(json_encode([ + 'timezoneId' => $tz->getName(), + 'slots' => [ + 'MO' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'TU' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'WE' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'TH' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'FR' => [ + [ + 'start' => $startTimestamp, + 'end' => $endTimestamp, + ] + ], + 'SA' => [], + 'SU' => [] + ] + ], JSON_THROW_ON_ERROR)); + $start = (new DateTimeImmutable())->setDate(2021, 11, 3)->setTime(9, 15); + $end = $start->modify('+15 minutes'); + + $slots = $this->generator->generate($config, $start->getTimestamp(), $end->getTimestamp()); + + self::assertCount(1, $slots); + } + public function testViennaComplexRule(): void { $tz = new DateTimeZone('Europe/Vienna'); $dateTime = (new DateTimeImmutable())->setTimezone($tz)->setDate(2021, 11, 22);