validate thirdparty merge

This commit is contained in:
Julie Lenaerts 2025-03-03 20:52:05 +01:00
parent 58291c7402
commit 9979378e78
3 changed files with 42 additions and 22 deletions

View File

@ -14,16 +14,16 @@ namespace Chill\ThirdPartyBundle\Controller;
use Chill\PersonBundle\Form\PersonConfimDuplicateType; use Chill\PersonBundle\Form\PersonConfimDuplicateType;
use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Form\ThirdpartyFindDuplicateType; use Chill\ThirdPartyBundle\Form\ThirdpartyFindDuplicateType;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService; use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Contracts\Translation\TranslatorInterface;
class ThirdpartyDuplicateController extends AbstractController class ThirdpartyDuplicateController extends AbstractController
{ {
public function __construct(private readonly ThirdPartyRepository $thirdPartyRepository, private readonly ThirdpartyMergeService $thirdPartyMergeService) {} public function __construct(private readonly ThirdpartyMergeService $thirdPartyMergeService, private readonly TranslatorInterface $translator) {}
/** /**
* @ParamConverter("thirdparty", options={"id": "thirdparty_id"}) * @ParamConverter("thirdparty", options={"id": "thirdparty_id"})
@ -74,10 +74,8 @@ class ThirdpartyDuplicateController extends AbstractController
#[Route(path: '/{_locale}/3party/{thirdparty1_id}/duplicate/{thirdparty2_id}/confirm', name: 'chill_thirdparty_duplicate_confirm')] #[Route(path: '/{_locale}/3party/{thirdparty1_id}/duplicate/{thirdparty2_id}/confirm', name: 'chill_thirdparty_duplicate_confirm')]
public function confirmAction(ThirdParty $thirdparty1, ThirdParty $thirdparty2, Request $request) public function confirmAction(ThirdParty $thirdparty1, ThirdParty $thirdparty2, Request $request)
{ {
if ($thirdparty1 === $thirdparty2) { try {
throw new \InvalidArgumentException('Can not merge same thirdparty'); $this->validateThirdpartyMerge($thirdparty1, $thirdparty2);
}
$form = $this->createForm(PersonConfimDuplicateType::class); $form = $this->createForm(PersonConfimDuplicateType::class);
$form->handleRequest($request); $form->handleRequest($request);
@ -94,5 +92,27 @@ class ThirdpartyDuplicateController extends AbstractController
'thirdparty2' => $thirdparty2, 'thirdparty2' => $thirdparty2,
'form' => $form->createView(), 'form' => $form->createView(),
]); ]);
} catch (\InvalidArgumentException $e) {
$this->addFlash('error', $this->translator->trans($e->getMessage()));
return $this->redirectToRoute('chill_thirdparty_find_duplicate', [
'thirdparty_id' => $thirdparty1->getId(),
]);
}
}
private function validateThirdpartyMerge(Thirdparty $thirdparty1, Thirdparty $thirdparty2): void
{
$constraints = [
[$thirdparty1 === $thirdparty2, 'thirdparty_duplicate.You cannot merge a thirdparty with itself. Please choose a different thirdparty'],
[$thirdparty1->getKind() !== $thirdparty2->getKind(), 'thirdparty_duplicate.A thirdparty can only be merged with a thirdparty of the same kind'],
[$thirdparty1->getParent() !== $thirdparty2->getParent(), 'thirdparty_duplicate.Two child thirdparties must have the same parent'],
];
foreach ($constraints as [$condition, $message]) {
if ($condition) {
throw new \InvalidArgumentException($message);
}
}
} }
} }

View File

@ -90,7 +90,6 @@ class ThirdpartyMergeService
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']); $joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) { 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; continue;
} }
@ -102,9 +101,7 @@ class ThirdpartyMergeService
'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", 'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
]; ];
} } elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) {
if ($assoc['type'] === 8 && isset($assoc['joinTable'])) {
$joinTable = $assoc['joinTable']['name']; $joinTable = $assoc['joinTable']['name'];
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name']; $joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
$queries[] = [ $queries[] = [

View File

@ -141,3 +141,6 @@ thirdparty_duplicate:
Thirdparty to delete explanation: Ce tiers sera supprimé. Seuls les contacts de ce tiers, énumérés ci-dessous, seront transférés. Thirdparty to delete explanation: Ce tiers sera supprimé. Seuls les contacts de ce tiers, énumérés ci-dessous, seront transférés.
Thirdparty to keep explanation: Ce tiers sera conserver Thirdparty to keep explanation: Ce tiers sera conserver
Data to keep: Données conservées Data to keep: Données conservées
You cannot merge a thirdparty with itself. Please choose a different thirdparty: Vous ne pouvez pas fusionner un tiers avec lui-même. Veuillez choisir un autre tiers.
A thirdparty can only be merged with a thirdparty of the same kind: Un tiers ne peut être fusionné qu'avec un tiers de même type.
Two child thirdparties must have the same parent: Deux tiers de type « contact » doivent avoir le même tiers parent.