EventHandler::handleTimeLineEvents

  1. Bitrix24 API (v. 23.675.0)
  2. sender
  3. EventHandler
  4. handleTimeLineEvents
  • Модуль: sender
  • Путь к файлу: ~/bitrix/modules/sender/lib/integration/crm/eventhandler.php
  • Класс: Bitrix\Sender\Integration\Crm\EventHandler
  • Вызов: EventHandler::handleTimeLineEvents
static function handleTimeLineEvents($letterId)
{
	static $isModuleIncluded = null;
	if ($isModuleIncluded === null)
	{
		$isModuleIncluded = Loader::includeModule('crm');
	}

	if (!$isModuleIncluded)
	{
		return "";
	}

	$entityTypeId = $entityId = null;
	$batchData = [];
	$idsToDelete = [];
	self::lockTimelineQueue($letterId);
	$letter = Entity\Letter::createInstanceById($letterId);

	try
	{
		$queuedRows = TimeLineQueueTable::getList([
			'filter' => [
				'=STATUS' => TimeLineQueueTable::STATUS_NEW,
				'=POSTING_ID' => $letterId
			],
			'limit' => self::TIME_LINE_COUNT_LIMIT
		])->fetchAll();

		if(empty($queuedRows))
		{
			return "";
		}

		foreach ($queuedRows as $row)
		{
			$idsToDelete[] = $row['ID'];
			if (!$letter)
			{
				continue;
			}

			$fields = Json::decode($row['FIELDS']);
			if (isset($fields['CRM_ENTITY_TYPE_ID']) && $fields['CRM_ENTITY_TYPE_ID'])
			{
				$entityTypeId = $fields['CRM_ENTITY_TYPE_ID'];
			}
			if (isset($fields['CRM_ENTITY_ID']) && $fields['CRM_ENTITY_ID'])
			{
				$entityId = $fields['CRM_ENTITY_ID'];
			}

			if (!$entityTypeId || !$entityId)
			{
				$selector = self::getEntitySelectorByRecipient(
					$row['CONTACT_TYPE_ID'],
					$row['CONTACT_CODE']
				);
			}
			else
			{
				$selector = self::getEntitySelectorById($entityTypeId, $entityId);
			}


			if (!$selector)
			{
				continue;
			}

			if (!$selector->search()->hasEntities())
			{
				continue;
			}

			$recipient = [
				'ID' => $row['RECIPIENT_ID'],
				'CONTACT_TYPE_ID' => $row['CONTACT_TYPE_ID'],
				'CONTACT_CODE' => $row['CONTACT_CODE'],
			];

			$batchData[] = static::buildTimeLineEvent($selector, $letter, $recipient);
		}

		if (!empty($batchData))
		{
			Timeline\RecipientEntry::createMulti($batchData);
		}

		TimeLineQueueTable::deleteList(['=ID' => $idsToDelete]);
	} catch (\Exception $e)
	{
	}

	self::unlockTimelineQueue($letterId);

	return TimeLineJob::getAgentName($letterId);
}

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