From 336645d8e2df062f7f9b2e738351ec0077a665a3 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Thu, 24 Oct 2024 16:36:54 -0500 Subject: [PATCH] feat:email on fail/success import --- app/Jobs/BackupImportJob.php | 13 ++++- app/Models/Portfolio.php | 3 + .../ImportFailedNotification.php | 56 +++++++++++++++++++ .../ImportSucceededNotification.php | 54 ++++++++++++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 app/Notifications/ImportFailedNotification.php create mode 100644 app/Notifications/ImportSucceededNotification.php diff --git a/app/Jobs/BackupImportJob.php b/app/Jobs/BackupImportJob.php index 05d2200..5d536da 100644 --- a/app/Jobs/BackupImportJob.php +++ b/app/Jobs/BackupImportJob.php @@ -5,10 +5,11 @@ namespace App\Jobs; use Throwable; use App\Models\User; use App\Models\BackupImport; -use Illuminate\Support\Facades\Auth; use Maatwebsite\Excel\Facades\Excel; use Illuminate\Foundation\Queue\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; +use App\Notifications\ImportSucceededNotification; +use App\Notifications\ImportFailedNotification; use App\Imports\BackupImport as BackupImportExcel; class BackupImportJob implements ShouldQueue @@ -34,12 +35,16 @@ class BackupImportJob implements ShouldQueue */ public $failOnTimeout = true; + public User $user; + /** * Create a new job instance. */ public function __construct( public BackupImport $backupImport - ) { } + ) { + $this->user = User::find($this->backupImport->user_id); + } /** * Execute the job. @@ -47,6 +52,8 @@ class BackupImportJob implements ShouldQueue public function handle(): void { Excel::import(new BackupImportExcel($this->backupImport), $this->backupImport->path, config('livewire.temporary_file_upload.disk', null)); + + $this->user->notify(new ImportSucceededNotification); } /** @@ -60,5 +67,7 @@ class BackupImportJob implements ShouldQueue 'has_errors' => true, 'completed_at' => now() ]); + + $this->user->notify(new ImportFailedNotification($e->getMessage())); } } diff --git a/app/Models/Portfolio.php b/app/Models/Portfolio.php index 07a5a02..e25268e 100644 --- a/app/Models/Portfolio.php +++ b/app/Models/Portfolio.php @@ -32,6 +32,9 @@ class Portfolio extends Model // enable queued jobs to create portfolios with owners static::$owner_id = auth()->user()?->id ?? $portfolio->attributes['owner_id']; + }); + + static::saving(function ($portfolio) { unset($portfolio->owner_id); }); diff --git a/app/Notifications/ImportFailedNotification.php b/app/Notifications/ImportFailedNotification.php new file mode 100644 index 0000000..3d615e2 --- /dev/null +++ b/app/Notifications/ImportFailedNotification.php @@ -0,0 +1,56 @@ + + */ + public function via(object $notifiable): array + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(object $notifiable): MailMessage + { + return (new MailMessage) + ->greeting('Oh no!') + ->subject("Your Investbrain import failed!") + ->line("Heads up, your Investbrain import was unable to successfully complete. There were errors which caused the import to fail.") + ->action("Try again?", route('import-export')) + ->line("**Technical details:**") + ->line($this->errorMessage); + } + + /** + * Get the array representation of the notification. + * + * @return array + */ + public function toArray(object $notifiable): array + { + return [ + // + ]; + } +} diff --git a/app/Notifications/ImportSucceededNotification.php b/app/Notifications/ImportSucceededNotification.php new file mode 100644 index 0000000..12ec924 --- /dev/null +++ b/app/Notifications/ImportSucceededNotification.php @@ -0,0 +1,54 @@ + + */ + public function via(object $notifiable): array + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(object $notifiable): MailMessage + { + return (new MailMessage) + ->greeting('Woot! 🎉') + ->subject("Your Investbrain import was successful!") + ->line("Just a heads up that your Investbrain import succeeded! Your portfolios, transactions, and daily changes are now available in your account.") + ->action("Get Started", route('dashboard')); + } + + /** + * Get the array representation of the notification. + * + * @return array + */ + public function toArray(object $notifiable): array + { + return [ + // + ]; + } +}