fix: quantity validation should not count current transaction

This commit is contained in:
hackerESQ
2025-08-29 15:47:18 -05:00
parent afcafa6031
commit a0bd776abb
6 changed files with 61 additions and 8 deletions
+2 -1
View File
@@ -39,7 +39,8 @@ class TransactionRequest extends FormRequest
$this->input('portfolio'), $this->input('portfolio'),
$this->requestOrModelValue('symbol', 'transaction'), $this->requestOrModelValue('symbol', 'transaction'),
$this->requestOrModelValue('transaction_type', 'transaction'), $this->requestOrModelValue('transaction_type', 'transaction'),
$this->requestOrModelValue('date', 'transaction') $this->requestOrModelValue('date', 'transaction'),
$this->transaction
), ),
], ],
'currency' => ['required', 'exists:currencies,currency'], 'currency' => ['required', 'exists:currencies,currency'],
+5 -3
View File
@@ -6,8 +6,8 @@ namespace App\Rules;
use App\Models\Portfolio; use App\Models\Portfolio;
use App\Models\Transaction; use App\Models\Transaction;
use Illuminate\Support\Carbon;
use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Carbon;
class QuantityValidationRule implements ValidationRule class QuantityValidationRule implements ValidationRule
{ {
@@ -20,8 +20,9 @@ class QuantityValidationRule implements ValidationRule
protected ?Portfolio $portfolio, protected ?Portfolio $portfolio,
protected ?string $symbol, protected ?string $symbol,
protected ?string $transactionType, protected ?string $transactionType,
protected string|Carbon|null $date protected string|Carbon|null $date,
) { } protected ?Transaction $transaction
) {}
/** /**
* Validate the attribute. * Validate the attribute.
@@ -42,6 +43,7 @@ class QuantityValidationRule implements ValidationRule
->sum('quantity'); ->sum('quantity');
$sales_qty = (float) $this->portfolio->transactions() $sales_qty = (float) $this->portfolio->transactions()
->where('id', '!=', $this->transaction?->id)
->symbol($this->symbol) ->symbol($this->symbol)
->sell() ->sell()
->whereDate('date', '<', $this->date) ->whereDate('date', '<', $this->date)
+4 -2
View File
@@ -122,19 +122,21 @@ class TransactionFactory extends Factory
]); ]);
} }
public function buy(): static public function buy($quantity = 1): static
{ {
return $this->state(fn (array $attributes) => [ return $this->state(fn (array $attributes) => [
'transaction_type' => 'BUY', 'transaction_type' => 'BUY',
'quantity' => $quantity,
'cost_basis' => $this->faker->randomFloat(2, 10, 500), 'cost_basis' => $this->faker->randomFloat(2, 10, 500),
'sale_price' => null, 'sale_price' => null,
]); ]);
} }
public function sell(): static public function sell($quantity = 1): static
{ {
return $this->state(fn (array $attributes) => [ return $this->state(fn (array $attributes) => [
'transaction_type' => 'SELL', 'transaction_type' => 'SELL',
'quantity' => $quantity,
'sale_price' => $this->faker->randomFloat(2, 10, 500), 'sale_price' => $this->faker->randomFloat(2, 10, 500),
'cost_basis' => null, 'cost_basis' => null,
]); ]);
@@ -19,7 +19,7 @@ new class extends Component
// props // props
public ?Portfolio $portfolio; public ?Portfolio $portfolio;
public ?Transaction $transaction; public ?Transaction $transaction = null;
public ?string $portfolio_id; public ?string $portfolio_id;
@@ -53,7 +53,7 @@ new class extends Component
'required', 'required',
'numeric', 'numeric',
'gt:0', 'gt:0',
new QuantityValidationRule($this->portfolio, $this->symbol, $this->transaction_type, $this->date), new QuantityValidationRule($this->portfolio, $this->symbol, $this->transaction_type, $this->date, $this->transaction),
], ],
'currency' => ['required', 'exists:currencies,currency'], 'currency' => ['required', 'exists:currencies,currency'],
'cost_basis' => 'exclude_if:transaction_type,SELL|min:0|numeric', 'cost_basis' => 'exclude_if:transaction_type,SELL|min:0|numeric',
+29
View File
@@ -114,6 +114,35 @@ class TransactionsTest extends TestCase
->assertJsonValidationErrors(['symbol']); ->assertJsonValidationErrors(['symbol']);
} }
public function test_cannot_sell_more_than_owned()
{
Artisan::call('db:seed', [
'--class' => CurrencySeeder::class,
'--force' => true,
]);
$this->actingAs($this->user);
$portfolio = Portfolio::factory()->create();
Transaction::factory(5)->buy()->lastYear()->portfolio($portfolio->id)->symbol('AAPL')->create();
$data = [
'symbol' => 'AAPL',
'portfolio_id' => $this->portfolio->id,
'transaction_type' => 'SELL',
'quantity' => 6,
'currency' => 'USD',
'date' => now()->toDateString(),
'sale_price' => 150,
];
$this->actingAs($this->user)
->postJson(route('api.transaction.store'), $data)
->assertUnprocessable()
->assertJsonValidationErrors(['quantity']);
}
public function test_can_show_a_transaction() public function test_can_show_a_transaction()
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
+19
View File
@@ -8,6 +8,7 @@ use App\Models\Holding;
use App\Models\Portfolio; use App\Models\Portfolio;
use App\Models\Transaction; use App\Models\Transaction;
use App\Models\User; use App\Models\User;
use App\Rules\QuantityValidationRule;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
class TransactionsTest extends TestCase class TransactionsTest extends TestCase
@@ -69,4 +70,22 @@ class TransactionsTest extends TestCase
0.01 0.01
); );
} }
public function test_cannot_sell_more_than_owned(): void
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
Transaction::factory(5)->buy()->lastYear()->portfolio($portfolio->id)->symbol('AAPL')->create();
$sale_transaction = Transaction::factory()->sell(6)->lastMonth()->portfolio($portfolio->id)->symbol('AAPL')->make();
$rule = new QuantityValidationRule($portfolio, $sale_transaction->symbol, 'SELL', $sale_transaction->date, $sale_transaction);
$rule->validate('quantity', $sale_transaction->quantity, function () {
$this->assertFalse(false, 'Not permitted to sell more than owned.');
});
$this->assertTrue(true);
}
} }