Add tasks to customer jobs
Phase 1, item 7, in the Jobs module (tasks are part of a job, and my jobs needs their assignees): - Tasks with name, work type, assignees, status (waiting, in progress, in review, done), due date, estimated hours and description. - Tasks can be added once the job is a quote request or later, not to an archived job. The work type must belong to the job's service units or the shared ones and be in use; assignees must be active. What a task already has stays allowed after being taken out of use. - Job page: a Tehtävät section with a status select per task (announced, focus kept), edit links and delete with a confirmation dialog. Task form with work types grouped by unit and service. - Access: adding, changing and removing for everyone but subcontractors; subcontractors may change task statuses on jobs they are attached to. - Task assignees now count in my jobs (list, Kanban, job access). - Spec and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\Contracts;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\TaskSummary;
|
||||
|
||||
interface TaskQueries
|
||||
{
|
||||
/**
|
||||
* The job's tasks: open ones by due date (none last), then done ones.
|
||||
*
|
||||
* @return list<TaskSummary>
|
||||
*/
|
||||
public function tasksOf(int $jobId): array;
|
||||
|
||||
/**
|
||||
* Null when the task does not exist or belongs to another job.
|
||||
*/
|
||||
public function findTask(int $jobId, int $taskId): ?TaskSummary;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* Input for adding or changing a task. The due date is Y-m-d.
|
||||
*/
|
||||
final readonly class TaskData
|
||||
{
|
||||
/**
|
||||
* @param list<int> $assigneeUserIds
|
||||
*/
|
||||
public function __construct(
|
||||
public string $title,
|
||||
public int $workTypeId,
|
||||
public string $status,
|
||||
public ?string $description = null,
|
||||
public ?string $dueOn = null,
|
||||
public ?string $estimatedHours = null,
|
||||
public array $assigneeUserIds = [],
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
|
||||
/**
|
||||
* A task with names resolved, for the job page and the task form.
|
||||
*/
|
||||
final readonly class TaskSummary
|
||||
{
|
||||
/**
|
||||
* @param list<NamedId> $assignees
|
||||
*/
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $jobId,
|
||||
public string $title,
|
||||
public ?string $description,
|
||||
public TaskStatus $status,
|
||||
public ?string $dueOn,
|
||||
public ?string $estimatedHours,
|
||||
public NamedId $workType,
|
||||
public array $assignees,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\Services;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Jobs\Domain\Entities\CustomerJob;
|
||||
use App\Modules\Jobs\Domain\Entities\Task;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
|
||||
/**
|
||||
* Checks that a task's work type belongs to the job's service units (or
|
||||
* the shared work types) and is in use, and that assignees are active.
|
||||
* What the task already has stays allowed after being taken out of use.
|
||||
*/
|
||||
final class TaskReferenceChecker
|
||||
{
|
||||
public function __construct(
|
||||
private CatalogQueries $catalog,
|
||||
private UserAccountQueries $users,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param list<int> $assigneeUserIds
|
||||
*
|
||||
* @throws InvalidTaskReference
|
||||
*/
|
||||
public function check(CustomerJob $job, int $workTypeId, array $assigneeUserIds, ?Task $current = null): void
|
||||
{
|
||||
if ($workTypeId !== $current?->workTypeId() && ! in_array($workTypeId, $this->workTypeIdsFor($job), true)) {
|
||||
throw new InvalidTaskReference('work_type');
|
||||
}
|
||||
|
||||
$activeIds = array_map(fn ($option): int => $option->id, $this->users->activeUserOptions());
|
||||
|
||||
foreach ($assigneeUserIds as $userId) {
|
||||
if (! in_array($userId, $activeIds, true) && ! in_array($userId, $current?->assigneeUserIds() ?? [], true)) {
|
||||
throw new InvalidTaskReference('assignees');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
private function workTypeIdsFor(CustomerJob $job): array
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
foreach ($this->catalog->activeWorkTypesFor($job->details()->serviceUnitIds) as $unit) {
|
||||
foreach ($unit->workTypes as $workType) {
|
||||
$ids[] = $workType->id;
|
||||
}
|
||||
|
||||
foreach ($unit->services as $service) {
|
||||
foreach ($service->workTypes as $workType) {
|
||||
$ids[] = $workType->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\TaskData;
|
||||
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
|
||||
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Entities\Task;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
final class AddTask
|
||||
{
|
||||
public function __construct(
|
||||
private CustomerJobRepository $jobs,
|
||||
private TaskRepository $tasks,
|
||||
private TaskReferenceChecker $references,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return int The new task's id.
|
||||
*
|
||||
* @throws CustomerJobNotFound
|
||||
* @throws TasksNotAllowedYet
|
||||
* @throws InvalidTaskReference
|
||||
*/
|
||||
public function execute(int $jobId, TaskData $data, int $actorUserId): int
|
||||
{
|
||||
$title = new TaskTitle($data->title);
|
||||
|
||||
return $this->transactions->run(function () use ($jobId, $data, $title, $actorUserId): int {
|
||||
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
|
||||
$this->references->check($job, $data->workTypeId, $data->assigneeUserIds);
|
||||
|
||||
$task = Task::create(
|
||||
$jobId,
|
||||
$job->details()->status,
|
||||
$data->workTypeId,
|
||||
$title,
|
||||
$data->description,
|
||||
TaskStatus::from($data->status),
|
||||
TaskInput::date($data->dueOn),
|
||||
TaskInput::hours($data->estimatedHours),
|
||||
$data->assigneeUserIds,
|
||||
$actorUserId,
|
||||
);
|
||||
|
||||
return (int) $this->tasks->save($task)->id();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
final class ChangeTaskStatus
|
||||
{
|
||||
public function __construct(
|
||||
private TaskRepository $tasks,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws TaskNotFound
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId, TaskStatus $status): void
|
||||
{
|
||||
$this->transactions->run(function () use ($jobId, $taskId, $status): void {
|
||||
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
||||
$task->changeStatus($status);
|
||||
$this->tasks->save($task);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Deletes a task. Once time is recorded on tasks, a task with time entries
|
||||
* cannot be removed.
|
||||
*/
|
||||
final class RemoveTask
|
||||
{
|
||||
public function __construct(
|
||||
private TaskRepository $tasks,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws TaskNotFound
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId): void
|
||||
{
|
||||
$this->transactions->run(function () use ($jobId, $taskId): void {
|
||||
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
||||
$this->tasks->delete($task);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Domain\ValueObjects\EstimatedHours;
|
||||
use DateTimeImmutable;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Parses the optional date and hours of task input.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class TaskInput
|
||||
{
|
||||
public static function date(?string $value): ?DateTimeImmutable
|
||||
{
|
||||
$value = $value === null ? '' : trim($value);
|
||||
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$date = DateTimeImmutable::createFromFormat('!Y-m-d', $value);
|
||||
|
||||
if ($date === false || $date->format('Y-m-d') !== $value) {
|
||||
throw new InvalidArgumentException("Not a Y-m-d date: {$value}");
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
public static function hours(?string $value): ?EstimatedHours
|
||||
{
|
||||
$value = $value === null ? '' : trim($value);
|
||||
|
||||
return $value === '' ? null : EstimatedHours::fromString($value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\TaskData;
|
||||
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
|
||||
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
final class UpdateTask
|
||||
{
|
||||
public function __construct(
|
||||
private CustomerJobRepository $jobs,
|
||||
private TaskRepository $tasks,
|
||||
private TaskReferenceChecker $references,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws CustomerJobNotFound
|
||||
* @throws TaskNotFound
|
||||
* @throws InvalidTaskReference
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId, TaskData $data): void
|
||||
{
|
||||
$title = new TaskTitle($data->title);
|
||||
|
||||
$this->transactions->run(function () use ($jobId, $taskId, $data, $title): void {
|
||||
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
|
||||
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
||||
|
||||
$this->references->check($job, $data->workTypeId, $data->assigneeUserIds, $task);
|
||||
|
||||
$task->changeDetails(
|
||||
$data->workTypeId,
|
||||
$title,
|
||||
$data->description,
|
||||
TaskStatus::from($data->status),
|
||||
TaskInput::date($data->dueOn),
|
||||
TaskInput::hours($data->estimatedHours),
|
||||
$data->assigneeUserIds,
|
||||
);
|
||||
|
||||
$this->tasks->save($task);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Contracts;
|
||||
|
||||
use App\Modules\Jobs\Domain\Entities\Task;
|
||||
|
||||
interface TaskRepository
|
||||
{
|
||||
/**
|
||||
* Null when the task does not exist or belongs to another job.
|
||||
*/
|
||||
public function findForJob(int $jobId, int $taskId): ?Task;
|
||||
|
||||
public function save(Task $task): Task;
|
||||
|
||||
public function delete(Task $task): void;
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Entities;
|
||||
|
||||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\EstimatedHours;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* A task (tehtävä) of a customer job: one step of the work, done by its
|
||||
* assignees under one work type. Time entries are recorded on tasks.
|
||||
*/
|
||||
final class Task
|
||||
{
|
||||
/**
|
||||
* @param list<int> $assigneeUserIds
|
||||
*/
|
||||
private function __construct(
|
||||
private ?int $id,
|
||||
private int $jobId,
|
||||
private int $workTypeId,
|
||||
private TaskTitle $title,
|
||||
private ?string $description,
|
||||
private TaskStatus $status,
|
||||
private ?DateTimeImmutable $dueOn,
|
||||
private ?EstimatedHours $estimatedHours,
|
||||
private array $assigneeUserIds,
|
||||
private int $createdBy,
|
||||
) {
|
||||
$this->assigneeUserIds = self::normaliseIds($assigneeUserIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $assigneeUserIds
|
||||
*
|
||||
* @throws TasksNotAllowedYet
|
||||
*/
|
||||
public static function create(
|
||||
int $jobId,
|
||||
JobStatus $jobStatus,
|
||||
int $workTypeId,
|
||||
TaskTitle $title,
|
||||
?string $description,
|
||||
TaskStatus $status,
|
||||
?DateTimeImmutable $dueOn,
|
||||
?EstimatedHours $estimatedHours,
|
||||
array $assigneeUserIds,
|
||||
int $createdBy,
|
||||
): self {
|
||||
if (! $jobStatus->allowsTasks()) {
|
||||
throw new TasksNotAllowedYet;
|
||||
}
|
||||
|
||||
return new self(null, $jobId, $workTypeId, $title, self::text($description), $status, $dueOn, $estimatedHours, $assigneeUserIds, $createdBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $assigneeUserIds
|
||||
*/
|
||||
public static function reconstitute(
|
||||
int $id,
|
||||
int $jobId,
|
||||
int $workTypeId,
|
||||
TaskTitle $title,
|
||||
?string $description,
|
||||
TaskStatus $status,
|
||||
?DateTimeImmutable $dueOn,
|
||||
?EstimatedHours $estimatedHours,
|
||||
array $assigneeUserIds,
|
||||
int $createdBy,
|
||||
): self {
|
||||
return new self($id, $jobId, $workTypeId, $title, $description, $status, $dueOn, $estimatedHours, $assigneeUserIds, $createdBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $assigneeUserIds
|
||||
*/
|
||||
public function changeDetails(
|
||||
int $workTypeId,
|
||||
TaskTitle $title,
|
||||
?string $description,
|
||||
TaskStatus $status,
|
||||
?DateTimeImmutable $dueOn,
|
||||
?EstimatedHours $estimatedHours,
|
||||
array $assigneeUserIds,
|
||||
): void {
|
||||
$this->workTypeId = $workTypeId;
|
||||
$this->title = $title;
|
||||
$this->description = self::text($description);
|
||||
$this->status = $status;
|
||||
$this->dueOn = $dueOn;
|
||||
$this->estimatedHours = $estimatedHours;
|
||||
$this->assigneeUserIds = self::normaliseIds($assigneeUserIds);
|
||||
}
|
||||
|
||||
public function changeStatus(TaskStatus $status): void
|
||||
{
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
public function id(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function jobId(): int
|
||||
{
|
||||
return $this->jobId;
|
||||
}
|
||||
|
||||
public function workTypeId(): int
|
||||
{
|
||||
return $this->workTypeId;
|
||||
}
|
||||
|
||||
public function title(): TaskTitle
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function description(): ?string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function status(): TaskStatus
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function dueOn(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->dueOn;
|
||||
}
|
||||
|
||||
public function estimatedHours(): ?EstimatedHours
|
||||
{
|
||||
return $this->estimatedHours;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
public function assigneeUserIds(): array
|
||||
{
|
||||
return $this->assigneeUserIds;
|
||||
}
|
||||
|
||||
public function createdBy(): int
|
||||
{
|
||||
return $this->createdBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $ids
|
||||
* @return list<int>
|
||||
*/
|
||||
private static function normaliseIds(array $ids): array
|
||||
{
|
||||
$ids = array_values(array_unique($ids));
|
||||
sort($ids);
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
private static function text(?string $value): ?string
|
||||
{
|
||||
$value = $value === null ? '' : trim($value);
|
||||
|
||||
return $value === '' ? null : $value;
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,14 @@ enum JobStatus: string
|
||||
return $this === self::Invoiced || $this === self::Billed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tasks can be added from a quote request on (K36), not to an archived job.
|
||||
*/
|
||||
public function allowsTasks(): bool
|
||||
{
|
||||
return ! in_array($this, [self::Prospect, self::Tentative, self::Archived], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Kanban columns: every status except archived.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Enums;
|
||||
|
||||
/**
|
||||
* Status of a task (spec 8.2). Per-service-unit columns come in phase 3.
|
||||
*/
|
||||
enum TaskStatus: string
|
||||
{
|
||||
case Waiting = 'waiting';
|
||||
case InProgress = 'in_progress';
|
||||
case InReview = 'in_review';
|
||||
case Done = 'done';
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* The chosen work type or assignee does not fit the task's job.
|
||||
*/
|
||||
final class InvalidTaskReference extends DomainException
|
||||
{
|
||||
public function __construct(public readonly string $field)
|
||||
{
|
||||
parent::__construct("The task's {$field} is not a valid choice.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidTaskTitle extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The task name must be 1–255 characters.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* The task does not exist or belongs to another job.
|
||||
*/
|
||||
final class TaskNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $jobId, public readonly int $taskId)
|
||||
{
|
||||
parent::__construct("Task {$taskId} of job {$jobId} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class TasksNotAllowedYet extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('Tasks can be added once the job is a quote request or later, and not to an archived job.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskTitle;
|
||||
|
||||
final readonly class TaskTitle
|
||||
{
|
||||
public const int MAX_LENGTH = 255;
|
||||
|
||||
public string $value;
|
||||
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
|
||||
|
||||
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
|
||||
throw new InvalidTaskTitle;
|
||||
}
|
||||
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Jobs\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Database\Factories\TaskFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model of a task, used only inside the Jobs Infrastructure
|
||||
* layer. Assignees are in task_assignees.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $customer_job_id
|
||||
* @property int $work_type_id
|
||||
* @property string $title
|
||||
* @property string|null $description
|
||||
* @property TaskStatus $status
|
||||
* @property CarbonImmutable|null $due_on
|
||||
* @property string|null $estimated_hours
|
||||
* @property int $created_by
|
||||
*/
|
||||
#[Table('tasks')]
|
||||
#[Fillable(['customer_job_id', 'work_type_id', 'title', 'description', 'status', 'due_on', 'estimated_hours', 'created_by'])]
|
||||
#[UseFactory(TaskFactory::class)]
|
||||
class TaskModel extends Model
|
||||
{
|
||||
/** @use HasFactory<TaskFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'customer_job_id' => 'integer',
|
||||
'work_type_id' => 'integer',
|
||||
'status' => TaskStatus::class,
|
||||
'due_on' => 'immutable_date',
|
||||
'created_by' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
+7
-2
@@ -236,7 +236,8 @@ class EloquentCustomerJobQueries implements CustomerJobQueries
|
||||
}
|
||||
|
||||
/**
|
||||
* Restricts to jobs where the user is responsible, contact, biller or member.
|
||||
* Restricts to jobs where the user is responsible, contact, biller,
|
||||
* member or an assignee of one of the job's tasks.
|
||||
*
|
||||
* @param Builder<CustomerJobModel> $query
|
||||
* @return Builder<CustomerJobModel>
|
||||
@@ -247,6 +248,10 @@ class EloquentCustomerJobQueries implements CustomerJobQueries
|
||||
->where('responsible_user_id', $userId)
|
||||
->orWhere('contact_user_id', $userId)
|
||||
->orWhere('biller_user_id', $userId)
|
||||
->orWhereIn('id', DB::table('customer_job_members')->select('customer_job_id')->where('user_id', $userId)));
|
||||
->orWhereIn('id', DB::table('customer_job_members')->select('customer_job_id')->where('user_id', $userId))
|
||||
->orWhereIn('id', DB::table('tasks')
|
||||
->join('task_assignees', 'task_assignees.task_id', '=', 'tasks.id')
|
||||
->select('tasks.customer_job_id')
|
||||
->where('task_assignees.user_id', $userId)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\NamedId;
|
||||
use App\Modules\Jobs\Application\DTOs\TaskSummary;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EloquentTaskQueries implements TaskQueries
|
||||
{
|
||||
public function __construct(
|
||||
private CatalogQueries $catalog,
|
||||
private UserAccountQueries $users,
|
||||
) {}
|
||||
|
||||
public function tasksOf(int $jobId): array
|
||||
{
|
||||
$models = TaskModel::query()
|
||||
->where('customer_job_id', $jobId)
|
||||
->orderByRaw('status = ?', [TaskStatus::Done->value])
|
||||
->orderByRaw('due_on IS NULL')
|
||||
->orderBy('due_on')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
return $this->summaries($models);
|
||||
}
|
||||
|
||||
public function findTask(int $jobId, int $taskId): ?TaskSummary
|
||||
{
|
||||
$model = TaskModel::query()->where('customer_job_id', $jobId)->find($taskId);
|
||||
|
||||
return $model === null ? null : $this->summaries(collect([$model]))[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, TaskModel> $models
|
||||
* @return list<TaskSummary>
|
||||
*/
|
||||
private function summaries(Collection $models): array
|
||||
{
|
||||
$assignees = DB::table('task_assignees')
|
||||
->whereIn('task_id', $models->pluck('id')->all())
|
||||
->get()
|
||||
->groupBy('task_id');
|
||||
$userNames = $this->users->namesByIds($assignees->flatten()->pluck('user_id')->map(fn ($id): int => (int) $id)->unique()->values()->all());
|
||||
|
||||
$workTypeNames = [];
|
||||
foreach ($models->pluck('work_type_id')->unique() as $workTypeId) {
|
||||
$workTypeNames[$workTypeId] = $this->catalog->findWorkType((int) $workTypeId)->name ?? '';
|
||||
}
|
||||
|
||||
return $models->map(fn (TaskModel $model): TaskSummary => new TaskSummary(
|
||||
id: $model->id,
|
||||
jobId: $model->customer_job_id,
|
||||
title: $model->title,
|
||||
description: $model->description,
|
||||
status: $model->status,
|
||||
dueOn: $model->due_on?->format('Y-m-d'),
|
||||
estimatedHours: $model->estimated_hours === null ? null : rtrim(rtrim($model->estimated_hours, '0'), '.'),
|
||||
workType: new NamedId($model->work_type_id, $workTypeNames[$model->work_type_id] ?? ''),
|
||||
assignees: ($assignees->get($model->id) ?? collect())
|
||||
->map(fn ($row): NamedId => new NamedId((int) $row->user_id, $userNames[(int) $row->user_id] ?? ''))
|
||||
->sortBy('name')
|
||||
->values()
|
||||
->all(),
|
||||
))->values()->all();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Entities\Task;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\EstimatedHours;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EloquentTaskRepository implements TaskRepository
|
||||
{
|
||||
public function findForJob(int $jobId, int $taskId): ?Task
|
||||
{
|
||||
$model = TaskModel::query()->where('customer_job_id', $jobId)->find($taskId);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function save(Task $task): Task
|
||||
{
|
||||
$model = $task->id() === null ? new TaskModel : TaskModel::query()->findOrFail($task->id());
|
||||
|
||||
$model->fill([
|
||||
'customer_job_id' => $task->jobId(),
|
||||
'work_type_id' => $task->workTypeId(),
|
||||
'title' => $task->title()->value,
|
||||
'description' => $task->description(),
|
||||
'status' => $task->status(),
|
||||
'due_on' => $task->dueOn()?->format('Y-m-d'),
|
||||
'estimated_hours' => $task->estimatedHours()?->value,
|
||||
'created_by' => $task->createdBy(),
|
||||
])->save();
|
||||
|
||||
DB::table('task_assignees')->where('task_id', $model->id)->whereNotIn('user_id', $task->assigneeUserIds())->delete();
|
||||
DB::table('task_assignees')->insertOrIgnore(array_map(
|
||||
fn (int $userId): array => ['task_id' => $model->id, 'user_id' => $userId],
|
||||
$task->assigneeUserIds(),
|
||||
));
|
||||
|
||||
return $this->toEntity($model->refresh());
|
||||
}
|
||||
|
||||
public function delete(Task $task): void
|
||||
{
|
||||
TaskModel::query()->whereKey($task->id())->delete();
|
||||
}
|
||||
|
||||
private function toEntity(TaskModel $model): Task
|
||||
{
|
||||
return Task::reconstitute(
|
||||
$model->id,
|
||||
$model->customer_job_id,
|
||||
$model->work_type_id,
|
||||
new TaskTitle($model->title),
|
||||
$model->description,
|
||||
$model->status,
|
||||
$model->due_on === null ? null : (DateTimeImmutable::createFromFormat('!Y-m-d', $model->due_on->format('Y-m-d')) ?: null),
|
||||
$model->estimated_hours === null ? null : new EstimatedHours((float) $model->estimated_hours),
|
||||
DB::table('task_assignees')->where('task_id', $model->id)->pluck('user_id')->map(fn ($id): int => (int) $id)->all(),
|
||||
$model->created_by,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,16 @@ namespace App\Modules\Jobs\Infrastructure\Providers;
|
||||
use App\Models\User;
|
||||
use App\Modules\Customers\Application\Contracts\CustomerRowUsage;
|
||||
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
||||
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\JobNumberSequence;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Infrastructure\CustomerRowUsageFromJobs;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\DatabaseJobNumberSequence;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentCustomerJobQueries;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentCustomerJobRepository;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskQueries;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskRepository;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@@ -30,6 +34,8 @@ class JobsServiceProvider extends ServiceProvider
|
||||
CustomerJobQueries::class => EloquentCustomerJobQueries::class,
|
||||
JobNumberSequence::class => DatabaseJobNumberSequence::class,
|
||||
CustomerRowUsage::class => CustomerRowUsageFromJobs::class,
|
||||
TaskRepository::class => EloquentTaskRepository::class,
|
||||
TaskQueries::class => EloquentTaskQueries::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
@@ -55,6 +61,13 @@ class JobsServiceProvider extends ServiceProvider
|
||||
|
||||
Gate::define('jobs.archive', $isManager);
|
||||
|
||||
// Adding, changing and removing tasks: everyone but subcontractors.
|
||||
Gate::define('jobs.manage-tasks', fn (User $user, int $jobId): bool => $user->role !== Role::Subcontractor);
|
||||
|
||||
// A subcontractor may change the status of tasks on the jobs they are attached to (spec 2.4).
|
||||
Gate::define('jobs.update-task-status', fn (User $user, int $jobId): bool => $user->role !== Role::Subcontractor
|
||||
|| app(CustomerJobQueries::class)->isMember($jobId, $user->id));
|
||||
|
||||
// "Lasku luotu" and "Laskutettu" are set when invoicing: management or the job's biller.
|
||||
// The argument is the job's biller (user id), when known.
|
||||
Gate::define('jobs.set-billing-status', fn (User $user, ?int $billerUserId = null): bool => $isManager($user)
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Jobs\Presentation\Livewire\Forms;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\NamedId;
|
||||
use App\Modules\Jobs\Application\DTOs\TaskData;
|
||||
use App\Modules\Jobs\Application\DTOs\TaskSummary;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskTitle;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Form;
|
||||
|
||||
/**
|
||||
* Task form shared by the add and edit pages.
|
||||
*/
|
||||
class TaskForm extends Form
|
||||
{
|
||||
public string $title = '';
|
||||
|
||||
public string $work_type_id = '';
|
||||
|
||||
public string $status = TaskStatus::Waiting->value;
|
||||
|
||||
public string $due_on = '';
|
||||
|
||||
public string $estimated_hours = '';
|
||||
|
||||
public string $description = '';
|
||||
|
||||
/** @var list<string> */
|
||||
public array $assignee_user_ids = [];
|
||||
|
||||
public function fillFromSummary(TaskSummary $task): void
|
||||
{
|
||||
$this->title = $task->title;
|
||||
$this->work_type_id = (string) $task->workType->id;
|
||||
$this->status = $task->status->value;
|
||||
$this->due_on = $task->dueOn ?? '';
|
||||
$this->estimated_hours = $task->estimatedHours ?? '';
|
||||
$this->description = $task->description ?? '';
|
||||
$this->assignee_user_ids = array_map(fn (NamedId $user): string => (string) $user->id, $task->assignees);
|
||||
}
|
||||
|
||||
public function toData(): TaskData
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
return new TaskData(
|
||||
title: $this->title,
|
||||
workTypeId: (int) $this->work_type_id,
|
||||
status: $this->status,
|
||||
description: trim($this->description) === '' ? null : $this->description,
|
||||
dueOn: $this->due_on === '' ? null : $this->due_on,
|
||||
estimatedHours: trim($this->estimated_hours) === '' ? null : $this->estimated_hours,
|
||||
assigneeUserIds: array_map('intval', $this->assignee_user_ids),
|
||||
);
|
||||
}
|
||||
|
||||
public function showDomainError(DomainException $exception): void
|
||||
{
|
||||
[$field, $key] = match (true) {
|
||||
$exception instanceof InvalidTaskTitle => ['title', 'jobs.tasks.errors.invalid_title'],
|
||||
$exception instanceof InvalidEstimatedHours => ['estimated_hours', 'jobs.errors.invalid_hours'],
|
||||
$exception instanceof TasksNotAllowedYet => ['title', 'jobs.tasks.not_allowed_yet'],
|
||||
$exception instanceof InvalidTaskReference => [$exception->field === 'work_type' ? 'work_type_id' : 'assignee_user_ids', 'jobs.errors.invalid_reference'],
|
||||
default => throw $exception,
|
||||
};
|
||||
|
||||
$this->addError($field, __($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, mixed>>
|
||||
*/
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
'work_type_id' => ['required', 'integer'],
|
||||
'status' => ['required', Rule::enum(TaskStatus::class)],
|
||||
'due_on' => ['nullable', 'date_format:Y-m-d'],
|
||||
'estimated_hours' => ['nullable', 'numeric', 'min:0', 'max:99999'],
|
||||
'description' => ['nullable', 'string', 'max:10000'],
|
||||
'assignee_user_ids' => ['array'],
|
||||
'assignee_user_ids.*' => ['integer'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function messages(): array
|
||||
{
|
||||
return [
|
||||
'title.required' => __('jobs.tasks.errors.title_required'),
|
||||
'title.max' => __('jobs.tasks.errors.invalid_title'),
|
||||
'work_type_id.required' => __('jobs.tasks.errors.work_type_required'),
|
||||
'due_on.date_format' => __('jobs.errors.invalid_date'),
|
||||
'estimated_hours.numeric' => __('jobs.errors.invalid_hours'),
|
||||
'estimated_hours.min' => __('jobs.errors.invalid_hours'),
|
||||
'estimated_hours.max' => __('jobs.errors.invalid_hours'),
|
||||
'description.max' => __('jobs.errors.too_long', ['max' => 10000]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,16 @@ use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
||||
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\CustomerJobDetails;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\JobChangeEntry;
|
||||
use App\Modules\Jobs\Application\DTOs\TaskSummary;
|
||||
use App\Modules\Jobs\Application\UseCases\ChangeTaskStatus;
|
||||
use App\Modules\Jobs\Application\UseCases\RemoveTask;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
@@ -19,6 +26,17 @@ new class extends Component
|
||||
#[Locked]
|
||||
public int $jobId;
|
||||
|
||||
/**
|
||||
* The task the remove dialog asks about.
|
||||
*/
|
||||
#[Locked]
|
||||
public ?int $removalTaskId = null;
|
||||
|
||||
#[Locked]
|
||||
public string $removalTaskTitle = '';
|
||||
|
||||
public string $taskStatusMessage = '';
|
||||
|
||||
public function mount(CustomerJobQueries $jobQueries, int $job): void
|
||||
{
|
||||
$this->authorize('jobs.view');
|
||||
@@ -45,6 +63,82 @@ new class extends Component
|
||||
return app(CustomerJobQueries::class)->changes($this->jobId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<TaskSummary>
|
||||
*/
|
||||
#[Computed]
|
||||
public function tasks(): array
|
||||
{
|
||||
return app(TaskQueries::class)->tasksOf($this->jobId);
|
||||
}
|
||||
|
||||
public function changeTaskStatus(int $taskId, string $status, ChangeTaskStatus $changeStatus): void
|
||||
{
|
||||
$this->authorize('jobs.update-task-status', $this->jobId);
|
||||
|
||||
$newStatus = TaskStatus::tryFrom($status);
|
||||
$task = app(TaskQueries::class)->findTask($this->jobId, $taskId);
|
||||
|
||||
if ($newStatus === null || $task === null) {
|
||||
$this->taskStatusMessage = __('jobs.tasks.not_found');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$changeStatus->execute($this->jobId, $taskId, $newStatus);
|
||||
} catch (TaskNotFound) {
|
||||
$this->taskStatusMessage = __('jobs.tasks.not_found');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
unset($this->tasks);
|
||||
$this->taskStatusMessage = __('jobs.tasks.status_changed', ['title' => $task->title, 'status' => __('jobs.tasks.statuses.'.$newStatus->value)]);
|
||||
|
||||
// The list is re-ordered (done tasks last): keep focus on the same select.
|
||||
$this->js("document.getElementById('task-status-{$taskId}')?.focus()");
|
||||
}
|
||||
|
||||
public function confirmTaskRemoval(int $taskId): void
|
||||
{
|
||||
$this->authorize('jobs.manage-tasks', $this->jobId);
|
||||
|
||||
$task = app(TaskQueries::class)->findTask($this->jobId, $taskId);
|
||||
|
||||
if ($task === null) {
|
||||
$this->taskStatusMessage = __('jobs.tasks.not_found');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->removalTaskId = $task->id;
|
||||
$this->removalTaskTitle = $task->title;
|
||||
|
||||
$this->js("document.getElementById('remove-task-dialog').showModal()");
|
||||
}
|
||||
|
||||
public function removeTask(RemoveTask $removeTask): void
|
||||
{
|
||||
$this->authorize('jobs.manage-tasks', $this->jobId);
|
||||
|
||||
if ($this->removalTaskId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$removeTask->execute($this->jobId, $this->removalTaskId);
|
||||
$this->taskStatusMessage = __('jobs.tasks.removed', ['title' => $this->removalTaskTitle]);
|
||||
} catch (TaskNotFound) {
|
||||
$this->taskStatusMessage = __('jobs.tasks.not_found');
|
||||
}
|
||||
|
||||
unset($this->tasks);
|
||||
$this->reset('removalTaskId', 'removalTaskTitle');
|
||||
|
||||
$this->js("document.getElementById('remove-task-dialog').close(); document.getElementById('tasks-heading').focus()");
|
||||
}
|
||||
|
||||
public function formatDate(?string $date): string
|
||||
{
|
||||
return $date === null ? __('jobs.not_set') : CarbonImmutable::parse($date)->format('j.n.Y');
|
||||
@@ -188,6 +282,113 @@ new class extends Component
|
||||
</dl>
|
||||
</section>
|
||||
|
||||
<section id="tasks" aria-labelledby="tasks-heading" class="space-y-4">
|
||||
<div class="flex flex-wrap items-center justify-between gap-4">
|
||||
<h2 id="tasks-heading" tabindex="-1" class="text-xl font-semibold focus:outline-none">{{ __('jobs.tasks.title') }}</h2>
|
||||
|
||||
@can('jobs.manage-tasks', $job->id)
|
||||
@if ($job->status->allowsTasks())
|
||||
<x-button :href="route('jobs.tasks.create', $job->id)" wire:navigate icon="plus" :text="__('jobs.tasks.new')" />
|
||||
@endif
|
||||
@endcan
|
||||
</div>
|
||||
|
||||
@can('jobs.manage-tasks', $job->id)
|
||||
@unless ($job->status->allowsTasks())
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.not_allowed_yet') }}</p>
|
||||
@endunless
|
||||
@endcan
|
||||
|
||||
<p role="status" aria-live="polite" class="text-sm">{{ $taskStatusMessage }}</p>
|
||||
|
||||
@if ($this->tasks === [])
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.empty') }}</p>
|
||||
@else
|
||||
@php($canChangeStatus = Gate::allows('jobs.update-task-status', $job->id))
|
||||
@php($canManageTasks = Gate::allows('jobs.manage-tasks', $job->id))
|
||||
|
||||
<ul class="space-y-3">
|
||||
@foreach ($this->tasks as $task)
|
||||
<li wire:key="task-{{ $task->id }}">
|
||||
<article aria-labelledby="task-{{ $task->id }}-title" class="space-y-3 rounded-lg border border-zinc-200 p-4 text-sm dark:border-zinc-700">
|
||||
<div class="flex flex-wrap items-start justify-between gap-3">
|
||||
<h3 id="task-{{ $task->id }}-title" class="text-base font-medium break-words">{{ $task->title }}</h3>
|
||||
<span>{{ __('jobs.tasks.statuses.'.$task->status->value) }}</span>
|
||||
</div>
|
||||
|
||||
<dl class="grid gap-x-6 gap-y-1 sm:grid-cols-[max-content_1fr]">
|
||||
<dt class="font-medium">{{ __('jobs.tasks.fields.work_type') }}</dt>
|
||||
<dd>{{ $task->workType->name }}</dd>
|
||||
<dt class="font-medium">{{ __('jobs.tasks.fields.assignees') }}</dt>
|
||||
<dd>{{ $task->assignees === [] ? __('jobs.not_set') : collect($task->assignees)->pluck('name')->join(', ') }}</dd>
|
||||
<dt class="font-medium">{{ __('jobs.tasks.fields.due_on') }}</dt>
|
||||
<dd><x-jobs.due-date :date="$task->dueOn" :status="$task->status" /></dd>
|
||||
<dt class="font-medium">{{ __('jobs.tasks.fields.estimated_hours') }}</dt>
|
||||
<dd>{{ $task->estimatedHours === null ? __('jobs.not_set') : str_replace('.', ',', $task->estimatedHours).' h' }}</dd>
|
||||
</dl>
|
||||
|
||||
@if ($task->description !== null)
|
||||
<p class="whitespace-pre-line">{{ $task->description }}</p>
|
||||
@endif
|
||||
|
||||
<div class="flex flex-wrap items-end gap-3">
|
||||
@if ($canChangeStatus)
|
||||
<div class="w-full sm:w-56">
|
||||
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||
<x-select.native
|
||||
id="task-status-{{ $task->id }}"
|
||||
wire:change="changeTaskStatus({{ $task->id }}, $event.target.value)"
|
||||
:label="__('jobs.tasks.fields.status')"
|
||||
:aria-label="__('jobs.tasks.status_of', ['title' => $task->title])"
|
||||
>
|
||||
@foreach (TaskStatus::cases() as $status)
|
||||
<option value="{{ $status->value }}" @selected($status === $task->status)>{{ __('jobs.tasks.statuses.'.$status->value) }}</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
@if ($canManageTasks)
|
||||
<x-button
|
||||
:href="route('jobs.tasks.edit', [$job->id, $task->id])"
|
||||
wire:navigate
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="pencil"
|
||||
:text="__('jobs.edit')"
|
||||
:aria-label="__('jobs.tasks.edit_named', ['title' => $task->title])"
|
||||
/>
|
||||
<x-button
|
||||
wire:click="confirmTaskRemoval({{ $task->id }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="trash"
|
||||
:text="__('customers.remove')"
|
||||
:aria-label="__('jobs.tasks.remove_named', ['title' => $task->title])"
|
||||
/>
|
||||
@endif
|
||||
</div>
|
||||
</article>
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
@endif
|
||||
|
||||
<x-confirm-dialog id="remove-task-dialog" :title="__('customers.removal.title')">
|
||||
<p>
|
||||
@if ($removalTaskId !== null)
|
||||
{{ __('jobs.tasks.remove_question', ['title' => $removalTaskTitle]) }}
|
||||
@endif
|
||||
</p>
|
||||
|
||||
<x-slot:actions>
|
||||
<x-button wire:click="removeTask" color="red" icon="trash" :text="__('customers.remove')" />
|
||||
</x-slot:actions>
|
||||
</x-confirm-dialog>
|
||||
</section>
|
||||
|
||||
<section aria-labelledby="links-heading" class="space-y-3">
|
||||
<h2 id="links-heading" class="text-xl font-semibold">{{ __('jobs.sections.links') }}</h2>
|
||||
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\NamedId;
|
||||
use App\Modules\Jobs\Application\UseCases\AddTask;
|
||||
use App\Modules\Jobs\Application\UseCases\UpdateTask;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Jobs\Presentation\Livewire\Forms\TaskForm;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Add page (no {task}) and edit page (with {task}) of a job's task.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $jobId;
|
||||
|
||||
#[Locked]
|
||||
public ?int $taskId = null;
|
||||
|
||||
#[Locked]
|
||||
public string $jobTitle = '';
|
||||
|
||||
/** @var list<int> */
|
||||
#[Locked]
|
||||
public array $serviceUnitIds = [];
|
||||
|
||||
/**
|
||||
* The task's work type when editing, offered even if taken out of use.
|
||||
*/
|
||||
#[Locked]
|
||||
public ?int $storedWorkTypeId = null;
|
||||
|
||||
public TaskForm $form;
|
||||
|
||||
public function mount(CustomerJobQueries $jobQueries, TaskQueries $taskQueries, int $job, ?int $task = null): void
|
||||
{
|
||||
$details = $jobQueries->findDetails($job);
|
||||
|
||||
abort_if($details === null, 404);
|
||||
|
||||
$this->authorize('jobs.manage-tasks', $details->id);
|
||||
|
||||
$this->jobId = $details->id;
|
||||
$this->jobTitle = $details->number.' '.$details->name;
|
||||
$this->serviceUnitIds = array_map(fn (NamedId $unit): int => $unit->id, $details->serviceUnits);
|
||||
|
||||
if ($task === null) {
|
||||
abort_unless($details->status->allowsTasks(), 404);
|
||||
$this->form->assignee_user_ids = [(string) auth()->id()];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$summary = $taskQueries->findTask($details->id, $task);
|
||||
|
||||
abort_if($summary === null, 404);
|
||||
|
||||
$this->taskId = $summary->id;
|
||||
$this->storedWorkTypeId = $summary->workType->id;
|
||||
$this->form->fillFromSummary($summary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Work types of the job's service units and the shared ones, grouped
|
||||
* as "Unit – Service" for <optgroup>.
|
||||
*
|
||||
* @return array<string, list<array{id: int, name: string}>>
|
||||
*/
|
||||
#[Computed]
|
||||
public function workTypeGroups(): array
|
||||
{
|
||||
$catalog = app(CatalogQueries::class);
|
||||
$groups = [];
|
||||
$seen = [];
|
||||
|
||||
foreach ($catalog->activeWorkTypesFor($this->serviceUnitIds) as $node) {
|
||||
if ($node->workTypes !== []) {
|
||||
$groups[$node->serviceUnit->name] = array_map(fn ($workType): array => ['id' => $workType->id, 'name' => $workType->name], $node->workTypes);
|
||||
}
|
||||
|
||||
foreach ($node->services as $serviceNode) {
|
||||
if ($serviceNode->workTypes !== []) {
|
||||
$groups[$node->serviceUnit->name.' – '.$serviceNode->service->name] = array_map(fn ($workType): array => ['id' => $workType->id, 'name' => $workType->name], $serviceNode->workTypes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($groups as $group) {
|
||||
foreach ($group as $workType) {
|
||||
$seen[$workType['id']] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->storedWorkTypeId !== null && ! isset($seen[$this->storedWorkTypeId]) && ($stored = $catalog->findWorkType($this->storedWorkTypeId)) !== null) {
|
||||
$groups[__('catalog.inactive')] = [['id' => $stored->id, 'name' => $stored->name]];
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{value: int, label: string, note: ?string}>
|
||||
*/
|
||||
#[Computed]
|
||||
public function userOptions(): array
|
||||
{
|
||||
return array_map(fn ($user): array => [
|
||||
'value' => $user->id,
|
||||
'label' => $user->name,
|
||||
'note' => $user->role === Role::Subcontractor ? __('users.roles.subcontractor') : null,
|
||||
], app(UserAccountQueries::class)->activeUserOptions());
|
||||
}
|
||||
|
||||
public function save(AddTask $addTask, UpdateTask $updateTask): void
|
||||
{
|
||||
$this->authorize('jobs.manage-tasks', $this->jobId);
|
||||
|
||||
$data = $this->form->toData();
|
||||
|
||||
try {
|
||||
if ($this->taskId === null) {
|
||||
$addTask->execute($this->jobId, $data, (int) auth()->id());
|
||||
session()->flash('status', __('jobs.tasks.created', ['title' => trim($data->title)]));
|
||||
} else {
|
||||
$updateTask->execute($this->jobId, $this->taskId, $data);
|
||||
session()->flash('status', __('jobs.tasks.updated', ['title' => trim($data->title)]));
|
||||
}
|
||||
} catch (CustomerJobNotFound|TaskNotFound) {
|
||||
abort(404);
|
||||
} catch (DomainException $exception) {
|
||||
$this->form->showDomainError($exception);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->redirect(route('jobs.show', $this->jobId).'#tasks', navigate: true);
|
||||
}
|
||||
|
||||
public function heading(): string
|
||||
{
|
||||
return __($this->taskId === null ? 'jobs.tasks.create_title' : 'jobs.tasks.edit_title');
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title($this->heading().' – '.$this->jobTitle);
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
|
||||
<div class="space-y-1">
|
||||
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">
|
||||
{{ __('jobs.tasks.job_label') }}:
|
||||
<x-link :href="route('jobs.show', $jobId)" navigate>{{ $jobTitle }}</x-link>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form wire:submit="save" class="space-y-6" novalidate>
|
||||
<x-input wire:model="form.title" :label="__('jobs.tasks.fields.title')" autocomplete="off" required />
|
||||
|
||||
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||
<x-select.native wire:model="form.work_type_id" :label="__('jobs.tasks.fields.work_type')" :aria-label="__('jobs.tasks.fields.work_type')" :hint="__('jobs.tasks.fields.work_type_description')" required>
|
||||
<option value="">{{ __('jobs.fields.none_selected') }}</option>
|
||||
@foreach ($this->workTypeGroups as $group => $workTypes)
|
||||
<optgroup label="{{ $group }}">
|
||||
@foreach ($workTypes as $workType)
|
||||
<option value="{{ $workType['id'] }}">{{ $workType['name'] }}</option>
|
||||
@endforeach
|
||||
</optgroup>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
|
||||
<x-checkbox-group
|
||||
:legend="__('jobs.tasks.fields.assignees')"
|
||||
model="form.assignee_user_ids"
|
||||
:options="$this->userOptions"
|
||||
/>
|
||||
|
||||
<div class="grid gap-6 sm:grid-cols-3">
|
||||
<x-select.native wire:model="form.status" :label="__('jobs.tasks.fields.status')" :aria-label="__('jobs.tasks.fields.status')">
|
||||
@foreach (TaskStatus::cases() as $status)
|
||||
<option value="{{ $status->value }}">{{ __('jobs.tasks.statuses.'.$status->value) }}</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
|
||||
<x-input wire:model="form.due_on" type="date" :label="__('jobs.tasks.fields.due_on')" />
|
||||
|
||||
<x-input
|
||||
wire:model="form.estimated_hours"
|
||||
type="number"
|
||||
step="0.25"
|
||||
min="0"
|
||||
inputmode="decimal"
|
||||
:label="__('jobs.tasks.fields.estimated_hours')"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<x-textarea wire:model="form.description" :label="__('jobs.tasks.fields.description')" resize-auto />
|
||||
|
||||
<div class="flex flex-wrap gap-3">
|
||||
<x-button submit :text="__('jobs.save')" />
|
||||
<x-button :href="route('jobs.show', $jobId).'#tasks'" wire:navigate subtle color="black" :text="__('jobs.cancel')" />
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@@ -12,4 +12,6 @@ Route::middleware(['web', 'auth', 'can:jobs.view'])
|
||||
Route::livewire('/board', 'jobs::board')->name('board');
|
||||
Route::livewire('/{job}', 'jobs::show')->whereNumber('job')->name('show');
|
||||
Route::livewire('/{job}/edit', 'jobs::form')->whereNumber('job')->middleware('can:jobs.manage')->name('edit');
|
||||
Route::livewire('/{job}/tasks/create', 'jobs::task-form')->whereNumber('job')->name('tasks.create');
|
||||
Route::livewire('/{job}/tasks/{task}/edit', 'jobs::task-form')->whereNumber(['job', 'task'])->name('tasks.edit');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user