feat:email on fail/success import

This commit is contained in:
hackerESQ
2024-10-24 16:36:54 -05:00
parent 11ae07d69f
commit 336645d8e2
4 changed files with 124 additions and 2 deletions
+11 -2
View File
@@ -5,10 +5,11 @@ namespace App\Jobs;
use Throwable; use Throwable;
use App\Models\User; use App\Models\User;
use App\Models\BackupImport; use App\Models\BackupImport;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Foundation\Queue\Queueable; use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use App\Notifications\ImportSucceededNotification;
use App\Notifications\ImportFailedNotification;
use App\Imports\BackupImport as BackupImportExcel; use App\Imports\BackupImport as BackupImportExcel;
class BackupImportJob implements ShouldQueue class BackupImportJob implements ShouldQueue
@@ -34,12 +35,16 @@ class BackupImportJob implements ShouldQueue
*/ */
public $failOnTimeout = true; public $failOnTimeout = true;
public User $user;
/** /**
* Create a new job instance. * Create a new job instance.
*/ */
public function __construct( public function __construct(
public BackupImport $backupImport public BackupImport $backupImport
) { } ) {
$this->user = User::find($this->backupImport->user_id);
}
/** /**
* Execute the job. * Execute the job.
@@ -47,6 +52,8 @@ class BackupImportJob implements ShouldQueue
public function handle(): void public function handle(): void
{ {
Excel::import(new BackupImportExcel($this->backupImport), $this->backupImport->path, config('livewire.temporary_file_upload.disk', null)); 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, 'has_errors' => true,
'completed_at' => now() 'completed_at' => now()
]); ]);
$this->user->notify(new ImportFailedNotification($e->getMessage()));
} }
} }
+3
View File
@@ -32,6 +32,9 @@ class Portfolio extends Model
// enable queued jobs to create portfolios with owners // enable queued jobs to create portfolios with owners
static::$owner_id = auth()->user()?->id ?? $portfolio->attributes['owner_id']; static::$owner_id = auth()->user()?->id ?? $portfolio->attributes['owner_id'];
});
static::saving(function ($portfolio) {
unset($portfolio->owner_id); unset($portfolio->owner_id);
}); });
@@ -0,0 +1,56 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ImportFailedNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(
public string $errorMessage
) { }
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
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<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
@@ -0,0 +1,54 @@
<?php
namespace App\Notifications;
use App\Models\User;
use App\Models\Portfolio;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ImportSucceededNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct() { }
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
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<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}