From 8c1de699b2d30a95ad8faeac3491ab719d83bde0 Mon Sep 17 00:00:00 2001 From: Louis Jeckel Date: Sun, 22 Nov 2020 12:15:14 +0100 Subject: [PATCH] orga change members type --- .../Actions/SetOrganizationMembersType.php | 79 +++++++++++++++++++ app/Nova/Actions/SetSubscriptionStatus.php | 27 ++++++- app/Nova/Actions/SubscribeUser.php | 5 +- app/Nova/Organization.php | 7 +- app/User.php | 22 ++++++ 5 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 app/Nova/Actions/SetOrganizationMembersType.php diff --git a/app/Nova/Actions/SetOrganizationMembersType.php b/app/Nova/Actions/SetOrganizationMembersType.php new file mode 100644 index 0000000..77d6315 --- /dev/null +++ b/app/Nova/Actions/SetOrganizationMembersType.php @@ -0,0 +1,79 @@ +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 []; + } +} diff --git a/app/Nova/Actions/SetSubscriptionStatus.php b/app/Nova/Actions/SetSubscriptionStatus.php index 0fed386..ff4ae34 100644 --- a/app/Nova/Actions/SetSubscriptionStatus.php +++ b/app/Nova/Actions/SetSubscriptionStatus.php @@ -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') + ] : []; } } diff --git a/app/Nova/Actions/SubscribeUser.php b/app/Nova/Actions/SubscribeUser.php index 995d5de..052a2cf 100644 --- a/app/Nova/Actions/SubscribeUser.php +++ b/app/Nova/Actions/SubscribeUser.php @@ -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(); } }); diff --git a/app/Nova/Organization.php b/app/Nova/Organization.php index 3ef8c5c..a12c012 100644 --- a/app/Nova/Organization.php +++ b/app/Nova/Organization.php @@ -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()); } } diff --git a/app/User.php b/app/User.php index 93d0b82..962f246 100644 --- a/app/User.php +++ b/app/User.php @@ -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 -- 2.39.5