From b4907bc5e63f67c0a297d2b1d719657e34b1e603 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 13 Jul 2026 10:38:56 +0200 Subject: [PATCH] ref 3006. Updated creation software profile bug. --- .../OgAgent/Webhook/StatusController.php | 7 +++--- src/Dto/Input/SoftwareProfileInput.php | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Controller/OgAgent/Webhook/StatusController.php b/src/Controller/OgAgent/Webhook/StatusController.php index 7942d15..2214629 100644 --- a/src/Controller/OgAgent/Webhook/StatusController.php +++ b/src/Controller/OgAgent/Webhook/StatusController.php @@ -440,11 +440,13 @@ class StatusController extends AbstractController $existingNames = array_map(fn($software) => $software->getName(), $existingSoftware); $newSoftwareNames = array_diff($softwareList, $existingNames); + $newSoftware = []; foreach ($newSoftwareNames as $software) { $softwareEntity = new Software(); $softwareEntity->setName($software); $softwareEntity->setType(SoftwareTypes::APPLICATION); $this->entityManager->persist($softwareEntity); + $newSoftware[] = $softwareEntity; } $image = $imageImageRepository?->getImage(); @@ -452,13 +454,12 @@ class StatusController extends AbstractController $softwareProfile = new SoftwareProfile(); $softwareProfile->setDescription('SW Profile - ' . $image->getClient()->getName()); - foreach ($existingSoftware as $softwareEntity) { + foreach ([...$existingSoftware, ...$newSoftware] as $softwareEntity) { $softwareEntity->addSoftwareProfile($softwareProfile); } - $image->setSoftwareProfile($softwareProfile); + $imageImageRepository->setSoftwareProfile($softwareProfile); - $this->entityManager->persist($image); $this->entityManager->persist($imageImageRepository); $this->entityManager->persist($softwareProfile); $this->entityManager->flush(); diff --git a/src/Dto/Input/SoftwareProfileInput.php b/src/Dto/Input/SoftwareProfileInput.php index 79331fe..dba162f 100644 --- a/src/Dto/Input/SoftwareProfileInput.php +++ b/src/Dto/Input/SoftwareProfileInput.php @@ -5,6 +5,8 @@ namespace App\Dto\Input; use ApiPlatform\Metadata\ApiProperty; use App\Dto\Output\ImageOutput; use App\Dto\Output\OperativeSystemOutput; +use App\Dto\Output\SoftwareOutput; +use App\Entity\Software; use App\Entity\SoftwareProfile; use Symfony\Component\Serializer\Annotation\Groups; @@ -26,6 +28,12 @@ final class SoftwareProfileInput #[ApiProperty(description: 'The operative system of the software profile')] public ?OperativeSystemOutput $operativeSystem = null; + /** + * @var SoftwareOutput[] + */ + #[Groups(['software-profile:write'])] + #[ApiProperty(description: 'The software collection of the software profile', readableLink: true)] + public array $softwareCollection = []; public function __construct(?SoftwareProfile $softwareProfile = null) { @@ -42,6 +50,11 @@ final class SoftwareProfileInput if($softwareProfile->getOperativeSystem()) { $this->operativeSystem = new OperativeSystemOutput($softwareProfile->getOperativeSystem()); } + + $this->softwareCollection = array_map( + fn(Software $software) => new SoftwareOutput($software), + $softwareProfile->getSoftwareCollection()->toArray() + ); } public function createOrUpdateEntity(?SoftwareProfile $softwareProfile = null): SoftwareProfile @@ -58,6 +71,18 @@ final class SoftwareProfileInput $softwareProfile->setOperativeSystem($this->operativeSystem->getEntity()); } + $selectedSoftware = array_map(fn(SoftwareOutput $software) => $software->getEntity(), $this->softwareCollection); + + foreach ($softwareProfile->getSoftwareCollection()->toArray() as $existingSoftware) { + if (!in_array($existingSoftware, $selectedSoftware, true)) { + $softwareProfile->removeSoftwareCollection($existingSoftware); + } + } + + foreach ($selectedSoftware as $software) { + $softwareProfile->addSoftwareCollection($software); + } + return $softwareProfile; } } \ No newline at end of file