Browse Source

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

Foi criado o modulo completo com todas as informações necessarias para envio de notificação para cleiente e prestador

fase:dev | origin:escopo
kayo henrique 2 tuần trước cách đây
mục cha
commit
cfedd96208

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

@@ -0,0 +1,80 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Notification;
+use Carbon\Carbon;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Support\Facades\Auth;
+
+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')
+            ->limit(50)
+            ->get()
+            ->map(function ($notification) {
+
+                return [
+                    'id' => $notification->id,
+
+                    'title' => $notification->title,
+
+                    'description' => $notification->description,
+
+                    'origin' => $notification->origin,
+
+                    'origin_id' => $notification->origin_id,
+
+                    'type' => $notification->type,
+
+                    'read' => $notification->read,
+
+                    'time' => Carbon::parse(
+                        $notification->created_at
+                    )->diffForHumans(),
+                ];
+            });
+
+        return $this->successResponse(
+            payload: $notifications
+        );
+    }
+
+    public function markAsRead(int $id): JsonResponse
+    {
+        $notification = Notification::where('id', $id)
+            ->where('user_id', Auth::id())
+            ->firstOrFail();
+
+        $notification->update([
+            'read' => true,
+
+            'read_at' => now(),
+        ]);
+
+        return $this->successResponse(
+            message: __('messages.updated')
+        );
+    }
+
+    public function markAllAsRead(): JsonResponse
+    {
+        Notification::where('user_id', Auth::id())
+            ->where('read', false)
+            ->update([
+                'read' => true,
+
+                'read_at' => now(),
+            ]);
+
+        return $this->successResponse(
+            message: __('messages.updated')
+        );
+    }
+}

+ 22 - 3
app/Services/CustomScheduleService.php

@@ -12,6 +12,8 @@ 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
 {
@@ -102,7 +104,7 @@ class CustomScheduleService
             return $createdCustomSchedules;
         } catch (\Exception $e) {
             DB::rollBack();
-            Log::error('Error creating custom schedule: '.$e->getMessage());
+            Log::error('Error creating custom schedule: ' . $e->getMessage());
             throw $e;
         }
     }
@@ -181,7 +183,7 @@ class CustomScheduleService
             ]);
         } catch (\Exception $e) {
             DB::rollBack();
-            Log::error('Error updating custom schedule: '.$e->getMessage());
+            Log::error('Error updating custom schedule: ' . $e->getMessage());
             throw $e;
         }
     }
@@ -204,7 +206,7 @@ class CustomScheduleService
             return $customSchedule;
         } catch (\Exception $e) {
             DB::rollBack();
-            Log::error('Error deleting custom schedule: '.$e->getMessage());
+            Log::error('Error deleting custom schedule: ' . $e->getMessage());
             throw $e;
         }
     }
@@ -364,6 +366,23 @@ class CustomScheduleService
                 '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,
+            ]);
+
             ScheduleProposal::where('schedule_id', $schedule->id)
                 ->where('id', '!=', $proposalId)
                 ->delete();

+ 64 - 25
app/Services/ScheduleService.php

