Преглед изворни кода

feat: :sparkles: feat (modulo-de-notificação) Refazendo o fluxo

Esta sendo refeita o fluxo do envio de notificações para que ele seja atualizado atraves do status passado

fase:dev | origin:escopo
kayo henrique пре 2 недеља
родитељ
комит
49c418afb4

+ 4 - 0
app/Http/Controllers/NotificationController.php

@@ -11,8 +11,12 @@ class NotificationController extends Controller
 {
     public function index(): JsonResponse
     {
+
+        
         $user = Auth::user();
 
+    
+
         $notifications = Notification::where('user_id', $user->id)
             ->orderBy('read', 'asc')
             ->orderBy('created_at', 'desc')

+ 4 - 19
app/Services/CustomScheduleService.php

@@ -12,8 +12,6 @@ use App\Rules\ScheduleBusinessRules;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
-use App\Enums\NotificationTypeEnum;
-use App\Services\NotificationService;
 
 class CustomScheduleService
 {
@@ -363,25 +361,12 @@ class CustomScheduleService
 
             $schedule->update([
                 'provider_id' => $proposal->provider_id,
-                'status'      => 'accepted',
             ]);
 
-            $notificationService = app(NotificationService::class);
-
-            $notificationService->create([
-                'title' => 'Proposta aceita!',
-
-                'description' =>
-                'O cliente aceitou sua proposta de diária.',
-
-                'origin' => 'schedule',
-
-                'origin_id' => $schedule->id,
-
-                'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_PROPOSAL_ACCEPTED->value,
-
-                'user_id' => $provider->user_id,
-            ]);
+            $schedule->refresh();
+            $schedule->load(['provider.user','client.user']);
+            
+            app(ScheduleService::class)->updateStatus($schedule->id, 'accepted');
 
             ScheduleProposal::where('schedule_id', $schedule->id)
                 ->where('id', '!=', $proposalId)

+ 165 - 48
app/Services/ScheduleService.php

@@ -97,8 +97,10 @@ class ScheduleService
 
     public function update($id, array $data)
     {
-        $schedule = Schedule::findOrFail($id);
 
+        unset($data['status']);
+
+        $schedule = Schedule::with(['provider.user', 'client.user', 'address'])->findOrFail($id);
         if (isset($data['provider_id']) || isset($data['period_type'])) {
             $providerId           = $data['provider_id'] ?? $schedule->provider_id;
             $periodType           = $data['period_type'] ?? $schedule->period_type;
@@ -273,10 +275,10 @@ class ScheduleService
     {
         try {
             DB::beginTransaction();
-            $schedule = Schedule::findOrFail($id);
+            $schedule = Schedule::with(['provider.user','client.user','address'])->findOrFail($id);
 
             $allowedTransitions = [
-                'pending'   => ['accepted', 'rejected'],
+                'pending' => ['accepted', 'rejected', 'cancelled'],
                 'accepted'  => ['paid', 'cancelled'],
                 'paid'      => ['cancelled', 'started'],
                 'started'   => ['finished'],
@@ -296,33 +298,67 @@ class ScheduleService
             }
 
             $schedule->update(['status' => $status]);
+            $schedule->refresh();
+
 
             switch ($status) {
 
                 case 'pending':
-
                     break;
 
                 case 'accepted':
 
                     $notificationService = app(NotificationService::class);
 
-                    // CLIENTE
-                    $notificationService->create([
-                        'title' => 'Agendamento aceito!',
+                    switch (Auth::user()->type) {
 
-                        'description' =>
-                        $schedule->provider->user->name .
-                            ' aceitou sua solicitação de diária.',
+                        case 'provider':
 
-                        'origin' => 'schedule',
+                            $notificationService->create([
+                                'title' => 'Agendamento aceito!',
 
-                        'origin_id' => $schedule->id,
+                                'description' =>
+                                $schedule->provider->user->name .
+                                    ' aceitou sua solicitação de diária.',
 
-                        'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_ACCEPTED->value,
+                                'origin' => 'schedule',
 
-                        'user_id' => $schedule->client->user_id,
-                    ]);
+                                'origin_id' => $schedule->id,
+
+                                'type' =>
+                                NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_ACCEPTED->value,
+
+                                'user_id' => $schedule->client->user_id,
+                            ]);
+
+                            break;
+
+                        case 'client':
+
+                            if ($schedule->provider_id) {
+
+                                $notificationService->create([
+                                    'title' => 'Proposta aceita!',
+
+                                    'description' =>
+                                    'O cliente aceitou sua proposta de diária.',
+
+                                    'origin' => 'schedule',
+
+                                    'origin_id' => $schedule->id,
+
+                                    'type' =>
+                                    NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_PROPOSAL_ACCEPTED->value,
+
+                                    'user_id' => $schedule->provider->user_id,
+                                ]);
+                            }
+
+                            break;
+
+                        default:
+                            break;
+                    }
 
                     break;
 
@@ -330,21 +366,55 @@ class ScheduleService
 
                     $notificationService = app(NotificationService::class);
 
-                    // CLIENTE
-                    $notificationService->create([
-                        'title' => 'Agendamento recusado!',
+                    switch (Auth::user()->type) {
 
-                        'description' =>
-                        'O diarista não poderá atender. Veja outros profissionais disponíveis.',
 
-                        'origin' => 'schedule',
+                        case 'provider':
 
-                        'origin_id' => $schedule->id,
+                            $notificationService->create([
+                                'title' => 'Agendamento recusado!',
 
-                        'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
+                                'description' =>
+                                'O diarista não poderá atender. Veja outros profissionais disponíveis.',
 
-                        'user_id' => $schedule->client->user_id,
-                    ]);
+                                'origin' => 'schedule',
+
+                                'origin_id' => $schedule->id,
+
+                                'type' =>
+                                NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
+
+                                'user_id' => $schedule->client->user_id,
+                            ]);
+
+                            break;
+
+                        case 'client':
+
+                            if ($schedule->provider_id) {
+
+                                $notificationService->create([
+                                    'title' => 'Proposta recusada!',
+
+                                    'description' =>
+                                    'O cliente recusou sua proposta de diária.',
+
+                                    'origin' => 'schedule',
+
+                                    'origin_id' => $schedule->id,
+
+                                    'type' =>
+                                    NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_CANCELLED->value,
+
+                                    'user_id' => $schedule->provider->user_id,
+                                ]);
+                            }
+
+                            break;
+
+                        default:
+                            break;
+                    }
 
                     break;
 
@@ -352,23 +422,33 @@ class ScheduleService
 
                     $notificationService = app(NotificationService::class);
 
-                    // PRESTADOR
-                    if ($schedule->provider_id) {
+                    switch (Auth::user()->type) {
+
+                        case 'client':
+
+                            if ($schedule->provider_id) {
+
+                                $notificationService->create([
+                                    'title' => 'Pagamento confirmado!',
 
-                        $notificationService->create([
-                            'title' => 'Pagamento confirmado!',
+                                    'description' =>
+                                    'O cliente confirmou o pagamento da diária.',
 
-                            'description' =>
-                            'O cliente confirmou o pagamento da diária.',
+                                    'origin' => 'schedule',
 
-                            'origin' => 'schedule',
+                                    'origin_id' => $schedule->id,
 
-                            'origin_id' => $schedule->id,
+                                    'type' =>
+                                    NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
 
-                            'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
+                                    'user_id' => $schedule->provider->user_id,
+                                ]);
+                            }
 
-                            'user_id' => $schedule->provider->user_id,
-                        ]);
+                            break;
+
+                        default:
+                            break;
                     }
 
                     $date_cleaned = Carbon::parse($schedule->date)
@@ -387,22 +467,24 @@ class ScheduleService
 
                     $notificationService = app(NotificationService::class);
 
-
                     switch (Auth::user()->type) {
+
                         case 'client':
 
-                            // PRESTADOR
                             if ($schedule->provider_id) {
 
-                                $notificationService->create(['title' => 'Agendamento cancelado!',
+                                $notificationService->create([
+                                    'title' => 'Agendamento cancelado!',
 
-                                    'description' =>'O cliente cancelou a diária.',
+                                    'description' =>
+                                    'O cliente cancelou a diária.',
 
                                     'origin' => 'schedule',
 
                                     'origin_id' => $schedule->id,
 
-                                    'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_CANCELLED->value,
+                                    'type' =>
+                                    NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_CANCELLED->value,
 
                                     'user_id' => $schedule->provider->user_id,
                                 ]);
@@ -412,7 +494,6 @@ class ScheduleService
 
                         case 'provider':
 
-                            // CLIENTE
                             $notificationService->create([
                                 'title' => 'Agendamento cancelado!',
 
@@ -424,7 +505,8 @@ class ScheduleService
 
                                 'origin_id' => $schedule->id,
 
-                                'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_CANCELLED->value,
+                                'type' =>
+                                NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_CANCELLED->value,
 
                                 'user_id' => $schedule->client->user_id,
                             ]);
@@ -441,7 +523,6 @@ class ScheduleService
 
                     $notificationService = app(NotificationService::class);
 
-                    // CLIENTE
                     $notificationService->create([
                         'title' => 'Diarista a caminho!',
 
@@ -454,18 +535,35 @@ class ScheduleService
 
                         'origin_id' => $schedule->id,
 
-                        'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_COMING->value,
+                        'type' =>
+                        NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_COMING->value,
 
                         'user_id' => $schedule->client->user_id,
                     ]);
 
+
+                    $notificationService->create([
+                        'title' => 'Você iniciou o deslocamento!',
+
+                        'description' =>
+                        'O cliente foi avisado que você está a caminho.',
+
+                        'origin' => 'schedule',
+
+                        'origin_id' => $schedule->id,
+
+                        'type' =>
+                        NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
+
+                        'user_id' => $schedule->provider->user_id,
+                    ]);
+
                     break;
 
                 case 'finished':
 
                     $notificationService = app(NotificationService::class);
 
-                    // CLIENTE
                     $notificationService->create([
                         'title' => 'Diária finalizada!',
 
@@ -476,14 +574,32 @@ class ScheduleService
 
                         'origin_id' => $schedule->id,
 
-                        'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_FINISHED->value,
+                        'type' =>
+                        NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_FINISHED->value,
 
                         'user_id' => $schedule->client->user_id,
                     ]);
 
+                    $notificationService->create([
+                        'title' => 'Diária concluída!',
+
+                        'description' =>
+                        'A diária foi finalizada com sucesso.',
+
+                        'origin' => 'schedule',
+
+                        'origin_id' => $schedule->id,
+
+                        'type' =>
+                        NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
+
+                        'user_id' => $schedule->provider->user_id,
+                    ]);
+
                     break;
             }
 
+
             DB::commit();
 
             return $schedule->fresh(['client.user', 'provider.user', 'address']);
@@ -508,11 +624,12 @@ class ScheduleService
             $cancelled_by = Auth::user()->type;
 
             $schedule->update([
-                'status'       => 'cancelled',
                 'cancel_text'  => $cancelText,
                 'cancelled_by' => $cancelled_by,
             ]);
 
+            $this->updateStatus($schedule->id, 'cancelled');
+
             DB::commit();
 
             return $schedule->fresh(['client.user', 'provider.user', 'address']);