adds validation to imports

This commit is contained in:
hackerESQ
2024-08-29 21:39:59 -05:00
parent fc4f44ff05
commit 62f3ca6ef1
6 changed files with 68 additions and 12 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ class PortfoliosSheet implements FromCollection, WithHeadings, WithTitle
public function headings(): array public function headings(): array
{ {
return [ return [
'ID', 'Portfolio ID',
'Title', 'Title',
'Notes', 'Notes',
'Wishlist', 'Wishlist',
+1 -1
View File
@@ -21,7 +21,7 @@ class BackupImport implements WithMultipleSheets
return [ return [
'Portfolios' => new PortfoliosSheet, 'Portfolios' => new PortfoliosSheet,
'Transactions' => new TransactionsSheet, 'Transactions' => new TransactionsSheet,
// 'Daily Changes' => new DailyChangesSheet, 'Daily Changes' => new DailyChangesSheet,
]; ];
// Can listen for AfterSheet to run clean up afterwards // Can listen for AfterSheet to run clean up afterwards
+25 -4
View File
@@ -8,19 +8,26 @@ use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow; use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
class DailyChangesSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows
{ {
// use Importable; // use Importable;
public function collection(Collection $dailyChanges) public function collection(Collection $dailyChanges)
{ {
foreach ($dailyChanges as $dailyChange) {
if ($dailyChange['user'] != auth()->user()->id) { $portfolios = auth()->user()->portfolios->pluck('id');
throw new Exception('Can\'t do that.'); $dailyChanges->pluck('portfolio_id')->unique()->each(function($portfolio) use ($portfolios) {
if (!$portfolios->contains($portfolio)) {
throw new Exception('You do not have permission to access that portfolio.');
} }
});
foreach ($dailyChanges as $dailyChange) {
DailyChange::updateOrCreate([ DailyChange::updateOrCreate([
'date' => $dailyChange['date'], 'date' => $dailyChange['date'],
@@ -37,4 +44,18 @@ class DailyChangesSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
]); ]);
} }
} }
public function rules(): array
{
return [
'portfolio_id' => ['required'],
'date' => ['required', 'date'],
'total_market_value' => ['sometimes', 'nullable', 'numeric'],
'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'],
'total_gain' => ['sometimes', 'nullable', 'numeric'],
'total_dividends' => ['sometimes', 'nullable', 'min:0', 'numeric'],
'realized_gains' => ['sometimes', 'nullable', 'numeric'],
'annotation' => ['sometimes', 'nullable', 'string'],
];
}
} }
+15 -3
View File
@@ -3,12 +3,14 @@
namespace App\Imports\Sheets; namespace App\Imports\Sheets;
use App\Models\Portfolio; use App\Models\Portfolio;
use Illuminate\Validation\Rule;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow; use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows class PortfoliosSheet implements ToCollection, WithValidation, WithHeadingRow, SkipsEmptyRows
{ {
// use Importable; // use Importable;
@@ -17,12 +19,12 @@ class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
foreach ($portfolios as $portfolio) { foreach ($portfolios as $portfolio) {
auth()->user()->portfolios() auth()->user()->portfolios()
->where(['id' => $portfolio['id']]) ->where(['id' => $portfolio['portfolio_id']])
->orWhere(['title' => $portfolio['title']]) ->orWhere(['title' => $portfolio['title']])
->firstOr(function () use ($portfolio) { ->firstOr(function () use ($portfolio) {
return Portfolio::make()->forceFill([ return Portfolio::make()->forceFill([
'id' => $portfolio['id'] ?? null, 'id' => $portfolio['portfolio_id'] ?? null,
'title' => $portfolio['title'], 'title' => $portfolio['title'],
'wishlist' => $portfolio['wishlist'] ?? false, 'wishlist' => $portfolio['wishlist'] ?? false,
'notes' => $portfolio['notes'], 'notes' => $portfolio['notes'],
@@ -30,4 +32,14 @@ class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
}); });
} }
} }
public function rules(): array
{
return [
'portfolio_id' => ['sometimes', 'nullable'],
'title' => ['required', 'string'],
'wishlist' => ['sometimes', 'nullable', 'boolean'],
'notes' => ['sometimes', 'nullable', 'string'],
];
}
} }
+18 -2
View File
@@ -7,15 +7,16 @@ use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow; use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\WithChunkReading; use Maatwebsite\Excel\Concerns\WithChunkReading;
class TransactionsSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows, WithChunkReading class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithChunkReading
{ {
// use Importable; // use Importable;
public function collection(Collection $transactions) public function collection(Collection $transactions)
{ {
foreach ($transactions as $transaction) { foreach ($transactions->sortBy('date') as $transaction) {
Transaction::where('id', $transaction['transaction_id']) Transaction::where('id', $transaction['transaction_id'])
->firstOr(function () use ($transaction) { ->firstOr(function () use ($transaction) {
@@ -35,6 +36,21 @@ class TransactionsSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows,
} }
} }
public function rules(): array
{
return [
'transaction_id' => ['sometimes', 'nullable'],
'symbol' => ['required', 'string'],
'portfolio_id' => ['required', 'exists:portfolios,id'],
'quantity' => ['required', 'min:0', 'numeric'],
'transaction_type' => ['required', 'in:BUY,SELL'],
'date' => ['required', 'date'],
'quantity' => ['required', 'min:0', 'numeric'],
'cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'],
'sale_price' => ['sometimes', 'nullable', 'min:0', 'numeric'],
];
}
public function chunkSize(): int public function chunkSize(): int
{ {
return 500; return 500;
@@ -20,8 +20,15 @@ new class extends Component {
$this->validate(); $this->validate();
try {
$import = (new BackupImport)->import($this->file); $import = (new BackupImport)->import($this->file);
} catch (\Throwable $e) {
dd($e);
return $this->error($e->getMessage());
}
$this->success(__('Successfully imported!')); $this->success(__('Successfully imported!'));
// Artisan::queue(RefreshHoldingData::class); // Artisan::queue(RefreshHoldingData::class);