mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-20 17:15:51 +02:00
Rename duplicate parameters and attachments when importing from an info provider
This fixes issue #840
This commit is contained in:
parent
2d3d05e956
commit
42ecb83155
2 changed files with 53 additions and 7 deletions
|
@ -174,9 +174,21 @@ final class DTOtoEntityConverter
|
|||
//Set the provider reference on the part
|
||||
$entity->setProviderReference(InfoProviderReference::fromPartDTO($dto));
|
||||
|
||||
$param_groups = [];
|
||||
|
||||
//Add parameters
|
||||
foreach ($dto->parameters ?? [] as $parameter) {
|
||||
$entity->addParameter($this->convertParameter($parameter));
|
||||
$new_param = $this->convertParameter($parameter);
|
||||
|
||||
$key = $new_param->getName() . '##' . $new_param->getGroup();
|
||||
//If there is already an parameter with the same name and group, rename the new parameter, by suffixing a number
|
||||
if (count($param_groups[$key] ?? []) > 0) {
|
||||
$new_param->setName($new_param->getName() . ' (' . (count($param_groups[$key]) + 1) . ')');
|
||||
}
|
||||
|
||||
$param_groups[$key][] = $new_param;
|
||||
|
||||
$entity->addParameter($new_param);
|
||||
}
|
||||
|
||||
//Add preview image
|
||||
|
@ -192,6 +204,8 @@ final class DTOtoEntityConverter
|
|||
$entity->setMasterPictureAttachment($preview_image);
|
||||
}
|
||||
|
||||
$attachments_grouped = [];
|
||||
|
||||
//Add other images
|
||||
$images = $this->files_unique($dto->images ?? []);
|
||||
foreach ($images as $image) {
|
||||
|
@ -200,14 +214,29 @@ final class DTOtoEntityConverter
|
|||
continue;
|
||||
}
|
||||
|
||||
$entity->addAttachment($this->convertFile($image, $image_type));
|
||||
$attachment = $this->convertFile($image, $image_type);
|
||||
|
||||
$attachments_grouped[$attachment->getName()][] = $attachment;
|
||||
if (count($attachments_grouped[$attachment->getName()] ?? []) > 1) {
|
||||
$attachment->setName($attachment->getName() . ' (' . (count($attachments_grouped[$attachment->getName()]) + 1) . ')');
|
||||
}
|
||||
|
||||
|
||||
$entity->addAttachment($attachment);
|
||||
}
|
||||
|
||||
//Add datasheets
|
||||
$datasheet_type = $this->getDatasheetType();
|
||||
$datasheets = $this->files_unique($dto->datasheets ?? []);
|
||||
foreach ($datasheets as $datasheet) {
|
||||
$entity->addAttachment($this->convertFile($datasheet, $datasheet_type));
|
||||
$attachment = $this->convertFile($datasheet, $datasheet_type);
|
||||
|
||||
$attachments_grouped[$attachment->getName()][] = $attachment;
|
||||
if (count($attachments_grouped[$attachment->getName()] ?? []) > 1) {
|
||||
$attachment->setName($attachment->getName() . ' (' . (count($attachments_grouped[$attachment->getName()])) . ')');
|
||||
}
|
||||
|
||||
$entity->addAttachment($attachment);
|
||||
}
|
||||
|
||||
//Add orderdetails and prices
|
||||
|
|
|
@ -30,6 +30,7 @@ use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
|
|||
use App\Services\InfoProviderSystem\DTOs\PriceDTO;
|
||||
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
|
||||
use App\Services\InfoProviderSystem\DTOtoEntityConverter;
|
||||
use PhpParser\Node\Param;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
|
@ -150,9 +151,9 @@ class DTOtoEntityConverterTest extends WebTestCase
|
|||
|
||||
public function testConvertPart(): void
|
||||
{
|
||||
$parameters = [new ParameterDTO('Test', 'Test')];
|
||||
$datasheets = [new FileDTO('https://invalid.invalid/file.pdf'), new FileDTO('https://invalid.invalid/file.pdf', name: 'TestFile')];
|
||||
$images = [new FileDTO('https://invalid.invalid/image.png'), new FileDTO('https://invalid.invalid/image2.png', name: 'TestImage2'), new FileDTO('https://invalid.invalid/image2.png')];
|
||||
$parameters = [new ParameterDTO('Test', 'Test'), new ParameterDTO('Duplicate', 'Test'), new ParameterDTO('Test', 'test', group: "Other"), new ParameterDTO('Duplicate', 'ds')];
|
||||
$datasheets = [new FileDTO('https://invalid.invalid/file.pdf'), new FileDTO('https://invalid.invalid/file.pdf', name: 'TestFile'), new FileDTO('https://invalid.invalid/file2.pdf', name: 'Duplicate'), new FileDTO('https://invalid.invalid/file3.pdf', name: 'Duplicate')];
|
||||
$images = [new FileDTO('https://invalid.invalid/image.png'), new FileDTO('https://invalid.invalid/image2.png', name: 'TestImage2'), new FileDTO('https://invalid.invalid/image3.png', name: "Duplicate")];
|
||||
$shopping_infos = [new PurchaseInfoDTO('TestDistributor', 'TestOrderNumber', [new PriceDTO(1, "10.0", 'EUR')])];
|
||||
|
||||
$dto = new PartDetailDTO(
|
||||
|
@ -182,15 +183,31 @@ class DTOtoEntityConverterTest extends WebTestCase
|
|||
$this->assertCount(count($parameters), $entity->getParameters());
|
||||
$this->assertCount(count($shopping_infos), $entity->getOrderdetails());
|
||||
|
||||
//Test that duplicate parameters get renamed:
|
||||
$this->assertSame('Test', $entity->getParameters()[0]->getName());
|
||||
$this->assertSame('Duplicate', $entity->getParameters()[1]->getName());
|
||||
$this->assertSame('Test', $entity->getParameters()[2]->getName());
|
||||
$this->assertSame('Duplicate (2)', $entity->getParameters()[3]->getName());
|
||||
|
||||
//Datasheets and images are stored as attachments and the duplicates, should be filtered out
|
||||
$this->assertCount(3, $entity->getAttachments());
|
||||
$this->assertCount(6, $entity->getAttachments());
|
||||
//The attachments should have the name of the named duplicate file
|
||||
$image1 = $entity->getAttachments()[0];
|
||||
$this->assertSame('Main image', $image1->getName());
|
||||
|
||||
$image1 = $entity->getAttachments()[1];
|
||||
$this->assertSame('TestImage2', $image1->getName());
|
||||
|
||||
$datasheet = $entity->getAttachments()[2];
|
||||
$this->assertSame('Duplicate', $datasheet->getName());
|
||||
|
||||
$datasheet = $entity->getAttachments()[3];
|
||||
$this->assertSame('TestFile', $datasheet->getName());
|
||||
|
||||
$datasheet = $entity->getAttachments()[4];
|
||||
$this->assertSame('Duplicate (2)', $datasheet->getName());
|
||||
|
||||
$datasheet = $entity->getAttachments()[5];
|
||||
$this->assertSame('Duplicate (3)', $datasheet->getName());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue