Skip to content

Commit

Permalink
🐛 Checkins in UK trains (#2113)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrLevin authored Nov 11, 2023
1 parent 3de2f81 commit 3932bfb
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;
Expand Down Expand Up @@ -142,10 +143,28 @@ private static function createTrainCheckin(
->where('arrival_planned', $arrival)
->first();

// In some rare occasions, the departure time of the origin station has a different timezone
// than the first stopover. In this case, we need to find it by comparing the departure time
// in a localtime string format.
if (empty($firstStop)) {
$firstStops = $trip->stopovers->where('train_station_id', $origin->id);

if ($firstStops->count() > 1) {
$firstStop = $firstStops->filter(function(TrainStopover $stopover) use ($departure) {
return $stopover->departure_planned->format('H:i') === $departure->format('H:i');
})->first();
} else {
$firstStop = $firstStops->first();
}
}


if (empty($firstStop) || empty($lastStop)) {
Log::debug('TrainCheckin: No stop found for origin or destination (HafasTrip ' . $trip->trip_id . ')');
Log::debug('TrainCheckin: Origin-ID: ' . $origin->id . ', Departure: ' . $departure->toIso8601String());
Log::debug('TrainCheckin: Destination-ID: ' . $destination->id . ', Arrival: ' . $arrival->toIso8601String());
Log::debug(
'TrainCheckin: Destination-ID: ' . $destination->id . ', Arrival: ' . $arrival->toIso8601String()
);
throw new StationNotOnTripException();
}

Expand Down

0 comments on commit 3932bfb

Please sign in to comment.