فهرست منبع

feat: :sparkles: feat (modulo-de-notificação) Foi finalizado por completo o modulo de notificação

O sistema agora envia notificações automaticamente de acordo com as transições de status do agendamento, além de suportar notificações independentes de status para fluxos específicos. Todo o fluxo de notificações foi estruturado e finalizado, contemplando cenários de aceite, recusa, cancelamento, pagamento, início e finalização de serviços.

fase:dev | origin:escopo
kayo henrique 2 هفته پیش
والد
کامیت
d9db4d8def

+ 2 - 2
app/Http/Controllers/NotificationController.php

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

+ 2 - 3
app/Jobs/FinishScheduleJob.php

@@ -8,6 +8,7 @@ use App\Models\Provider;
 use App\Models\Schedule;
 use App\Models\User;
 use App\Services\EmailService;
+use App\Services\ScheduleService;
 use Carbon\Carbon;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
@@ -61,9 +62,7 @@ class FinishScheduleJob implements ShouldQueue
 
             Log::channel('schedule_end_jobs')->info('Validado com sucesso, atualizado agendamento id: '.$schedule->id.' para status finalizado');
 
-            $schedule->update([
-                'status' => 'finished',
-            ]);
+            app(ScheduleService::class)->updateStatus($schedule->id, 'finished');
 
             $provider = Provider::find($schedule->provider_id);
             $provider->update([

+ 3 - 3
app/Jobs/StartScheduleJob.php

@@ -7,6 +7,7 @@ use Carbon\Carbon;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
+use App\Services\ScheduleService;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
 use Illuminate\Support\Facades\Log;
@@ -55,9 +56,8 @@ class StartScheduleJob implements ShouldQueue
 
             Log::channel('schedule_start_jobs')->info('Validado com sucesso, atualizado agendamento id: '.$schedule->id.' para status iniciado');
 
-            $schedule->update([
-                'status' => 'started',
-            ]);
+            app(ScheduleService::class)->updateStatus($schedule->id, 'started');
+
             $date_time_dispatch = Carbon::parse($date_cleaned.' '.$schedule->end_time);
             FinishScheduleJob::dispatch($schedule->id)->delay($date_time_dispatch);
 

+ 71 - 4
app/Services/CustomScheduleService.php

@@ -9,6 +9,8 @@ use App\Models\Schedule;
 use App\Models\ScheduleProposal;
 use App\Models\ScheduleRefuse;
 use App\Rules\ScheduleBusinessRules;
+use App\Services\NotificationService;
+use App\Enums\NotificationTypeEnum;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -321,6 +323,32 @@ class CustomScheduleService
 
         $this->checkProviderAvailability($providerId, $schedule);
 
+        $provider = Provider::with([
+            'user'
+        ])->findOrFail($providerId);
+
+        $schedule->load([
+            'client.user'
+        ]);
+
+        $notificationService = app(NotificationService::class);
+
+        $notificationService->create([
+            'title' => 'Nova proposta recebida!',
+
+            'description' =>
+            $provider->user->name .
+                ' enviou uma proposta para seu agendamento sob medida.',
+
+            'origin' => 'schedule',
+
+            'origin_id' => $schedule->id,
+
+            'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_NEW_SOLICITATION->value,
+
+            'user_id' => $schedule->client->user_id,
+        ]);
+
         return ScheduleProposal::create([
             'schedule_id' => $scheduleId,
             'provider_id' => $providerId,
@@ -338,8 +366,6 @@ class CustomScheduleService
                 throw new \Exception(__('validation.custom.opportunity.already_assigned'));
             }
 
-            Log::info('vrauu2');
-
             $provider = Provider::find($proposal->provider_id);
             switch ($schedule->period_type) {
                 case '8':
@@ -364,8 +390,8 @@ class CustomScheduleService
             ]);
 
             $schedule->refresh();
-            $schedule->load(['provider.user','client.user']);
-            
+            $schedule->load(['provider.user', 'client.user']);
+
             app(ScheduleService::class)->updateStatus($schedule->id, 'accepted');
 
             ScheduleProposal::where('schedule_id', $schedule->id)
@@ -386,6 +412,25 @@ class CustomScheduleService
                 'provider_id' => $proposal->provider_id,
             ]);
 
+            $notificationService = app(NotificationService::class);
+
+            $notificationService->create([
+                'title' => 'Proposta recusada!',
+
+                'description' =>
+                'O cliente recusou sua proposta de diária.',
+
+                'origin' => 'schedule',
+
+                'origin_id' => $proposal->schedule_id,
+
+                'type' =>
+                NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
+
+                'user_id' => $proposal->provider->user_id,
+            ]);
+
+
             $proposal->delete();
 
             return true;
