Skip to content
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

fix: Contact merges now correctly merge custom items #329

Open
wants to merge 1 commit into
base: staging
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
1 change: 1 addition & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@
'custom_item.route.provider',
'mautic.custom.model.item',
'custom_object.config.provider',
'custom_item.xref.contact.repository',
],
],
'custom_item.post_save.subscriber' => [
Expand Down
21 changes: 20 additions & 1 deletion EventListener/ContactSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Doctrine\ORM\EntityManager;
use Mautic\LeadBundle\Entity\LeadEventLog;
use Mautic\LeadBundle\Entity\LeadEventLogRepository;
use Mautic\LeadBundle\Event\LeadMergeEvent;
use Mautic\LeadBundle\Event\LeadTimelineEvent;
use Mautic\LeadBundle\LeadEvents;
use MauticPlugin\CustomObjectsBundle\Exception\NotFoundException;
use MauticPlugin\CustomObjectsBundle\Model\CustomItemModel;
use MauticPlugin\CustomObjectsBundle\Provider\ConfigProvider;
use MauticPlugin\CustomObjectsBundle\Provider\CustomItemRouteProvider;
use MauticPlugin\CustomObjectsBundle\Repository\CustomItemXrefContactRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Translation\TranslatorInterface;

Expand Down Expand Up @@ -43,18 +45,25 @@ class ContactSubscriber implements EventSubscriberInterface
*/
private $configProvider;

/**
* @var CustomItemXrefContactRepository
*/
private $customItemXrefContactRepository;

public function __construct(
EntityManager $entityManager,
TranslatorInterface $translator,
CustomItemRouteProvider $routeProvider,
CustomItemModel $customItemModel,
ConfigProvider $configProvider
ConfigProvider $configProvider,
CustomItemXrefContactRepository $customItemXrefContactRepository,
) {
$this->entityManager = $entityManager;
$this->translator = $translator;
$this->routeProvider = $routeProvider;
$this->customItemModel = $customItemModel;
$this->configProvider = $configProvider;
$this->customItemXrefContactRepository = $customItemXrefContactRepository;
}

/**
Expand All @@ -64,6 +73,7 @@ public static function getSubscribedEvents(): array
{
return [
LeadEvents::TIMELINE_ON_GENERATE => 'onTimelineGenerate',
LeadEvents::LEAD_POST_MERGE => 'onContactMerge',
];
}

Expand Down Expand Up @@ -99,6 +109,15 @@ public function onTimelineGenerate(LeadTimelineEvent $event): void
}
}

public function onContactMerge(LeadMergeEvent $event): void
{
if (!$this->configProvider->pluginIsEnabled()) {
return;
}

$this->customItemXrefContactRepository->mergeLead($event->getVictor(), $event->getLoser());
}

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

}
}