Selaa lähdekoodia

Merge branch 'fix/diaria-kay-correções-backend' of Softpar/sfp_api_laravel_diarista into development

zntt 1 viikko sitten
vanhempi
commit
acefced319
1 muutettua tiedostoa jossa 33 lisäystä ja 0 poistoa
  1. 33 0
      app/Services/CustomScheduleService.php

+ 33 - 0
app/Services/CustomScheduleService.php

@@ -14,6 +14,7 @@ use App\Models\ServicePackage;
 use App\Notifications\Push\Cliente\Agendamento\PrestadorAceitouPush;
 use App\Rules\ScheduleBusinessRules;
 use App\Services\NotificationService;
+use App\Services\DistanceService;
 use App\Services\PushNotificationService;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
@@ -22,6 +23,8 @@ use Illuminate\Support\Facades\Storage;
 
 class CustomScheduleService
 {
+    private const NEARBY_RADIUS_KM = 20.0;
+
     public function __construct(
         private readonly ZipCodeCoordinatesService $zipCodeCoordinatesService,
     ) {}
@@ -255,6 +258,10 @@ class CustomScheduleService
             ->orderBy('is_primary', 'desc')
             ->first();
 
+        $providerCityId = $providerAddress?->city_id;
+        $providerLat = $providerAddress?->latitude !== null ? (float) $providerAddress->latitude : null;
+        $providerLng = $providerAddress?->longitude !== null ? (float) $providerAddress->longitude : null;
+
         $opportunities = Schedule::with([
             'client.user',
             'client.profileMedia',
@@ -266,11 +273,37 @@ class CustomScheduleService
                 $join->on('schedules.id', '=', 'schedule_refuses.schedule_id')
                     ->where('schedule_refuses.provider_id', $providerId);
             })
+            ->leftJoin('addresses as opportunity_address', 'opportunity_address.id', '=', 'schedules.address_id')
             ->whereNull('schedule_refuses.id')
             ->where('schedules.schedule_type', 'custom')
             ->where('schedules.status', 'pending')
             ->whereNull('schedules.provider_id')
             ->whereDate('schedules.date', '>=', now()->toDateString())
+            ->where(function ($query) use ($providerCityId, $providerLat, $providerLng) {
+                if ($providerCityId !== null) {
+                    $query->where('opportunity_address.city_id', $providerCityId);
+                }
+
+                if ($providerLat !== null && $providerLng !== null) {
+                    $method = $providerCityId !== null ? 'orWhereRaw' : 'whereRaw';
+
+                    $query->{$method}(
+                        DistanceService::withinRadiusSqlCondition(
+                            $providerLat,
+                            $providerLng,
+                            self::NEARBY_RADIUS_KM,
+                            'opportunity_address.latitude',
+                            'opportunity_address.longitude',
+                        )
+                    );
+
+                    return;
+                }
+
+                if ($providerCityId === null) {
+                    $query->whereRaw('1 = 0');
+                }
+            })
             ->select(
                 'schedules.id',
                 'schedules.client_id',