@@ -577,11 +622,33 @@ class CustomScheduleService
 
     public function refuseOpportunity($scheduleId, $providerId)
     {
+
+        $schedule = Schedule::with(['client.user'])->findOrFail($scheduleId);
+
+        $provider = Provider::with(['user'])->findOrFail($providerId);
+        
         $schedule_refuse = ScheduleRefuse::create([
             'schedule_id' => $scheduleId,
             'provider_id' => $providerId,
         ]);
 
+        $notificationService = app(NotificationService::class);
+
+        $notificationService->create([
+            'title' => 'Oportunidade recusada!',
+
+            'description' => $provider->user->name . ' recusou sua solicitação sob medida.',
+
+            'origin' => 'schedule',
+
+            'origin_id' => $scheduleId,
+
+            'type' =>
+            NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
+
+            'user_id' => $schedule->client->user_id,
+        ]);
+
         return $schedule_refuse;
     }
 }

+ 80 - 128
app/Services/ScheduleService.php

@@ -8,6 +8,7 @@ use App\Models\Schedule;
 use App\Rules\ScheduleBusinessRules;
 use App\Services\NotificationService;
 use App\Enums\NotificationTypeEnum;
+use App\Enums\UserTypeEnum;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\DB;
@@ -275,7 +276,7 @@ class ScheduleService
     {
         try {
             DB::beginTransaction();
-            $schedule = Schedule::with(['provider.user','client.user','address'])->findOrFail($id);
+            $schedule = Schedule::with(['provider.user', 'client.user', 'address'])->findOrFail($id);
 
             $allowedTransitions = [
                 'pending' => ['accepted', 'rejected', 'cancelled'],
@@ -300,7 +301,8 @@ class ScheduleService
             $schedule->update(['status' => $status]);
             $schedule->refresh();
 
-
+            $currentStatus = $schedule->status;
+        
             switch ($status) {
 
                 case 'pending':
@@ -312,7 +314,7 @@ class ScheduleService
 
                     switch (Auth::user()->type) {
 
-                        case 'provider':
+                        case UserTypeEnum::PROVIDER:
 
                             $notificationService->create([
                                 'title' => 'Agendamento aceito!',
@@ -333,7 +335,7 @@ class ScheduleService
 
                             break;
 
-                        case 'client':
+                        case UserTypeEnum::CLIENT:
 
                             if ($schedule->provider_id) {
 
@@ -362,144 +364,40 @@ class ScheduleService
 
                     break;
 
-                case 'rejected':
+                case 'cancelled':
 
                     $notificationService = app(NotificationService::class);
 
                     switch (Auth::user()->type) {
 
-
-                        case 'provider':
+                        case UserTypeEnum::CLIENT:
 
                             $notificationService->create([
-                                'title' => 'Agendamento recusado!',
+                                'title' => 'Agendamento cancelado!',
 
                                 'description' =>
-                                'O diarista não poderá atender. Veja outros profissionais disponíveis.',
+                                'O cliente cancelou o agendamento.',
 
                                 'origin' => 'schedule',
 
                                 'origin_id' => $schedule->id,
 
                                 'type' =>
-                                NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
+                                NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_CANCELLED->value,
 
-                                'user_id' => $schedule->client->user_id,
+                                'user_id' => $schedule->provider->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;
-
-                case 'paid':
-
-                    $notificationService = app(NotificationService::class);
-
-                    switch (Auth::user()->type) {
-
-                        case 'client':
-
-                            if ($schedule->provider_id) {
-
-                                $notificationService->create([
-                                    'title' => 'Pagamento confirmado!',
-
-                                    'description' =>
-                                    'O cliente confirmou o pagamento da diária.',
-
-                                    'origin' => 'schedule',
-
-                                    'origin_id' => $schedule->id,
-
-                                    'type' =>
-                                    NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
-
-                                    'user_id' => $schedule->provider->user_id,
-                                ]);
-                            }
-
-                            break;
-
-                        default:
-                            break;
-                    }
-
-                    $date_cleaned = Carbon::parse($schedule->date)
-                        ->format('Y-m-d');
-
-                    $date_time_dispatch = Carbon::parse(
-                        $date_cleaned . ' ' . $schedule->start_time
-                    )->subHour();
-
-                    StartScheduleJob::dispatch($schedule->id)
-                        ->delay($date_time_dispatch);
-
-                    break;
-
-                case 'cancelled':
-
-                    $notificationService = app(NotificationService::class);
-
-                    switch (Auth::user()->type) {
-
-                        case 'client':
-
-                            if ($schedule->provider_id) {
-
-                                $notificationService->create([
-                                    'title' => 'Agendamento cancelado!',
-
-                                    'description' =>
-                                    'O cliente cancelou a diária.',
-
-                                    'origin' => 'schedule',
-
-                                    'origin_id' => $schedule->id,
-
-                                    'type' =>
-                                    NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_CANCELLED->value,
-
-                                    'user_id' => $schedule->provider->user_id,
-                                ]);
-                            }
-
-                            break;
-
-                        case 'provider':
+                        case UserTypeEnum::PROVIDER:
 
                             $notificationService->create([
                                 'title' => 'Agendamento cancelado!',
 
                                 'description' =>
                                 $schedule->provider->user->name .
-                                    ' cancelou a diária.',
+                                    ' cancelou sua solicitação de diária.',
 
                                 'origin' => 'schedule',
 
@@ -523,13 +421,12 @@ class ScheduleService
 
                     $notificationService = app(NotificationService::class);
 
+                    // CLIENTE
                     $notificationService->create([
                         'title' => 'Diarista a caminho!',
 
                         'description' =>
-                        'Informe o código ' .
-                            $schedule->code .
-                            ' para confirmar a chegada da diarista e liberar o início do serviço.',
+                        'Informe o código ' . $schedule->code . ' para liberar o início do serviço.',
 
                         'origin' => 'schedule',
 
@@ -541,12 +438,12 @@ class ScheduleService
                         'user_id' => $schedule->client->user_id,
                     ]);
 
-
+                    // PRESTADOR
                     $notificationService->create([
-                        'title' => 'Você iniciou o deslocamento!',
+                        'title' => 'Início do serviço!',
 
                         'description' =>
-                        'O cliente foi avisado que você está a caminho.',
+                        'Solicite o código ao cliente para iniciar a diária.',
 
                         'origin' => 'schedule',
 
@@ -564,11 +461,12 @@ class ScheduleService
 
                     $notificationService = app(NotificationService::class);
 
+                    // CLIENTE
                     $notificationService->create([
-                        'title' => 'Diária finalizada!',
+                        'title' => 'Serviço finalizado!',
 
                         'description' =>
-                        'Avalie o serviço feito pelo diarista e conte-nos como foi sua experiência.',
+                        'Sua diária foi finalizada com sucesso.',
 
                         'origin' => 'schedule',
 
@@ -580,26 +478,80 @@ class ScheduleService
                         'user_id' => $schedule->client->user_id,
                     ]);
 
+                    break;
+
+                case 'paid':
+
+                    $notificationService = app(NotificationService::class);
+
+                    switch (Auth::user()->type) {
+
+                        case UserTypeEnum::CLIENT:
+
+                            if ($schedule->provider_id) {
+
+                                $notificationService->create([
+                                    'title' => 'Pagamento confirmado!',
+
+                                    'description' =>
+                                    'O cliente confirmou o pagamento da diária.',
+
+                                    'origin' => 'schedule',
+
+                                    'origin_id' => $schedule->id,
+
+                                    'type' =>
+                                    NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
+
+                                    'user_id' => $schedule->provider->user_id,
+                                ]);
+                            }
+
+                            break;
+
+                        default:
+                            break;
+                    }
+
+                    $date_cleaned = Carbon::parse($schedule->date)
+                        ->format('Y-m-d');
+
+                    $date_time_dispatch = Carbon::parse(
+                        $date_cleaned . ' ' . $schedule->start_time
+                    )->subHour();
+
+                    StartScheduleJob::dispatch($schedule->id)
+                        ->delay($date_time_dispatch);
+
+                    break;
+
+                case 'rejected':
+
+                    $notificationService = app(NotificationService::class);
+
                     $notificationService->create([
-                        'title' => 'Diária concluída!',
+                        'title' => 'Agendamento recusado!',
 
                         'description' =>
-                        'A diária foi finalizada com sucesso.',
+                        'O diarista não poderá atender. Veja outros profissionais disponíveis.',
 
                         'origin' => 'schedule',
 
                         'origin_id' => $schedule->id,
 
                         'type' =>
-                        NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
+                        NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
 
-                        'user_id' => $schedule->provider->user_id,
+                        'user_id' => $schedule->client->user_id,
                     ]);
 
                     break;
             }
 
 
+
+
+
             DB::commit();
 
             return $schedule->fresh(['client.user', 'provider.user', 'address']);