Bladeren bron

fix: corrige endpoint de login para permitir prestador nao verificado a logar (todas outras acoes sao restritas via middleware) + ajuste resource de client favourite providers para exibir preco com taxa aplicada

Gustavo Mantovani 1 week geleden
bovenliggende
commit
719e3b8620

+ 48 - 10
app/Http/Resources/ClientFavoriteProviderResource.php

@@ -4,26 +4,64 @@ namespace App\Http\Resources;
 
 use Illuminate\Http\Request;
 use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Support\Facades\Storage;
 
 class ClientFavoriteProviderResource extends JsonResource
 {
     public function toArray(Request $request): array
     {
+        $provider = $this->resource->relationLoaded('provider') ? $this->resource->provider : null;
+
+        $profileMediaPath = $this->profile_media_path
+            ?? ($provider?->relationLoaded('profileMedia') ? $provider->profileMedia?->path : null);
+
+        $dailyPrice8h = $this->daily_price_8h ?? $provider?->daily_price_8h;
+        $dailyPrice6h = $this->daily_price_6h ?? $provider?->daily_price_6h;
+        $dailyPrice4h = $this->daily_price_4h ?? $provider?->daily_price_4h;
+        $dailyPrice2h = $this->daily_price_2h ?? $provider?->daily_price_2h;
+
         return [
             'id'             => $this->id,
             'client_id'      => $this->client_id,
             'provider_id'    => $this->provider_id,
-            'provider_name'  => $this->provider_name ?? $this->provider?->user?->name,
+            'provider_name'  => $this->provider_name ?? ($provider?->relationLoaded('user') ? $provider->user?->name : null),
             'city_name'      => $this->city_name ?? null,
-            'average_rating' => $this->average_rating ?? null,
-            'daily_price_8h' => $this->daily_price_8h ?? null,
-            'daily_price_6h' => $this->daily_price_6h ?? null,
-            'daily_price_4h' => $this->daily_price_4h ?? null,
-            'daily_price_2h' => $this->daily_price_2h ?? null,
-            'total_services' => $this->total_services ?? null,
-            'notes'          => $this->notes,
-            'created_at'     => $this->created_at?->format('Y-m-d H:i'),
-            'updated_at'     => $this->updated_at?->format('Y-m-d H:i'),
+            'average_rating' => $this->average_rating ?? $provider?->average_rating,
+
+            'profile_media_url' => $profileMediaPath
+                ? Storage::temporaryUrl($profileMediaPath, now()->addMinutes(60))
+                : null,
+
+            'daily_price_8h_base' => $dailyPrice8h,
+            'daily_price_6h_base' => $dailyPrice6h,
+            'daily_price_4h_base' => $dailyPrice4h,
+            'daily_price_2h_base' => $dailyPrice2h,
+            'daily_price_8h'      => $this->applyCreditCardFee($dailyPrice8h),
+            'daily_price_6h'      => $this->applyCreditCardFee($dailyPrice6h),
+            'daily_price_4h'      => $this->applyCreditCardFee($dailyPrice4h),
+            'daily_price_2h'      => $this->applyCreditCardFee($dailyPrice2h),
+            'total_services '     => $this->total_services ?? $provider?->total_services,
+            'notes'               => $this->notes,
+            'created_at'          => $this->created_at?->format('Y-m-d H:i'),
+            'updated_at'          => $this->updated_at?->format('Y-m-d H:i'),
         ];
     }
+
+    private function applyCreditCardFee(mixed $price): ?float
+    {
+        if ($price === null) {
+            return null;
+        }
+
+        $rate = config('services.pagarme.platform_credit_card_fee_rate', 0.16);
+
+        if (is_string($rate)) {
+            $rate = str_replace(',', '.', trim($rate));
+        }
+
+        $rate = (float) $rate;
+        $rate = $rate > 1 ? $rate / 100 : $rate;
+
+        return round((float) $price * (1 + $rate), 2);
+    }
 }

+ 0 - 15
app/Services/AuthService.php

@@ -2,7 +2,6 @@
 
 namespace App\Services;
 
-use App\Enums\ApprovalStatusEnum;
 use App\Enums\UserTypeEnum;
 use App\Models\Address;
 use App\Models\Client;
@@ -223,12 +222,6 @@ class AuthService
             if ($user) {
                 $provider = Provider::where('user_id', $user->id)->first();
 
-                if ($provider && $provider->approval_status->value !== ApprovalStatusEnum::ACCEPTED->value) {
-                    DB::rollBack();
-
-                    return ['error' => 'provider_not_accepted'];
-                }
-
                 $isLogin = $provider !== null;
 
                 $user->code           = $code;
@@ -334,14 +327,6 @@ class AuthService
                 return ['error' => 'registration_incomplete'];
             }
 
-            if ($provider->approval_status === ApprovalStatusEnum::PENDING) {
-                return ['error' => 'provider_pending'];
-            }
-
-            if ($provider->approval_status === ApprovalStatusEnum::REJECTED) {
-                return ['error' => 'provider_rejected'];
-            }
-
             return $this->loginWithEmail($user->email, $code, UserTypeEnum::PROVIDER->value);
         }
 

+ 5 - 3
app/Services/ClientFavoriteProviderService.php

@@ -16,6 +16,7 @@ class ClientFavoriteProviderService
             ->whereRaw("providers.recipient_default_bank_account::text <> '{}'")
             ->whereRaw("providers.recipient_default_bank_account::text <> '[]'")
             ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
+            ->leftJoin('media as provider_profile_media', 'provider_profile_media.id', '=', 'providers.profile_media_id')
             ->leftJoin('addresses as provider_address', function ($join) {
                 $join->on('provider_address.source_id', '=', 'providers.id')
                     ->where('provider_address.source', 'provider')
@@ -30,6 +31,7 @@ class ClientFavoriteProviderService
                 'client_favorite_providers.created_at',
                 'client_favorite_providers.updated_at',
                 'provider_user.name as provider_name',
+                'provider_profile_media.path as profile_media_path',
                 'providers.average_rating',
                 'providers.daily_price_8h',
                 'providers.daily_price_6h',
@@ -44,7 +46,7 @@ class ClientFavoriteProviderService
 
     public function getById(int $id): ClientFavoriteProviderResource
     {
-        $favorite = ClientFavoriteProvider::with('provider')->findOrFail($id);
+        $favorite = ClientFavoriteProvider::with(['provider.user', 'provider.profileMedia'])->findOrFail($id);
 
         return new ClientFavoriteProviderResource($favorite);
     }
@@ -53,7 +55,7 @@ class ClientFavoriteProviderService
     {
         $favorite = ClientFavoriteProvider::create($data);
 
-        $favorite->load('provider');
+        $favorite->load(['provider.user', 'provider.profileMedia']);
 
         return new ClientFavoriteProviderResource($favorite);
     }
@@ -64,7 +66,7 @@ class ClientFavoriteProviderService
 
         $favorite->update($data);
 
-        $favorite->load('provider');
+        $favorite->load(['provider.user', 'provider.profileMedia']);
 
         return new ClientFavoriteProviderResource($favorite);
     }