diff --git a/SPECIFICATION.md b/SPECIFICATION.md index fe51f1d..78224bd 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -686,6 +686,12 @@ employees — sick leave is the canonical example (`employee_requestable = false change its type to one that employees request: the edit keeps the record `APPROVED`, and nobody decides their own leave (§5.5). The API answers 403, and somebody else from HR can make that change. +- **Leave that is already over.** Only HR may enter leave that ended before today + (an employee is told they cannot request leave entirely in the past). It is a + correction of the record, not a request: it is booked `APPROVED` at once, of any + type and for anybody, HR's own included, needs no replacement, and sends no + notification or email to anyone (no HR team, no manager, no replacement). It + shows in the activity stream and the request history like any record. - **Correcting a record.** HR reaches individual records through the **Absences** view (§15.2) — or by selecting one in *Who's off* / the *Sick leave* drilldown — and edits or cancels it from the standard detail sidebar. There is **no delete**: a wrong diff --git a/lib/Service/RequestService.php b/lib/Service/RequestService.php index a492df0..87010b8 100644 --- a/lib/Service/RequestService.php +++ b/lib/Service/RequestService.php @@ -670,8 +670,13 @@ public function create(string $actorUid, #[\SensitiveParameter] array $data): Le $start = $this->normaliseDate((string)($data['startDate'] ?? '')); $end = $this->normaliseDate((string)($data['endDate'] ?? '')); $this->validateRange($actorUid, $start, $end, $type, (string)($data['reason'] ?? '')); - // Not demanded when HR is recording for somebody else — see resolveReplacement(). - $replacementUid = $this->resolveReplacement($employeeUid, $type, $data['replacementUid'] ?? null, required: !$onBehalf); + // HR adding leave that is already over corrects the record (§5.6): only HR + // may enter it at all (validateRange()), it is booked straight through, and + // nobody is asked or pinged about days that are gone, HR's own included. + $correction = $isHr && $end < $this->clock->userToday(); + // Not demanded when HR is recording for somebody else, nor for a correction — + // see resolveReplacement(). + $replacementUid = $this->resolveReplacement($employeeUid, $type, $data['replacementUid'] ?? null, required: !$onBehalf && !$correction); // The employee enters the number of working days; the manager verifies it (§7). $workingDays = $this->normaliseWorkingDays($data['workingDays'] ?? null); @@ -686,7 +691,7 @@ public function create(string $actorUid, #[\SensitiveParameter] array $data): Le // The overlap check only means anything if nothing else can insert for this // employee between it and the insert below. $request = $this->withEmployeeLock($employeeUid, fn (): LeaveRequest => $this->atomic(function () use ( - $actorUid, $employeeUid, $onBehalf, $type, $start, $end, $workingDays, $replacementUid, $managerUid, $now, $data, $disability, + $actorUid, $employeeUid, $onBehalf, $correction, $type, $start, $end, $workingDays, $replacementUid, $managerUid, $now, $data, $disability, ): LeaveRequest { $this->assertNoOverlap($employeeUid, $start, $end); @@ -703,9 +708,10 @@ public function create(string $actorUid, #[\SensitiveParameter] array $data): Le $request->setCreatedAt($now); $request->setUpdatedAt($now); - // Non-requestable types, auto-approve types, and any HR-recorded leave are - // booked straight to APPROVED with no approval workflow (§4.1, §5.6). - $recordedDirectly = !$type->getEmployeeRequestable() || !$type->getRequiresApproval() || $onBehalf; + // Non-requestable types, auto-approve types, any HR-recorded leave and HR's + // corrections of the past are booked straight to APPROVED with no approval + // workflow (§4.1, §5.6). + $recordedDirectly = !$type->getEmployeeRequestable() || !$type->getRequiresApproval() || $onBehalf || $correction; if ($recordedDirectly) { $request->setStatus(LeaveRequest::STATUS_APPROVED); $request->setDecidedBy($actorUid); @@ -730,10 +736,14 @@ public function create(string $actorUid, #[\SensitiveParameter] array $data): Le // Side effects by resulting status. After the commit — see the class docblock. if ($request->getStatus() === LeaveRequest::STATUS_APPROVED) { - $this->notifications->notifyReplacementAssigned($request); + // A correction of the past pings nobody: there is no replacement to brief + // and no manager who could still act on it + if (!$correction) { + $this->notifications->notifyReplacementAssigned($request); + } // HR recorded it for somebody else (§5.6): their line manager was never // asked, so tell them. Not when the manager is the one who recorded it. - if ($onBehalf && $managerUid !== null && $managerUid !== $actorUid) { + if ($onBehalf && !$correction && $managerUid !== null && $managerUid !== $actorUid) { $this->notifications->notifyAbsenceRecorded($request, $managerUid); } $this->activity->publish(ActivityPublisher::SUBJECT_CREATED, $this->activityParams($request), [$employeeUid, $actorUid], $request); diff --git a/tests/Integration/HrOwnLeaveTest.php b/tests/Integration/HrOwnLeaveTest.php index ff05033..c14b987 100644 --- a/tests/Integration/HrOwnLeaveTest.php +++ b/tests/Integration/HrOwnLeaveTest.php @@ -18,9 +18,11 @@ use OCA\Absence\Exception\ForbiddenException; use OCA\Absence\Service\ConfigService; use OCA\Absence\Service\RequestService; +use OCA\Absence\Tests\Integration\Fixtures\RecordingNotifications; use OCP\IAppConfig; use OCP\IGroupManager; use OCP\IUserManager; +use OCP\Notification\INotification; use OCP\Server; use PHPUnit\Framework\TestCase; @@ -118,6 +120,38 @@ public function testHrChangesTheTypeOfSomebodyElsesRecord(): void { self::assertSame(LeaveRequest::STATUS_APPROVED, $changed->getStatus()); } + public function testHrEnteringTheirOwnPastLeaveCorrectsTheRecordAndPingsNobody(): void { + RecordingNotifications::register(); + $past = $this->service->create(self::HR, [ + 'typeId' => $this->requested->getId(), + 'startDate' => '2026-01-05', + 'endDate' => '2026-01-09', + 'workingDays' => 5, + ]); + + self::assertSame(LeaveRequest::STATUS_APPROVED, $past->getStatus()); + self::assertSame(self::HR, $past->getDecidedBy()); + self::assertSame(0, $this->notificationsFor(self::OTHER_HR)); + } + + public function testHrStillAsksAnotherHrMemberForTheirOwnLeaveAhead(): void { + RecordingNotifications::register(); + $ahead = $this->service->create(self::HR, [ + 'typeId' => $this->requested->getId(), + 'startDate' => '2027-06-07', + 'endDate' => '2027-06-07', + 'workingDays' => 1, + 'replacementUid' => self::EMPLOYEE, + ]); + + self::assertNotSame(LeaveRequest::STATUS_APPROVED, $ahead->getStatus()); + self::assertGreaterThan(0, $this->notificationsFor(self::OTHER_HR)); + } + + private function notificationsFor(string $uid): int { + return count(array_filter(RecordingNotifications::$sent, static fn (INotification $n): bool => $n->getUser() === $uid)); + } + /** An absence of the recorded type that the first HR member books for $employeeUid. */ private function record(string $employeeUid): LeaveRequest { return $this->service->create(self::HR, [ diff --git a/tests/Integration/LeaveImportTest.php b/tests/Integration/LeaveImportTest.php index 9d2faf1..945f721 100644 --- a/tests/Integration/LeaveImportTest.php +++ b/tests/Integration/LeaveImportTest.php @@ -408,9 +408,11 @@ public function testImportHasNoSideEffects(): void { self::assertSame([], $this->changeEvents, 'no change event'); // The spies do see a normal HR booking, so the silence above is the import's. + // One ahead: HR entering leave that is already over pings nobody either. $sickId = self::typeId('sick'); + $ahead = (new \DateTimeImmutable('+30 days'))->format('Y-m-d'); Server::get(RequestService::class)->create(self::HR, [ - 'typeId' => $sickId, 'employeeUid' => self::SAM, 'startDate' => '2025-09-01', 'endDate' => '2025-09-01', + 'typeId' => $sickId, 'employeeUid' => self::SAM, 'startDate' => $ahead, 'endDate' => $ahead, 'workingDays' => 1, 'replacementUid' => self::ROBIN, ]); self::assertNotSame([], SpyNotificationApp::$received);