Added achievement data
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\UserFlight;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
#[Signature('app:update-departed-flights')]
|
||||
#[Description('Command description')]
|
||||
class UpdateDepartedFlights extends Command
|
||||
{
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$now = now()->utc();
|
||||
$oneHourAgo = $now->copy()->subHours(1);
|
||||
|
||||
$userFlights = UserFlight::whereBetween('arrival_date', [
|
||||
$oneHourAgo->toDateTimeString(),
|
||||
$now->toDateTimeString(),
|
||||
])
|
||||
->where('auto_update', true)
|
||||
->whereNotNull('flight_number')
|
||||
->get();
|
||||
|
||||
$this->info("Found {$userFlights->count()} flights.");
|
||||
|
||||
foreach ($userFlights as $flight) {
|
||||
// Split "QF22" into ["QF", "22"]
|
||||
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}");
|
||||
continue;
|
||||
}
|
||||
|
||||
$airlineCode = strtoupper($matches[1]);
|
||||
$flightNumber = $matches[2];
|
||||
|
||||
$arrivalDate = $flight->arrival_date->setTimezone($flight->arrivalAirport->timezone);
|
||||
$year = $arrivalDate->year;
|
||||
$month = $arrivalDate->month;
|
||||
$day = $arrivalDate->day;
|
||||
|
||||
$url = "https://www.flightstats.com/v2/api-next/flight-tracker/{$airlineCode}/{$flightNumber}/{$year}/{$month}/{$day}";
|
||||
|
||||
$response = Http::get($url);
|
||||
|
||||
if (!$response->successful()) {
|
||||
$this->warn("Failed to fetch data for {$flight->flight_number}: HTTP {$response->status()}");
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = $response->json();
|
||||
|
||||
$flightData = $data['data'] ?? [];
|
||||
|
||||
if (empty($flightData)) {
|
||||
$this->warn("No flight data returned for {$airlineCode}{$flightNumber}");
|
||||
continue;
|
||||
}
|
||||
|
||||
$tailNumber = $flightData['positional']['flexTrack']['tailNumber'] ?? null;
|
||||
$estimatedDepartureUtc = $flightData['schedule']['estimatedActualDepartureUTC'] ?? null;
|
||||
$estimatedArrivalUtc = $flightData['schedule']['estimatedActualArrivalUTC'] ?? null;
|
||||
$equipmentCode = $flightData['additionalFlightInfo']['equipment']['iata'] ?? null;
|
||||
|
||||
$this->info("Flight {$airlineCode}{$flightNumber} — Tail: {$tailNumber}, Equipment: {$equipmentCode}");
|
||||
$this->info("Departure: {$estimatedDepartureUtc} | Arrival: {$estimatedArrivalUtc}");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,7 +210,7 @@ class FlightController extends Controller
|
||||
{
|
||||
$this->authorize('delete', $flight);
|
||||
|
||||
$snapshot = $this->flightSnapshot($flight->id);
|
||||
$snapshot = $flight->snapshot($flight->id);
|
||||
|
||||
if(now()->utc()->isBefore($flight->departure_date)){
|
||||
$action = 'flight_deleted';
|
||||
|
||||
@@ -145,7 +145,7 @@ class UserFlight extends Model
|
||||
protected function distance(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn() => $this->calculateGreatCircleDistance()
|
||||
get: fn() => round($this->calculateGreatCircleDistance())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -224,7 +224,7 @@ class UserFlight extends Model
|
||||
public function liveryUrl(): Attribute{
|
||||
return Attribute::make(
|
||||
get: function () {
|
||||
if($this->airline) {
|
||||
if($this->airline && $this->aircraft) {
|
||||
$fileName = "{$this->airline->internal_name}_{$this->aircraft->designator}.png";
|
||||
$file = public_path("img/liveries/generated/$fileName");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user