Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

Reject negative product quantities on order creation (#1231)

Dave Earleyd5aa644c0a44
diff --git a/backend/app/Services/Domain/Order/OrderCreateRequestValidationService.php b/backend/app/Services/Domain/Order/OrderCreateRequestValidationService.php
index 43f9dd76e..04b09c163 100644
--- a/backend/app/Services/Domain/Order/OrderCreateRequestValidationService.php
+++ b/backend/app/Services/Domain/Order/OrderCreateRequestValidationService.php
@@ -84,7 +84,7 @@ class OrderCreateRequestValidationService
'products' => 'required|array',
'products.*.product_id' => 'required|integer',
'products.*.quantities' => 'required|array',
- 'products.*.quantities.*.quantity' => 'required|integer',
+ 'products.*.quantities.*.quantity' => 'required|integer|min:0',
'products.*.quantities.*.price_id' => 'required|integer',
'products.*.quantities.*.price' => 'numeric|min:0',
]);
diff --git a/backend/tests/Unit/Services/Domain/Order/OrderCreateRequestValidationServiceTest.php b/backend/tests/Unit/Services/Domain/Order/OrderCreateRequestValidationServiceTest.php
index 3996f194f..65e772e45 100644
--- a/backend/tests/Unit/Services/Domain/Order/OrderCreateRequestValidationServiceTest.php
+++ b/backend/tests/Unit/Services/Domain/Order/OrderCreateRequestValidationServiceTest.php
@@ -151,6 +151,40 @@ class OrderCreateRequestValidationServiceTest extends TestCase
$this->service->validateRequestData($eventId, $data);
}
+ public function testNegativeQuantityOnAPriceTierIsRejected(): void
+ {
+ $eventId = 1;
+ $productId = 10;
+ $cheapPriceId = 101;
+ $expensivePriceId = 102;
+
+ $this->setupMocks(
+ eventId: $eventId,
+ productId: $productId,
+ priceIds: [$cheapPriceId, $expensivePriceId],
+ priceLabels: ['Cheap', 'VIP'],
+ availabilities: [
+ ['price_id' => $cheapPriceId, 'quantity_available' => 100, 'quantity_reserved' => 0],
+ ['price_id' => $expensivePriceId, 'quantity_available' => 100, 'quantity_reserved' => 0],
+ ],
+ );
+
+ $data = [
+ 'products' => [
+ [
+ 'product_id' => $productId,
+ 'quantities' => [
+ ['price_id' => $cheapPriceId, 'quantity' => 5],
+ ['price_id' => $expensivePriceId, 'quantity' => -1],
+ ],
+ ],
+ ],
+ ];
+
+ $this->expectException(ValidationException::class);
+ $this->service->validateRequestData($eventId, $data);
+ }
+
public function testUnrelatedOverReservedCapacityDoesNotBlockSelectedProduct(): void
{
$eventId = 1;