Files

72 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2026-03-15 17:08:23 +07:00
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Console\Commands;
use Illuminate\Console\Command;
use Modules\RestaurantDelivery\Enums\DeliveryStatus;
use Modules\RestaurantDelivery\Jobs\AssignRiderJob;
use Modules\RestaurantDelivery\Models\Delivery;
class AssignPendingDeliveries extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'delivery:assign-pending
{--limit=50 : Maximum number of deliveries to process}
{--restaurant= : Filter by restaurant ID}';
/**
* The console command description.
*/
protected $description = 'Assign riders to pending deliveries that need assignment';
/**
* Execute the console command.
*/
public function handle(): int
{
$limit = (int) $this->option('limit');
$restaurantId = $this->option('restaurant');
$this->info('Looking for deliveries that need rider assignment...');
$query = Delivery::query()
->where('status', DeliveryStatus::READY_FOR_PICKUP)
->whereNull('rider_id');
if ($restaurantId) {
$query->where('restaurant_id', $restaurantId);
}
$deliveries = $query->limit($limit)
->orderBy('created_at', 'asc')
->get();
if ($deliveries->isEmpty()) {
$this->info('No deliveries found that need rider assignment.');
return self::SUCCESS;
}
$this->info("Found {$deliveries->count()} deliveries to process.");
$bar = $this->output->createProgressBar($deliveries->count());
$bar->start();
foreach ($deliveries as $delivery) {
dispatch(new AssignRiderJob($delivery));
$bar->advance();
}
$bar->finish();
$this->newLine();
$this->info("Dispatched {$deliveries->count()} assignment jobs to queue.");
return self::SUCCESS;
}
}