Skip to content

Commit

Permalink
Merge pull request #5825 from nextcloud/backport/5820/stable4.6
Browse files Browse the repository at this point in the history
[stable4.6] fix(appointments): Round by increment
  • Loading branch information
miaulalala authored Mar 4, 2024
2 parents b378b21 + 6bb8e2b commit b030459
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/Service/Appointments/AvailabilityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
255 changes: 251 additions & 4 deletions tests/php/unit/Service/Appointments/AvailabilityGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b030459

Please sign in to comment.