email assignment notification

This commit is contained in:
Yuri Kuznetsov
2024-10-05 11:31:45 +03:00
parent 805a648145
commit 964180ed24
3 changed files with 54 additions and 27 deletions
@@ -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 {
@@ -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();
@@ -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);
}