DispatchVacancyLiftJobCommand.php 1.71 KB
<?php

namespace App\Console\Commands;

use App\Jobs\LiftVacancyJob;
use App\Jobs\SendVacancyToTelegramJob;
use App\Models\EmployerAutoliftOption;
use App\Models\WorkerAutoliftOption;
use Illuminate\Console\Command;

class DispatchVacancyLiftJobCommand extends Command
{
    protected $signature ='vacancy:dispatch';

    public function handle()
    {
        $now = now()->timezone('Europe/Moscow')->format('H:i');

        $employers = EmployerAutoliftOption::query()
            ->where(function ($query) use ($now) {
                $query->where('times_per_day', 1)
                    ->where('time_send_first', $now);
            })
            ->orWhere(function ($query) use ($now) {
                $query->where('times_per_day', 2)
                    ->where(function ($query) use ($now) {
                        $query->where('time_send_first', $now)
                            ->orWhere('time_send_second', $now);
                    });
            })
            ->orWhere(function ($query) use ($now) {
                $query->where('times_per_day', 3)
                    ->where(function ($query) use ($now) {
                        $query->where('time_send_first', $now)
                            ->orWhere('time_send_second', $now)
                            ->orWhere('time_send_third', $now);
                    });

            })
            ->orWhere('time_send_tg', $now)
            ->get();

        LiftVacancyJob::dispatch(
            $employers->whereNotNull('time_send_first')->pluck('employer_id')->toArray(),
        );

        SendVacancyToTelegramJob::dispatch(
            $employers->whereNotNull('time_send_tg')->pluck('employer_id')->toArray()
        );

        return Command::SUCCESS;
    }
}