feat:improved background importing
This commit is contained in:
@@ -4,13 +4,17 @@ namespace App\Imports;
|
||||
|
||||
use App\Imports\Sheets\PortfoliosSheet;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use App\Console\Commands\SyncHoldingData;
|
||||
use App\Imports\Sheets\DailyChangesSheet;
|
||||
use App\Imports\Sheets\TransactionsSheet;
|
||||
use Maatwebsite\Excel\Events\AfterImport;
|
||||
use Maatwebsite\Excel\Concerns\Importable;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Events\BeforeImport;
|
||||
use Maatwebsite\Excel\Events\ImportFailed;
|
||||
use App\Console\Commands\RefreshMarketData;
|
||||
use App\Console\Commands\RefreshDividendData;
|
||||
use App\Models\BackupImport as BackupImportModel;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
|
||||
class BackupImport implements WithMultipleSheets, WithEvents
|
||||
@@ -18,25 +22,51 @@ class BackupImport implements WithMultipleSheets, WithEvents
|
||||
|
||||
use Importable;
|
||||
|
||||
public function __construct(
|
||||
public BackupImportModel $backupImportModel
|
||||
) { }
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
AfterImport::class =>
|
||||
fn() => Artisan::queue(RefreshMarketData::class, ['--force' => true])->chain([
|
||||
fn() => Artisan::call(RefreshDividendData::class, ['--force' => true])
|
||||
])
|
||||
BeforeImport::class => fn() => $this->backupImportModel->update([
|
||||
'status' => 'in_progress',
|
||||
'message' => __('Import is in progress...'),
|
||||
]),
|
||||
AfterImport::class => function () {
|
||||
|
||||
$this->backupImportModel->update([
|
||||
'status' => 'success',
|
||||
'message' => 'Import completed successfully!',
|
||||
'completed_at' => now()
|
||||
]);
|
||||
|
||||
Artisan::queue(RefreshMarketData::class, ['--user' => $this->backupImportModel->user_id, '--force' => true])
|
||||
->chain([
|
||||
fn() => Artisan::call(RefreshDividendData::class, ['--user' => $this->backupImportModel->user_id, '--force' => true])
|
||||
])
|
||||
->chain([
|
||||
fn() => Artisan::call(SyncHoldingData::class, ['--user' => $this->backupImportModel->user_id])
|
||||
]);
|
||||
},
|
||||
ImportFailed::class => fn(ImportFailed $event) => $this->backupImportModel->update([
|
||||
'status' => 'failed',
|
||||
'message' => 'Error: '. substr($event->getException()->getMessage(), 0, 220),
|
||||
'has_errors' => true,
|
||||
'completed_at' => now()
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function sheets(): array
|
||||
{
|
||||
return [
|
||||
'Portfolios' => new PortfoliosSheet,
|
||||
'Transactions' => new TransactionsSheet,
|
||||
'Daily Changes' => new DailyChangesSheet,
|
||||
'Portfolios' => new PortfoliosSheet($this->backupImportModel),
|
||||
'Transactions' => new TransactionsSheet($this->backupImportModel),
|
||||
'Daily Changes' => new DailyChangesSheet($this->backupImportModel),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user