Files
FlightsAPI/app/Console/Commands/UpdateDepartedFlights.php
T
2026-05-10 22:42:37 +10:00

221 lines
8.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\DTOs\FlightStatData;
use App\Models\Aircraft;
use App\Models\IataEquipmentCode;
use App\Models\Notification;
use App\Models\UserFlight;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
#[Signature('app:update-departed-flights')]
#[Description('Command description')]
class UpdateDepartedFlights extends Command
{
/**
* Fetch live flight data from FlightStats.
* Returns null if the request fails or no data is found.
*/
protected function fetchFlightData(string $airlineCode, string $flightNumber, CarbonImmutable $date): ?FlightStatData
{
$url = sprintf(
'https://www.flightstats.com/v2/api-next/flight-tracker/%s/%s/%d/%d/%d',
$airlineCode,
$flightNumber,
$date->year,
$date->month,
$date->day,
);
$response = Http::withOptions([
'verify' => config('app.verify_ssl'),
])->get($url);
if (!$response->successful()) {
Log::warning("FlightStats request failed for {$airlineCode}{$flightNumber}: HTTP {$response->status()}");
return null;
}
$flightData = $response->json('data');
if (empty($flightData)) {
return null;
}
return FlightStatData::fromApiResponse($flightData);
}
/**
* Attempt to resolve the best matching Aircraft record for a given IATA equipment code.
* Prefers passenger variants over freighters/BBJs where multiple matches exist.
*/
protected function guessAircraftFromIata(string $iataCode): ?Aircraft
{
$equipment = IataEquipmentCode::where('iata_code', $iataCode)->first();
if (!$equipment) {
Log::info("Unknown IATA equipment code: {$iataCode}");
return null;
}
$candidates = Aircraft::where('designator', $equipment->icao_code)->get();
if ($candidates->isEmpty()) {
Log::info("No aircraft found for ICAO: {$equipment->icao_code} (IATA: {$iataCode})");
return null;
}
if ($candidates->count() === 1) {
return $candidates->first();
}
// Prefer passenger variants — deprioritise freighters, BBJs, and convertibles
$deprioritised = ['freighter', 'bbj', 'combi', 'mixed', 'cargo', 'prestige', 'winglet', 'sharklet', 'freight'];
$pattern = implode('|', $deprioritised);
$passengerVariants = $candidates->filter(
fn(Aircraft $a) => !preg_match("/({$pattern})/i", $a->display_name_short)
);
if ($passengerVariants->count() === 1) {
return $passengerVariants->first();
}
if ($passengerVariants->isNotEmpty()) {
return $passengerVariants->first();
}
return $candidates->first();
}
protected function notifyDataError(UserFlight $flight): void
{
Notification::create([
'user_id' => $flight->user_id,
'title' => "Auto update failed for {$flight->flight_number}",
'body' => "There was an error fetching flight data for {$flight->flight_number}. Please manually check the aircraft type, registration and departure/arrival times.",
'url' => '/flights/' . $flight->id . '/edit'
]);
$flight->update(['auto_update' => false]);
}
/**
* Execute the console command.
*/
public function handle()
{
$now = now()->utc();
$oneHourAgo = $now->copy()->subHours(1);
$userFlights = UserFlight::where('arrival_date', '<=', $now->copy()->subHour()->toDateTimeString())
->where('auto_update', true)
->whereNotNull('flight_number')
->get();
$this->info("Found {$userFlights->count()} flights.");
foreach ($userFlights as $flight) {
preg_match('/^([A-Z]{2,3})(\d+)$/i', $flight->flight_number, $matches);
if (empty($matches)) {
$this->warn("Could not parse flight number: {$flight->flight_number}");
$this->notifyDataError($flight);
continue;
}
$airlineCode = strtoupper($matches[1]);
$flightNumber = $matches[2];
$arrivalDate = $flight->arrival_date->setTimezone($flight->arrivalAirport->timezone);
$data = $this->fetchFlightData($airlineCode, $flightNumber, $arrivalDate);
if (!$data) {
$this->warn("No flight data returned for {$airlineCode}{$flightNumber}");
$this->notifyDataError($flight);
continue;
}
if ($data->departure_iata !== $flight->departureAirport->iata_code ||
$data->arrival_iata !== $flight->arrivalAirport->iata_code) {
$this->warn("Airport mismatch for {$airlineCode}{$flightNumber} — API: {$data->departure_iata}{$data->arrival_iata}, expected: {$flight->departureAirport->iata_code}{$flight->arrivalAirport->iata_code}");
$this->notifyDataError($flight);
continue;
}
$updates = [];
if ($data->aircraft_registration && $data->aircraft_registration !== $flight->aircraft_registration) {
$updates['aircraft_registration'] = $data->aircraft_registration;
}
if ($data->estimated_departure_utc?->ne($flight->departure_date)) {
$updates['departure_date'] = $data->estimated_departure_utc;
}
if ($data->estimated_arrival_utc?->ne($flight->arrival_date)) {
$updates['arrival_date'] = $data->estimated_arrival_utc;
}
if ($data->equipment_iata) {
$currentAircraft = $flight->aircraft;
if ($currentAircraft?->iata_code !== $data->equipment_iata) {
$match = $this->guessAircraftFromIata($data->equipment_iata);
if ($match) {
$updates['aircraft_id'] = $match->id;
} else {
Log::info("No aircraft match for IATA code {$data->equipment_iata} on flight {$airlineCode}{$flightNumber}");
}
}
}
if (!empty($updates)) {
$flight->update($updates);
$this->info("Updated flight {$airlineCode}{$flightNumber}: " . implode(', ', array_keys($updates)));
$changeDescriptions = [];
if (isset($updates['aircraft_registration'])) {
$changeDescriptions[] = "Registration updated to {$updates['aircraft_registration']}";
}
if (isset($updates['departure_date'])) {
$changeDescriptions[] = "Departure updated to {$updates['departure_date']}";
}
if (isset($updates['arrival_date'])) {
$changeDescriptions[] = "Arrival updated to {$updates['arrival_date']}";
}
if (isset($updates['aircraft_id'])) {
$aircraft = Aircraft::find($updates['aircraft_id']);
$changeDescriptions[] = "Aircraft type updated to {$aircraft->display_name_short}";
}
Notification::create([
'user_id' => $flight->user_id,
'title' => "Flight {$airlineCode}{$flightNumber} updated",
'body' => implode("\n", $changeDescriptions),
]);
} else {
$this->info("No changes for {$airlineCode}{$flightNumber}");
Notification::create([
'user_id' => $flight->user_id,
'title' => "Flight {$airlineCode}{$flightNumber} updated — no changes",
'body' => "Your flight was completed and no updates were made to aircraft, registration, or departure/arrival times.",
]);
}
$flight->update(['auto_update' => false]);
}
}
}