distinct usage rmoval

This commit is contained in:
Yuri Kuznetsov
2025-06-08 19:21:27 +03:00
parent cd860cb7d0
commit 9982ed0bef
@@ -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(),
];
$orBuilder = OrGroup::createBuilder();
if ($entity->isPortal() && $entity->get('contactId')) {
$where['contactsLeftMiddle.contactId'] = $entity->get('contactId');
$orBuilder->add(
Cond::equal(Expr::column('usersLeftMiddle.userId'), $entity->getId())
);
$builder
->leftJoin('contacts', 'contactsLeft')
->distinct()
->where([
'OR' => $where,
]);
} else {
$builder->where($where);
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(),
];
$orBuilder = OrGroup::createBuilder();
if ($entity->isPortal() && $entity->get('contactId')) {
$where['contactsLeftMiddle.contactId'] = $entity->get('contactId');
$orBuilder->add(
Cond::equal(Expr::column('usersLeftMiddle.userId'), $entity->getId())
);
$builder
->leftJoin('contacts', 'contactsLeft')
->distinct()
->where([
'OR' => $where,
]);
} else {
$builder->where($where);
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,