Kaynağa Gözat

fix: :bug: fix(correções backend) Foi ajustado o envio da notificação por push de pagamento

Foi ajustado o envio da notificação por push de pagamento

fase:dev | origin:escopo
kayo henrique 4 gün önce
ebeveyn
işleme
13f08a4151

+ 1 - 13
app/Services/CustomScheduleService.php

@@ -578,21 +578,9 @@ class CustomScheduleService
                 'user_id'     => $provider->user_id,
             ]);
 
-            try {
-                app(PushNotificationService::class)->sendToUser(
-                    $provider->user,
-                    new ClienteAceitouPush($schedule->client->user->name)
-                );
-            } catch (\Throwable $exception) {
-                Log::error('Falha ao enviar push de cliente aceitou proposta', [
-                    'schedule_id' => $schedule->id,
-                    'provider_id' => $provider->id,
-                    'error' => $exception->getMessage(),
-                ]);
-            }
 
 
-            app(ScheduleService::class)->updateStatus($schedule->id, 'accepted');
+            app(ScheduleService::class)->updateStatus($schedule->id, 'paid');
 
             ScheduleProposal::where('schedule_id', $schedule->id)
                 ->where('id', '!=', $proposalId)

+ 10 - 4
app/Services/PaymentService.php

@@ -16,6 +16,7 @@ use App\Models\PaymentSplit;
 use App\Models\Schedule;
 use App\Models\ScheduleProposal;
 use App\Services\Pagarme\PagarmePaymentService;
+use App\Services\ScheduleService;
 use Carbon\Carbon;
 use Illuminate\Auth\Access\AuthorizationException;
 use Illuminate\Database\Eloquent\Collection;
@@ -817,10 +818,15 @@ Log::info('10');
           ->where('status', 'accepted')
           ->get();
 
-        Schedule::query()
-          ->whereIn('id', $schedulesToPay->pluck('id'))
-          ->where('status', 'accepted')
-          ->update(['status' => 'paid']);
+
+          foreach ($schedulesToPay as $schedule) {
+             app(ScheduleService::class)->updateStatus($schedule->id, 'paid');
+          }
+
+        // Schedule::query()
+        //   ->whereIn('id', $schedulesToPay->pluck('id'))
+        //   ->where('status', 'accepted')
+        //   ->update(['status' => 'paid']);
 
         if ($servicePackage->status !== ServicePackageStatusEnum::PAID) {
           $servicePackage->update(['status' => ServicePackageStatusEnum::PAID->value]);

+ 14 - 21
app/Services/ScheduleService.php

@@ -166,7 +166,7 @@ class ScheduleService
             }
 
             $allowedTransitions = [
-                'pending'   => ['accepted', 'rejected', 'cancelled'],
+                'pending'   => ['accepted', 'rejected', 'paid', 'cancelled'],
                 'accepted'  => ['paid', 'cancelled'],
                 'paid'      => ['cancelled', 'started'],
                 'started'   => ['finished'],
@@ -182,6 +182,7 @@ class ScheduleService
             }
 
             if (! in_array($status, data_get($allowedTransitions, $currentStatus))) {
+                log::info("Transição de status inválida: {$currentStatus} para {$status}");
                 throw new ScheduleStatusTransitionException;
             }
 
@@ -196,7 +197,6 @@ class ScheduleService
 
                 case 'accepted':
                     $notificationService = app(NotificationService::class);
-
                     switch (Auth::user()?->type) {
                         case UserTypeEnum::PROVIDER:
                             $notificationService->create([
@@ -315,25 +315,18 @@ class ScheduleService
 
                 case 'paid':
                     $notificationService = app(NotificationService::class);
-
-                    switch (Auth::user()?->type) {
-                        case UserTypeEnum::CLIENT:
-                            if ($schedule->provider_id) {
-                                $notificationService->create([
-                                    'title'       => __('notifications.payment_confirmed_title'),
-                                    'description' => __('notifications.payment_confirmed_description'),
-                                    'origin'      => 'schedule',
-                                    'origin_id'   => $schedule->id,
-                                    'type'        => NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
-                                    'user_id'     => $schedule->provider->user_id,
-                                ]);
-                            }
-                            $this->sendClientPaymentPush($schedule);
-                            break;
-
-                        default:
-                            break;
+                    if ($schedule->provider_id) {
+                
+                        $notificationService->create([
+                            'title'       => __('notifications.payment_confirmed_title'),
+                            'description' => __('notifications.payment_confirmed_description'),
+                            'origin'      => 'schedule',
+                            'origin_id'   => $schedule->id,
+                            'type'        => NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
+                            'user_id'     => $schedule->provider->user_id,
+                        ]);
                     }
+                    $this->sendClientPaymentPush($schedule);
 
                     $date_cleaned = Carbon::parse($schedule->date)
                         ->format('Y-m-d');
@@ -669,7 +662,6 @@ class ScheduleService
     private function sendClientPaymentPush(Schedule $schedule): void
     {
         $user = $schedule->provider?->user;
-
         if (! $user) {
             Log::warning('Push de pagamento ignorado: prestador sem usuário', [
                 'schedule_id' => $schedule->id,
@@ -684,6 +676,7 @@ class ScheduleService
                 new ClienteEfetuouPagamentoPush(
                     $schedule->client?->user?->name ?? 'Cliente'
                 )
+
             );
         } catch (\Throwable $exception) {
             Log::error('Falha ao enviar push de pagamento ao prestador', [