45
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Admin\Concerns\ScopedToTenant;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\DirectoryUser;
|
||||
use App\Signature\SignatureBuilder;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
/** « Qui reçoit quoi » : template résolu pour chaque agent, pour vérifier les priorités. */
|
||||
class AgentController extends Controller
|
||||
{
|
||||
use ScopedToTenant;
|
||||
|
||||
public function index(Request $request, SignatureBuilder $builder): View
|
||||
{
|
||||
$q = trim((string) $request->query('q'));
|
||||
$agents = $this->search($q)->with('groups:id,display_name')->paginate(25)->withQueryString();
|
||||
$resolved = $agents->getCollection()->mapWithKeys(fn ($a) => [$a->id => $builder->templateFor($a)?->name]);
|
||||
|
||||
return view('admin.agents.index', compact('agents', 'resolved', 'q'));
|
||||
}
|
||||
|
||||
public function show(DirectoryUser $agent, SignatureBuilder $builder): View
|
||||
{
|
||||
$this->ensureTenant($agent);
|
||||
|
||||
return view('admin.agents.show', [
|
||||
'agent' => $agent->load('groups:id,display_name'),
|
||||
'template' => $builder->templateFor($agent),
|
||||
'full' => $builder->build($agent, 'new'),
|
||||
'reply' => $builder->build($agent, 'reply'),
|
||||
]);
|
||||
}
|
||||
|
||||
/** Autocomplétion pour le choix de l'agent d'aperçu. */
|
||||
public function lookup(Request $request): JsonResponse
|
||||
{
|
||||
$q = trim((string) $request->query('q'));
|
||||
abort_if(mb_strlen($q) < 2, 422);
|
||||
|
||||
return response()->json($this->search($q)->limit(15)->get()->map(fn ($a) => [
|
||||
'id' => $a->id,
|
||||
'label' => trim($a->display_name.' — '.$a->mail),
|
||||
]));
|
||||
}
|
||||
|
||||
private function search(string $q)
|
||||
{
|
||||
return DirectoryUser::where('tenant_id', $this->tenantId())
|
||||
->where('account_enabled', true)
|
||||
->when($q !== '', fn ($query) => $query->where(fn ($w) => $w
|
||||
->where('display_name', 'like', "%{$q}%")
|
||||
->orWhere('mail', 'like', "%{$q}%")
|
||||
->orWhere('department', 'like', "%{$q}%")))
|
||||
->orderBy('display_name');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user