]> _ Git - psq.git/commitdiff
orga change members type
authorLouis Jeckel <louis.jeckel@outlook.com>
Sun, 22 Nov 2020 11:15:14 +0000 (12:15 +0100)
committerLouis Jeckel <louis.jeckel@outlook.com>
Sun, 22 Nov 2020 11:15:14 +0000 (12:15 +0100)
app/Nova/Actions/SetOrganizationMembersType.php [new file with mode: 0644]
app/Nova/Actions/SetSubscriptionStatus.php
app/Nova/Actions/SubscribeUser.php
app/Nova/Organization.php
app/User.php

diff --git a/app/Nova/Actions/SetOrganizationMembersType.php b/app/Nova/Actions/SetOrganizationMembersType.php
new file mode 100644 (file)
index 0000000..77d6315
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+
+namespace App\Nova\Actions;
+
+use App\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\Actions\DestructiveAction;
+use Laravel\Nova\Fields\ActionFields;
+
+class SetOrganizationMembersType extends DestructiveAction
+{
+    use InteractsWithQueue, Queueable;
+
+
+    protected $type;
+    protected $action;
+
+    public $showOnTableRow = true;
+
+    public function __construct(int $type)
+    {
+        $this->type = $type;
+
+        $this->action = "Action inconnue";
+        $more = "";
+
+        switch($this->type) {
+            case User::TYPE_PROSPECT:
+                $this->action = "prospection";
+                break;
+            case User::TYPE_DISCOVER:
+                $this->action = "découverte";
+                $more = "Cette action n'activera pas de période découverte.";
+                break;
+        }
+
+        $this->name = "Passer en $this->action";
+
+        $this->confirmText = "Êtes vous sur de vouloir passer tous les membres de cette organisation en $this->action ? Cela désactivera l'abonnement pour tous les membres. ".$more;
+
+
+
+    }
+
+
+
+    /**
+     * 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)
+    {
+        $models->map(function(Organization $orga) {
+            $orga->update(['subscription_active' => 0]);
+
+            $orga->members()->update(['type' => $this->type]);
+        });
+
+        return Action::message("Membres passés en $this->action avec succès");
+    }
+
+    /**
+     * Get the fields available on the action.
+     *
+     * @return array
+     */
+    public function fields()
+    {
+        return [];
+    }
+}
index 0fed3868beaa01f8ad01bb3cfc75f7bdb76f6f20..ff4ae348cf1f8be62cdbd1eae7b532803dce8e62 100644 (file)
@@ -2,12 +2,15 @@
 
 namespace App\Nova\Actions;
 
+use App\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\Boolean;
 
 class SetSubscriptionStatus extends Action
 {
@@ -16,7 +19,7 @@ class SetSubscriptionStatus extends Action
     private $setActive;
 
     public $showOnTableRow = true;
-    public $withoutConfirmation = true;
+//    public $withoutConfirmation = true;
 
     public function __construct($setActive)
     {
@@ -42,6 +45,24 @@ class SetSubscriptionStatus extends Action
     {
         $models->map->update(['subscription_active' => $this->setActive]);
 
+        if($this->setActive) {
+            $models->map(function (Organization $orga) use($fields){
+
+                /** @see User::prepareForSubscription() */
+                $orga->members()->update([
+                    'discover_ends_at' => null,
+                    'trial_ends_at'  => null,
+                    'type' => User::TYPE_SUBSCRIBER,
+                ]);
+
+                if(! $fields->dont_notify) {
+                    $orga->members->map->sendSubscribedNotification();
+                }
+            });
+        }
+
+
+
         $message = $this->setActive ?
             "Abonnement activé avec succès" :
             "Abonnement désactivé avec succès";
@@ -57,6 +78,8 @@ class SetSubscriptionStatus extends Action
      */
     public function fields()
     {
-        return [];
+        return $this->setActive ? [
+            Boolean::make("Ne pas notifier", 'dont_notify')
+        ] : [];
     }
 }
index 995d5dea84b61871b3b910d0085e5b60c7c048f3..052a2cfc296683b72ab1aeb5f5e836152ddedf4d 100644 (file)
@@ -38,10 +38,7 @@ class SubscribeUser extends Action
             $user->organization()->associate(\App\Organization::query()->findOrFail($fields->org_id));
             $user->save();
             if(! $fields->dont_notify) {
-                $template = $user->reg_complete ?
-                    'just_subscribed_reg_ok' :
-                    'just_subscribed_need_reg';
-                $user->sendEmailFromTemplate($template);
+                $user->sendSubscribedNotification();
             }
         });
 
index 3ef8c5c18d260afa4a6a5f0e7c767f9e00cabf32..a12c012acb7cd6f9d72aa04aa051da23b30d4331 100644 (file)
@@ -4,6 +4,7 @@ namespace App\Nova;
 
 use App\Nova\Actions\SendCustomNotification;
 use App\Nova\Actions\SendNotification;
+use App\Nova\Actions\SetOrganizationMembersType;
 use App\Nova\Actions\SetSubscriptionStatus;
 use App\Nova\Filters\SubscriptionStatusFilter;
 use App\Nova\Filters\TypeFilter;
@@ -58,7 +59,7 @@ class Organization extends Resource
     public function fields(Request $request)
     {
         return [
-            ID::make()->sortable(),
+//            ID::make()->sortable(),
             Text::make('Nom', 'name'),
             HasMany::make('Membres', 'members', User::class),
             BelongsTo::make('Type', 'type', OrganizationType::class),
@@ -113,8 +114,10 @@ class Organization extends Resource
     {
         return array_merge([
             new SetSubscriptionStatus(true),
-            new SetSubscriptionStatus(false),
+            new SetOrganizationMembersType(\App\User::TYPE_PROSPECT),
             new SendNotification,
+            new SetOrganizationMembersType(\App\User::TYPE_DISCOVER),
+            new SetSubscriptionStatus(false),
         ], SendCustomNotification::getActions());
     }
 }
index 93d0b82f0461171db9ce39a84897b65b6d5911e7..962f246af77bfc25934ff6b7893c221952720a85 100644 (file)
@@ -350,6 +350,28 @@ class User extends Authenticatable implements MustVerifyEmail, SendsEmails
         $this->update(['unsubscribed' => 1]);
     }
 
+    /**
+     * Prepares model for subscription (disables trial, discover, sets type)
+     */
+    public function prepareForSubscription()
+    {
+        $this->update([
+            'discover_ends_at' => null,
+            'trial_ends_at'  => null,
+            'type' => self::TYPE_SUBSCRIBER,
+        ]);
+    }
+
+    /**
+     * Sends correct notification to a just subscribed user
+     */
+    public function sendSubscribedNotification()
+    {
+        $template = $this->reg_complete ?
+            'just_subscribed_reg_ok' :
+            'just_subscribed_need_reg';
+        $this->sendEmailFromTemplate($template);
+    }
 
     /**
      * @return MailgunValidation