mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
validate thirdparty merge
This commit is contained in:
parent
58291c7402
commit
9979378e78
@ -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,10 +74,8 @@ 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);
|
||||
@ -94,5 +92,27 @@ class ThirdpartyDuplicateController extends AbstractController
|
||||
'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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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[] = [
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user