chore: code style
This commit is contained in:
@@ -2,39 +2,35 @@
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Imports\Sheets\PortfoliosSheet;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use App\Console\Commands\RefreshDividendData;
|
||||
use App\Console\Commands\RefreshMarketData;
|
||||
use App\Console\Commands\SyncDailyChange;
|
||||
use App\Console\Commands\SyncHoldingData;
|
||||
use App\Imports\Sheets\DailyChangesSheet;
|
||||
use App\Imports\Sheets\PortfoliosSheet;
|
||||
use App\Imports\Sheets\TransactionsSheet;
|
||||
use Maatwebsite\Excel\Events\AfterImport;
|
||||
use App\Models\BackupImport as BackupImportModel;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Maatwebsite\Excel\Concerns\Importable;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
use Maatwebsite\Excel\Events\AfterImport;
|
||||
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
|
||||
class BackupImport implements WithEvents, WithMultipleSheets
|
||||
{
|
||||
|
||||
use Importable;
|
||||
|
||||
public function __construct(
|
||||
public BackupImportModel $backupImportModel
|
||||
) { }
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
BeforeImport::class => fn() => $this->backupImportModel->update([
|
||||
BeforeImport::class => fn () => $this->backupImportModel->update([
|
||||
'status' => 'in_progress',
|
||||
'message' => __('Import is in progress...'),
|
||||
]),
|
||||
@@ -43,24 +39,24 @@ class BackupImport implements WithMultipleSheets, WithEvents
|
||||
$this->backupImportModel->update([
|
||||
'status' => 'success',
|
||||
'message' => 'Import completed successfully!',
|
||||
'completed_at' => now()
|
||||
'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]),
|
||||
fn() => Artisan::call(SyncHoldingData::class, ['--user' => $this->backupImportModel->user_id]),
|
||||
fn() => User::find($this->backupImportModel->user_id)->portfolios->each(function($portfolio) {
|
||||
fn () => Artisan::call(RefreshDividendData::class, ['--user' => $this->backupImportModel->user_id, '--force' => true]),
|
||||
fn () => Artisan::call(SyncHoldingData::class, ['--user' => $this->backupImportModel->user_id]),
|
||||
fn () => User::find($this->backupImportModel->user_id)->portfolios->each(function ($portfolio) {
|
||||
|
||||
Artisan::queue(SyncDailyChange::class, ['portfolio_id' => $portfolio->id]);
|
||||
})
|
||||
}),
|
||||
]);
|
||||
},
|
||||
ImportFailed::class => fn(ImportFailed $event) => $this->backupImportModel->update([
|
||||
ImportFailed::class => fn (ImportFailed $event) => $this->backupImportModel->update([
|
||||
'status' => 'failed',
|
||||
'message' => 'Error: '. substr($event->getException()->getMessage(), 0, 220),
|
||||
'message' => 'Error: '.substr($event->getException()->getMessage(), 0, 220),
|
||||
'has_errors' => true,
|
||||
'completed_at' => now()
|
||||
'completed_at' => now(),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -3,42 +3,39 @@
|
||||
namespace App\Imports\Sheets;
|
||||
|
||||
use App\Imports\ValidatesPortfolioAccess;
|
||||
use App\Models\DailyChange;
|
||||
use App\Models\BackupImport;
|
||||
use App\Models\DailyChange;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Events\BeforeSheet;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
use Maatwebsite\Excel\Events\BeforeSheet;
|
||||
|
||||
class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithEvents
|
||||
class DailyChangesSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadingRow, WithValidation
|
||||
{
|
||||
use ValidatesPortfolioAccess;
|
||||
|
||||
public function __construct(
|
||||
public BackupImport $backupImport
|
||||
) { }
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
BeforeSheet::class => function(BeforeSheet $event) {
|
||||
BeforeSheet::class => function (BeforeSheet $event) {
|
||||
DB::commit();
|
||||
$this->backupImport->update([
|
||||
'message' => __('Importing daily changes...'),
|
||||
]);
|
||||
DB::beginTransaction();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function collection(Collection $dailyChanges)
|
||||
{
|
||||
$dailyChanges->chunk($this->batchSize())->each(function ($chunk) {
|
||||
@@ -56,7 +53,7 @@ class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation,
|
||||
'realized_gains' => $dailyChange['realized_gains'],
|
||||
'annotation' => $dailyChange['annotation'],
|
||||
'portfolio_id' => $dailyChange['portfolio_id'],
|
||||
'date' => Carbon::parse($dailyChange['date'])->format('Y-m-d')
|
||||
'date' => Carbon::parse($dailyChange['date'])->format('Y-m-d'),
|
||||
];
|
||||
});
|
||||
|
||||
@@ -71,7 +68,7 @@ class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation,
|
||||
'realized_gains',
|
||||
'annotation',
|
||||
'portfolio_id',
|
||||
'date'
|
||||
'date',
|
||||
]
|
||||
);
|
||||
});
|
||||
@@ -85,7 +82,7 @@ class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation,
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'portfolio_id' => ['required', 'uuid'],
|
||||
'portfolio_id' => ['required', 'uuid'],
|
||||
'date' => ['required', 'date'],
|
||||
'total_market_value' => ['sometimes', 'nullable', 'numeric'],
|
||||
'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'],
|
||||
|
||||
@@ -2,36 +2,33 @@
|
||||
|
||||
namespace App\Imports\Sheets;
|
||||
|
||||
use App\Models\Portfolio;
|
||||
use App\Models\BackupImport;
|
||||
use App\Models\Portfolio;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Events\BeforeSheet;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
use Maatwebsite\Excel\Events\BeforeSheet;
|
||||
|
||||
class PortfoliosSheet implements ToCollection, WithValidation, WithHeadingRow, SkipsEmptyRows, WithEvents
|
||||
class PortfoliosSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadingRow, WithValidation
|
||||
{
|
||||
public function __construct(
|
||||
public BackupImport $backupImport
|
||||
) { }
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
) {}
|
||||
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
BeforeSheet::class => function(BeforeSheet $event) {
|
||||
BeforeSheet::class => function (BeforeSheet $event) {
|
||||
DB::commit();
|
||||
$this->backupImport->update([
|
||||
'message' => __('Importing portfolios...'),
|
||||
]);
|
||||
DB::beginTransaction();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@@ -42,7 +39,7 @@ class PortfoliosSheet implements ToCollection, WithValidation, WithHeadingRow, S
|
||||
Portfolio::unguard(); // ensures we can set an owner for the portfolio
|
||||
|
||||
$portfolio = Portfolio::fullAccess($this->backupImport->user_id)->updateOrCreate([
|
||||
'id' => $portfolio['portfolio_id']
|
||||
'id' => $portfolio['portfolio_id'],
|
||||
], [
|
||||
'id' => $portfolio['portfolio_id'] ?? null,
|
||||
'title' => $portfolio['title'],
|
||||
|
||||
@@ -3,42 +3,38 @@
|
||||
namespace App\Imports\Sheets;
|
||||
|
||||
use App\Imports\ValidatesPortfolioAccess;
|
||||
use App\Models\BackupImport;
|
||||
use App\Models\Holding;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\BackupImport;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Events\BeforeSheet;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Illuminate\Support\Str;
|
||||
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
use Maatwebsite\Excel\Events\BeforeSheet;
|
||||
|
||||
class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithEvents
|
||||
class TransactionsSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadingRow, WithValidation
|
||||
{
|
||||
|
||||
use ValidatesPortfolioAccess;
|
||||
|
||||
public function __construct(
|
||||
public BackupImport $backupImport
|
||||
) { }
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
BeforeSheet::class => function(BeforeSheet $event) {
|
||||
BeforeSheet::class => function (BeforeSheet $event) {
|
||||
DB::commit();
|
||||
$this->backupImport->update([
|
||||
'message' => __('Importing transactions...'),
|
||||
]);
|
||||
DB::beginTransaction();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@@ -62,7 +58,7 @@ class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation,
|
||||
'sale_price' => $transaction['sale_price'],
|
||||
'split' => boolval($transaction['split']) ? 1 : 0,
|
||||
'reinvested_dividend' => boolval($transaction['reinvested_dividend']) ? 1 : 0,
|
||||
'date' => Carbon::parse($transaction['date'])->format('Y-m-d')
|
||||
'date' => Carbon::parse($transaction['date'])->format('Y-m-d'),
|
||||
];
|
||||
});
|
||||
|
||||
@@ -79,23 +75,23 @@ class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation,
|
||||
'sale_price',
|
||||
'split',
|
||||
'reinvested_dividend',
|
||||
'date'
|
||||
'date',
|
||||
]
|
||||
);
|
||||
|
||||
// stub out related holdings
|
||||
$chunk->unique(fn($item) => $item['symbol'] . $item['portfolio_id'])
|
||||
->each(function($holding) {
|
||||
|
||||
Holding::firstOrCreate([
|
||||
'symbol' => $holding['symbol'],
|
||||
'portfolio_id' => $holding['portfolio_id']
|
||||
], [
|
||||
'quantity' => 0,
|
||||
'average_cost_basis' => 0,
|
||||
'splits_synced_at' => now(),
|
||||
]);
|
||||
});
|
||||
$chunk->unique(fn ($item) => $item['symbol'].$item['portfolio_id'])
|
||||
->each(function ($holding) {
|
||||
|
||||
Holding::firstOrCreate([
|
||||
'symbol' => $holding['symbol'],
|
||||
'portfolio_id' => $holding['portfolio_id'],
|
||||
], [
|
||||
'quantity' => 0,
|
||||
'average_cost_basis' => 0,
|
||||
'splits_synced_at' => now(),
|
||||
]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -6,19 +6,18 @@ use App\Models\Portfolio;
|
||||
|
||||
trait ValidatesPortfolioAccess
|
||||
{
|
||||
|
||||
public function validatePortfolioAccess($collection)
|
||||
{
|
||||
|
||||
$uniquePortfolios = $collection->unique('portfolio_id')->pluck('portfolio_id');
|
||||
$countPortfoliosWithAccess = Portfolio::fullAccess($this->backupImport->user_id)
|
||||
->whereIn('id', $uniquePortfolios)
|
||||
->count();
|
||||
->whereIn('id', $uniquePortfolios)
|
||||
->count();
|
||||
|
||||
if (
|
||||
$countPortfoliosWithAccess < $uniquePortfolios->count()
|
||||
) {
|
||||
throw new \Exception(__("You do not have access to that portfolio."));
|
||||
throw new \Exception(__('You do not have access to that portfolio.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user