Generalize per-vehicle-option booking rules to Front Seat too
Front Seat previously had a hardcoded max-per-booking check with no enable toggle; now every Vehicle Option (front_seat/back_seat/whole_vehicle) gets the same config-driven pair — an on/off toggle and a max passenger_count — surfaced via new BOOKING_*_ENABLED/BOOKING_*_MAX_PER_BOOKING env vars, editable from ManageAppSettings, and exposed on route pricing as max_per_booking.
This commit is contained in:
@@ -9,9 +9,9 @@ use RuntimeException;
|
||||
|
||||
class InvalidVehicleSelectionException extends RuntimeException
|
||||
{
|
||||
public static function frontSeatLimitExceeded(int $requested, int $max): self
|
||||
public static function passengerLimitExceeded(VehicleOption $vehicleOption, int $requested, int $max): self
|
||||
{
|
||||
return new self("Front seat request [{$requested}] exceeds the max of [{$max}] per booking.");
|
||||
return new self("Vehicle option [{$vehicleOption->value}] passenger count [{$requested}] exceeds the max of [{$max}] per booking.");
|
||||
}
|
||||
|
||||
public static function optionDisabled(VehicleOption $vehicleOption): self
|
||||
|
||||
@@ -9,11 +9,11 @@ use Modules\Shared\Enums\VehicleOption;
|
||||
class BookingService
|
||||
{
|
||||
/**
|
||||
* Enforces the only v1 inventory rule (max Front Seats per booking), the
|
||||
* blunt config toggles for Back Seat / Whole Vehicle availability, and
|
||||
* shape rules around combining options in one booking (no duplicate
|
||||
* option lines, Whole Vehicle can't be mixed with anything else since it
|
||||
* already covers the whole car).
|
||||
* Enforces the same two blunt, config-driven rules for every Vehicle
|
||||
* Option — an on/off toggle and a max passenger_count per booking (see
|
||||
* domain.md §2) — plus shape rules around combining options in one
|
||||
* booking (no duplicate option lines, Whole Vehicle can't be mixed with
|
||||
* anything else since it already covers the whole car).
|
||||
*
|
||||
* Deliberately does not check real capacity/availability — that's an
|
||||
* explicitly deferred future phase (domain.md §2, §7).
|
||||
@@ -43,26 +43,23 @@ class BookingService
|
||||
|
||||
private function validateOption(VehicleOption $vehicleOption, int $passengerCount): void
|
||||
{
|
||||
match ($vehicleOption) {
|
||||
VehicleOption::FrontSeat => $this->validateFrontSeat($passengerCount),
|
||||
VehicleOption::BackSeat => $this->validateEnabled($vehicleOption, 'booking.back_seat_enabled'),
|
||||
VehicleOption::WholeVehicle => $this->validateEnabled($vehicleOption, 'booking.whole_vehicle_enabled'),
|
||||
};
|
||||
$this->validateEnabled($vehicleOption);
|
||||
$this->validateMax($vehicleOption, $passengerCount);
|
||||
}
|
||||
|
||||
private function validateFrontSeat(int $passengerCount): void
|
||||
private function validateEnabled(VehicleOption $vehicleOption): void
|
||||
{
|
||||
$max = config('booking.front_seat_max_per_booking');
|
||||
|
||||
if ($passengerCount > $max) {
|
||||
throw InvalidVehicleSelectionException::frontSeatLimitExceeded($passengerCount, $max);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateEnabled(VehicleOption $vehicleOption, string $configKey): void
|
||||
{
|
||||
if (! config($configKey)) {
|
||||
if (! config("booking.{$vehicleOption->value}_enabled")) {
|
||||
throw InvalidVehicleSelectionException::optionDisabled($vehicleOption);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateMax(VehicleOption $vehicleOption, int $passengerCount): void
|
||||
{
|
||||
$max = config("booking.{$vehicleOption->value}_max_per_booking");
|
||||
|
||||
if ($passengerCount > $max) {
|
||||
throw InvalidVehicleSelectionException::passengerLimitExceeded($vehicleOption, $passengerCount, $max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +97,37 @@ test('front-seat-limit rejection surfaces as 422', function () {
|
||||
['vehicle_option' => 'front_seat', 'passenger_count' => 2],
|
||||
]))
|
||||
->assertStatus(422)
|
||||
->assertJsonPath('message', 'Front seat request [2] exceeds the max of [1] per booking.');
|
||||
->assertJsonPath('message', 'Vehicle option [front_seat] passenger count [2] exceeds the max of [1] per booking.');
|
||||
|
||||
expect(Booking::count())->toBe(0);
|
||||
});
|
||||
|
||||
test('back-seat-limit rejection surfaces as 422', function () {
|
||||
config(['booking.back_seat_max_per_booking' => 1]);
|
||||
|
||||
[$route, $timeSlot] = bookableRouteAndSlot([[VehicleOption::BackSeat, '9000.00']]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->postJson('/api/v1/bookings', bookingPayload($route, $timeSlot, [
|
||||
['vehicle_option' => 'back_seat', 'passenger_count' => 2],
|
||||
]))
|
||||
->assertStatus(422)
|
||||
->assertJsonPath('message', 'Vehicle option [back_seat] passenger count [2] exceeds the max of [1] per booking.');
|
||||
|
||||
expect(Booking::count())->toBe(0);
|
||||
});
|
||||
|
||||
test('whole-vehicle-limit rejection surfaces as 422', function () {
|
||||
config(['booking.whole_vehicle_max_per_booking' => 1]);
|
||||
|
||||
[$route, $timeSlot] = bookableRouteAndSlot([[VehicleOption::WholeVehicle, '30000.00']]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->postJson('/api/v1/bookings', bookingPayload($route, $timeSlot, [
|
||||
['vehicle_option' => 'whole_vehicle', 'passenger_count' => 2],
|
||||
]))
|
||||
->assertStatus(422)
|
||||
->assertJsonPath('message', 'Vehicle option [whole_vehicle] passenger count [2] exceeds the max of [1] per booking.');
|
||||
|
||||
expect(Booking::count())->toBe(0);
|
||||
});
|
||||
|
||||
@@ -30,40 +30,52 @@ test('front seat and back seat can be selected together in one booking', functio
|
||||
]))->not->toThrow(InvalidVehicleSelectionException::class);
|
||||
});
|
||||
|
||||
test('requesting more front seats than the configured max is rejected', function () {
|
||||
test('requesting more passengers than the configured max is rejected', function (VehicleOption $option) {
|
||||
config(["booking.{$option->value}_max_per_booking" => 1]);
|
||||
|
||||
$service = new BookingService;
|
||||
|
||||
expect(fn () => $service->validateSelections([new VehicleSelectionData($option, 2)]))
|
||||
->toThrow(InvalidVehicleSelectionException::class);
|
||||
})->with([
|
||||
'front_seat' => [VehicleOption::FrontSeat],
|
||||
'back_seat' => [VehicleOption::BackSeat],
|
||||
'whole_vehicle' => [VehicleOption::WholeVehicle],
|
||||
]);
|
||||
|
||||
test('requesting passengers up to the configured max passes', function (VehicleOption $option) {
|
||||
config(["booking.{$option->value}_max_per_booking" => 2]);
|
||||
|
||||
$service = new BookingService;
|
||||
|
||||
expect(fn () => $service->validateSelections([new VehicleSelectionData($option, 2)]))
|
||||
->not->toThrow(InvalidVehicleSelectionException::class);
|
||||
})->with([
|
||||
'front_seat' => [VehicleOption::FrontSeat],
|
||||
'back_seat' => [VehicleOption::BackSeat],
|
||||
'whole_vehicle' => [VehicleOption::WholeVehicle],
|
||||
]);
|
||||
|
||||
test('an option is rejected when disabled via config', function (VehicleOption $option) {
|
||||
config(["booking.{$option->value}_enabled" => false]);
|
||||
|
||||
$service = new BookingService;
|
||||
|
||||
expect(fn () => $service->validateSelections([new VehicleSelectionData($option)]))
|
||||
->toThrow(InvalidVehicleSelectionException::class, "Vehicle option [{$option->value}] is not currently available for booking.");
|
||||
})->with([
|
||||
'front_seat' => [VehicleOption::FrontSeat],
|
||||
'back_seat' => [VehicleOption::BackSeat],
|
||||
'whole_vehicle' => [VehicleOption::WholeVehicle],
|
||||
]);
|
||||
|
||||
test('exceeding the max produces the expected message', function () {
|
||||
config(['booking.front_seat_max_per_booking' => 1]);
|
||||
|
||||
$service = new BookingService;
|
||||
|
||||
expect(fn () => $service->validateSelections([new VehicleSelectionData(VehicleOption::FrontSeat, 2)]))
|
||||
->toThrow(InvalidVehicleSelectionException::class);
|
||||
});
|
||||
|
||||
test('requesting front seats up to the configured max passes', function () {
|
||||
config(['booking.front_seat_max_per_booking' => 2]);
|
||||
|
||||
$service = new BookingService;
|
||||
|
||||
expect(fn () => $service->validateSelections([new VehicleSelectionData(VehicleOption::FrontSeat, 2)]))
|
||||
->not->toThrow(InvalidVehicleSelectionException::class);
|
||||
});
|
||||
|
||||
test('back seat is rejected when disabled via config', function () {
|
||||
config(['booking.back_seat_enabled' => false]);
|
||||
|
||||
$service = new BookingService;
|
||||
|
||||
expect(fn () => $service->validateSelections([new VehicleSelectionData(VehicleOption::BackSeat)]))
|
||||
->toThrow(InvalidVehicleSelectionException::class);
|
||||
});
|
||||
|
||||
test('whole vehicle is rejected when disabled via config', function () {
|
||||
config(['booking.whole_vehicle_enabled' => false]);
|
||||
|
||||
$service = new BookingService;
|
||||
|
||||
expect(fn () => $service->validateSelections([new VehicleSelectionData(VehicleOption::WholeVehicle)]))
|
||||
->toThrow(InvalidVehicleSelectionException::class);
|
||||
->toThrow(InvalidVehicleSelectionException::class, 'Vehicle option [front_seat] passenger count [2] exceeds the max of [1] per booking.');
|
||||
});
|
||||
|
||||
test('the same vehicle option cannot be selected twice in one booking', function () {
|
||||
|
||||
Reference in New Issue
Block a user