64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\TimeTracking\Application\UseCases;
|
|
|
|
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
use App\Modules\TimeTracking\Domain\Contracts\PinnedTaskRepository;
|
|
use App\Modules\TimeTracking\Domain\Exceptions\TaskNotBookable;
|
|
use App\Modules\TimeTracking\Domain\Exceptions\TimeLocked;
|
|
use App\Modules\Users\Application\Contracts\Clock;
|
|
|
|
/**
|
|
* Pins tasks to the "Lukitut työt" part of a user's time sheet (★), or
|
|
* unpins them. Only tasks that can take hours (open task of an unlocked
|
|
* job) can be pinned; unpinning always works.
|
|
*/
|
|
final class PinTimesheetTasks
|
|
{
|
|
public function __construct(
|
|
private PinnedTaskRepository $pins,
|
|
private TaskQueries $tasks,
|
|
private TransactionManager $transactions,
|
|
private Clock $clock,
|
|
) {}
|
|
|
|
/**
|
|
* @param list<int> $taskIds
|
|
*
|
|
* @throws TaskNotBookable
|
|
* @throws TimeLocked
|
|
*/
|
|
public function pin(int $userId, array $taskIds): void
|
|
{
|
|
$now = $this->clock->now();
|
|
|
|
$this->transactions->run(function () use ($userId, $taskIds, $now): void {
|
|
foreach ($taskIds as $taskId) {
|
|
$task = $this->tasks->bookableTask($taskId) ?? throw new TaskNotBookable($taskId);
|
|
|
|
if ($task->isLocked()) {
|
|
throw new TimeLocked;
|
|
}
|
|
|
|
$this->pins->pin($userId, $taskId, $now);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function unpin(int $userId, int $taskId): void
|
|
{
|
|
$this->transactions->run(fn () => $this->pins->unpin($userId, $taskId));
|
|
}
|
|
|
|
/**
|
|
* "Poista kaikki kiinnitykset".
|
|
*/
|
|
public function unpinAll(int $userId): int
|
|
{
|
|
return $this->transactions->run(fn (): int => $this->pins->unpinAll($userId));
|
|
}
|
|
}
|