Skip to content

EspoCRM – Open Source CRM Application

distinct usage rmoval

Yuri Kuznetsov9982ed0befb7
diff --git a/application/Espo/Modules/Crm/Tools/Activities/Service.php b/application/Espo/Modules/Crm/Tools/Activities/Service.php
index dc6a75fe36..dacb796c0c 100644
--- a/application/Espo/Modules/Crm/Tools/Activities/Service.php
+++ b/application/Espo/Modules/Crm/Tools/Activities/Service.php
@@ -36,6 +36,7 @@ use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Name\Field;
+use Espo\Core\Select\Helpers\RelationQueryHelper;
use Espo\Core\ServiceFactory;
use Espo\Core\Templates\Entities\Company;
use Espo\Core\Templates\Entities\Person;
@@ -51,6 +52,9 @@ use Espo\Modules\Crm\Entities\Meeting;
use Espo\Modules\Crm\Entities\Reminder;
use Espo\ORM\EntityCollection;
use Espo\ORM\EntityManager;
+use Espo\ORM\Query\Part\Condition as Cond;
+use Espo\ORM\Query\Part\Expression as Expr;
+use Espo\ORM\Query\Part\Where\OrGroup;
use Espo\ORM\Query\UnionBuilder;
use Espo\ORM\Query\SelectBuilder;
@@ -86,7 +90,8 @@ class Service
private Acl $acl,
private ServiceFactory $serviceFactory,
private EntityManager $entityManager,
- private User $user
+ private User $user,
+ private RelationQueryHelper $relationQueryHelper,
) {}
protected function isPerson(string $scope): bool
@@ -144,23 +149,30 @@ class Service
throw new RuntimeException($e->getMessage());
}
- $where = [
- 'usersLeftMiddle.userId' => $entity->getId(),
- ];
-
- if ($entity->isPortal() && $entity->get('contactId')) {
- $where['contactsLeftMiddle.contactId'] = $entity->get('contactId');
-
- $builder
- ->leftJoin('contacts', 'contactsLeft')
- ->distinct()
- ->where([
- 'OR' => $where,
- ]);
- } else {
- $builder->where($where);
+ $orBuilder = OrGroup::createBuilder();
+
+ $orBuilder->add(
+ Cond::equal(Expr::column('usersLeftMiddle.userId'), $entity->getId())
+ );
+
+ if ($entity->isPortal() && $entity->getContactId()) {
+ $contactsRelation = $this->entityManager
+ ->getDefs()
+ ->getEntity(Meeting::ENTITY_TYPE)
+ ->getRelation('contacts');
+
+ $orBuilder->add(
+ $this->relationQueryHelper->prepareLinkWhere(
+ $contactsRelation,
+ Meeting::ENTITY_TYPE,
+ $entity->getContactId(),
+ $builder
+ ) ?? throw new RuntimeException()
+ );
}
+ $builder->where($orBuilder->build());
+
if (!empty($statusList)) {
$builder->where([
'status' => $statusList,
@@ -216,23 +228,30 @@ class Service
throw new RuntimeException($e->getMessage());
}
- $where = [
- 'usersLeftMiddle.userId' => $entity->getId(),
- ];
-
- if ($entity->isPortal() && $entity->get('contactId')) {
- $where['contactsLeftMiddle.contactId'] = $entity->get('contactId');
-
- $builder
- ->leftJoin('contacts', 'contactsLeft')
- ->distinct()
- ->where([
- 'OR' => $where,
- ]);
- } else {
- $builder->where($where);
+ $orBuilder = OrGroup::createBuilder();
+
+ $orBuilder->add(
+ Cond::equal(Expr::column('usersLeftMiddle.userId'), $entity->getId())
+ );
+
+ if ($entity->isPortal() && $entity->getContactId()) {
+ $contactsRelation = $this->entityManager
+ ->getDefs()
+ ->getEntity(Meeting::ENTITY_TYPE)
+ ->getRelation('contacts');
+
+ $orBuilder->add(
+ $this->relationQueryHelper->prepareLinkWhere(
+ $contactsRelation,
+ Call::ENTITY_TYPE,
+ $entity->getContactId(),
+ $builder
+ ) ?? throw new RuntimeException()
+ );
}
+ $builder->where($orBuilder->build());
+
if (!empty($statusList)) {
$builder->where([
'status' => $statusList,