ref 3006. Updated creation software profile bug.

pull/125/head
Manuel Aranda Rosales 2026-07-13 10:38:56 +02:00
parent 3390734b10
commit b4907bc5e6
2 changed files with 29 additions and 3 deletions

View File

@ -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();

View File

@ -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;
}
}