From 96c1e74282f64363aaee0ee84e64ba417ba05760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20St=C3=B6ckel?= Date: Fri, 25 Sep 2026 22:09:34 +0200 Subject: [PATCH] :bug: fix duplicate entry errors for stops listed twice --- .../Repositories/TripRepository.php | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/DataProviders/Repositories/TripRepository.php b/app/DataProviders/Repositories/TripRepository.php index b1642ae8e..28d4d2e2c 100644 --- a/app/DataProviders/Repositories/TripRepository.php +++ b/app/DataProviders/Repositories/TripRepository.php @@ -5,6 +5,7 @@ use App\Enum\TripSource; use App\Models\Stopover; use App\Models\Trip; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; @@ -38,7 +39,7 @@ public function updateOrCreateStopovers(Trip $trip, array $stopoverData): Collec $claimedIds = []; $unmatched = []; - foreach ($stopoverData as $index => $data) { + foreach ($this->withoutRepeatedStops($stopoverData) as $index => $data) { try { $existing = $this->queryStopoversByPlannedTimes($trip, $data, $claimedIds) ->where('train_station_id', $data['train_station_id']) @@ -79,6 +80,27 @@ public function updateOrCreateStopovers(Trip $trip, array $stopoverData): Collec return $stopovers->sortKeys()->values(); } + private function withoutRepeatedStops(array $stopoverData): array + { + $seen = []; + + return array_filter($stopoverData, function (array $data) use (&$seen): bool { + $key = implode('|', [ + $data['train_station_id'], + Carbon::make($data['arrival_planned'])?->toDateTimeString(), + Carbon::make($data['departure_planned'])?->toDateTimeString(), + ]); + + if (isset($seen[$key])) { + return false; + } + + $seen[$key] = true; + + return true; + }); + } + /** * @param array $data */