Files

125 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2026-03-15 17:08:23 +07:00
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Console\Commands;
use Illuminate\Console\Command;
use Modules\RestaurantDelivery\Enums\DeliveryStatus;
use Modules\RestaurantDelivery\Models\Delivery;
use Modules\RestaurantDelivery\Models\Rider;
use Modules\RestaurantDelivery\Services\Firebase\FirebaseService;
class SyncFirebaseData extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'delivery:sync-firebase
{--type=all : Type to sync (all, riders, deliveries)}
{--direction=to-firebase : Sync direction (to-firebase, from-firebase)}';
/**
* The console command description.
*/
protected $description = 'Synchronize data between database and Firebase';
/**
* Execute the console command.
*/
public function handle(FirebaseService $firebase): int
{
if (! $firebase->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.");
}
}