From ab3fc73a18355eb2a75fc366c56ca09cf7f50778 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 20 Jan 2022 13:30:37 +0200 Subject: [PATCH] dev --- .../Crm/EntryPoints/SubscribeAgain.php | 63 +++++++--------- .../Modules/Crm/EntryPoints/Unsubscribe.php | 61 +++++++--------- .../Modules/Crm/Tools/MassEmail/Queue.php | 26 ++++--- .../Espo/Modules/Crm/Tools/MassEmail/Util.php | 73 +++++++++++++++++++ 4 files changed, 144 insertions(+), 79 deletions(-) create mode 100644 application/Espo/Modules/Crm/Tools/MassEmail/Util.php diff --git a/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php b/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php index d837609a33..ab8b436749 100644 --- a/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php +++ b/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php @@ -31,6 +31,8 @@ namespace Espo\Modules\Crm\EntryPoints; use Espo\Repositories\EmailAddress as EmailAddressRepository; +use Espo\Modules\Crm\Tools\MassEmail\Util as MassEmailUtil; + use Espo\Core\{ Exceptions\NotFound, Exceptions\BadRequest, @@ -80,13 +82,16 @@ class SubscribeAgain implements EntryPoint */ protected $hasher; + private MassEmailUtil $util; + public function __construct( EntityManager $entityManager, ClientManager $clientManager, HookManager $hookManager, Config $config, Metadata $metadata, - Hasher $hasher + Hasher $hasher, + MassEmailUtil $util ) { $this->entityManager = $entityManager; $this->clientManager = $clientManager; @@ -94,6 +99,7 @@ class SubscribeAgain implements EntryPoint $this->config = $config; $this->metadata = $metadata; $this->hasher = $hasher; + $this->util = $util; } public function run(Request $request, Response $response): void @@ -160,46 +166,33 @@ class SubscribeAgain implements EntryPoint } } - $link = null; + $link = $this->util->getLinkByEntityType($target->getEntityType()); - $m = [ - 'Account' => 'accounts', - 'Contact' => 'contacts', - 'Lead' => 'leads', - 'User' => 'users', - ]; + $targetListList = $this->entityManager + ->getRDBRepository('MassEmail') + ->getRelation($massEmail, 'targetLists') + ->find(); - if (!empty($m[$target->getEntityType()])) { - $link = $m[$target->getEntityType()]; - } + foreach ($targetListList as $targetList) { + $optedInResult = $this->entityManager + ->getRDBRepository('TargetList') + ->updateRelation($targetList, $link, $target->getId(), ['optedOut' => false]); - if ($link) { - $targetListList = $this->entityManager - ->getRDBRepository('MassEmail') - ->getRelation($massEmail, 'targetLists') - ->find(); + if ($optedInResult) { + $hookData = [ + 'link' => $link, + 'targetId' => $targetId, + 'targetType' => $targetType, + ]; - foreach ($targetListList as $targetList) { - $optedInResult = $this->entityManager - ->getRDBRepository('TargetList') - ->updateRelation($targetList, $link, $target->getId(), ['optedOut' => false]); - - if ($optedInResult) { - $hookData = [ - 'link' => $link, - 'targetId' => $targetId, - 'targetType' => $targetType, - ]; - - $this->hookManager - ->process('TargetList', 'afterCancelOptOut', $targetList, [], $hookData); - } + $this->hookManager + ->process('TargetList', 'afterCancelOptOut', $targetList, [], $hookData); } - - $this->hookManager->process($target->getEntityType(), 'afterCancelOptOut', $target, [], []); - - $this->display(['queueItemId' => $queueItemId]); } + + $this->hookManager->process($target->getEntityType(), 'afterCancelOptOut', $target, [], []); + + $this->display(['queueItemId' => $queueItemId]); } } } diff --git a/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php b/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php index 70f8d0359a..25fd50059c 100644 --- a/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php +++ b/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php @@ -32,6 +32,8 @@ namespace Espo\Modules\Crm\EntryPoints; use Espo\Modules\Crm\Services\Campaign as Service; use Espo\Repositories\EmailAddress as EmailAddressRepository; +use Espo\Modules\Crm\Tools\MassEmail\Util as MassEmailUtil; + use Espo\Core\{ Exceptions\NotFound, Exceptions\BadRequest, @@ -86,6 +88,8 @@ class Unsubscribe implements EntryPoint */ protected $service; + private MassEmailUtil $util; + public function __construct( EntityManager $entityManager, ClientManager $clientManager, @@ -93,7 +97,8 @@ class Unsubscribe implements EntryPoint Config $config, Metadata $metadata, Hasher $hasher, - Service $service + Service $service, + MassEmailUtil $util ) { $this->entityManager = $entityManager; $this->clientManager = $clientManager; @@ -102,6 +107,7 @@ class Unsubscribe implements EntryPoint $this->metadata = $metadata; $this->hasher = $hasher; $this->service = $service; + $this->util = $util; } public function run(Request $request, Response $response): void @@ -168,45 +174,32 @@ class Unsubscribe implements EntryPoint } } - $link = null; + $link = $this->util->getLinkByEntityType($target->getEntityType()); - $m = [ - 'Account' => 'accounts', - 'Contact' => 'contacts', - 'Lead' => 'leads', - 'User' => 'users', - ]; + $targetListList = $this->entityManager + ->getRDBRepository('MassEmail') + ->getRelation($massEmail, 'targetLists') + ->find(); - if (!empty($m[$target->getEntityType()])) { - $link = $m[$target->getEntityType()]; - } + foreach ($targetListList as $targetList) { + $optedOutResult = $this->entityManager + ->getRDBRepository('TargetList') + ->updateRelation($targetList, $link, $target->getId(), ['optedOut' => true]); - if ($link) { - $targetListList = $this->entityManager - ->getRDBRepository('MassEmail') - ->getRelation($massEmail, 'targetLists') - ->find(); + if ($optedOutResult) { + $hookData = [ + 'link' => $link, + 'targetId' => $targetId, + 'targetType' => $targetType, + ]; - foreach ($targetListList as $targetList) { - $optedOutResult = $this->entityManager - ->getRDBRepository('TargetList') - ->updateRelation($targetList, $link, $target->getId(), ['optedOut' => true]); - - if ($optedOutResult) { - $hookData = [ - 'link' => $link, - 'targetId' => $targetId, - 'targetType' => $targetType, - ]; - - $this->hookManager->process('TargetList', 'afterOptOut', $targetList, [], $hookData); - } + $this->hookManager->process('TargetList', 'afterOptOut', $targetList, [], $hookData); } - - $this->hookManager->process($target->getEntityType(), 'afterOptOut', $target, [], []); - - $this->display(['queueItemId' => $queueItemId]); } + + $this->hookManager->process($target->getEntityType(), 'afterOptOut', $target, [], []); + + $this->display(['queueItemId' => $queueItemId]); } } } diff --git a/application/Espo/Modules/Crm/Tools/MassEmail/Queue.php b/application/Espo/Modules/Crm/Tools/MassEmail/Queue.php index 57cc7e07fa..209509487d 100644 --- a/application/Espo/Modules/Crm/Tools/MassEmail/Queue.php +++ b/application/Espo/Modules/Crm/Tools/MassEmail/Queue.php @@ -37,22 +37,28 @@ use Espo\Modules\Crm\Entities\MassEmail; use Espo\Modules\Crm\Entities\EmailQueueItem; use Espo\Core\Exceptions\Error; -use Espo\Core\ORM\EntityManager; + +use Espo\ORM\EntityManager; + +use Espo\Core\Utils\Metadata; class Queue { - protected $targetsLinkList = [ - 'accounts', - 'contacts', - 'leads', - 'users', - ]; + /** + * @var string[] + */ + protected array $targetLinkList; protected EntityManager $entityManager; - public function __construct(EntityManager $entityManager) + private Metadata $metadata; + + public function __construct(EntityManager $entityManager, Metadata $metadata) { $this->entityManager = $entityManager; + $this->metadata = $metadata; + + $this->targetLinkList = $this->metadata->get(['scopes', 'TargetList', 'targetLinkList']) ?? []; } protected function cleanupQueueItems(MassEmail $massEmail): void @@ -96,7 +102,7 @@ class Queue ->find(); foreach ($excludingTargetListList as $excludingTargetList) { - foreach ($this->targetsLinkList as $link) { + foreach ($this->targetLinkList as $link) { $excludingList = $em->getRDBRepository(TargetList::ENTITY_TYPE) ->findRelated( $excludingTargetList, @@ -127,7 +133,7 @@ class Queue ->find(); foreach ($targetListCollection as $targetList) { - foreach ($this->targetsLinkList as $link) { + foreach ($this->targetLinkList as $link) { $recordList = $em->getRDBRepository('TargetList') ->getRelation($targetList, $link) ->select(['id', 'emailAddress']) diff --git a/application/Espo/Modules/Crm/Tools/MassEmail/Util.php b/application/Espo/Modules/Crm/Tools/MassEmail/Util.php new file mode 100644 index 0000000000..0ce5d725bb --- /dev/null +++ b/application/Espo/Modules/Crm/Tools/MassEmail/Util.php @@ -0,0 +1,73 @@ +ormDefs = $ormDefs; + $this->metadata = $metadata; + + $this->targetLinkList = $this->metadata->get(['scopes', 'TargetList', 'targetLinkList']) ?? []; + } + + public function getLinkByEntityType(string $entityType): string + { + foreach ($this->targetLinkList as $link) { + $itemEntityType = $this->ormDefs + ->getEntity(TargetList::ENTITY_TYPE) + ->getRelation($link) + ->getForeignEntityType(); + + if ($itemEntityType === $entityType) { + return $link; + } + } + + throw new Error(); + } +}