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,77 @@
<?php
namespace Modules\Shared\Support;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
/**
* Writes KEY=VALUE pairs directly into the .env file, preserving every
* other line untouched backs the Access group's "App Settings" page
* (Filament), which edits real env-backed config values (config('app.*'),
* config('booking.*')) in place rather than introducing a parallel
* DB-backed settings table. Existing keys are replaced in place; missing
* keys are appended.
*
* Requires the .env file to be writable by the web server process not
* guaranteed on every deployment target (e.g. an immutable/read-only
* container filesystem). Callers should surface a clear error if the write
* fails rather than silently losing the change.
*/
class EnvFileWriter
{
private readonly string $path;
public function __construct(?string $path = null)
{
$this->path = $path ?? base_path('.env');
}
/**
* @param array<string, bool|int|string|null> $values
*/
public function write(array $values): void
{
$contents = File::exists($this->path) ? File::get($this->path) : '';
foreach ($values as $key => $value) {
$contents = $this->setKey($contents, $key, $value);
}
File::put($this->path, $contents);
}
private function setKey(string $contents, string $key, bool|int|string|null $value): string
{
$line = $key.'='.$this->formatValue($value);
$pattern = '/^'.preg_quote($key, '/').'=.*$/m';
if (preg_match($pattern, $contents) === 1) {
return (string) preg_replace($pattern, $line, $contents, 1);
}
return rtrim($contents, "\n")."\n".$line."\n";
}
private function formatValue(bool|int|string|null $value): string
{
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
if ($value === null || $value === '') {
return '';
}
if (is_int($value)) {
return (string) $value;
}
// Quote values containing whitespace or characters that would
// otherwise break .env parsing (matches the convention already used
// by hand-written entries in this project's .env.example).
return Str::contains($value, [' ', '#', '"'])
? '"'.str_replace('"', '\\"', $value).'"'
: $value;
}
}
@@ -0,0 +1,60 @@
<?php
use Modules\Shared\Support\EnvFileWriter;
beforeEach(function () {
$this->path = sys_get_temp_dir().'/env-file-writer-test-'.uniqid().'.env';
});
afterEach(function () {
@unlink($this->path);
});
test('it replaces an existing key in place without touching other lines', function () {
file_put_contents($this->path, "APP_NAME=Laravel\nAPP_ENV=local\n");
(new EnvFileWriter($this->path))->write(['APP_NAME' => 'New Name']);
expect(file_get_contents($this->path))->toBe("APP_NAME=\"New Name\"\nAPP_ENV=local\n");
});
test('it appends a missing key at the end of the file', function () {
file_put_contents($this->path, "APP_NAME=Laravel\n");
(new EnvFileWriter($this->path))->write(['SUPPORT_EMAIL' => 'support@example.com']);
expect(file_get_contents($this->path))->toBe("APP_NAME=Laravel\nSUPPORT_EMAIL=support@example.com\n");
});
test('it formats booleans as bare true/false', function () {
file_put_contents($this->path, '');
(new EnvFileWriter($this->path))->write(['BOOKING_BACK_SEAT_ENABLED' => false]);
expect(file_get_contents($this->path))->toContain('BOOKING_BACK_SEAT_ENABLED=false');
});
test('it quotes values containing whitespace', function () {
file_put_contents($this->path, '');
(new EnvFileWriter($this->path))->write(['APP_NAME' => 'My Company']);
expect(file_get_contents($this->path))->toContain('APP_NAME="My Company"');
});
test('it writes multiple keys in one call', function () {
file_put_contents($this->path, "APP_NAME=Laravel\n");
(new EnvFileWriter($this->path))->write([
'APP_NAME' => 'Renamed',
'APP_CURRENCY' => 'MMK',
'BOOKING_FRONT_SEAT_MAX_PER_BOOKING' => 2,
]);
$contents = file_get_contents($this->path);
expect($contents)
->toContain('APP_NAME=Renamed')
->toContain('APP_CURRENCY=MMK')
->toContain('BOOKING_FRONT_SEAT_MAX_PER_BOOKING=2');
});