瀏覽代碼

fix: :bug: fix(correções backend ) Foi corrigido a questão da distancia do pedido sob medida

Foi corrigido a questão da distancia do pedido sobmedida, agora ele pegar 20km ou a cidade!

origin: bug-interno
kayo henrique 1 周之前
父節點
當前提交
bfd66ab749
共有 1 個文件被更改,包括 33 次插入0 次删除
  1. 33 0
      app/Services/CustomScheduleService.php

+ 33 - 0
app/Services/CustomScheduleService.php

@@ -13,6 +13,7 @@ use App\Models\ScheduleRefuse;
 use App\Models\ServicePackage;
 use App\Rules\ScheduleBusinessRules;
 use App\Services\NotificationService;
+use App\Services\DistanceService;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -20,6 +21,8 @@ use Illuminate\Support\Facades\Storage;
 
 class CustomScheduleService
 {
+    private const NEARBY_RADIUS_KM = 20.0;
+
     public function __construct(
         private readonly ZipCodeCoordinatesService $zipCodeCoordinatesService,
     ) {}
@@ -253,6 +256,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',
@@ -264,11 +271,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',