Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion app/DataProviders/Repositories/TripRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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<string, mixed> $data
*/
Expand Down
Loading