EventHandler::onMailEventMailChangeStatus

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. EventHandler
  4. onMailEventMailChangeStatus
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/integration/main/eventhandler.php
  • Класс: Bitrix\Crm\Integration\Main\EventHandler
  • Вызов: EventHandler::onMailEventMailChangeStatus
static function onMailEventMailChangeStatus(Result $result) : void
{
	$isBelongCrm = (
		$result->isBelongTo("crm","rpa")
		|| $result->isBelongTo("crm","act")
		|| $result->isBelongTo('crm', MailManager::CALLBACK_ENTITY_TYPE)
	);
	if (!$isBelongCrm || !$result->isError() || !$result->isPermanentError())
	{
		return;
	}
	if (0 === $id = \CCrmActivity::ParseUrn($result->getEntityId())["ID"])
	{
		return;
	}

	/**@var $statusChangeRegistry */
	static $statusChangeRegistry;
	if(!$statusChangeRegistry)
	{
		$statusChangeRegistry = [];
		Application::getInstance()->addBackgroundJob(
			static function() use (&$statusChangeRegistry) :void {
				/**@var string[] $results*/
				foreach ($statusChangeRegistry as $id => $results)
				{
					$activity = ActivityTable::query()
						->setSelect(["ID","SETTINGS","AUTHOR_ID","EDITOR_ID","SUBJECT"])
						->setCacheTtl(self::ACTIVITY_QUERY_TTL)
						->where('ID', $id)
						->exec();

					if (!$activity = $activity->fetch())
					{
						return;
					}

					$activity["SETTINGS"] = $activity["SETTINGS"] ?? [];
					$meta = $activity["SETTINGS"]["EMAIL_META"] ?? [];
					$emails = array_filter(
						array_merge(
							explode(", ",$meta["to"] ?? ""),
							explode(", ",$meta["cc"] ?? ""),
							explode(", ",$meta["bcc"] ?? "")
						)
					);
					$diff = array_diff(
						array_unique($emails),
						array_unique($results)
					);

					$status = !empty($diff)? Email::ERROR_TYPE_PARTIAL : Email::ERROR_TYPE_FULL;
					if ($activity["SETTINGS"]["SENT_ERROR"] !== $status)
					{
						$settings = ["SETTINGS" => ["SENT_ERROR" => $status] + $activity["SETTINGS"]];
						ActivityTable::update($id, $settings);
					}
				}
			}
		);
	}

	$statusChangeRegistry[$id] = $statusChangeRegistry[$id] ?? [];
	$statusChangeRegistry[$id][] = $result->getEmail();
}

Добавить комментарий