diff --git a/application/Espo/Classes/AssignmentNotificators/Email.php b/application/Espo/Classes/AssignmentNotificators/Email.php index 4dcfe8d40a..f0c413bd79 100644 --- a/application/Espo/Classes/AssignmentNotificators/Email.php +++ b/application/Espo/Classes/AssignmentNotificators/Email.php @@ -30,6 +30,7 @@ namespace Espo\Classes\AssignmentNotificators; use Espo\Core\Field\DateTime; +use Espo\Core\Notification\DefaultAssignmentNotificator; use Espo\Entities\EmailAddress; use Espo\Entities\EmailFolder; use Espo\Modules\Crm\Entities\Account; @@ -56,25 +57,14 @@ class Email implements AssignmentNotificator { private const DAYS_THRESHOLD = 2; - private User $user; - private EntityManager $entityManager; - private UserEnabledChecker $userChecker; - private AclManager $aclManager; - private StreamService $streamService; - public function __construct( - User $user, - EntityManager $entityManager, - UserEnabledChecker $userChecker, - AclManager $aclManager, - StreamService $streamService - ) { - $this->user = $user; - $this->entityManager = $entityManager; - $this->userChecker = $userChecker; - $this->aclManager = $aclManager; - $this->streamService = $streamService; - } + private User $user, + private EntityManager $entityManager, + private UserEnabledChecker $userChecker, + private AclManager $aclManager, + private StreamService $streamService, + private DefaultAssignmentNotificator $defaultAssignmentNotificator, + ) {} /** * @param EmailEntity $entity @@ -94,6 +84,14 @@ class Email implements AssignmentNotificator return; } + if ($entity->getStatus() !== EmailEntity::STATUS_BEING_IMPORTED) { + // @todo Find out whether it may happen + $this->defaultAssignmentNotificator->process( + $entity, + $params->withOption(DefaultAssignmentNotificator::OPTION_FORCE_ASSIGNED_USER, true) + ); + } + if ($params->getOption('isJustSent')) { $previousUserIdList = []; } else { diff --git a/application/Espo/Core/Notification/AssignmentNotificator/Params.php b/application/Espo/Core/Notification/AssignmentNotificator/Params.php index 3be6bd3e71..52f37777e0 100644 --- a/application/Espo/Core/Notification/AssignmentNotificator/Params.php +++ b/application/Espo/Core/Notification/AssignmentNotificator/Params.php @@ -39,12 +39,17 @@ class Params */ private $options = []; + /** + * Whether an option is set. + */ public function hasOption(string $option): bool { return array_key_exists($option, $this->options); } /** + * Get an option. + * * @return mixed */ public function getOption(string $option) @@ -72,6 +77,22 @@ class Params return $obj; } + /** + * Clone with an option. + * + * @since 8.5.0 + */ + public function withOption(string $option, mixed $value): self + { + $obj = clone $this; + $obj->options[$option] = $value; + + return $obj; + } + + /** + * Create an instance. + */ public static function create(): self { return new self(); diff --git a/application/Espo/Core/Notification/DefaultAssignmentNotificator.php b/application/Espo/Core/Notification/DefaultAssignmentNotificator.php index b0ddde8ca8..3ce20d98b6 100644 --- a/application/Espo/Core/Notification/DefaultAssignmentNotificator.php +++ b/application/Espo/Core/Notification/DefaultAssignmentNotificator.php @@ -41,6 +41,11 @@ use Espo\Core\Notification\AssignmentNotificator\Params; */ class DefaultAssignmentNotificator implements AssignmentNotificator { + public const OPTION_FORCE_ASSIGNED_USER = 'forceAssignedUser'; + + private const FIELD_ASSIGNED_USERS = 'assignedUsers'; + private const ATTR_ASSIGNED_USER_ID = 'assignedUserId'; + public function __construct( protected User $user, protected EntityManager $entityManager, @@ -53,13 +58,16 @@ class DefaultAssignmentNotificator implements AssignmentNotificator return; } - if ($entity->hasLinkMultipleField('assignedUsers')) { - $userIdList = $entity->getLinkMultipleIdList('assignedUsers'); - /** @var string[] $fetchedAssignedUserIdList */ - $fetchedAssignedUserIdList = $entity->getFetched('assignedUsersIds') ?? []; + if ( + $entity->hasLinkMultipleField(self::FIELD_ASSIGNED_USERS) && + !$params->getOption(self::OPTION_FORCE_ASSIGNED_USER) + ) { + $userIds = $entity->getLinkMultipleIdList(self::FIELD_ASSIGNED_USERS); + /** @var string[] $fetchedIds */ + $fetchedIds = $entity->getFetched(self:: FIELD_ASSIGNED_USERS . 'Ids') ?? []; - foreach ($userIdList as $userId) { - if (in_array($userId, $fetchedAssignedUserIdList)) { + foreach ($userIds as $userId) { + if (in_array($userId, $fetchedIds)) { continue; } @@ -69,15 +77,15 @@ class DefaultAssignmentNotificator implements AssignmentNotificator return; } - if (!$entity->get('assignedUserId')) { + if (!$entity->get(self::ATTR_ASSIGNED_USER_ID)) { return; } - if (!$entity->isAttributeChanged('assignedUserId')) { + if (!$entity->isAttributeChanged(self::ATTR_ASSIGNED_USER_ID)) { return; } - $assignedUserId = $entity->get('assignedUserId'); + $assignedUserId = $entity->get(self::ATTR_ASSIGNED_USER_ID); $this->processForUser($entity, $assignedUserId); }