Parcourir la source

feat: :sparkles: feat(notificação chamativa)foi adicionado o fluxo de salvamento da notificação no backend

Foi implementado o fluxo responsável pelo salvamento e controle das notificações no backend, permitindo que as notificações enviadas pela aplicação sejam registradas corretamente e associadas ao usuário destinatário

fase:dev | origin:escopo
kayo henrique il y a 4 jours
Parent
commit
ace5fd3467

+ 1 - 0
app/Http/Requests/UpdateMeRequest.php

@@ -23,6 +23,7 @@ class UpdateMeRequest extends FormRequest
             'document'      => 'sometimes|string|nullable',
             'avatar'        => 'sometimes|file|image|mimes:jpg,jpeg,png,webp|max:5120',
             'avatar_base64' => 'sometimes|string|nullable',
+            'push_notifications_enabled' => 'sometimes|boolean',
         ];
     }
 }

+ 1 - 0
app/Http/Requests/UserRequest.php

@@ -27,6 +27,7 @@ class UserRequest extends FormRequest
             'language' => ['sometimes', Rule::enum(LanguageEnum::class)],
             'phone'    => 'sometimes|string|nullable',
             'gender'   => 'sometimes|string|in:'.implode(',', GenderEnum::toArray()),
+            'push_notifications_enabled' => 'sometimes|boolean',
         ];
 
         if ($this->isMethod('post')) {

+ 2 - 0
app/Http/Resources/UserResource.php

@@ -27,6 +27,8 @@ class UserResource extends JsonResource
       'type'        => $this->type,
       'provider_id' => $this->provider?->id,
 
+      'push_notifications_enabled' => $this->push_notifications_enabled,
+
       'provider_daily_price_8h' => $this->provider?->daily_price_8h,
       'provider_daily_price_6h' => $this->provider?->daily_price_6h,
       'provider_daily_price_4h' => $this->provider?->daily_price_4h,

+ 1 - 0
app/Models/User.php

@@ -83,6 +83,7 @@ class User extends Authenticatable
             'type'                  => UserTypeEnum::class,
             'language'              => LanguageEnum::class,
             'registration_complete' => 'boolean',
+            'push_notifications_enabled' => 'boolean',
         ];
     }
 

+ 11 - 2
app/Services/PushNotificationService.php

@@ -22,6 +22,10 @@ class PushNotificationService
      */
     public function sendToUser(User $user, BasePushNotification $notification): void
     {
+        if (! $user->push_notifications_enabled) {
+            return;
+        }
+
         $tokens = DeviceToken::where('user_id', $user->id)
             ->where('app_type', $notification->target()->value)
             ->where('active', true)
@@ -33,10 +37,15 @@ class PushNotificationService
         }
 
         $message = CloudMessage::new()
-            ->withNotification(Notification::create($notification->title(), $notification->body()))
+            ->withNotification(
+                Notification::create(
+                    $notification->title(),
+                    $notification->body()
+                )
+            )
             ->withAndroidConfig(AndroidConfig::fromArray([
                 'notification' => ['channel_id' => 'diaria'],
-                'priority'     => 'high',
+                'priority' => 'high',
             ]));
 
         $report = $this->messaging->sendMulticast($message, $tokens);

+ 1 - 1
app/Services/UserService.php

@@ -102,7 +102,7 @@ class UserService
             ]);
 
             $userFields = array_filter(
-                array_intersect_key($data, array_flip(['name', 'email', 'phone', 'language'])),
+                array_intersect_key($data, array_flip(['name', 'email', 'phone', 'language', 'push_notifications_enabled'])),
                 fn ($v) => $v !== null,
             );
 

+ 23 - 0
database/migrations/2026_08_10_092331_add_push_notifications_enabled_to_users_table.php

@@ -0,0 +1,23 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->boolean('push_notifications_enabled')
+                ->default(false);
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropColumn('push_notifications_enabled');
+        });
+    }
+};