Add Booking module: model, create/read/cancel API, Filament resource (T4.1-T4.7)

- Booking model with booking_vehicle_options line items (supports mixing
  vehicle options like front_seat + back_seat in one booking), price
  snapshot, status machine, and driver/car assignment fields
- BookingService: front-seat max, disabled-option toggles, duplicate-option
  and whole-vehicle-exclusivity guards
- CreateBookingAction, CancelBookingAction, AssignDriverAction
- BookingRefGenerator: sequential EVB-AAAAA1-style refs via row lock
- POST/GET/cancel booking API endpoints (Sanctum, ownership + admin policy)
- BookingPlugin + Filament BookingResource: list, detail view, Cancel and
  Assign Driver actions (shared between table and detail page)
- domain.md updated for multi-vehicle-option bookings (§2) and driver/
  vehicle assignment (§5a)
This commit is contained in:
Nyan Lin Paing
2026-08-08 21:43:15 +07:00
parent 4da9ecfe7d
commit 5b68f4fa38
51 changed files with 2698 additions and 42 deletions
@@ -0,0 +1,76 @@
<?php
use Modules\Booking\Actions\AssignDriverAction;
use Modules\Booking\Data\AssignDriverData;
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Exceptions\DriverAssignmentNotAllowedException;
use Modules\Booking\Models\Booking;
test('it assigns driver and car details to a confirmed booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
$updated = (new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'U Aung',
driverPhone: '+959111222333',
carPlateNumber: 'YGN-1234',
carModel: 'Tesla Model Y',
));
expect($updated->driver_name)->toBe('U Aung')
->and($updated->driver_phone)->toBe('+959111222333')
->and($updated->car_plate_number)->toBe('YGN-1234')
->and($updated->car_model)->toBe('Tesla Model Y')
->and($booking->refresh()->driver_name)->toBe('U Aung');
});
test('car_model is optional', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
$updated = (new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'U Aung',
driverPhone: '+959111222333',
carPlateNumber: 'YGN-1234',
));
expect($updated->car_model)->toBeNull();
});
test('it guards against assigning a driver to a pending_payment booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::PendingPayment]);
expect(fn () => (new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'U Aung',
driverPhone: '+959111222333',
carPlateNumber: 'YGN-1234',
)))->toThrow(DriverAssignmentNotAllowedException::class);
expect($booking->refresh()->driver_name)->toBeNull();
});
test('it guards against assigning a driver to a cancelled booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Cancelled]);
expect(fn () => (new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'U Aung',
driverPhone: '+959111222333',
carPlateNumber: 'YGN-1234',
)))->toThrow(DriverAssignmentNotAllowedException::class);
});
test('reassigning a different driver on a still-confirmed booking overwrites the previous values', function () {
$booking = Booking::factory()->create([
'status' => BookingStatus::Confirmed,
'driver_name' => 'U Aung',
'driver_phone' => '+959111222333',
'car_plate_number' => 'YGN-1234',
]);
(new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'Daw Hla',
driverPhone: '+959444555666',
carPlateNumber: 'YGN-5678',
));
expect($booking->refresh()->driver_name)->toBe('Daw Hla')
->and($booking->car_plate_number)->toBe('YGN-5678');
});
@@ -0,0 +1,40 @@
<?php
use Modules\Booking\Models\Booking;
use Modules\Booking\Services\BookingRefGenerator;
test('the first booking ref starts the sequence at AAAAA1', function () {
$ref = (new BookingRefGenerator)->generate();
expect($ref)->toBe('EVB-AAAAA1');
});
test('the ref increments digit by digit through the alphabet', function () {
Booking::factory()->create(['booking_ref' => 'EVB-AAAAA9']);
expect((new BookingRefGenerator)->generate())->toBe('EVB-AAAAAA');
});
test('the ref carries over into the next position once the alphabet is exhausted', function () {
Booking::factory()->create(['booking_ref' => 'EVB-AAAAZZ']);
expect((new BookingRefGenerator)->generate())->toBe('EVB-AAAB11');
});
test('generated refs are unique across repeated calls', function () {
$refs = [];
for ($i = 0; $i < 20; $i++) {
$ref = (new BookingRefGenerator)->generate();
Booking::factory()->create(['booking_ref' => $ref]);
$refs[] = $ref;
}
expect($refs)->toEqual(array_unique($refs));
});
test('an unrecognised existing ref format resets the sequence rather than throwing', function () {
Booking::factory()->create(['booking_ref' => 'LEGACY-2024-0001']);
expect((new BookingRefGenerator)->generate())->toBe('EVB-AAAAA1');
});
@@ -0,0 +1,92 @@
<?php
use Modules\Booking\Data\VehicleSelectionData;
use Modules\Booking\Exceptions\InvalidVehicleSelectionException;
use Modules\Booking\Services\BookingService;
use Modules\Shared\Enums\VehicleOption;
test('a normal single-option selection of each vehicle option passes', function () {
config([
'booking.back_seat_enabled' => true,
'booking.whole_vehicle_enabled' => true,
]);
$service = new BookingService;
foreach (VehicleOption::cases() as $option) {
expect(fn () => $service->validateSelections([new VehicleSelectionData($option)]))
->not->toThrow(InvalidVehicleSelectionException::class);
}
});
test('front seat and back seat can be selected together in one booking', function () {
config(['booking.back_seat_enabled' => true]);
$service = new BookingService;
expect(fn () => $service->validateSelections([
new VehicleSelectionData(VehicleOption::FrontSeat, 1),
new VehicleSelectionData(VehicleOption::BackSeat, 2),
]))->not->toThrow(InvalidVehicleSelectionException::class);
});
test('requesting more front seats than the configured max is rejected', 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);
});
test('the same vehicle option cannot be selected twice in one booking', function () {
config(['booking.back_seat_enabled' => true]);
$service = new BookingService;
expect(fn () => $service->validateSelections([
new VehicleSelectionData(VehicleOption::BackSeat, 1),
new VehicleSelectionData(VehicleOption::BackSeat, 1),
]))->toThrow(InvalidVehicleSelectionException::class);
});
test('whole vehicle cannot be combined with another vehicle option', function () {
config([
'booking.back_seat_enabled' => true,
'booking.whole_vehicle_enabled' => true,
]);
$service = new BookingService;
expect(fn () => $service->validateSelections([
new VehicleSelectionData(VehicleOption::WholeVehicle),
new VehicleSelectionData(VehicleOption::BackSeat),
]))->toThrow(InvalidVehicleSelectionException::class);
});
@@ -0,0 +1,38 @@
<?php
use Modules\Booking\Actions\CancelBookingAction;
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Exceptions\BookingCannotBeCancelledException;
use Modules\Booking\Models\Booking;
test('it cancels a pending_payment booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::PendingPayment]);
$cancelled = (new CancelBookingAction)->handle($booking);
expect($cancelled->status)->toBe(BookingStatus::Cancelled)
->and($booking->refresh()->status)->toBe(BookingStatus::Cancelled);
});
test('it guards against cancelling a confirmed booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
expect(fn () => (new CancelBookingAction)->handle($booking))
->toThrow(BookingCannotBeCancelledException::class);
expect($booking->refresh()->status)->toBe(BookingStatus::Confirmed);
});
test('it guards against cancelling an already cancelled booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Cancelled]);
expect(fn () => (new CancelBookingAction)->handle($booking))
->toThrow(BookingCannotBeCancelledException::class);
});
test('it guards against cancelling an expired booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Expired]);
expect(fn () => (new CancelBookingAction)->handle($booking))
->toThrow(BookingCannotBeCancelledException::class);
});