Files
Toiminnanohjaus/app/Modules/Dashboard/Application/UseCases/PersonalOverviewQuery.php
T
petriandClaude Sonnet 5.5 487a7d34ba Replace the demo home page with a dashboard
Everyone sees their open tasks on an agenda, their open jobs, this
week's hours and quick links. Admin and management also get a lazily
loaded management panel: things to do, the month so far against the
previous month, jobs and late work, and everyone's workload.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 09:20:02 +02:00

56 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Dashboard\Application\UseCases;
use App\Modules\Dashboard\Application\Contracts\DashboardTime;
use App\Modules\Dashboard\Application\Contracts\DashboardWork;
use App\Modules\Dashboard\Application\DTOs\PersonalOverview;
use App\Modules\Dashboard\Application\DTOs\WorkJob;
use App\Modules\Dashboard\Domain\Enums\AgendaBucket;
use DateTimeImmutable;
/**
* The user's own part of the home page on a day: open tasks they are the
* assignee or a member of, their open jobs (5.6) and this week's hours.
*/
final readonly class PersonalOverviewQuery
{
public const int JOB_LIMIT = 10;
public function __construct(
private DashboardWork $work,
private DashboardTime $time,
) {}
public function execute(int $userId, DateTimeImmutable $today): PersonalOverview
{
$day = $today->format('Y-m-d');
$tomorrow = $today->modify('+1 day')->format('Y-m-d');
$monday = $today->modify('monday this week');
$sunday = $monday->modify('+6 days')->format('Y-m-d');
$buckets = ['overdue' => [], 'soon' => [], 'this_week' => [], 'later' => []];
foreach ($this->work->openTasksOf($userId) as $task) {
$bucket = AgendaBucket::of($task->dueOn, $task->plannedFrom, $day, $tomorrow, $sunday);
$buckets[$bucket->value][] = $bucket === AgendaBucket::Overdue ? $task->late() : $task;
}
$jobs = array_map(
fn (WorkJob $job): WorkJob => $job->dueOn !== null && $job->dueOn < $day ? $job->late() : $job,
$this->work->openJobsOf($userId),
);
return new PersonalOverview(
$buckets['overdue'],
$buckets['soon'],
$buckets['this_week'],
count($buckets['later']),
array_slice($jobs, 0, self::JOB_LIMIT),
count($jobs),
$this->time->week($userId, $monday->format('Y-m-d')),
);
}
}