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
14 changes: 8 additions & 6 deletions SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1326,12 +1326,14 @@ All four channels are required.
- **Carry-over about to expire** (→ the employee, §6.2): how many carried-over
days expire and on which date, as a notification and an email that link to My
leave. It names no leave type and carries no buttons.
- **Manager changed** (→ the employee and HR, never the person who made the change,
§2.1): only for a change a signed-in user made. A notification without email and
without buttons: "Your manager was changed" for the employee, "The manager of
{name} was changed" for HR, and "{actor} changed it from {before} to {after}." It
stores uids only, and the names are read when it is shown. It links to the
employee's profile, where the change is in the history.
- **Manager changed** (→ the employee, their new managers and their former ones,
never the person who made the change, §2.1; not HR, who find it in the person's
history): only for a change a signed-in user made. A notification without email
and without buttons: "Your manager was changed" for the employee, "{name} now
reports to you" for a new manager, "{name} no longer reports to you" for a former
one, and "{actor} changed it from {before} to {after}." It stores uids only, and
the names are read when it is shown. It links to the employee's profile, where the
change is in the history, and a former manager to their team.
- **Actionable where the recipient owes an answer.** The four notifications that ask
for a decision — new request, escalation, reminder, withdrawal — carry three
buttons: **Approve**, **Decline** and **Review**.
Expand Down
26 changes: 19 additions & 7 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,26 +219,38 @@ private function addDecisionActions(INotification $notification, IL10N $l, strin
}

/**
* The employee reads that their manager changed, HR whose manager changed;
* both read who changed it, from whom to whom.
* The employee reads that their manager changed, the new manager that the
* employee now reports to them, the former one that they no longer do (and
* lands on their team, as the profile may be closed to them by now). Older
* notifications HR got read whose manager changed. All read who changed it,
* from whom to whom.
*/
private function prepareManagerChange(INotification $notification, IL10N $l): INotification {
$params = $notification->getSubjectParameters();
$employeeUid = (string)($params['employee'] ?? '');
if ($employeeUid === '') {
throw new UnknownNotificationException('Incomplete manager change');
}
$own = $notification->getUser() === $employeeUid;
return $notification->setParsedSubject($own
? $l->t('Your manager was changed')
: $l->t('The manager of %s was changed', [$this->displayName($l, $employeeUid)]))
$user = $notification->getUser();
$own = $user === $employeeUid;
$name = $this->displayName($l, $employeeUid);
$isNew = in_array($user, (array)($params['after'] ?? []), true);
$isFormer = !$isNew && in_array($user, (array)($params['before'] ?? []), true);
$subject = match (true) {
$own => $l->t('Your manager was changed'),
$isNew => $l->t('%s now reports to you', [$name]),
$isFormer => $l->t('%s no longer reports to you', [$name]),
default => $l->t('The manager of %s was changed', [$name]),
};
$link = $isFormer ? '#/team' : '#/profile' . ($own ? '' : '/' . rawurlencode($employeeUid));
return $notification->setParsedSubject($subject)
->setParsedMessage($l->t('%1$s changed it from %2$s to %3$s.', [
$this->displayName($l, (string)($params['actor'] ?? '')),
$this->names($l, $params['before'] ?? []),
$this->names($l, $params['after'] ?? []),
]))
->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath(ConfigService::APP_ID, 'app-dark.svg')))
->setLink($this->urlGenerator->linkToRouteAbsolute('absence.page.index') . '#/profile' . ($own ? '' : '/' . rawurlencode($employeeUid)));
->setLink($this->urlGenerator->linkToRouteAbsolute('absence.page.index') . $link);
}

