Add Access group admin surfaces, booking soft deletes, refund crash fix

Access group (Filament):
- StaffResource: manage users with an admin-tier role, gated by manage_staff
- CustomerResource: read-only view of role-less users, gated by view_customers
- RoleResource: edit permissions per role (fixed role set), gated by manage_roles
- ManageAppSettings: tabbed General/Booking settings page that reads/writes
  real .env keys via new EnvFileWriter (no parallel DB settings table, so
  BookingService/config('booking.*') stay unchanged)
- Moved Access above Catalog in the nav group order
- New permissions: manage_staff, manage_roles, view_customers, manage_settings

Booking soft deletes:
- bookings.deleted_at + SoftDeletes on the Booking model
- BookingPolicy::delete (manage_bookings, cancelled/expired only) and
  ::restore (manage_bookings)
- DeleteBookingTableAction/RestoreBookingTableAction + TrashedFilter on
  BookingsTable, using authorize() so the policy is enforced at call time,
  not just cosmetically hidden

Refund crash fix:
- ProcessRefundAction passed a nullable $payment->booking into
  RefundBookingAction's non-nullable Booking param — a soft-deleted
  booking's payment reaching the refund picker was an uncaught TypeError.
  Excluded such payments from the picker and added a defensive guard.
- Same unguarded $event->payment->booking / $event->refund->payment->booking
  pattern fixed in the MarkBookingPaid/MarkBookingRefunded queued listeners.

