From 53ebe28b14efa53f4ad645cddac01b37356917cb Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Mon, 27 Jan 2025 23:08:23 -0600 Subject: [PATCH] fix: makes portfolio available to form request --- app/Http/Requests/TransactionRequest.php | 12 ++++++++++-- tests/Api/TransactionsTest.php | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/Http/Requests/TransactionRequest.php b/app/Http/Requests/TransactionRequest.php index b4c8349..01907f1 100644 --- a/app/Http/Requests/TransactionRequest.php +++ b/app/Http/Requests/TransactionRequest.php @@ -10,6 +10,14 @@ use App\Rules\QuantityValidationRule; class TransactionRequest extends FormRequest { + protected function prepareForValidation(): void + { + + $this->merge([ + 'portfolio' => Portfolio::find($this->requestOrModelValue('portfolio_id', 'transaction')) + ]); + } + /** * Get the validation rules that apply to the request. * @@ -17,7 +25,7 @@ class TransactionRequest extends FormRequest */ public function rules(): array { - + $rules = [ 'portfolio_id' => ['required', 'exists:portfolios,id'], 'symbol' => ['required', 'string', new SymbolValidationRule], @@ -28,7 +36,7 @@ class TransactionRequest extends FormRequest 'numeric', 'min:0', new QuantityValidationRule( - $this->requestOrModelValue('symbol', 'transaction'), + $this->input('portfolio'), $this->requestOrModelValue('symbol', 'transaction'), $this->requestOrModelValue('transaction_type', 'transaction'), $this->requestOrModelValue('date', 'transaction') diff --git a/tests/Api/TransactionsTest.php b/tests/Api/TransactionsTest.php index c0cfbba..321e101 100644 --- a/tests/Api/TransactionsTest.php +++ b/tests/Api/TransactionsTest.php @@ -77,7 +77,8 @@ class TransactionsTest extends TestCase 'cost_basis' => 150, ]; - $this->postJson(route('api.transaction.store'), $data) + $this->actingAs($this->user) + ->postJson(route('api.transaction.store'), $data) ->assertCreated() ->assertJsonStructure([ 'id',