Skip to content

feat: Improve contact merge to use database query instead of loading potentially many many items #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 3 additions & 32 deletions EventListener/ContactSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
];
}

Expand Down Expand Up @@ -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());
}

/**
Expand Down
28 changes: 28 additions & 0 deletions Repository/CustomItemXrefContactRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
}
Loading