private function names(IL10N $l, mixed $uids): string {
Expand Down
8 changes: 4 additions & 4 deletions lib/Service/EmployeeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function __construct(
private IEventDispatcher $dispatcher,
private NotificationService $notifications,
private HolidayService $holidays,
private PermissionService $permission,
private IL10N $l,
) {
}
Expand Down Expand Up @@ -225,8 +224,9 @@ public function recordRegionChange(string $uid, string $before, string $after, ?
/**
* Keeps a change of someone's managers (§2.1) in their history and the admin
* audit log. A change a signed-in person made is also told to the employee
* and HR; one without, from occ, a background job or an account sync, is
* only kept, so that a first sync does not notify HR of every employee.
* and their former and new managers; one without, from occ, a background job
* or an account sync, is only kept, so that a first sync does not notify
* every manager of every employee.
*
* @param array<array-key, mixed> $before the configured manager uids
* @param array<array-key, mixed> $after
Expand All @@ -242,7 +242,7 @@ public function recordManagerChange(string $uid, array $before, array $after, ?s
'Manager of %s changed from "%s" to "%s" by %s', [$uid, implode(',', $before), implode(',', $after), $actor ?? 'system'],
));
if ($actor !== null) {
$this->notifications->notifyManagerChanged($uid, $before, $after, $actor, $this->permission->getHrUids());
$this->notifications->notifyManagerChanged($uid, $before, $after, $actor);
}
}

Expand Down
13 changes: 7 additions & 6 deletions lib/Service/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,17 @@ public function notifyCarryOverExpiring(string $uid, float $days, string $expiry
}

/**
* Tells an employee and HR that the employee's manager changed (§2.1), and
* who changed it. The notification carries uids only; the names are read
* when it is shown. Nobody is told about their own change.
* Tells the employee, their new managers and their former ones that the
* employee's manager changed (§2.1), and who changed it. Not HR: the change
* is in the person's history, where HR looks. The notification carries uids
* only; the names are read when it is shown. Nobody is told about their own
* change.
*
* @param list<string> $before
* @param list<string> $after
* @param string[] $hrUids
*/
public function notifyManagerChanged(string $employeeUid, array $before, array $after, string $actorUid, array $hrUids): void {
foreach (array_unique([$employeeUid, ...$hrUids]) as $uid) {
public function notifyManagerChanged(string $employeeUid, array $before, array $after, string $actorUid): void {
foreach (array_unique([$employeeUid, ...$after, ...$before]) as $uid) {
if ($uid === $actorUid || $this->userManager->get($uid)?->isEnabled() !== true) {
continue;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/ManagerChangeHistoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function tearDown(): void {
parent::tearDown();
}

public function testAChangeBySomeoneIsInTheHistoryAndToldToTheEmployeeAndHr(): void {
public function testAChangeBySomeoneIsInTheHistoryAndToldToTheEmployeeAndTheFormerManager(): void {
$users = Server::get(IUserManager::class);
Server::get(IUserSession::class)->setUser($users->get(self::SUBADMIN));

Expand All @@ -68,7 +68,8 @@ public function testAChangeBySomeoneIsInTheHistoryAndToldToTheEmployeeAndHr(): v
$latest = Server::get(EmployeeService::class)->history(self::EMPLOYEE)[0];
self::assertSame(['manager', self::SUBADMIN, [self::FORMER], [self::SUBADMIN]], [$latest['field'], $latest['actor'], $latest['before'], $latest['after']]);
$told = array_filter(RecordingNotifications::$sent, static fn (INotification $n): bool => $n->getSubject() === NotificationService::SUBJECT_MANAGER_CHANGED);
self::assertEqualsCanonicalizing([self::EMPLOYEE, self::HR], array_map(static fn (INotification $n): string => $n->getUser(), $told));
// The new manager made the change, so the employee and the former manager are told; HR is not
self::assertEqualsCanonicalizing([self::EMPLOYEE, self::FORMER], array_map(static fn (INotification $n): string => $n->getUser(), $told));
}

public function testAChangeWithoutASessionIsInTheHistoryOnly(): void {
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,28 @@ public function testHrReadsWhoseManagerChangedAndOpensTheirProfile(): void {
self::assertSame('Admin changed it from nobody to Carol, Boss.', $this->parsedMessage);
self::assertSame('https://cloud.example/apps/absence/#/profile/emp', $this->link);
}

public function testTheNewManagerReadsThatTheEmployeeNowReportsToThem(): void {
$this->prepare(NotificationService::SUBJECT_MANAGER_CHANGED, [
'employee' => 'emp',
'actor' => 'admin',
'before' => ['boss'],
'after' => ['carol'],
], 'carol');

self::assertSame('Emp now reports to you', $this->parsedSubject);
self::assertSame('https://cloud.example/apps/absence/#/profile/emp', $this->link);
}

public function testTheFormerManagerReadsThatTheEmployeeNoLongerReportsToThemAndOpensTheirTeam(): void {
$this->prepare(NotificationService::SUBJECT_MANAGER_CHANGED, [
'employee' => 'emp',
'actor' => 'admin',
'before' => ['boss'],
'after' => ['carol'],
], 'boss');

self::assertSame('Emp no longer reports to you', $this->parsedSubject);
self::assertSame('https://cloud.example/apps/absence/#/team', $this->link);
}
}
8 changes: 2 additions & 6 deletions tests/Unit/Service/EmployeeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use OCA\Absence\Service\EmployeeService;
use OCA\Absence\Service\HolidayService;
use OCA\Absence\Service\NotificationService;
use OCA\Absence\Service\PermissionService;
use OCA\Absence\Tests\Unit\ClockMockTrait;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
Expand Down Expand Up @@ -58,8 +57,6 @@ protected function setUp(): void {
$this->directory = $this->createMock(EmployeeDirectory::class);
$this->directory->method('isEmployee')->willReturnCallback(static fn (string $uid): bool => $uid !== 'guest');
$this->notifications = $this->createMock(NotificationService::class);
$permission = $this->createMock(PermissionService::class);
$permission->method('getHrUids')->willReturn(['hr1', 'hr2']);

$this->service = new EmployeeService(
$this->createMock(EmploymentMapper::class),
Expand All @@ -72,14 +69,13 @@ protected function setUp(): void {
$dispatcher,
$this->notifications,
$this->createMock(HolidayService::class),
$permission,
$this->createMock(IL10N::class),
);
}

public function testAChangeBySomeoneIsKeptAuditedAndToldToTheEmployeeAndHr(): void {
public function testAChangeBySomeoneIsKeptAuditedAndTold(): void {
$this->notifications->expects(self::once())->method('notifyManagerChanged')
->with('emp', ['boss'], ['carol'], 'carol', ['hr1', 'hr2']);
->with('emp', ['boss'], ['carol'], 'carol');

$this->service->recordManagerChange('emp', ['boss'], ['carol'], 'carol');

Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Service/NotificationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,13 @@ public function testCarryOverReminderSendsNoEmailForAnAccountThatIsGone(): void
$this->service->notifyCarryOverExpiring('robin', 3.0, '2032-03-31');
}

public function testAManagerChangeIsToldToTheEmployeeAndHrButNotToWhoeverMadeIt(): void {
$this->withUsers('emp', 'hr1', 'hr2', 'hr3-disabled', 'carol');
public function testAManagerChangeIsToldToTheEmployeeAndTheirManagersButNotToWhoeverMadeIt(): void {
$this->withUsers('emp', 'boss', 'carol', 'dave-disabled', 'hr2');
$this->mailer->expects(self::never())->method('send');

$this->service->notifyManagerChanged('emp', ['boss'], ['carol'], 'hr2', ['hr1', 'hr2', 'hr3-disabled', 'emp']);
$this->service->notifyManagerChanged('emp', ['boss', 'hr2'], ['carol', 'dave-disabled'], 'hr2');

self::assertSame(['emp', 'hr1'], $this->recipients);
self::assertSame(['employee' => 'emp', 'actor' => 'hr2', 'before' => ['boss'], 'after' => ['carol']], $this->pushed[0]);
self::assertSame(['emp', 'carol', 'boss'], $this->recipients);
self::assertSame(['employee' => 'emp', 'actor' => 'hr2', 'before' => ['boss', 'hr2'], 'after' => ['carol', 'dave-disabled']], $this->pushed[0]);
}
}
Loading