diff --git a/config/services/api_platform.yaml b/config/services/api_platform.yaml index 466d458..4441b4e 100644 --- a/config/services/api_platform.yaml +++ b/config/services/api_platform.yaml @@ -8,7 +8,7 @@ services: api_platform.filter.client.search: parent: 'api_platform.doctrine.orm.search_filter' - arguments: [ { 'id': 'exact', 'name': 'partial', 'serialNumber': 'exact', organizationalUnit.id: 'exact' } ] + arguments: [ { 'id': 'exact', 'name': 'partial', 'serialNumber': 'exact', organizationalUnit.id: 'exact', mac: 'exact', ip: 'exact' } ] tags: [ 'api_platform.filter' ] api_platform.filter.command.order: diff --git a/src/Dto/Input/CommandTaskInput.php b/src/Dto/Input/CommandTaskInput.php index dd6b89a..c7e08e7 100644 --- a/src/Dto/Input/CommandTaskInput.php +++ b/src/Dto/Input/CommandTaskInput.php @@ -3,6 +3,7 @@ namespace App\Dto\Input; use ApiPlatform\Metadata\ApiProperty; +use App\Dto\Output\ClientOutput; use App\Dto\Output\CommandGroupOutput; use App\Dto\Output\CommandOutput; use App\Entity\CommandTask; @@ -33,6 +34,16 @@ final class CommandTaskInput )] public array $commandGroups = []; + /** + * @var ClientOutput[] + */ + #[Groups(['command-task:write'])] + #[ApiProperty( + description: 'Los clientes de la tarea', + example: 'Clientes de la tarea', + )] + public array $clients = []; + #[Assert\NotBlank(message: 'validators.command_task.datetime.not_blank')] #[Groups(['command-task:write'])] #[ApiProperty( @@ -66,6 +77,12 @@ final class CommandTaskInput } } + if ($commandTask->getClients()) { + foreach ($commandTask->getClients() as $client) { + $this->clients[] = new ClientOutput($client); + } + } + $this->dateTime = $commandTask->getDatetime(); $this->notes = $commandTask->getNotes(); } @@ -87,6 +104,13 @@ final class CommandTaskInput } $commandTask->setCommandGroups( $commandGroupsToAdd ?? [] ); + + foreach ($this->clients as $client) { + $clientsToAdd[] = $client->getEntity(); + } + + $commandTask->setClients( $clientsToAdd ?? [] ); + $commandTask->setDatetime($this->dateTime); $commandTask->setStatus(CommandTaskStatus::PENDING); $commandTask->setNotes($this->notes); diff --git a/src/Dto/Output/CommandTaskOutput.php b/src/Dto/Output/CommandTaskOutput.php index c5852fe..76c519c 100644 --- a/src/Dto/Output/CommandTaskOutput.php +++ b/src/Dto/Output/CommandTaskOutput.php @@ -3,6 +3,7 @@ namespace App\Dto\Output; use ApiPlatform\Metadata\Get; +use App\Entity\Client; use App\Entity\Command; use App\Entity\CommandGroup; use App\Entity\CommandTask; @@ -17,6 +18,9 @@ final class CommandTaskOutput extends AbstractOutput #[Groups(['command-task:read'])] public array $commandGroups = []; + #[Groups(['command-task:read'])] + public array $clients = []; + #[Groups(['command-task:read'])] public \DateTimeInterface $dateTime; @@ -45,6 +49,10 @@ final class CommandTaskOutput extends AbstractOutput fn(CommandGroup $commandGroup) => new CommandGroupOutput($commandGroup) )->toArray(); + $this->clients = $commandTask->getClients()->map( + fn(Client $client) => new ClientOutput($client) + )->toArray(); + $this->dateTime = $commandTask->getDateTime(); $this->notes = $commandTask->getNotes(); $this->status = $commandTask->getStatus(); diff --git a/src/Entity/CommandTask.php b/src/Entity/CommandTask.php index 8335067..b742212 100644 --- a/src/Entity/CommandTask.php +++ b/src/Entity/CommandTask.php @@ -32,12 +32,19 @@ class CommandTask extends AbstractEntity #[ORM\Column(length: 255)] private ?string $status = null; + /** + * @var Collection + */ + #[ORM\ManyToMany(targetEntity: Client::class)] + private Collection $clients; + public function __construct() { parent::__construct(); $this->commands = new ArrayCollection(); $this->commandGroups = new ArrayCollection(); + $this->clients = new ArrayCollection(); } /** @@ -145,4 +152,39 @@ class CommandTask extends AbstractEntity return $this; } + + /** + * @return Collection + */ + public function getClients(): Collection + { + return $this->clients; + } + + public function addClient(Client $client): static + { + if (!$this->clients->contains($client)) { + $this->clients->add($client); + } + + return $this; + } + + public function removeClient(Client $client): static + { + $this->clients->removeElement($client); + + return $this; + } + + public function setClients(array $clients): static + { + $this->clients->clear(); + + foreach ($clients as $client){ + $this->addClient($client); + } + + return $this; + } }