]> _ Git - psq.git/commitdiff
create just subscribed notification + subscribe action
authorLouis Jeckel <louis.jeckel@outlook.cm>
Thu, 17 Sep 2020 18:19:53 +0000 (20:19 +0200)
committerLouis Jeckel <louis.jeckel@outlook.cm>
Thu, 17 Sep 2020 18:19:53 +0000 (20:19 +0200)
app/Notifications/JustSubscribed.php [new file with mode: 0644]
app/Nova/Actions/SendNotification.php
app/Nova/Actions/SubscribeUser.php [new file with mode: 0644]
app/Nova/DiscoverUsers.php
app/Nova/Prospect.php

diff --git a/app/Notifications/JustSubscribed.php b/app/Notifications/JustSubscribed.php
new file mode 100644 (file)
index 0000000..f0a783d
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Notifications;
+
+use App\Mail\MailMessage;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Notifications\Notification;
+
+class JustSubscribed extends Notification
+{
+    use Queueable;
+
+    /**
+     * Create a new notification instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Get the notification's delivery channels.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function via($notifiable)
+    {
+        return ['mail'];
+    }
+
+    /**
+     * Get the mail representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return \Illuminate\Notifications\Messages\MailMessage
+     */
+    public function toMail($notifiable)
+    {
+        $message = (new MailMessage)
+                    ->subject('PRESCRIPTION SANTÉ QUOTIDIEN : Abonnement activé !')
+                    ->line('Votre abonnement à PRESCRIPTION SANTÉ QUOTIDIEN est désormais actif !')
+                    ->addTag('just-subscribed');
+
+
+        if($notifiable->reg_complete) {
+            $message->line("Votre compte est déjà activé, vous n'avez rien de plus à faire.");
+        } else {
+            $link = $notifiable->routeWithToken('account.index', [], now()->addDays(7));
+            $message
+                ->line("En cliquant sur le bouton ci-dessous vous devez vous créer un mot de passe.")
+                ->action('Créer mon mot de passe', $link)
+                ->line("Cela ne prendra pas plus d'une minute !");
+        }
+
+        return $message;
+
+    }
+
+    /**
+     * Get the array representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function toArray($notifiable)
+    {
+        return [
+            //
+        ];
+    }
+}
index 404616da00b01cf23777f6aa2cd7d53f334c56b1..8155480f70372b77bcaec5a9dce942ef20c3a6a3 100644 (file)
@@ -47,8 +47,9 @@ class SendNotification extends Action
     {
         return [
             Select::make('Notification')->options([
-                Notifications\Welcome::class => 'Message de bienvenue',
+//                Notifications\Welcome::class => 'Message de bienvenue',
                 Notifications\RegistrationPending::class => 'Rappel',
+                Notifications\JustSubscribed::class => 'Message abonnement activé'
             ])
         ];
     }
diff --git a/app/Nova/Actions/SubscribeUser.php b/app/Nova/Actions/SubscribeUser.php
new file mode 100644 (file)
index 0000000..215fd25
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+namespace App\Nova\Actions;
+
+use App\Notifications\JustSubscribed;
+use App\Nova\Organization;
+use App\User;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Support\Collection;
+use Laravel\Nova\Actions\Action;
+use Laravel\Nova\Fields\ActionFields;
+use Laravel\Nova\Fields\BelongsTo;
+use Laravel\Nova\Fields\Boolean;
+use Laravel\Nova\Fields\Select;
+
+class SubscribeUser extends Action
+{
+    use InteractsWithQueue, Queueable;
+
+    public $showOnTableRow = true;
+    public $name = "Activer abonnement";
+    /**
+     * Perform the action on the given models.
+     *
+     * @param  \Laravel\Nova\Fields\ActionFields  $fields
+     * @param  \Illuminate\Support\Collection  $models
+     * @return mixed
+     */
+    public function handle(ActionFields $fields, Collection $models)
+    {
+        $users = $models->filter(fn($user) => $user->type !== User::TYPE_SUBSCRIBER);
+
+        $users->map(function(User $user) use($fields) {
+
+            $user->type = User::TYPE_SUBSCRIBER;
+            $user->organization()->associate(\App\Organization::query()->findOrFail($fields->org_id));
+            $user->save();
+            if(! $fields->dont_notify) {
+                $user->notify(new JustSubscribed);
+            }
+        });
+
+        return Action::message(count($users). " abonnements activés !");
+    }
+
+    /**
+     * Get the fields available on the action.
+     *
+     * @return array
+     */
+    public function fields()
+    {
+        return [
+            Select::make('Organisation', 'org_id')->options(
+                \App\Organization::query()
+                    ->orderBy('name')
+                    ->where('subscription_active', 1)
+                    ->get()
+                    ->pluck('name', 'id')
+
+            )->searchable(),
+            Boolean::make("Ne pas notifier", 'dont_notify')
+        ];
+    }
+}
index 120a749a0c0351d2311367301ea057adb22c64fd..041723054ca915f9c302b3f6e6baa7d2d4a63f6b 100644 (file)
@@ -4,9 +4,11 @@
 namespace App\Nova;
 
 
+use App\Nova\Actions\SubscribeUser;
 use Carbon\Carbon;
 use Laravel\Nova\Fields\DateTime;
 use Laravel\Nova\Fields\Text;
+use Illuminate\Http\Request;
 use Laravel\Nova\Http\Requests\NovaRequest;
 
 class DiscoverUsers extends User
@@ -40,5 +42,11 @@ class DiscoverUsers extends User
         ];
     }
 
+    public function actions(Request $request)
+    {
+        return array_merge(parent::actions($request), [
+            new SubscribeUser,
+        ]);
+    }
 
 }
index 3a3a655530299ea3c50d55fc69cfbd7042fdecd3..26691795234ad0e10a7bb024011f00704fbcf28a 100644 (file)
@@ -4,6 +4,8 @@
 namespace App\Nova;
 
 
+use App\Nova\Actions\SubscribeUser;
+use Illuminate\Http\Request;
 use Laravel\Nova\Http\Requests\NovaRequest;
 
 class Prospect extends User
@@ -23,4 +25,11 @@ class Prospect extends User
         return "Prospect";
     }
 
+    public function actions(Request $request)
+    {
+        return array_merge(parent::actions($request), [
+            new SubscribeUser,
+        ]);
+    }
+
 }