From 5ab129bd9b50b3811ac7b834d4dba31d7ad702c9 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 23 Feb 2023 13:06:09 +0200 Subject: [PATCH] ref --- .../Modules/Crm/Hooks/CaseObj/Contacts.php | 135 ++++++++++++++++++ .../Espo/Modules/Crm/Repositories/CaseObj.php | 84 +---------- 2 files changed, 137 insertions(+), 82 deletions(-) create mode 100644 application/Espo/Modules/Crm/Hooks/CaseObj/Contacts.php diff --git a/application/Espo/Modules/Crm/Hooks/CaseObj/Contacts.php b/application/Espo/Modules/Crm/Hooks/CaseObj/Contacts.php new file mode 100644 index 0000000000..ffcab49a84 --- /dev/null +++ b/application/Espo/Modules/Crm/Hooks/CaseObj/Contacts.php @@ -0,0 +1,135 @@ + + */ +class Contacts implements AfterSave +{ + private ?StreamService $streamService = null; + + public function __construct( + private EntityManager $entityManager, + private InjectableFactory $injectableFactory + ) {} + + /** + * @param CaseObj $entity + */ + public function afterSave(Entity $entity, SaveOptions $options): void + { + if (!$entity->isAttributeChanged('contactId')) { + return; + } + + $contactId = $entity->get('contactId'); + $contactIdList = $entity->get('contactsIds') ?? []; + $fetchedContactId = $entity->getFetched('contactId'); + + $relation = $this->entityManager + ->getRDBRepositoryByClass(CaseObj::class) + ->getRelation($entity, 'contacts'); + + if ($fetchedContactId) { + $previousPortalUser = $this->entityManager + ->getRDBRepository(User::ENTITY_TYPE) + ->select(['id']) + ->where([ + 'contactId' => $fetchedContactId, + 'type' => User::TYPE_PORTAL, + ]) + ->findOne(); + + if ($previousPortalUser) { + $this->getStreamService()->unfollowEntity($entity, $previousPortalUser->getId()); + } + } + + if (!$contactId && $fetchedContactId) { + $relation->unrelate($fetchedContactId); + + return; + } + + if (!$contactId) { + return; + } + + $portalUser = $this->entityManager + ->getRDBRepository(User::ENTITY_TYPE) + ->select(['id']) + ->where([ + 'contactId' => $contactId, + 'type' => User::TYPE_PORTAL, + 'isActive' => true, + ]) + ->findOne(); + + if ($portalUser) { + $this->getStreamService()->followEntity($entity, $portalUser->getId(), true); + } + + if (in_array($contactId, $contactIdList)) { + return; + } + + $contact = $this->entityManager->getEntityById(Contact::ENTITY_TYPE, $contactId); + + if (!$contact) { + return; + } + + if ($relation->isRelated($contact)) { + return; + } + + $relation->relate($contactId); + } + + private function getStreamService(): StreamService + { + if (!$this->streamService) { + $this->streamService = $this->injectableFactory->create(StreamService::class); + } + + return $this->streamService; + } +} diff --git a/application/Espo/Modules/Crm/Repositories/CaseObj.php b/application/Espo/Modules/Crm/Repositories/CaseObj.php index cb06d97368..38762e7801 100644 --- a/application/Espo/Modules/Crm/Repositories/CaseObj.php +++ b/application/Espo/Modules/Crm/Repositories/CaseObj.php @@ -30,89 +30,9 @@ namespace Espo\Modules\Crm\Repositories; use Espo\Core\Repositories\Database; -use Espo\Entities\User; -use Espo\ORM\Entity; -use Espo\Tools\Stream\Service as StreamService; -use Espo\Core\Di; /** * @extends Database<\Espo\Modules\Crm\Entities\CaseObj> */ -class CaseObj extends Database implements - Di\InjectableFactoryAware -{ - use Di\InjectableFactorySetter; - - protected ?StreamService $streamService = null; - - protected function afterSave(Entity $entity, array $options = []) - { - parent::afterSave($entity, $options); - - $this->handleAfterSaveContacts($entity); - } - - protected function getStreamService(): StreamService - { - if (!$this->streamService) { - $this->streamService = $this->injectableFactory->create(StreamService::class); - } - - return $this->streamService; - } - - /** - * @todo Move to hooks. - */ - protected function handleAfterSaveContacts(Entity $entity): void - { - if (!$entity->isAttributeChanged('contactId')) { - return; - } - - $contactId = $entity->get('contactId'); - $contactIdList = $entity->get('contactsIds') ?? []; - $fetchedContactId = $entity->getFetched('contactId'); - - if ($fetchedContactId) { - $previousPortalUser = $this->entityManager - ->getRDBRepository(User::ENTITY_TYPE) - ->select(['id']) - ->where([ - 'contactId' => $fetchedContactId, - 'type' => User::TYPE_PORTAL, - ]) - ->findOne(); - - if ($previousPortalUser) { - $this->getStreamService()->unfollowEntity($entity, $previousPortalUser->getId()); - } - } - - if (!$contactId) { - if ($fetchedContactId) { - $this->unrelate($entity, 'contacts', $fetchedContactId); - } - - return; - } - - $portalUser = $this->entityManager - ->getRDBRepository(User::ENTITY_TYPE) - ->select(['id']) - ->where([ - 'contactId' => $contactId, - 'type' => User::TYPE_PORTAL, - 'isActive' => true, - ]) - ->findOne(); - - if ($portalUser) { - $this->getStreamService()->followEntity($entity, $portalUser->getId(), true); - } - - if (!in_array($contactId, $contactIdList) && !$this->isRelated($entity, 'contacts', $contactId)) { - $this->relate($entity, 'contacts', $contactId); - } - } -} +class CaseObj extends Database +{}