From: Louis Jeckel Date: Wed, 21 Oct 2020 16:36:58 +0000 (+0200) Subject: new notifications in DB X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=bc9da7f44d86f90a5cf44fc4b1e92a2c266d4f26;p=psq.git new notifications in DB --- diff --git a/app/EmailTemplate.php b/app/EmailTemplate.php index 55615be..f5fabd7 100644 --- a/app/EmailTemplate.php +++ b/app/EmailTemplate.php @@ -17,6 +17,17 @@ class EmailTemplate extends Model public $timestamps = false; + /** + * @param $slug + * @return EmailTemplate + */ + public static function findSlug($slug) + { + return self::query() + ->where('slug', $slug) + ->firstOrFail(); + } + /** * @param $user * @param $content diff --git a/app/Http/Controllers/Auth/VerificationController.php b/app/Http/Controllers/Auth/VerificationController.php index cbb8e29..f637b14 100644 --- a/app/Http/Controllers/Auth/VerificationController.php +++ b/app/Http/Controllers/Auth/VerificationController.php @@ -3,8 +3,8 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; -use App\Notifications\EmailValidated; use App\Providers\RouteServiceProvider; +use App\User; use Illuminate\Foundation\Auth\VerifiesEmails; use Illuminate\Http\Request; @@ -47,6 +47,13 @@ class VerificationController extends Controller */ protected function verified(Request $request) { - $request->user()->notify(new EmailValidated); + /** @var User $user */ + $user = $request->user(); + + $template = ($user->self_registered && $user->type === User::TYPE_DISCOVER) ? + 'email_validated_with_trial' : + 'email_validated'; + + $user->sendEmailFromTemplate($template); } } diff --git a/app/Jobs/CheckForTrialExpirationDates.php b/app/Jobs/CheckForTrialExpirationDates.php index 96d345a..e9e07b5 100644 --- a/app/Jobs/CheckForTrialExpirationDates.php +++ b/app/Jobs/CheckForTrialExpirationDates.php @@ -2,8 +2,7 @@ namespace App\Jobs; -use App\Notifications\TrialExpired; -use App\Notifications\TrialExpiresSoon; + use App\User; use Carbon\Carbon; use Illuminate\Bus\Queueable; @@ -40,9 +39,9 @@ class CheckForTrialExpirationDates implements ShouldQueue */ public function handle() { - self::getExpiresSoonProfiles()->map(fn(User $user) => $user->notify(new TrialExpiresSoon)); +// self::getExpiresSoonProfiles()->map(fn(User $user) => $user->notify(new TrialExpiresSoon)); - self::getLastEditionTodayProfiles()->map(fn(User $user) => $user->notify(new TrialExpired)); +// self::getLastEditionTodayProfiles()->map(fn(User $user) => $user->notify(new TrialExpired)); } diff --git a/app/Mail/TemplateMail.php b/app/Mail/TemplateMail.php index 27167b1..c3012a2 100644 --- a/app/Mail/TemplateMail.php +++ b/app/Mail/TemplateMail.php @@ -22,10 +22,12 @@ class TemplateMail extends Mailable * Create a new message instance. * * @param $content + * @param $subject */ - public function __construct($content) + public function __construct($content, $subject) { $this->content = $content; + $this->subject = $subject; } /** diff --git a/app/Notifications/EmailValidated.php b/app/Notifications/EmailValidated.php deleted file mode 100644 index c7a6a9e..0000000 --- a/app/Notifications/EmailValidated.php +++ /dev/null @@ -1,61 +0,0 @@ -subject('Email validé !') - ->line("Votre adresse email à bien été validée."); - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } -} diff --git a/app/Notifications/JustSubscribed.php b/app/Notifications/JustSubscribed.php deleted file mode 100644 index 0bdadd9..0000000 --- a/app/Notifications/JustSubscribed.php +++ /dev/null @@ -1,75 +0,0 @@ -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.complete-reg', [], now()->addDays(7)); - $message - ->line("Cliquez sur le bouton ci-dessous pour créer un mot de passe et activer votre compte pour ne rater aucun numéro de notre quotidien !") - ->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 [ - // - ]; - } -} diff --git a/app/Notifications/RegistrationComplete.php b/app/Notifications/RegistrationComplete.php deleted file mode 100644 index 5e2c195..0000000 --- a/app/Notifications/RegistrationComplete.php +++ /dev/null @@ -1,65 +0,0 @@ -subject('PRESCRIPTION SANTÉ QUOTIDIEN : Activation réussie !') - ->success() - ->greeting('Cher lecteur,') - ->line("Votre compte à bien été activé !") - ->line("Vous allez pouvoir recevoir les prochains numéros de *Prescription Santé : Le Quotidien*") - ->addTag('reg_complete'); - - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } -} diff --git a/app/Notifications/RegistrationPending.php b/app/Notifications/RegistrationPending.php deleted file mode 100644 index 6d44511..0000000 --- a/app/Notifications/RegistrationPending.php +++ /dev/null @@ -1,68 +0,0 @@ -routeWithToken('account.complete-reg', [], now()->addDays(14)); - return (new MailMessage) - ->subject('[Relance] PRESCRIPTION SANTÉ QUOTIDIEN : Activation en attente') - ->greeting('Cher lecteur,') - ->line("Nous avons remarqué que votre compte n'est toujours pas activé.") - ->line("Veuillez cliquer sur ce lien pour créer votre mot de passe et ne rater aucun numéro de *Prescription Santé : Le Quotidien* !") - ->line("Cela ne vous prendra pas plus d'une minute.") - ->action('Compléter mon inscription', $link) - ->addTag('reg_pending'); - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } -} diff --git a/app/Notifications/TrialExpired.php b/app/Notifications/TrialExpired.php deleted file mode 100644 index 98880f3..0000000 --- a/app/Notifications/TrialExpired.php +++ /dev/null @@ -1,67 +0,0 @@ -addTag('trial-expired') - ->subject("Votre période découverte est arrivée à son terme !") - ->greeting('Cher Lecteur,') - ->line("Au cours des deux dernières semaines, vous avez reçu gratuitement les éditions de **Prescription Santé Quotidien** dans le cadre de notre offre Découverte.") - ->line("Malheureusement, cette offre, non reconductible, est arrivée à son terme aujourd’hui et l'édition transmise ce matin est donc la dernière. **Vous pouvez dès maintenant nous contacter en cliquant sur le bouton ci-dessous** pour nous préciser le cadre d’un éventuel abonnement, et ce afin de ne pas rater une seule de nos éditions.") - ->action("Nous contacter", route('contact.index')) - ->line("Nous vous répondrons sous 24 heures et vous pourrez ainsi faire un choix éclairé.") - ->line("En espérant vous compter bientôt parmi nos abonnés") - ->salutation("LE RÉDACTEUR EN CHEF, OLIVIER ROBICHON"); - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } -} diff --git a/app/Notifications/TrialExpiresSoon.php b/app/Notifications/TrialExpiresSoon.php deleted file mode 100644 index 209a9a8..0000000 --- a/app/Notifications/TrialExpiresSoon.php +++ /dev/null @@ -1,68 +0,0 @@ -addTag('trial-expires-soon') - ->subject("Votre période découverte expire bientôt !") - ->greeting('Cher Lecteur,') - ->line("Depuis quelques jours vous recevez gratuitement les éditions de **Prescription Santé Quotidien** dans le cadre de notre offre Découverte.") - ->line("Nous espérons que vous appréciez ce média différent à destination des acteurs industriels de la santé et de leurs partenaires.") - ->line("**Dans 48 heures cette offre, non reconductible, arrivera à son terme.** Vous pouvez dès maintenant **nous contacter en cliquant sur le bouton ci-dessous** pour nous préciser le cadre d’un éventuel abonnement, et ce afin de ne pas rater une seule de nos éditions.") - ->action("Nous contacter", route('contact.index')) - ->line("Nous vous répondrons sous 24 heures et vous pourrez ainsi faire un choix éclairé.") - ->line("En espérant vous compter bientôt parmi nos abonnés") - ->salutation("LE RÉDACTEUR EN CHEF, OLIVIER ROBICHON"); - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } -} diff --git a/app/Notifications/Welcome.php b/app/Notifications/Welcome.php deleted file mode 100644 index 1d58a4a..0000000 --- a/app/Notifications/Welcome.php +++ /dev/null @@ -1,69 +0,0 @@ -routeWithToken('account.index', [], now()->addDays(7)); - return (new MailMessage) - ->subject('PRESCRIPTION SANTÉ QUOTIDIEN : Votre nouvelle plateforme') - ->greeting('Cher Lecteur abonné à notre « petit » quotidien !') - ->line("Ce dernier est justement de moins en moins petit et nous allons donc vous faire profiter, dès lundi d’une nouvelle formule dont le contenu comme la forme seront tout à la fois plus riches et plus « agréables » à lire.") - ->line("Mais pour cela, nous devons vous demander un tout petit effort de quelques secondes à peine !") - ->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("Ainsi dès Lundi prochain vous pourrez accéder en un clic à votre quotidien préféré.") - ->line("A très vite, donc ! ") - ->salutation("OLIVIER ROBICHON") - ->addTag('welcome'); - } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } -} diff --git a/app/Nova/Actions/SendNotification.php b/app/Nova/Actions/SendNotification.php index 8155480..b24e3e5 100644 --- a/app/Nova/Actions/SendNotification.php +++ b/app/Nova/Actions/SendNotification.php @@ -2,6 +2,7 @@ namespace App\Nova\Actions; +use App\EmailTemplate; use App\User; use FontLib\Table\Type\name; use Illuminate\Bus\Queueable; @@ -34,7 +35,9 @@ class SendNotification extends Action */ public function handle(ActionFields $fields, Collection $models) { - $models->map(fn(User $user) => $user->notify(new $fields->notification)); + + $models->map(fn(User $user) => $user->sendEmailFromTemplate($fields->notification)); + return Action::message(sprintf('%d messages envoyés', $models->count())); } @@ -45,12 +48,12 @@ class SendNotification extends Action */ public function fields() { + $options = EmailTemplate::query() + ->pluck('name', 'slug') + ->toArray(); + return [ - Select::make('Notification')->options([ -// Notifications\Welcome::class => 'Message de bienvenue', - Notifications\RegistrationPending::class => 'Rappel', - Notifications\JustSubscribed::class => 'Message abonnement activé' - ]) + Select::make('Notification')->options($options) ]; } } diff --git a/app/Nova/Actions/StartTrial.php b/app/Nova/Actions/StartTrial.php index 3c9fad6..7be1b45 100644 --- a/app/Nova/Actions/StartTrial.php +++ b/app/Nova/Actions/StartTrial.php @@ -3,12 +3,16 @@ namespace App\Nova\Actions; use App\User; +use Carbon\Carbon; 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\Date; +use Laravel\Nova\Fields\Number; +use Laravel\Nova\Fields\Trix; class StartTrial extends Action { @@ -16,7 +20,8 @@ class StartTrial extends Action public $showOnTableRow = true; public $confirmButtonText = "Appliquer"; - public $name = "Démarrer période de découverte"; + public $name = "Période de découverte"; + public function __construct() { @@ -35,7 +40,20 @@ class StartTrial extends Action */ public function handle(ActionFields $fields, Collection $models) { - $models->map->startDiscover(); + $endDate = Carbon::parse($fields->end_date)->endOfDay(); + $models->map(function(User $user) use($endDate){ + $wasOnTrial = $user->onTrial() || $user->isOnDiscoverUser(); + + if($user->reg_complete) { + $user->startTrial($endDate); + } else { + $user->startDiscover($endDate); + } + + if(! $wasOnTrial){ + $user->sendEmailFromTemplate('start_trial'); + } + }); return Action::message(sprintf("La période de découverte à bien été activée pour %d utilisteur(s)", $models->count())); } @@ -47,6 +65,10 @@ class StartTrial extends Action */ public function fields() { - return []; + return [ + Date::make('Date de fin', 'end_date') + ->withMeta(['value' => now()->addMonth()->format('Y-m-d')]) + ->firstDayOfWeek(1) + ]; } } diff --git a/app/Nova/Actions/SubscribeUser.php b/app/Nova/Actions/SubscribeUser.php index 215fd25..995d5de 100644 --- a/app/Nova/Actions/SubscribeUser.php +++ b/app/Nova/Actions/SubscribeUser.php @@ -38,7 +38,10 @@ class SubscribeUser extends Action $user->organization()->associate(\App\Organization::query()->findOrFail($fields->org_id)); $user->save(); if(! $fields->dont_notify) { - $user->notify(new JustSubscribed); + $template = $user->reg_complete ? + 'just_subscribed_reg_ok' : + 'just_subscribed_need_reg'; + $user->sendEmailFromTemplate($template); } }); diff --git a/app/Nova/Actions/ValidateAddress.php b/app/Nova/Actions/ValidateAddress.php index c82905f..e52ea18 100644 --- a/app/Nova/Actions/ValidateAddress.php +++ b/app/Nova/Actions/ValidateAddress.php @@ -13,6 +13,7 @@ class ValidateAddress extends Action implements ShouldQueue { use InteractsWithQueue, Queueable; + public $name = "Validation Mailgun"; /** * Perform the action on the given models. * diff --git a/app/Nova/EmailTemplate.php b/app/Nova/EmailTemplate.php index c0db024..ba51fbc 100644 --- a/app/Nova/EmailTemplate.php +++ b/app/Nova/EmailTemplate.php @@ -57,7 +57,7 @@ class EmailTemplate extends Resource Text::make('Nom code', 'slug')->hideFromIndex(), Text::make('Nom', 'name'), Text::make('Sujet', 'subject'), - CKEditor5Classic::make('Contenu', 'content')->alwaysShow(), + Trix::make('Contenu', 'content')->alwaysShow(), ]; } diff --git a/app/Nova/User.php b/app/Nova/User.php index ac29fe4..0fdac4b 100644 --- a/app/Nova/User.php +++ b/app/Nova/User.php @@ -194,7 +194,7 @@ class User extends Resource new ImportUsers, new SendNotification, new StartTrial, - new VerifyEmail, +// new VerifyEmail, new ValidateAddress, ]; } diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index d39dc17..ffb38c2 100644 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -2,7 +2,6 @@ namespace App\Observers; -use App\Notifications\RegistrationComplete; use App\User; class UserObserver @@ -40,7 +39,7 @@ class UserObserver $user->isDirty('reg_complete') && $user->getOriginal()['reg_complete'] === false ) { - $user->notify(new RegistrationComplete); + $user->sendEmailFromTemplate('reg_complete'); \Session::flash('registration_complete'); } diff --git a/app/PdfFile.php b/app/PdfFile.php index d03f764..90d065f 100644 --- a/app/PdfFile.php +++ b/app/PdfFile.php @@ -390,7 +390,7 @@ class PdfFile extends TwillModel implements Sortable if($user->receives_pdf) { return $this->getDownloadUrl(); } - return ($user->type === User::TYPE_DISCOVER && !$user->self_registered) ? + return ($user->type === User::TYPE_DISCOVER && !$user->reg_complete) ? $this->getSignedUrl($user) : $this->getUrlWithToken($user); diff --git a/app/TemplateVariables.php b/app/TemplateVariables.php index a7c5d2d..f9f3cde 100644 --- a/app/TemplateVariables.php +++ b/app/TemplateVariables.php @@ -4,6 +4,7 @@ namespace App; +use App\Mail\TemplateMail; use Illuminate\Support\Str; trait TemplateVariables @@ -15,6 +16,21 @@ trait TemplateVariables ]; + /** + * @param string|EmailTemplate $template + */ + public function sendEmailFromTemplate($template) + { + if(is_string($template)) { + $template = EmailTemplate::findSlug($template); + } + + \Mail::to($this)->send(new TemplateMail( + $template->fillFor($this), + $template->subject + )); + } + /** * Determine if a getter exists for a template attribute. * @@ -59,12 +75,40 @@ trait TemplateVariables * @return string * @throws \Throwable */ - public function getMyAccountTemplate() + public function getMyAccountButtonTemplate() { return EmailTemplate::button( - $this->routeWithToken('account.index'), + $this->routeWithToken('account.index', [], now()->addDays(7)), 'Accéder à mon compte' ); } + + /** + * @return string + * @throws \Throwable + */ + public function getCompleteRegistrationButtonTemplate() + { + return EmailTemplate::button( + $this->routeWithToken('account.complete-reg', [], now()->addDays(7)), + 'Créer mon mot de passe' + ); + + } + + + /** + * @return string + * @throws \Throwable + */ + public function getContactButtonTemplate() + { + return EmailTemplate::button( + route('contact.index'), + 'Nous contacter' + ); + + } + } diff --git a/app/User.php b/app/User.php index 246d820..251a5d7 100644 --- a/app/User.php +++ b/app/User.php @@ -13,7 +13,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\VerifiesEmails; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Arr; -use Illuminate\Support\Carbon; +use Carbon\Carbon; use Laravel\Cashier\Billable; use Laravel\Scout\Searchable; @@ -70,6 +70,7 @@ class User extends Authenticatable implements MustVerifyEmail 'position', 'wants_pdf', 'accepts_polls', + 'self_registered' ]; /** @@ -105,11 +106,12 @@ class User extends Authenticatable implements MustVerifyEmail /** * Trial duration in days */ - public const TRIAL_DURATION_DAYS = 15; + public const TRIAL_DURATION_DAYS = 30; + /** * Discover duration in days */ - public const DISCOVER_DURATION_DAYS = 15; + public const DISCOVER_DURATION_DAYS = 30; /** * Possible Statuses @@ -263,19 +265,21 @@ class User extends Authenticatable implements MustVerifyEmail /** * Starts trial period + * @param Carbon|null $endDate */ - public function startTrial(): void + public function startTrial(?Carbon $endDate = null): void { - $this->trial_ends_at = now()->addDays(self::TRIAL_DURATION_DAYS)->endOfDay(); + $this->trial_ends_at = $endDate ?? now()->addDays(self::TRIAL_DURATION_DAYS)->endOfDay(); $this->save(); } /** * Starts discover period + * @param Carbon|null $endDate */ - public function startDiscover(): void + public function startDiscover(?Carbon $endDate = null): void { - $this->discover_ends_at = now()->addDays(self::DISCOVER_DURATION_DAYS)->endOfDay(); + $this->discover_ends_at = $endDate ?? now()->addDays( self::DISCOVER_DURATION_DAYS)->endOfDay(); $this->type = self::TYPE_DISCOVER; $this->save(); }