isEnabled()) { $this->error('Firebase is not enabled. Check your configuration.'); return self::FAILURE; } $type = $this->option('type'); $direction = $this->option('direction'); $this->info("Syncing {$type} {$direction}..."); if ($type === 'all' || $type === 'riders') { $this->syncRiders($firebase, $direction); } if ($type === 'all' || $type === 'deliveries') { $this->syncDeliveries($firebase, $direction); } $this->info('Sync completed successfully.'); return self::SUCCESS; } /** * Sync rider data. */ protected function syncRiders(FirebaseService $firebase, string $direction): void { $this->info('Syncing rider locations...'); $riders = Rider::where('is_online', true) ->whereNotNull('current_latitude') ->whereNotNull('current_longitude') ->get(); $bar = $this->output->createProgressBar($riders->count()); $bar->start(); foreach ($riders as $rider) { if ($direction === 'to-firebase') { $firebase->updateRiderLocation( $rider->id, (float) $rider->current_latitude, (float) $rider->current_longitude ); $firebase->updateRiderStatus($rider->id, $rider->status); } $bar->advance(); } $bar->finish(); $this->newLine(); $this->info("Synced {$riders->count()} online riders."); } /** * Sync delivery data. */ protected function syncDeliveries(FirebaseService $firebase, string $direction): void { $this->info('Syncing active deliveries...'); $deliveries = Delivery::whereIn('status', array_map( fn ($s) => $s->value, DeliveryStatus::riderActiveStatuses() )) ->whereNotNull('rider_id') ->with('rider') ->get(); $bar = $this->output->createProgressBar($deliveries->count()); $bar->start(); foreach ($deliveries as $delivery) { if ($direction === 'to-firebase') { $firebase->initializeDeliveryTracking($delivery->id, [ 'status' => $delivery->status->value, 'rider_id' => $delivery->rider_id, 'pickup_latitude' => $delivery->pickup_latitude, 'pickup_longitude' => $delivery->pickup_longitude, 'drop_latitude' => $delivery->drop_latitude, 'drop_longitude' => $delivery->drop_longitude, ]); } $bar->advance(); } $bar->finish(); $this->newLine(); $this->info("Synced {$deliveries->count()} active deliveries."); } }