289 tests passing.
This commit is contained in:
Nyan Lin Paing
2026-08-09 23:21:11 +07:00
parent 46f9b8d5a3
commit fd3a195453
43 changed files with 1450 additions and 3 deletions
@@ -0,0 +1,35 @@
<?php
use App\Models\User;
use Livewire\Livewire;
use Modules\Identity\Database\Seeders\RolePermissionSeeder;
use Modules\Identity\Filament\Resources\Customers\Pages\ListCustomers;
beforeEach(function () {
$this->seed(RolePermissionSeeder::class);
$this->staff = User::factory()->create();
$this->staff->assignRole('support');
});
test('staff with view_customers can list customers', function () {
$this->actingAs($this->staff)->get('/admin/customers')->assertSuccessful();
});
test('the customer resource only lists users without any role', function () {
$customer = User::factory()->create();
$this->actingAs($this->staff);
Livewire::test(ListCustomers::class)
->assertCanSeeTableRecords([$customer])
->assertCanNotSeeTableRecords([$this->staff]);
});
test('a customer view page loads for staff', function () {
$customer = User::factory()->create();
$this->actingAs($this->staff)
->get("/admin/customers/{$customer->id}")
->assertSuccessful();
});
@@ -0,0 +1,71 @@
<?php
use App\Models\User;
use Livewire\Livewire;
use Modules\Identity\Database\Seeders\RolePermissionSeeder;
use Modules\Identity\Filament\Pages\ManageAppSettings;
use Modules\Shared\Support\EnvFileWriter;
beforeEach(function () {
$this->seed(RolePermissionSeeder::class);
// Never let a test write to the real project .env — bind the writer to
// a throwaway temp file instead.
$this->envPath = sys_get_temp_dir().'/manage-app-settings-test-'.uniqid().'.env';
file_put_contents($this->envPath, "APP_NAME=Laravel\n");
app()->instance(EnvFileWriter::class, new EnvFileWriter($this->envPath));
});
afterEach(function () {
@unlink($this->envPath);
});
test('an admin without manage_settings is forbidden from the app settings page', function () {
$support = User::factory()->create();
$support->assignRole('support');
$this->actingAs($support)->get('/admin/manage-app-settings')->assertForbidden();
});
test('a super_admin can view and save app settings, writing them to .env', function () {
$superAdmin = User::factory()->create();
$superAdmin->assignRole('super_admin');
$this->actingAs($superAdmin);
Livewire::test(ManageAppSettings::class)
->assertOk()
->fillForm([
'site_name' => 'EV Booking Co',
'support_email' => 'help@evbooking.test',
'support_phone' => '+95912345678',
'timezone' => 'Asia/Yangon',
'currency' => 'MMK',
'back_seat_enabled' => false,
'whole_vehicle_enabled' => true,
'front_seat_max_per_booking' => 2,
])
->call('save')
->assertHasNoFormErrors();
$contents = file_get_contents($this->envPath);
expect($contents)
->toContain('APP_NAME="EV Booking Co"')
->toContain('SUPPORT_EMAIL=help@evbooking.test')
->toContain('APP_TIMEZONE=Asia/Yangon')
->toContain('APP_CURRENCY=MMK')
->toContain('BOOKING_BACK_SEAT_ENABLED=false')
->toContain('BOOKING_WHOLE_VEHICLE_ENABLED=true')
->toContain('BOOKING_FRONT_SEAT_MAX_PER_BOOKING=2');
});
test('front seat max per booking must be at least 1', function () {
$superAdmin = User::factory()->create();
$superAdmin->assignRole('super_admin');
$this->actingAs($superAdmin);
Livewire::test(ManageAppSettings::class)
->fillForm(['front_seat_max_per_booking' => 0])
->call('save')
->assertHasFormErrors(['front_seat_max_per_booking']);
});
@@ -0,0 +1,36 @@
<?php
use App\Models\User;
use Modules\Identity\Database\Seeders\RolePermissionSeeder;
use Modules\Identity\Filament\Resources\Roles\RoleResource;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->seed(RolePermissionSeeder::class);
$this->superAdmin = User::factory()->create();
$this->superAdmin->assignRole('super_admin');
});
test('a super_admin can list and edit roles', function () {
$this->actingAs($this->superAdmin)->get('/admin/roles')->assertSuccessful();
$role = Role::where('name', 'support')->firstOrFail();
$this->actingAs($this->superAdmin)->get("/admin/roles/{$role->id}/edit")->assertSuccessful();
});
test('an admin without manage_roles is forbidden from the role resource', function () {
$admin = User::factory()->create();
$admin->assignRole('admin');
$this->actingAs($admin)->get('/admin/roles')->assertForbidden();
});
test('roles cannot be created or deleted from the resource', function () {
expect(RoleResource::canCreate())->toBeFalse();
$role = Role::where('name', 'support')->firstOrFail();
expect(RoleResource::canDelete($role))->toBeFalse();
});
@@ -0,0 +1,59 @@
<?php
use App\Models\User;
use Livewire\Livewire;
use Modules\Identity\Database\Seeders\RolePermissionSeeder;
use Modules\Identity\Filament\Resources\Staff\Pages\ListStaff;
use Modules\Identity\Filament\Resources\Staff\StaffResource;
beforeEach(function () {
$this->seed(RolePermissionSeeder::class);
$this->superAdmin = User::factory()->create();
$this->superAdmin->assignRole('super_admin');
});
test('a super_admin can list, create, and edit staff', function () {
$this->actingAs($this->superAdmin)->get('/admin/staff')->assertSuccessful();
$this->actingAs($this->superAdmin)->get('/admin/staff/create')->assertSuccessful();
$other = User::factory()->create();
$other->assignRole('support');
$this->actingAs($this->superAdmin)->get("/admin/staff/{$other->id}/edit")->assertSuccessful();
});
test('an admin without manage_staff is forbidden from the staff resource', function () {
$admin = User::factory()->create();
$admin->assignRole('admin');
$this->actingAs($admin)->get('/admin/staff')->assertForbidden();
});
test('the staff resource only lists users carrying an admin-tier role', function () {
$support = User::factory()->create();
$support->assignRole('support');
$customer = User::factory()->create();
$this->actingAs($this->superAdmin);
Livewire::test(ListStaff::class)
->assertCanSeeTableRecords([$support])
->assertCanNotSeeTableRecords([$customer]);
});
test('a super_admin cannot delete their own staff account', function () {
$this->actingAs($this->superAdmin);
expect(StaffResource::canDelete($this->superAdmin))->toBeFalse();
});
test('a super_admin can delete another staff account', function () {
$other = User::factory()->create();
$other->assignRole('support');
$this->actingAs($this->superAdmin);
expect(StaffResource::canDelete($other))->toBeTrue();
});