2828use App \Entity \Parts \Supplier ;
2929use App \Form \InfoProviderSystem \GlobalFieldMappingType ;
3030use App \Services \InfoProviderSystem \PartInfoRetriever ;
31- use App \Services \InfoProviderSystem \ProviderRegistry ;
3231use App \Services \InfoProviderSystem \ExistingPartFinder ;
3332use Doctrine \ORM \EntityManagerInterface ;
3433use Psr \Log \LoggerInterface ;
3736use Symfony \Component \HttpFoundation \Request ;
3837use Symfony \Component \HttpFoundation \Response ;
3938use Symfony \Component \Routing \Attribute \Route ;
39+ use App \Entity \UserSystem \User ;
4040
4141#[Route('/tools/bulk-info-provider-import ' )]
4242class BulkInfoProviderImportController extends AbstractController
4343{
4444 public function __construct (
45- private readonly ProviderRegistry $ providerRegistry ,
4645 private readonly PartInfoRetriever $ infoRetriever ,
4746 private readonly ExistingPartFinder $ existingPartFinder ,
4847 private readonly EntityManagerInterface $ entityManager
@@ -108,7 +107,11 @@ public function step1(Request $request, LoggerInterface $exceptionLogger): Respo
108107 $ job ->setPartIds (array_map (fn ($ part ) => $ part ->getId (), $ parts ));
109108 $ job ->setFieldMappings ($ fieldMappings );
110109 $ job ->setPrefetchDetails ($ prefetchDetails );
111- $ job ->setCreatedBy ($ this ->getUser ());
110+ $ user = $ this ->getUser ();
111+ if (!$ user instanceof User) {
112+ throw new \RuntimeException ('User must be authenticated and of type User ' );
113+ }
114+ $ job ->setCreatedBy ($ user );
112115
113116 $ this ->entityManager ->persist ($ job );
114117 $ this ->entityManager ->flush ();
@@ -124,6 +127,7 @@ public function step1(Request $request, LoggerInterface $exceptionLogger): Respo
124127
125128 // Collect all DTOs from all applicable field mappings
126129 $ allDtos = [];
130+ $ dtoMetadata = []; // Store source field info separately
127131
128132 foreach ($ fieldMappings as $ mapping ) {
129133 $ field = $ mapping ['field ' ];
@@ -142,10 +146,13 @@ public function step1(Request $request, LoggerInterface $exceptionLogger): Respo
142146 providers: $ providers
143147 );
144148
145- // Add field info to each DTO for tracking
149+ // Store field info for each DTO separately
146150 foreach ($ dtos as $ dto ) {
147- $ dto ->_source_field = $ field ;
148- $ dto ->_source_keyword = $ keyword ;
151+ $ dtoKey = $ dto ->provider_key . '| ' . $ dto ->provider_id ;
152+ $ dtoMetadata [$ dtoKey ] = [
153+ 'source_field ' => $ field ,
154+ 'source_keyword ' => $ keyword
155+ ];
149156 }
150157
151158 $ allDtos = array_merge ($ allDtos , $ dtos );
@@ -160,16 +167,28 @@ public function step1(Request $request, LoggerInterface $exceptionLogger): Respo
160167 $ uniqueDtos = [];
161168 $ seenKeys = [];
162169 foreach ($ allDtos as $ dto ) {
163- $ key = $ dto ->provider_key . '| ' . $ dto ->provider_id ;
164- if (!in_array ($ key , $ seenKeys )) {
170+ if ($ dto === null || !isset ($ dto ->provider_key , $ dto ->provider_id )) {
171+ continue ;
172+ }
173+ $ key = "{$ dto ->provider_key }| {$ dto ->provider_id }" ;
174+ if (!in_array ($ key , $ seenKeys , true )) {
165175 $ seenKeys [] = $ key ;
166176 $ uniqueDtos [] = $ dto ;
167177 }
168178 }
169179
170- // Convert DTOs to result format
180+ // Convert DTOs to result format with metadata
171181 $ partResult ['search_results ' ] = array_map (
172- fn ($ dto ) => ['dto ' => $ dto , 'localPart ' => $ this ->existingPartFinder ->findFirstExisting ($ dto )],
182+ function ($ dto ) use ($ dtoMetadata ) {
183+ $ dtoKey = $ dto ->provider_key . '| ' . $ dto ->provider_id ;
184+ $ metadata = $ dtoMetadata [$ dtoKey ] ?? [];
185+ return [
186+ 'dto ' => $ dto ,
187+ 'localPart ' => $ this ->existingPartFinder ->findFirstExisting ($ dto ),
188+ 'source_field ' => $ metadata ['source_field ' ] ?? null ,
189+ 'source_keyword ' => $ metadata ['source_keyword ' ] ?? null
190+ ];
191+ },
173192 $ uniqueDtos
174193 );
175194
@@ -182,7 +201,7 @@ public function step1(Request $request, LoggerInterface $exceptionLogger): Respo
182201 $ this ->entityManager ->flush ();
183202
184203 // Prefetch details if requested
185- if ($ prefetchDetails && ! empty ( $ searchResults ) ) {
204+ if ($ prefetchDetails ) {
186205 $ this ->prefetchDetailsForResults ($ searchResults , $ exceptionLogger );
187206 }
188207
@@ -387,8 +406,8 @@ private function serializeSearchResults(array $searchResults): array
387406 'mpn ' => $ dto ->mpn ,
388407 'provider_url ' => $ dto ->provider_url ,
389408 'preview_image_url ' => $ dto ->preview_image_url ,
390- '_source_field ' => $ dto -> _source_field ?? null ,
391- '_source_keyword ' => $ dto -> _source_keyword ?? null ,
409+ '_source_field ' => $ result [ ' source_field ' ] ?? null ,
410+ '_source_keyword ' => $ result [ ' source_keyword ' ] ?? null ,
392411 ],
393412 'localPart ' => $ result ['localPart ' ] ? $ result ['localPart ' ]->getId () : null
394413 ];
@@ -435,18 +454,16 @@ private function deserializeSearchResults(array $serializedResults, array $parts
435454 preview_image_url: $ dtoData ['preview_image_url ' ]
436455 );
437456
438- // Add the source field info
439- $ dto ->_source_field = $ dtoData ['_source_field ' ];
440- $ dto ->_source_keyword = $ dtoData ['_source_keyword ' ];
441-
442457 $ localPart = null ;
443458 if ($ resultData ['localPart ' ]) {
444459 $ localPart = $ this ->entityManager ->getRepository (Part::class)->find ($ resultData ['localPart ' ]);
445460 }
446461
447462 $ partResult ['search_results ' ][] = [
448463 'dto ' => $ dto ,
449- 'localPart ' => $ localPart
464+ 'localPart ' => $ localPart ,
465+ 'source_field ' => $ dtoData ['_source_field ' ] ?? null ,
466+ 'source_keyword ' => $ dtoData ['_source_keyword ' ] ?? null
450467 ];
451468 }
452469
0 commit comments