Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/CalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
11 changes: 9 additions & 2 deletions lib/Service/CoverageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand All @@ -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));
}

/**
Expand Down
166 changes: 166 additions & 0 deletions tests/Integration/TeamScopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Absence\Tests\Integration;

use OCA\Absence\ConfigLexicon;
use OCA\Absence\Controller\CalendarController;
use OCA\Absence\Controller\CoverageController;
use OCA\Absence\Db\LeaveRequest;
use OCA\Absence\Db\LeaveRequestMapper;
use OCA\Absence\Db\LeaveType;
use OCA\Absence\Db\LeaveTypeMapper;
use OCA\Absence\Service\ConfigService;
use OCA\Absence\Service\CoverageService;
use OCA\Absence\Service\EmployeeDirectory;
use OCA\Absence\Service\HolidayService;
use OCA\Absence\Service\ManagerResolver;
use OCA\Absence\Service\PermissionService;
use OCP\Config\IUserConfig;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IAppConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Server;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* Who is on a team's timeline and who counts toward its coverage (§8), against
* a real server: an employee also sees their own manager on the timeline, the
* coverage counts stay with the team the manager decides for.
*/
class TeamScopeTest extends TestCase {
private const BOSS = 'itest-team-boss';
private const MANAGER = 'itest-team-manager';
private const ALICE = 'itest-team-alice';
private const BOB = 'itest-team-bob';
private const USERS = [self::BOSS, self::MANAGER, self::ALICE, self::BOB];
private const TYPE_KEY = 'itest-team';

private int $typeId = 0;
private string $visibilityBefore = '';

protected function setUp(): void {
parent::setUp();
$users = Server::get(IUserManager::class);
foreach (self::USERS as $uid) {
$users->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<array<string,mixed>>
*/
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<string,mixed>
*/
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<array<string,mixed>> $events
* @return list<string> 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);
}
}
Loading