Skip to content

Commit

Permalink
fix(userstatus): trigger absence start job even for absences starting…
Browse files Browse the repository at this point in the history
… in the past

Signed-off-by: Anna Larch <[email protected]>
  • Loading branch information
miaulalala committed Aug 12, 2024
1 parent b34edf2 commit 3296290
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
43 changes: 24 additions & 19 deletions apps/dav/lib/Service/AbsenceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,32 @@ public function createOrUpdateAbsence(
}

$now = $this->timeFactory->getTime();
if ($eventData->getStartDate() > $now) {
$this->jobList->scheduleAfter(
OutOfOfficeEventDispatcherJob::class,
$eventData->getStartDate(),
[
'id' => $absence->getId(),
'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
],
);
}
if ($eventData->getEndDate() > $now) {
$this->jobList->scheduleAfter(
OutOfOfficeEventDispatcherJob::class,
$eventData->getEndDate(),
[
'id' => $absence->getId(),
'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
],
);
if($eventData->getEndDate() < $now) {
// absence is in the past
return $absence;
}

// If the absence is for today the timestamp will be smaller than $now
// so run the job now if that is the case
$runJobAt = ($eventData->getStartDate() < $now) ? $now : $eventData->getStartDate();
$this->jobList->scheduleAfter(
OutOfOfficeEventDispatcherJob::class,
$runJobAt,
[
'id' => $absence->getId(),
'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
],
);

$this->jobList->scheduleAfter(
OutOfOfficeEventDispatcherJob::class,
$eventData->getEndDate(),
[
'id' => $absence->getId(),
'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
],
);

return $absence;
}

Expand Down
34 changes: 24 additions & 10 deletions apps/dav/tests/unit/Service/AbsenceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,21 @@ public function testCreateAbsenceSchedulesOnlyEndJob() {
->method('getUserTimezone')
->with('user')
->willReturn($tz->getName());
$time = (new DateTimeImmutable('2023-01-07', $tz))->getTimestamp();
$this->timeFactory->expects(self::once())
->method('getTime')
->willReturn((new DateTimeImmutable('2023-01-07', $tz))->getTimestamp());
$this->jobList->expects(self::once())
->willReturn($time);
$this->jobList->expects(self::exactly(2))
->method('scheduleAfter')
->with(OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [
'id' => '1',
'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
->willReturnMap([
[OutOfOfficeEventDispatcherJob::class, $time, [
'id' => '1',
'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
]],
[OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [
'id' => '1',
'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
]],
]);

$this->absenceService->createOrUpdateAbsence(
Expand Down Expand Up @@ -391,14 +398,21 @@ public function testUpdateSchedulesOnlyEndJob() {
->method('getUserTimezone')
->with('user')
->willReturn($tz->getName());
$time = (new DateTimeImmutable('2023-01-07', $tz))->getTimestamp();
$this->timeFactory->expects(self::once())
->method('getTime')
->willReturn((new DateTimeImmutable('2023-01-07', $tz))->getTimestamp());
$this->jobList->expects(self::once())
->willReturn($time);
$this->jobList->expects(self::exactly(2))
->method('scheduleAfter')
->with(OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 23 * 3600 + 59 * 60, [
'id' => '1',
'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
->willReturnMap([
[OutOfOfficeEventDispatcherJob::class, $time, [
'id' => '1',
'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
]],
[OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [
'id' => '1',
'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
]],
]);

$this->absenceService->createOrUpdateAbsence(
Expand Down

0 comments on commit 3296290

Please sign in to comment.