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\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Form\ThirdpartyFindDuplicateType;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Contracts\Translation\TranslatorInterface;
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"})
@ -74,25 +74,45 @@ class ThirdpartyDuplicateController extends AbstractController
#[Route(path: '/{_locale}/3party/{thirdparty1_id}/duplicate/{thirdparty2_id}/confirm', name: 'chill_thirdparty_duplicate_confirm')]
public function confirmAction(ThirdParty $thirdparty1, ThirdParty $thirdparty2, Request $request)
{
if ($thirdparty1 === $thirdparty2) {
throw new \InvalidArgumentException('Can not merge same thirdparty');
try {
$this->validateThirdpartyMerge($thirdparty1, $thirdparty2);
$form = $this->createForm(PersonConfimDuplicateType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->thirdPartyMergeService->merge($thirdparty1, $thirdparty2);
return $this->redirectToRoute('chill_crud_3party_3party_view', ['id' => $thirdparty1->getId()]);
}
return $this->render('@ChillThirdParty/ThirdPartyDuplicate/confirm.html.twig', [
'thirdparty' => $thirdparty1,
'thirdparty2' => $thirdparty2,
'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(),
]);
}
}
$form = $this->createForm(PersonConfimDuplicateType::class);
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'],
];
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->thirdPartyMergeService->merge($thirdparty1, $thirdparty2);
return $this->redirectToRoute('chill_crud_3party_3party_view', ['id' => $thirdparty1->getId()]);
foreach ($constraints as [$condition, $message]) {
if ($condition) {
throw new \InvalidArgumentException($message);
}
}
return $this->render('@ChillThirdParty/ThirdPartyDuplicate/confirm.html.twig', [
'thirdparty' => $thirdparty1,
'thirdparty2' => $thirdparty2,
'form' => $form->createView(),
]);
}
}

View File

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