From fabaccd3da2f37acf42e6efd4876392adf2f760e Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 11 Jan 2024 12:13:11 +0200 Subject: [PATCH] case compose email support person --- .../Espo/Modules/Crm/Tools/Case/Service.php | 88 ++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/application/Espo/Modules/Crm/Tools/Case/Service.php b/application/Espo/Modules/Crm/Tools/Case/Service.php index 9627076cf2..68854b9e2a 100644 --- a/application/Espo/Modules/Crm/Tools/Case/Service.php +++ b/application/Espo/Modules/Crm/Tools/Case/Service.php @@ -36,12 +36,16 @@ use Espo\Core\Exceptions\Forbidden; use Espo\Core\Field\EmailAddress; use Espo\Core\Record\ServiceContainer; use Espo\Core\Select\SelectBuilderFactory; +use Espo\Core\Templates\Entities\Company; +use Espo\Core\Templates\Entities\Person; +use Espo\Core\Utils\Metadata; use Espo\Modules\Crm\Entities\Account; use Espo\Modules\Crm\Entities\Contact; use Espo\Modules\Crm\Entities\CaseObj; use Espo\Modules\Crm\Entities\Lead; use Espo\ORM\Collection; use Espo\ORM\EntityManager; +use Espo\ORM\Type\RelationType; use Espo\Tools\Email\EmailAddressEntityPair; use RuntimeException; @@ -51,7 +55,8 @@ class Service private ServiceContainer $serviceContainer, private Acl $acl, private EntityManager $entityManager, - private SelectBuilderFactory $selectBuilderFactory + private SelectBuilderFactory $selectBuilderFactory, + private Metadata $metadata ) {} /** @@ -100,6 +105,14 @@ class Service } } + if ($list === []) { + $item = $this->findPersonEmailAddress($entity); + + if ($item) { + $list[] = $item; + } + } + return $list; } @@ -131,6 +144,10 @@ class Service return null; } + if (!$this->acl->checkField(Account::ENTITY_TYPE, 'emailAddress')) { + return null; + } + foreach ($dataList as $item) { if ($item->getEmailAddress()->getAddress() === $emailAddress) { return null; @@ -168,6 +185,10 @@ class Service return null; } + if (!$this->acl->checkField(Lead::ENTITY_TYPE, 'emailAddress')) { + return null; + } + foreach ($dataList as $item) { if ($item->getEmailAddress()->getAddress() === $emailAddress) { return null; @@ -268,4 +289,69 @@ class Service return $dataList; } + + private function findPersonEmailAddress(CaseObj $entity): ?EmailAddressEntityPair + { + $relations = $this->entityManager + ->getDefs() + ->getEntity(CaseObj::ENTITY_TYPE) + ->getRelationList(); + + foreach ($relations as $relation) { + if ( + $relation->getType() !== RelationType::BELONGS_TO && + $relation->getType() !== RelationType::HAS_ONE + ) { + continue; + } + + $foreignEntityType = $relation->getForeignEntityType(); + + if ( + $this->metadata->get("scopes.$foreignEntityType.type") !== Person::TEMPLATE_TYPE && + $this->metadata->get("scopes.$foreignEntityType.type") !== Company::TEMPLATE_TYPE + ) { + continue; + } + + $address = $this->getPersonEmailAddress($entity, $relation->getName()); + + if ($address) { + return $address; + } + } + + return null; + } + + private function getPersonEmailAddress(CaseObj $entity, string $link): ?EmailAddressEntityPair + { + $foreignEntity = $this->entityManager + ->getRDBRepositoryByClass(CaseObj::class) + ->getRelation($entity, $link) + ->findOne(); + + if (!$foreignEntity) { + return null; + } + + if (!$this->acl->checkEntityRead($foreignEntity)) { + return null; + } + + if (!$this->acl->checkField($foreignEntity->getEntityType(), 'emailAddress')) { + return null; + } + + /** @var ?string $address */ + $address = $foreignEntity->get('emailAddress'); + + if (!$address) { + return null; + } + + $emailAddress = EmailAddress::create($address); + + return new EmailAddressEntityPair($emailAddress, $foreignEntity); + } }