departureRepository = new DepartureRepository(); $this->capacityService = new CapacityService(); } /** * Run the capacity sync process */ public function run(): array { $results = [ 'total_departures' => 0, 'updated_departures' => 0, 'errors' => [] ]; try { // Get all departures $allDepartures = $this->departureRepository->findAll(); $results['total_departures'] = count($allDepartures); foreach ($allDepartures as $departure) { $date = $departure->start_date ?: $departure->date; // Get correct capacity from availability $correctCapacity = $this->capacityService->getCapacityForDate($departure->trip_id, $date, $departure->time ?? null); // Only update if capacity is different and correct capacity > 0 if ($correctCapacity > 0 && $departure->max_capacity !== $correctCapacity) { $updated = $this->departureRepository->update($departure->id, [ 'max_capacity' => $correctCapacity ]); if ($updated) { $results['updated_departures']++; } } } } catch (\Throwable $e) { $results['errors'][] = $e->getMessage(); } return $results; } }