File: /var/www/quadcode-site/app/Console/Commands/SyncBitrixNotAnsweredLeads.php
<?php
namespace App\Console\Commands;
use App\Helpers\ActiveCampaignHelper;
use App\Helpers\BitrixHelper;
use Symfony\Component\Console\Command\Command as CommandAlias;
class SyncBitrixNotAnsweredLeads extends AbstractSyncBitrixAndActiveCampaign
{
protected $signature = 'bitrix:sync-not-answered-leads';
protected $description = 'Sync Bitrix "not-answered" leads to Active Campaign';
public function handle(BitrixHelper $bitrixHelper, ActiveCampaignHelper $activeCampaignHelper): int
{
$this->info($this->description);
$this->activeCampaignHelper = $activeCampaignHelper;
if (!$bitrixHelper->check()) {
$this->error('Need to fill BITRIX24_HOST, BITRIX24_USER_ID or BITRIX24_TOKEN in .env');
return CommandAlias::FAILURE;
}
$listId = getenv('ACTIVE_CAMPAIGN_BITRIX_NOT_ANSWERED_LIST_ID');
if (!$activeCampaignHelper->check() || empty($listId)) {
$this->error('Need to fill ACTIVE_CAMPAIGN_HOST, ACTIVE_CAMPAIGN_TOKEN or ACTIVE_CAMPAIGN_BITRIX_NOT_ANSWERED_LIST_ID in .env');
return CommandAlias::FAILURE;
}
$filter = [
'STATUS_ID' => BitrixHelper::STATUS_JUNK,
'>LAST_ACTIVITY_TIME' => date('Y-m-d', strtotime('-2 week')),
];
$select = ['NAME', 'EMAIL', 'PHONE'];
$leads = $bitrixHelper->leadList($filter, select: $select);
$contacts = [];
foreach ($leads as $lead) {
$contact = $this->convertBitrixEntityToActiveCampaignContact($lead);
if (empty($contact)) {
continue;
}
$contacts[$contact['email']] = $contact;
}
$uniqueEmails = array_unique(array_column($contacts, 'email'));
$this->line('Contacts found: ' . count($contacts));
$this->line('Unique contacts found: ' . count($uniqueEmails));
$this->syncContactsToActiveCampaign($listId, $contacts);
$this->info('Done');
return CommandAlias::SUCCESS;
}
}