From b8242dbc879386366e43eda0d339d30a8b98dcee Mon Sep 17 00:00:00 2001 From: miguel456 Date: Mon, 7 Mar 2022 21:14:51 +0000 Subject: [PATCH] feat: add account deletion notifications This also sets up notifications for all site admins --- app/Jobs/ProcessAccountDelete.php | 17 +++++- app/Mail/UserAccountDeleteConfirmation.php | 2 +- app/Notifications/AccountDeleted.php | 69 +++++++++++++++++++++ app/Notifications/UserDeletedAccount.php | 70 ++++++++++++++++++++++ app/Observers/UserObserver.php | 2 +- 5 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 app/Notifications/AccountDeleted.php create mode 100644 app/Notifications/UserDeletedAccount.php diff --git a/app/Jobs/ProcessAccountDelete.php b/app/Jobs/ProcessAccountDelete.php index 7cd8bb9..f287751 100644 --- a/app/Jobs/ProcessAccountDelete.php +++ b/app/Jobs/ProcessAccountDelete.php @@ -2,6 +2,8 @@ namespace App\Jobs; +use App\Notifications\AccountDeleted; +use App\Notifications\UserDeletedAccount; use App\User; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; @@ -10,6 +12,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Notification; class ProcessAccountDelete implements ShouldQueue { @@ -40,12 +43,20 @@ class ProcessAccountDelete implements ShouldQueue */ public function handle() { - // It shouldn't need the suspension service, because if it was dispatched, the account was already locked - Log::alert('[Worker] Processing account deletion request', [ 'email' => $this->user->email ]); - $this->user->delete(); + $email = $this->user->email; + $name = $this->user->name; + + if ($this->user->delete()) { + Notification::route('mail', [ + $email => $name + ])->notify(new AccountDeleted($name)); + + // Notify admins + Notification::send(User::role('admin')->get(), new UserDeletedAccount($email)); + } } } diff --git a/app/Mail/UserAccountDeleteConfirmation.php b/app/Mail/UserAccountDeleteConfirmation.php index c4baf0a..3ab4110 100755 --- a/app/Mail/UserAccountDeleteConfirmation.php +++ b/app/Mail/UserAccountDeleteConfirmation.php @@ -58,7 +58,7 @@ class UserAccountDeleteConfirmation extends Mailable */ public function build() { - return $this->subject('[ACTION REQUIRED] Please confirm account removal') + return $this->subject(config('app.name') . ' - please confirm account removal (action required)') ->view('mail.deleted-account'); } } diff --git a/app/Notifications/AccountDeleted.php b/app/Notifications/AccountDeleted.php new file mode 100644 index 0000000..4300d9d --- /dev/null +++ b/app/Notifications/AccountDeleted.php @@ -0,0 +1,69 @@ +name = $name; + } + + /** + * 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) + { + // Adjust to notify external user + return (new MailMessage) + ->greeting('Hi ' . $this->name . ',') + ->from(config('notification.sender.address'), config('notification.sender.name')) + ->subject(config('app.name').' - account deleted permanently') + ->line('Thank you for confirming your account deletion request. We\'re sorry to see you go!') + ->line('Unless you sign up again, this is the last email you\'ll be receiving from us.') + ->line('Please let us know if there\'s any feedback you\'d like to share. You can use the feedback widget located on the left-hand side of our website, or the chat widget located on the lower right corner.') + ->line('See you around!') + ->salutation('The team at ' . config('app.name')); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/app/Notifications/UserDeletedAccount.php b/app/Notifications/UserDeletedAccount.php new file mode 100644 index 0000000..29fe4f6 --- /dev/null +++ b/app/Notifications/UserDeletedAccount.php @@ -0,0 +1,70 @@ +deletedEmail = $deletedEmail; + } + + /** + * 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) + { + return (new MailMessage) + ->greeting('Hi ' . $notifiable->name . ',') + ->from(config('notification.sender.address'), config('notification.sender.name')) + ->subject(config('app.name').' - someone deleted their account') + ->line("The user {$this->deletedEmail} has just deleted their account. You may wish to review the situation.") + ->line('You are receiving this email because you\'re a site admin.') + ->action('View current users', url(route('registeredPlayerList'))) + ->salutation('The team at ' . config('app.name')); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index 486e257..3f4130d 100755 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -29,7 +29,7 @@ class UserObserver { public function __construct() { - Log::debug('User observer has been initialised and ready for use!'); + // } /**