@@ -43,19 +43,52 @@ class ScheduleService
     public function createSingleOrMultiple(array $baseData, array $schedules)
     {
         try {
+
             DB::beginTransaction();
+
             $createdSchedules = [];
+
             foreach ($schedules as $schedule) {
+
                 $datasMerged = array_merge($baseData, $schedule);
+
                 $this->validateProviderAvailability($datasMerged, null);
+
                 $scheduleData = array_merge($datasMerged, [
                     'code' => str_pad(random_int(0, 9999), 4, '0', STR_PAD_LEFT),
                 ]);
-                $createdSchedules[] = Schedule::create($scheduleData);
+
+                $newSchedule = Schedule::create($scheduleData);
+
+                /*NOTIFICAÇÃO PRESTADOR*/
+                if ($newSchedule->provider_id) {
+
+                    $notificationService = app(NotificationService::class);
+
+                    $notificationService->create([
+                        'title' => 'Nova solicitação de diária!',
+
+                        'description' =>
+                        'Você recebeu uma nova solicitação de diária.',
+
+                        'origin' => 'schedule',
+
+                        'origin_id' => $newSchedule->id,
+
+                        'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_NEW_SOLICITATION->value,
+
+                        'user_id' => $newSchedule->provider->user_id,
+                    ]);
+                }
+
+                $createdSchedules[] = $newSchedule;
             }
+
             DB::commit();
         } catch (\Exception $e) {
+
             DB::rollBack();
+
             throw new \Exception(__($e->getMessage()));
         }
 
@@ -264,8 +297,6 @@ class ScheduleService
 
             $schedule->update(['status' => $status]);
 
-            $schedule->update(['status' => $status]);
-
             switch ($status) {
 
                 case 'pending':
@@ -357,43 +388,51 @@ class ScheduleService
                     $notificationService = app(NotificationService::class);
 
 
-                    if ($schedule->cancelled_by === 'client') {
+                    switch (Auth::user()->type) {
+                        case 'client':
+
+                            // PRESTADOR
+                            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;
 
-                        if ($schedule->provider_id) {
+                        case 'provider':
 
+                            // CLIENTE
                             $notificationService->create([
                                 'title' => 'Agendamento cancelado!',
 
                                 'description' =>
-                                'O cliente cancelou o agendamento.',
+                                $schedule->provider->user->name .
+                                    ' cancelou a diária.',
 
                                 'origin' => 'schedule',
 
                                 'origin_id' => $schedule->id,
 
-                                'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_CANCELLED->value,
+                                'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_CANCELLED->value,
 
-                                'user_id' => $schedule->provider->user_id,
+                                'user_id' => $schedule->client->user_id,
                             ]);
-                        }
-                    } else {
-
-                        $notificationService->create([
-                            'title' => 'Agendamento cancelado!',
 
-                            'description' =>
-                            'Infelizmente ' .
-                                $schedule->provider->user->name .
-                                ' cancelou o agendamento. O valor pago será extornado em breve.',
+                            break;
 
-                            'origin' => 'schedule',
-
-                            'origin_id' => $schedule->id,
-
-                            'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_CANCELLED->value,
-
-                            'user_id' => $schedule->client->user_id,
-                        ]);
+                        default:
+                            break;
                     }
 
                     break;

+ 7 - 0
database/seeders/PermissionSeeder.php

@@ -202,6 +202,13 @@ class PermissionSeeder extends Seeder
                         'bits'        => 271,
                         'children'    => [],
                     ],
+
+                    [
+                        'scope'       => 'notification',
+                        'description' => 'Notificações',
+                        'bits'        => 271,
+                        'children'    => [],
+                    ],
                 ],
             ],
         ];

+ 3 - 0
database/seeders/UserTypePermissionSeeder.php

@@ -53,6 +53,7 @@ class UserTypePermissionSeeder extends Seeder
                         ['scope' => 'config.speciality', 'bits' => 271],
                         ['scope' => 'config.review', 'bits' => 271],
                         ['scope' => 'config.review_improvement', 'bits' => 271],
+                        ['scope' => 'notification', 'bits' => 271],
                     ];
                     $this->seedUserTypePermissions($userPermissions, UserTypeEnum::USER->value);
                     break;
@@ -74,6 +75,7 @@ class UserTypePermissionSeeder extends Seeder
                         ['scope' => 'config.improvement_type', 'bits' => 1],
                         ['scope' => 'config.review', 'bits' => 271],
                         ['scope' => 'config.provider_client_block', 'bits' => 9],
+                        ['scope' => 'notification', 'bits' => 271],
                     ];
                     $this->seedUserTypePermissions($providerPermissions, UserTypeEnum::PROVIDER->value);
                     break;
@@ -96,6 +98,7 @@ class UserTypePermissionSeeder extends Seeder
                         ['scope' => 'config.custom_schedule', 'bits' => 271],
                         ['scope' => 'config.speciality', 'bits' => 271],
                         ['scope' => 'config.service_type', 'bits' => 271],
+                        ['scope' => 'notification', 'bits' => 271],
                     ];
                     $this->seedUserTypePermissions($clientPermissions, UserTypeEnum::CLIENT->value);
                     break;

+ 10 - 0
routes/authRoutes/notifications.php

@@ -0,0 +1,10 @@
+<?php
+
+use App\Http\Controllers\NotificationController;
+use Illuminate\Support\Facades\Route;
+
+Route::get('/notifications',[NotificationController::class, 'index'])->middleware('permission:notification,view');
+
+Route::put('/notifications/{id}/read',[NotificationController::class, 'markAsRead'])->middleware('permission:notification,add');
+
+Route::put('/notifications/read-all',[NotificationController::class, 'markAllAsRead'])->middleware('permission:notification,add');