diff --git a/src/Bundle/ChillThirdPartyBundle/Controller/ThirdpartyDuplicateController.php b/src/Bundle/ChillThirdPartyBundle/Controller/ThirdpartyDuplicateController.php index a7fb87458..0c0671aaf 100644 --- a/src/Bundle/ChillThirdPartyBundle/Controller/ThirdpartyDuplicateController.php +++ b/src/Bundle/ChillThirdPartyBundle/Controller/ThirdpartyDuplicateController.php @@ -21,7 +21,7 @@ use Symfony\Component\Routing\Annotation\Route; class ThirdpartyDuplicateController extends AbstractController { - public function __construct(private ThirdPartyRepository $thirdPartyRepository, private ThirdpartyMergeService $thirdPartyMergeService) {} + public function __construct(private readonly ThirdPartyRepository $thirdPartyRepository, private readonly ThirdpartyMergeService $thirdPartyMergeService) {} #[Route(path: '/{_locale}/3party/{thirdparty_id}/find-manually', name: 'chill_thirdparty_find_duplicate')] public function findManuallyDuplicateAction(mixed $thirdparty_id, Request $request) @@ -41,10 +41,6 @@ class ThirdpartyDuplicateController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $thirdparty2 = $form->get('thirdparty')->getData(); - if (null === $thirdparty2) { - throw $this->createNotFoundException("Thirdparty with id {$thirdparty2->getId}() not".' found on this server'); - } - $direction = $form->get('direction')->getData(); if ('starting' === $direction) { diff --git a/src/Bundle/ChillThirdPartyBundle/Form/ThirdpartyFindDuplicateType.php b/src/Bundle/ChillThirdPartyBundle/Form/ThirdpartyFindDuplicateType.php index 8b2534bca..e16cddf54 100644 --- a/src/Bundle/ChillThirdPartyBundle/Form/ThirdpartyFindDuplicateType.php +++ b/src/Bundle/ChillThirdPartyBundle/Form/ThirdpartyFindDuplicateType.php @@ -1,5 +1,14 @@ ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()] + 'params' => ['toDelete' => $toDelete->getId(), 'toKeep' => $toKeep->getId()], ]; } @@ -86,24 +86,40 @@ class ThirdpartyMergeService continue; } - if ($assoc['type'] & ClassMetadata::TO_ONE) { + if (ClassMetadata::TO_ONE === $assoc['type']) { $joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']); - $sql = "UPDATE {$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete"; - $queries[] = ['sql' => $sql, 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()]]; - } + if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) { + // TODO what with 'address_id' and 'civility_id'? This condition also contains columns like updatedBy_id which we want to ignore... + continue; + } + + $schemaPrefix = (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' === $joinColumn) + ? 'chill_3party.' + : ''; - if ($assoc['type'] & ClassMetadata::TO_MANY && isset($assoc['joinTable'])) { - $joinTable = $assoc['joinTable']['name']; - $joinColumn = ThirdParty::class === $assoc['targetEntity'] - ? $assoc['joinTable']['inverseJoinColumns'][0]['name'] - : $assoc['joinTable']['joinColumns'][0]['name']; -//TODO problem remaining for ManyToMany relations when UPDATE triggers a UNIQUE CONSTRAINT...in the case of Thirdparty -> thirdparty_category and thirdparty_center $queries[] = [ - 'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", + 'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], ]; } + + if (ClassMetadata::TO_MANY === $assoc['type'] && isset($assoc['joinTable'])) { + + $joinTable = $assoc['joinTable']['name']; + $joinColumn = $assoc['joinTable']['joinColumns'][0]['name']; + if ('thirdparty_id' === $joinColumn) { + $queries[] = [ + 'sql' => "DELETE FROM {$joinTable} WHERE {$joinColumn} = :toDelete", + 'params' => ['toDelete' => $toDelete->getId()], + ]; + } else { + $queries[] = [ + 'sql' => "UPDATE {$joinTable} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", + 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], + ]; + } + } } } diff --git a/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php b/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php index de11cea7a..42e7ec0d1 100644 --- a/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php +++ b/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php @@ -1,5 +1,14 @@ connection->rollBack(); - } catch (\Exception $e) { + } catch (\Exception) { $this->connection->close(); } @@ -47,7 +61,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase $toKeep->setName('Thirdparty ToKeep'); $toKeep->setEmail('keep@example.com'); - $toDelete = new Thirdparty(); + $toDelete = new ThirdParty(); $toDelete->setName('Thirdparty ToDelete'); // This should be ignored $toDelete->setTelephone(new PhoneNumber('123456789')); @@ -69,7 +83,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase $this->em->clear(); // Verify data was merged correctly - $mergedThirdparty = $this->em->getRepository(Thirdparty::class)->find($toKeep->getId()); + $mergedThirdparty = $this->em->getRepository(ThirdParty::class)->find($toKeep->getId()); $this->assertNotNull($mergedThirdparty); $this->assertEquals('Primary Name', $mergedThirdparty->getName(), 'Name should remain unchanged'); $this->assertEquals('keep@example.com', $mergedThirdparty->getEmail(), 'Email should remain unchanged'); @@ -82,7 +96,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase 'Activity should be linked to the merged Thirdparty' ); $this->assertFalse( - $updatedActivity->getThirdParties()->exists(fn($key, $tp) => $tp->getId() === $toDelete->getId()), + $updatedActivity->getThirdParties()->exists(fn ($key, $tp) => $tp->getId() === $toDelete->getId()), 'Activity should no longer reference the deleted Thirdparty' ); @@ -94,7 +108,7 @@ class ThirdpartyMergeServiceTest extends KernelTestCase ); // Ensure the 'toDelete' entity is removed - $deletedThirdparty = $this->em->getRepository(Thirdparty::class)->find($toDelete->getId()); + $deletedThirdparty = $this->em->getRepository(ThirdParty::class)->find($toDelete->getId()); $this->assertNull($deletedThirdparty, 'The deleted Thirdparty should no longer exist in the database'); } }