HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcode/app/Console/Commands/PipedriveSender.php
<?php

namespace App\Console\Commands;

use App\Helpers\PipedriveHelper;
use App\Models\PipedriveQueue;
use App\Repository\PipedriveQueueRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Command\Command as CommandAlias;
use Throwable;

class PipedriveSender extends Command
{

    protected $signature = 'pipedrive:send';

    protected $description = 'Process pipedrive queue';

    private int $errorsCount = 0;

    private string $token;

    private PipedriveQueueRepository $pipedriveQueueRepository;

    private PipedriveHelper $pipedriveHelper;

    public function __construct(PipedriveQueueRepository $pipedriveQueueRepository, PipedriveHelper $pipedriveHelper)
    {
        parent::__construct();

        $this->pipedriveQueueRepository = $pipedriveQueueRepository;

        $this->token = env('PIPEDRIVE_TOKEN');
        if (!empty($this->token)) {
            $this->pipedriveHelper = $pipedriveHelper;
        }
    }

    private function setError(PipedriveQueue $pipedriveQueueItem, string $error): void
    {
        $this->warn($error);
        $pipedriveQueueItem->status = PipedriveQueue::STATUS_ERROR;
        $pipedriveQueueItem->error = $error;
        $pipedriveQueueItem->save();
        $this->errorsCount++;
    }

    public function handle(): int
    {
        if (empty($this->token)) {
            $this->error('Need to fill PIPEDRIVE_TOKEN in .env');
        }
        $this->info('Send leads to Pipedrive');

        $pipedriveQueue = $this->pipedriveQueueRepository->getNeedToSend();
        $this->line('Found items: ' . $pipedriveQueue->count());
        foreach ($pipedriveQueue as $pipedriveQueueItem) {
            try {
                $pipedriveQueueItem->status = PipedriveQueue::STATUS_IN_PROGRESS;
                $pipedriveQueueItem->save();

                $personId = null;

                /** @var PipedriveQueue $pipedriveQueueItem */
                $data = json_decode($pipedriveQueueItem->data, true);

                $name = $data['first_name'] . (empty($data['last_name']) ? '' : ' ' . $data['last_name']);
                unset($data['first_name'], $data['last_name']);

                $email = strtolower($data['email']);
                unset($data['email']);
                $phone = empty($data['phone']) ? null : $data['phone'];
                unset($data['phone']);

                Log::channel('pipedrive')->info('Search person request', ['email' => $email]);
                $response = $this->pipedriveHelper->searchPerson($email);
                Log::channel('pipedrive')->info('Search person response', $response);

                if ($response['success'] && !empty($response['data']['items'][0]['item'])) {
                    $personId = $response['data']['items'][0]['item']['id'];
                }

                if (empty($personId)) {
                    $this->line(PHP_EOL . 'Create person ' . $name . ' ' . $email . ' ' . $phone);
                    Log::channel('pipedrive')->info('Add person request', ['name' => $name, 'email' => $email, 'phone' => $phone]);
                    $response = $this->pipedriveHelper->addPerson($name, $email, $phone);
                    Log::channel('pipedrive')->info('Add person response', $response);

                    if (!empty($response['error_info'])) {
                        $error = 'Person error: ' . $response['error_info'];
                        $this->setError($pipedriveQueueItem, $error);
                        continue;
                    }
                    if ($response['success'] && !empty($response['data']['id'])) {
                        $personId = $response['data']['id'];
                    }
                }

                if (empty($personId)) {
                    $error = 'Person not found or create';
                    $this->setError($pipedriveQueueItem, $error);
                    continue;
                }

                if (empty($data['landing_url'])) {
                    $url = explode('://', env('APP_URL'));
                    $landingUrl = $url[1] ?? $url[0] . $data['referrer'];
                    $data['landing_url'] = $landingUrl;
                }

                $title = 'New lead from ' . $data['landing_url'] . ' from ' . $email;
                Log::channel('pipedrive')->info('Add lead request', ['title' => $title, 'personId' => $personId, 'additional' => $data]);
                $response = $this->pipedriveHelper->addLead($title, $personId, $data);
                Log::channel('pipedrive')->info('Add lead response', $response);

                if (!empty($response['error_info'])) {
                    $error = 'Lead error: ' . $response['error_info'];
                    $this->setError($pipedriveQueueItem, $error);
                    continue;
                }

                $pipedriveQueueItem->whats_app_status = PipedriveQueue::STATUS_NEW;
                $pipedriveQueueItem->lead_id = $response['data']['id'];
                $pipedriveQueueItem->lead_created_at = Carbon::now();
                $pipedriveQueueItem->step = 1;

                $pipedriveQueueItem->status = PipedriveQueue::STATUS_DONE;
                $pipedriveQueueItem->error = null;
                $pipedriveQueueItem->save();
            } catch (Throwable $exception) {
                $this->setError($pipedriveQueueItem, $exception->getMessage());
            }
        }

        if (!empty($this->errorsCount)) {
            $this->warn('Errors count: ' . $this->errorsCount);
        }

        $this->info(PHP_EOL . 'Done');

        return CommandAlias::SUCCESS;
    }

}