Intégration fonctionnalites V1
This commit is contained in:
@@ -4,9 +4,17 @@ namespace App\Http\Controllers\Supervisor;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Supervisor\Concerns\ScopesSupervisorResources;
|
||||
use App\Models\Establishment;
|
||||
use App\Models\Machine;
|
||||
use App\Models\MachineCommand;
|
||||
use App\Models\MachineEvent;
|
||||
use App\Models\MachineIntegration;
|
||||
use App\Models\MachineStatusHistory;
|
||||
use App\Models\PricingRule;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
@@ -20,6 +28,7 @@ class MachinePageController extends Controller
|
||||
->with('establishment:id,name,uuid');
|
||||
|
||||
$this->scopeEstablishmentQuery($query);
|
||||
$this->applyEstablishmentFilter($request, $query);
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->string('status'));
|
||||
@@ -38,23 +47,100 @@ class MachinePageController extends Controller
|
||||
}
|
||||
|
||||
$machines = $query
|
||||
->orderBy('name')
|
||||
->paginate(20)
|
||||
->tap(fn ($builder) => $this->applySort($request, $builder, [
|
||||
'name' => 'machines.name',
|
||||
'establishment' => fn ($builder, string $direction) => $builder
|
||||
->leftJoin('establishments', 'machines.establishment_id', '=', 'establishments.id')
|
||||
->orderBy('establishments.name', $direction)
|
||||
->select('machines.*'),
|
||||
'type' => 'machines.type',
|
||||
'status' => 'machines.status',
|
||||
'last_heartbeat_at' => 'machines.last_heartbeat_at',
|
||||
], 'name', 'asc'))
|
||||
->paginate($this->supervisorListPerPage($request))
|
||||
->withQueryString()
|
||||
->through(fn (Machine $machine) => $this->formatMachineListItem($machine));
|
||||
|
||||
return Inertia::render('Supervisor/Machines/Index', [
|
||||
'machines' => $machines,
|
||||
'establishments' => $this->establishmentOptions(),
|
||||
'canManageMachines' => $this->canManageMachines(),
|
||||
'filters' => [
|
||||
'establishment_id' => $request->filled('establishment_id') ? (int) $request->input('establishment_id') : null,
|
||||
'status' => $request->string('status')->toString() ?: null,
|
||||
'type' => $request->string('type')->toString() ?: null,
|
||||
'search' => $request->string('search')->toString() ?: null,
|
||||
'sort' => $request->string('sort')->toString() ?: 'name',
|
||||
'direction' => $request->string('direction')->toString() === 'desc' ? 'desc' : 'asc',
|
||||
'per_page' => $this->supervisorListPerPage($request),
|
||||
],
|
||||
'statusOptions' => $this->statusOptions(),
|
||||
'typeOptions' => $this->typeOptions(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
abort_unless($this->canManageMachines(), 403);
|
||||
|
||||
$validated = $this->validateMachine($request);
|
||||
$this->assertEstablishmentAccessible((int) $validated['establishment_id']);
|
||||
|
||||
$validated['qr_code'] = $this->resolveQrCode($validated['qr_code'] ?? null);
|
||||
$validated['status'] = $validated['status'] ?? 'available';
|
||||
|
||||
$machine = Machine::query()->create($validated);
|
||||
$this->createSimulatedIntegration($machine);
|
||||
|
||||
return redirect()->route('supervisor.machines.index')
|
||||
->with('success', 'Machine créée.');
|
||||
}
|
||||
|
||||
public function update(Request $request, string $uuid): RedirectResponse
|
||||
{
|
||||
abort_unless($this->canManageMachines(), 403);
|
||||
|
||||
$machine = $this->findAccessibleMachine($uuid);
|
||||
$validated = $this->validateMachine($request, $machine);
|
||||
$this->assertEstablishmentAccessible((int) $validated['establishment_id']);
|
||||
|
||||
if (array_key_exists('qr_code', $validated) && blank($validated['qr_code'])) {
|
||||
unset($validated['qr_code']);
|
||||
}
|
||||
|
||||
$machine->update($validated);
|
||||
|
||||
return redirect()->route('supervisor.machines.index')
|
||||
->with('success', 'Machine mise à jour.');
|
||||
}
|
||||
|
||||
public function destroy(string $uuid): RedirectResponse
|
||||
{
|
||||
abort_unless($this->canManageMachines(), 403);
|
||||
|
||||
$machine = $this->findAccessibleMachine($uuid);
|
||||
|
||||
if (in_array($machine->status, ['running', 'reserved'], true)) {
|
||||
return redirect()->route('supervisor.machines.index')
|
||||
->withErrors(['delete' => 'Impossible de supprimer une machine en cours d\'utilisation.']);
|
||||
}
|
||||
|
||||
if ($machine->bookings()->exists() || $machine->washes()->exists()) {
|
||||
return redirect()->route('supervisor.machines.index')
|
||||
->withErrors(['delete' => 'Impossible de supprimer une machine avec un historique de réservations ou de lavages.']);
|
||||
}
|
||||
|
||||
PricingRule::query()->where('machine_id', $machine->id)->update(['machine_id' => null]);
|
||||
MachineCommand::query()->where('machine_id', $machine->id)->delete();
|
||||
MachineEvent::query()->where('machine_id', $machine->id)->delete();
|
||||
MachineStatusHistory::query()->where('machine_id', $machine->id)->delete();
|
||||
$machine->machineIntegration()?->delete();
|
||||
$machine->delete();
|
||||
|
||||
return redirect()->route('supervisor.machines.index')
|
||||
->with('success', 'Machine supprimée.');
|
||||
}
|
||||
|
||||
public function show(string $uuid): Response
|
||||
{
|
||||
$query = Machine::query()->where('uuid', $uuid);
|
||||
@@ -115,9 +201,11 @@ class MachinePageController extends Controller
|
||||
{
|
||||
return [
|
||||
'uuid' => $machine->uuid,
|
||||
'establishment_id' => $machine->establishment_id,
|
||||
'name' => $machine->name,
|
||||
'type' => $machine->type,
|
||||
'type_label' => $this->typeLabel($machine->type),
|
||||
'qr_code' => $machine->qr_code,
|
||||
'status' => $machine->status,
|
||||
'status_label' => $this->statusLabel($machine->status),
|
||||
'establishment_name' => $machine->establishment?->name,
|
||||
@@ -125,6 +213,68 @@ class MachinePageController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function validateMachine(Request $request, ?Machine $machine = null): array
|
||||
{
|
||||
return $request->validate([
|
||||
'establishment_id' => ['required', 'integer', 'exists:establishments,id'],
|
||||
'name' => ['required', 'string', 'max:100'],
|
||||
'type' => ['required', 'in:washer_small,washer_large,dryer_small,dryer_large'],
|
||||
'qr_code' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('machines', 'qr_code')->ignore($machine?->id),
|
||||
],
|
||||
'status' => ['required', 'in:available,reserved,running,maintenance,offline,error'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function findAccessibleMachine(string $uuid): Machine
|
||||
{
|
||||
$query = Machine::query()->where('uuid', $uuid);
|
||||
$this->scopeEstablishmentQuery($query);
|
||||
|
||||
return $query->firstOrFail();
|
||||
}
|
||||
|
||||
private function resolveQrCode(?string $qrCode): string
|
||||
{
|
||||
if (filled($qrCode)) {
|
||||
return $qrCode;
|
||||
}
|
||||
|
||||
do {
|
||||
$candidate = 'LAVERIE-'.strtoupper(Str::random(8));
|
||||
} while (Machine::query()->where('qr_code', $candidate)->exists());
|
||||
|
||||
return $candidate;
|
||||
}
|
||||
|
||||
private function createSimulatedIntegration(Machine $machine): void
|
||||
{
|
||||
$machine->load('establishment');
|
||||
|
||||
MachineIntegration::query()->create([
|
||||
'machine_id' => $machine->id,
|
||||
'provider' => config('laverie.simulation.provider_name', 'simulated'),
|
||||
'external_machine_id' => 'SIM-'.$machine->uuid,
|
||||
'external_site_id' => 'SITE-'.$machine->establishment?->uuid,
|
||||
'mode' => 'simulated',
|
||||
'config' => [
|
||||
'cycle_duration_seconds' => config('laverie.simulation.cycle_duration_seconds', 30),
|
||||
],
|
||||
'is_active' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
private function canManageMachines(): bool
|
||||
{
|
||||
return $this->supervisor()->role !== 'viewer';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user