diff --git a/application/Espo/Entities/User.php b/application/Espo/Entities/User.php index 56c4d32a4d..99e35dc113 100644 --- a/application/Espo/Entities/User.php +++ b/application/Espo/Entities/User.php @@ -40,8 +40,8 @@ class User extends Person { public const ENTITY_TYPE = 'User'; - public const ATTRIBUTE_TYPE = 'type'; - public const ATTRIBUTE_IS_ACTIVE = 'isActive'; + public const ATTR_TYPE = 'type'; + public const ATTR_IS_ACTIVE = 'isActive'; public const LINK_ACCOUNTS = 'accounts'; public const LINK_CONTACT = 'contact'; diff --git a/application/Espo/Tools/Notification/Service.php b/application/Espo/Tools/Notification/Service.php index a8af901b1a..ccafc34c54 100644 --- a/application/Espo/Tools/Notification/Service.php +++ b/application/Espo/Tools/Notification/Service.php @@ -29,6 +29,7 @@ namespace Espo\Tools\Notification; +use Espo\Core\Field\LinkParent; use Espo\Core\Name\Field; use Espo\Core\Utils\Id\RecordIdGenerator; use Espo\Entities\Note; @@ -53,15 +54,15 @@ class Service public function notifyAboutMentionInPost(string $userId, Note $note): void { - $this->entityManager->createEntity(Notification::ENTITY_TYPE, [ - 'type' => Notification::TYPE_MENTION_IN_POST, - 'data' => [ - 'noteId' => $note->getId(), - ], - 'userId' => $userId, - 'relatedId' => $note->getId(), - 'relatedType' => Note::ENTITY_TYPE, - ]); + $notification = $this->entityManager->getRDBRepositoryByClass(Notification::class)->getNew(); + + $notification + ->setType(Notification::TYPE_MENTION_IN_POST) + ->setData(['noteId' => $note->getId()]) + ->setUserId($userId) + ->setRelated(LinkParent::createFromEntity($note)); + + $this->entityManager->saveEntity($notification); } /** @@ -74,41 +75,45 @@ class Service if ($note->getRelatedType() === Email::ENTITY_TYPE) { $related = $this->entityManager ->getRDBRepository(Email::ENTITY_TYPE) - ->select([Attribute::ID, 'sentById', 'createdById']) + ->select([ + Attribute::ID, + 'sentById', + 'createdById', + ]) ->where([Attribute::ID => $note->getRelatedId()]) ->findOne(); } $now = date(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT); - $collection = $this->entityManager - ->getCollectionFactory() - ->create(); + $collection = $this->entityManager->getCollectionFactory()->create(); - $userList = $this->entityManager + $users = $this->entityManager ->getRDBRepository(User::ENTITY_TYPE) - ->select([Attribute::ID, 'type']) + ->select([ + Attribute::ID, + User::ATTR_TYPE, + ]) ->where([ - 'isActive' => true, + User::ATTR_IS_ACTIVE => true, Attribute::ID => $userIdList, ]) ->find(); - foreach ($userList as $user) { + foreach ($users as $user) { $userId = $user->getId(); if (!$this->checkUserNoteAccess($user, $note)) { continue; } - if ($note->get('createdById') === $user->getId()) { + if ($note->getCreatedById() === $user->getId()) { continue; } if ( - $related && - $related->getEntityType() === Email::ENTITY_TYPE && - $related->get('sentById') === $user->getId() + $related instanceof Email && + $related->getSentBy()?->getId() === $user->getId() ) { continue; } @@ -117,21 +122,19 @@ class Service continue; } - $notification = $this->entityManager->getNewEntity(Notification::ENTITY_TYPE); + $notification = $this->entityManager->getRDBRepositoryByClass(Notification::class)->getNew(); - $notification->set([ - Attribute::ID => $this->idGenerator->generate(), - 'data' => [ - 'noteId' => $note->getId(), - ], - 'type' => Notification::TYPE_NOTE, - 'userId' => $userId, - Field::CREATED_AT => $now, - 'relatedId' => $note->getId(), - 'relatedType' => Note::ENTITY_TYPE, - 'relatedParentId' => $note->getParentId(), - 'relatedParentType' => $note->getParentType(), - ]); + $notification + ->set(Attribute::ID, $this->idGenerator->generate()) + ->set(Field::CREATED_AT, $now) + ->setData(['noteId' => $note->getId()]) + ->setType(Notification::TYPE_NOTE) + ->setUserId($userId) + ->setRelated(LinkParent::createFromEntity($notification)) + ->setRelatedParent( + $note->getParentType() && $note->getParentId() ? + LinkParent::create($note->getParentType(), $note->getParentId()) : null + ); $collection[] = $notification; } @@ -160,16 +163,12 @@ class Service return true; } - if ($note->getRelatedType()) { - if (!$this->aclManager->checkScope($user, $note->getRelatedType())) { - return false; - } + if ($note->getRelatedType() && !$this->aclManager->checkScope($user, $note->getRelatedType())) { + return false; } - if ($note->getParentType()) { - if (!$this->aclManager->checkScope($user, $note->getParentType())) { - return false; - } + if ($note->getParentType() && !$this->aclManager->checkScope($user, $note->getParentType())) { + return false; } return true;