diff --git a/EventListener/ContactSubscriber.php b/EventListener/ContactSubscriber.php index 24cf17c35..267210d51 100644 --- a/EventListener/ContactSubscriber.php +++ b/EventListener/ContactSubscriber.php @@ -10,7 +10,6 @@ use Mautic\LeadBundle\Event\LeadMergeEvent; use Mautic\LeadBundle\Event\LeadTimelineEvent; use Mautic\LeadBundle\LeadEvents; -use MauticPlugin\CustomObjectsBundle\Entity\CustomItemXrefContact; use MauticPlugin\CustomObjectsBundle\Exception\NotFoundException; use MauticPlugin\CustomObjectsBundle\Model\CustomItemModel; use MauticPlugin\CustomObjectsBundle\Provider\ConfigProvider; @@ -38,7 +37,7 @@ public static function getSubscribedEvents(): array { return [ LeadEvents::TIMELINE_ON_GENERATE => 'onTimelineGenerate', - LeadEvents::LEAD_PRE_MERGE => 'onCongactPreMerge', + LeadEvents::LEAD_POST_MERGE => 'onContactMerge', ]; } @@ -74,41 +73,13 @@ public function onTimelineGenerate(LeadTimelineEvent $event): void } } - /** - * Moves the custom item links from the loser to the victor if they don't exist in the victor's list. - * Removes the remaining links from the loser. - */ - public function onCongactPreMerge(LeadMergeEvent $event): void + public function onContactMerge(LeadMergeEvent $event): void { if (!$this->configProvider->pluginIsEnabled()) { return; } - $loser = $event->getLoser(); - - /** @var CustomItemXrefContact[] $loserLinks */ - $loserLinks = $this->customItemXrefContactRepository->findBy(['contact' => $loser]); - - if (!$loserLinks) { - return; - } - - $victor = $event->getVictor(); - - /** @var CustomItemXrefContact[] $victorLinks */ - $victorLinks = $this->customItemXrefContactRepository->findBy(['contact' => $victor]); - $victorItemsIds = array_map(fn (CustomItemXrefContact $link) => $link->getCustomItem()->getId(), $victorLinks); - - foreach ($loserLinks as $loserLink) { - if (!in_array($loserLink->getCustomItem()->getId(), $victorItemsIds)) { - $newLink = new CustomItemXrefContact($loserLink->getCustomItem(), $victor, $loserLink->getDateAdded()); - $this->entityManager->persist($newLink); - } - - $this->entityManager->remove($loserLink); - } - - $this->entityManager->flush(); + $this->customItemXrefContactRepository->mergeLead($event->getVictor(), $event->getLoser()); } /** diff --git a/Repository/CustomItemXrefContactRepository.php b/Repository/CustomItemXrefContactRepository.php index 34d491e63..811f6281e 100644 --- a/Repository/CustomItemXrefContactRepository.php +++ b/Repository/CustomItemXrefContactRepository.php @@ -62,4 +62,32 @@ public function getContactIdsLinkedToCustomItem(int $customItemId, int $limit, i ->getQuery() ->getResult(); } + + public function mergeLead(Lead $victor, Lead $loser): void + { + // Move all custom item references to the victor lead, but only if the victor doesn't already have + // a reference to the custom item + $existingAlias = CustomItemXrefContact::TABLE_ALIAS.'_Check'; + + $this->createQueryBuilder(CustomItemXrefContact::TABLE_ALIAS) + ->update() + ->set(CustomItemXrefContact::TABLE_ALIAS.'.contact', ':victor') + ->where(CustomItemXrefContact::TABLE_ALIAS.'.contact = :loser') + ->andWhere( + $this->createQueryBuilder(CustomItemXrefContact::TABLE_ALIAS) + ->expr() + ->notIn( + CustomItemXrefContact::TABLE_ALIAS.'.customItem', + $this->createQueryBuilder($existingAlias) + ->select('IDENTITY('.$existingAlias.'.customItem)') + ->where($existingAlias.'.contact = :victor') + ->getDQL() + ) + ) + ->setParameter('victor', $victor->getId()) + ->setParameter('loser', $loser->getId()) + ->getQuery() + ->execute(); + + } }