diff --git a/SPECIFICATION.md b/SPECIFICATION.md index 1ffcd736..3b31450d 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -1051,7 +1051,14 @@ admins' to fill, so they remind nobody. - **Team calendar / who's-off:** managers see their direct reports; HR sees the whole company; every employee sees their own team (peers who share the same - `manager_uid`). A month/timeline view rendered from approved + pending requests. + `manager_uid`) and their own manager, so they see when the manager is away. A + month/timeline view rendered from approved + pending requests. + - The manager's leave follows the same type rules as a colleague's (below). A + manager's own timeline stays their reports and themselves, without their own + manager. + - The manager is only on the timeline. Team coverage (the request dialog's + overlap note and its threshold) counts the team the manager decides for, the + same people the manager's review counts, so the manager is not part of it. - **Each person's public holidays are shaded on their row** (§7.2), from the region code they set, so a team spread over several regions reads correctly. Whoever sees a person on the timeline sees these days, and so can tell where that @@ -1710,7 +1717,8 @@ NcContent(app-name="absence") while pending), weekend shading, each person's public holidays, a "today" line, month navigation + a "Today" jump, and a legend. Every day of the month fits the width; when a day column would get narrower than 24 px the day area scrolls sideways and the name rail stays in place. - `scope="team"` for managers, `scope="company"` for HR. With + `scope="team"` for managers and employees (an employee's team includes their own + manager, §8), `scope="company"` for HR. With `selectable` the pills become buttons that open the request in the sidebar; only *Who's off* sets it, because HR may read every request the org-wide calendar shows while a team timeline can include leave the viewer is not allowed to open. diff --git a/lib/Controller/CalendarController.php b/lib/Controller/CalendarController.php index 2baeaf5f..86ccac59 100644 --- a/lib/Controller/CalendarController.php +++ b/lib/Controller/CalendarController.php @@ -40,7 +40,7 @@ public function __construct( #[UserRateLimit(limit: 60, period: 60)] public function index(string $from, string $to, string $scope = 'team'): DataResponse { return $this->handle(function () use ($from, $to, $scope) { - $uids = $this->service->resolveScopeUids((string)$this->userId, $scope); + $uids = $this->service->resolveScopeUids((string)$this->userId, $scope, withOwnManager: true); $coverage = $this->service->getCoverage($uids, $from, $to, null, (string)$this->userId); // The people on show are the ones with an absence: their own public // holidays are shaded on their row (§15.2). diff --git a/lib/Service/CoverageService.php b/lib/Service/CoverageService.php index f8dfdf97..a66927e9 100644 --- a/lib/Service/CoverageService.php +++ b/lib/Service/CoverageService.php @@ -46,9 +46,11 @@ public function __construct( * Resolve the set of employees an actor may see for a given scope. Nobody for * an account that is neither an employee nor HR (§2.2). * + * @param bool $withOwnManager add the manager of somebody who manages nobody to + * their team scope; they are shown, not counted (§8) * @return string[] */ - public function resolveScopeUids(string $actorUid, string $scope): array { + public function resolveScopeUids(string $actorUid, string $scope, bool $withOwnManager = false): array { if (!$this->permission->isEmployeeOrHr($actorUid)) { return []; } @@ -60,7 +62,12 @@ public function resolveScopeUids(string $actorUid, string $scope): array { if ($reports !== []) { return array_values(array_unique([...$reports, $actorUid])); } - return array_values(array_unique([...$this->managerResolver->getPeers($actorUid), $actorUid])); + $team = [...$this->managerResolver->getPeers($actorUid), $actorUid]; + $managerUid = $withOwnManager ? $this->managerResolver->getManagerUid($actorUid) : null; + if ($managerUid !== null) { + $team[] = $managerUid; + } + return array_values(array_unique($team)); } /** diff --git a/tests/Integration/TeamScopeTest.php b/tests/Integration/TeamScopeTest.php new file mode 100644 index 00000000..0a071edf --- /dev/null +++ b/tests/Integration/TeamScopeTest.php @@ -0,0 +1,166 @@ +createUser($uid, 'Itest-Password-team-' . $uid); + } + $users->get(self::MANAGER)->setManagerUids([self::BOSS]); + $users->get(self::ALICE)->setManagerUids([self::MANAGER]); + $users->get(self::BOB)->setManagerUids([self::MANAGER]); + + $this->visibilityBefore = Server::get(ConfigService::class)->getSharedCalendarVisibility(); + Server::get(IAppConfig::class)->setValueString(ConfigService::APP_ID, ConfigLexicon::KEY_SHARED_VISIBILITY, ConfigService::VISIBILITY_NEUTRAL); + + $type = new LeaveType(); + $type->setKey(self::TYPE_KEY); + $type->setLabel('itest team'); + $type->setCountsAgainstBalance(false); + $this->typeId = Server::get(LeaveTypeMapper::class)->insert($type)->getId(); + + $this->seed(self::BOSS, null); + $this->seed(self::MANAGER, self::BOSS); + $this->seed(self::BOB, self::MANAGER); + } + + protected function tearDown(): void { + $db = Server::get(IDBConnection::class); + $qb = $db->getQueryBuilder(); + $qb->delete('absence_requests') + ->where($qb->expr()->in('employee_uid', $qb->createNamedParameter(self::USERS, IQueryBuilder::PARAM_STR_ARRAY))) + ->executeStatement(); + $qb = $db->getQueryBuilder(); + $qb->delete('absence_leave_types')->where($qb->expr()->eq('key', $qb->createNamedParameter(self::TYPE_KEY)))->executeStatement(); + foreach (self::USERS as $uid) { + Server::get(IUserManager::class)->get($uid)?->delete(); + } + Server::get(IAppConfig::class)->setValueString(ConfigService::APP_ID, ConfigLexicon::KEY_SHARED_VISIBILITY, $this->visibilityBefore); + parent::tearDown(); + } + + public function testAnEmployeeSeesTheirManagerOnTheTeamTimeline(): void { + $events = $this->timelineOf(self::ALICE); + + self::assertSame([self::BOB, self::MANAGER], $this->people($events)); + $managerEvent = array_values(array_filter($events, static fn (array $event): bool => $event['employeeUid'] === self::MANAGER))[0]; + self::assertNull($managerEvent['typeId'], 'the manager\'s leave type follows the neutral policy'); + } + + public function testAManagerStillSeesTheirReportsAndNotTheirOwnManager(): void { + self::assertSame([self::BOB, self::MANAGER], $this->people($this->timelineOf(self::MANAGER))); + } + + public function testTheManagerDoesNotCountTowardTheTeamCoverage(): void { + $coverage = $this->coverageOf(self::ALICE); + + self::assertSame([self::BOB], $this->people($coverage['events'])); + self::assertSame(1, $coverage['maxConcurrent']); + } + + private function seed(string $employeeUid, ?string $managerUid): void { + $request = new LeaveRequest(); + $request->setEmployeeUid($employeeUid); + $request->setManagerUid($managerUid); + $request->setTypeId($this->typeId); + $request->setStartDate('2027-06-07'); + $request->setEndDate('2027-06-11'); + $request->setWorkingDays(5.0); + $request->setStatus(LeaveRequest::STATUS_APPROVED); + $request->setCreatedAt(new \DateTime('-1 day')); + $request->setUpdatedAt(new \DateTime('-1 day')); + Server::get(LeaveRequestMapper::class)->insert($request); + } + + /** + * @return list> + */ + private function timelineOf(string $uid): array { + $controller = new CalendarController('absence', Server::get(IRequest::class), $uid, $this->coverage(), + Server::get(HolidayService::class), Server::get(IFactory::class)->get('absence'), Server::get(LoggerInterface::class)); + return $controller->index('2027-06-01', '2027-06-30')->getData()['events']; + } + + /** + * @return array + */ + private function coverageOf(string $uid): array { + $controller = new CoverageController('absence', Server::get(IRequest::class), $uid, $this->coverage(), + Server::get(IFactory::class)->get('absence'), Server::get(LoggerInterface::class)); + return $controller->index('2027-06-01', '2027-06-30')->getData(); + } + + /** + * @param list> $events + * @return list the people with an event, sorted + */ + private function people(array $events): array { + $uids = array_values(array_unique(array_column($events, 'employeeUid'))); + sort($uids); + return $uids; + } + + /** + * A fresh service: the resolver and the directory remember what they read + * for the length of a web request. + */ + private function coverage(): CoverageService { + $directory = new EmployeeDirectory(Server::get(IUserManager::class), Server::get(IGroupManager::class), Server::get(ConfigService::class), Server::get(LoggerInterface::class)); + $resolver = new ManagerResolver(Server::get(IUserManager::class), Server::get(IUserConfig::class), $directory, Server::get(LoggerInterface::class)); + $l = Server::get(IFactory::class)->get('absence'); + $permission = new PermissionService(Server::get(IGroupManager::class), $resolver, Server::get(ConfigService::class), + Server::get(LeaveTypeMapper::class), $l, Server::get(LoggerInterface::class), $directory); + return new CoverageService(Server::get(LeaveRequestMapper::class), Server::get(LeaveTypeMapper::class), $resolver, $permission, + Server::get(ConfigService::class), $directory, Server::get(IUserManager::class), $l); + } +}