Implémantation de la gestion des Services pouvant avoir des taches
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Models\ServiceTask;
|
||||
use App\Enums\IntegrationStatus;
|
||||
use App\Enums\ServiceTaskStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class DashboardController extends Controller
|
||||
@@ -24,7 +25,7 @@ class DashboardController extends Controller
|
||||
}
|
||||
|
||||
// If user has a service role
|
||||
$serviceRoles = ['DSI', 'Batiment', 'Parc Auto'];
|
||||
$serviceRoles = \App\Models\Service::pluck('name')->toArray();
|
||||
foreach ($serviceRoles as $role) {
|
||||
if ($user->hasRole($role)) {
|
||||
return $this->serviceDashboard($role);
|
||||
@@ -40,9 +41,17 @@ class DashboardController extends Controller
|
||||
|
||||
protected function adminDashboard()
|
||||
{
|
||||
$driver = DB::connection()->getDriverName();
|
||||
$diffQuery = 'TIMESTAMPDIFF(DAY, created_at, completed_at)';
|
||||
if ($driver === 'pgsql') {
|
||||
$diffQuery = 'EXTRACT(EPOCH FROM (completed_at - created_at)) / 86400';
|
||||
} elseif ($driver === 'sqlite') {
|
||||
$diffQuery = 'CAST(julianday(completed_at) - julianday(created_at) AS INTEGER)';
|
||||
}
|
||||
|
||||
$avgCompletionTime = IntegrationRequest::where('status', IntegrationStatus::Completed)
|
||||
->whereNotNull('completed_at')
|
||||
->selectRaw('AVG(TIMESTAMPDIFF(DAY, created_at, completed_at)) as avg_days')
|
||||
->selectRaw("AVG({$diffQuery}) as avg_days")
|
||||
->value('avg_days') ?? 0;
|
||||
|
||||
$serviceDistribution = \App\Models\Service::withCount('serviceTasks')->get()->map(function ($service) {
|
||||
|
||||
82
app/Http/Controllers/PermissionController.php
Normal file
82
app/Http/Controllers/PermissionController.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
class PermissionController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Permission/Index', [
|
||||
'permissions' => Permission::all(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Permission/Create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|unique:permissions,name',
|
||||
]);
|
||||
|
||||
Permission::create(['name' => $validated['name']]);
|
||||
|
||||
return redirect()->route('permissions.index')->with('success', 'Permission créée avec succès.');
|
||||
}
|
||||
|
||||
public function edit(Permission $permission)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Permission/Edit', [
|
||||
'permission' => $permission,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Permission $permission)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|unique:permissions,name,' . $permission->id,
|
||||
]);
|
||||
|
||||
$permission->update(['name' => $validated['name']]);
|
||||
|
||||
return redirect()->route('permissions.index')->with('success', 'Permission mise à jour avec succès.');
|
||||
}
|
||||
|
||||
public function destroy(Permission $permission)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$permission->delete();
|
||||
|
||||
return redirect()->route('permissions.index')->with('success', 'Permission supprimée avec succès.');
|
||||
}
|
||||
}
|
||||
136
app/Http/Controllers/ServiceController.php
Normal file
136
app/Http/Controllers/ServiceController.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Service;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
class ServiceController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Service/Index', [
|
||||
'services' => Service::all(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Service/Create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255|unique:services,name',
|
||||
'code' => 'required|string|max:50|unique:services,code',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
$service = Service::create([
|
||||
'name' => $validated['name'],
|
||||
'code' => strtolower($validated['code']),
|
||||
'is_active' => $validated['is_active'] ?? true,
|
||||
]);
|
||||
|
||||
// Création de la Permission (tâche)
|
||||
$permissionName = 'manage ' . strtolower($service->name) . ' tasks';
|
||||
Permission::firstOrCreate(['name' => $permissionName]);
|
||||
|
||||
// Création du Rôle pour ce Service
|
||||
$role = Role::firstOrCreate(['name' => $service->name]);
|
||||
$role->givePermissionTo([
|
||||
$permissionName,
|
||||
'view dashboard',
|
||||
]);
|
||||
|
||||
return redirect()->route('services.index')->with('success', 'Service créé avec succès.');
|
||||
}
|
||||
|
||||
public function edit(Service $service)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Service/Edit', [
|
||||
'service' => $service,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Service $service)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255|unique:services,name,' . $service->id,
|
||||
'code' => 'required|string|max:50|unique:services,code,' . $service->id,
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
$oldName = $service->name;
|
||||
|
||||
$service->update([
|
||||
'name' => $validated['name'],
|
||||
'code' => strtolower($validated['code']),
|
||||
'is_active' => $validated['is_active'] ?? true,
|
||||
]);
|
||||
|
||||
// Si le nom du service change, on met à jour le rôle et la permission correspondante
|
||||
if ($oldName !== $service->name) {
|
||||
$oldPermissionName = 'manage ' . strtolower($oldName) . ' tasks';
|
||||
$newPermissionName = 'manage ' . strtolower($service->name) . ' tasks';
|
||||
|
||||
$permission = Permission::where('name', $oldPermissionName)->first();
|
||||
if ($permission) {
|
||||
$permission->update(['name' => $newPermissionName]);
|
||||
}
|
||||
|
||||
$role = Role::where('name', $oldName)->first();
|
||||
if ($role) {
|
||||
$role->update(['name' => $service->name]);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('services.index')->with('success', 'Service mis à jour avec succès.');
|
||||
}
|
||||
|
||||
public function destroy(Service $service)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$permissionName = 'manage ' . strtolower($service->name) . ' tasks';
|
||||
$permission = Permission::where('name', $permissionName)->first();
|
||||
if ($permission) {
|
||||
$permission->delete();
|
||||
}
|
||||
|
||||
$role = Role::where('name', $service->name)->first();
|
||||
if ($role) {
|
||||
$role->delete();
|
||||
}
|
||||
|
||||
$service->delete();
|
||||
|
||||
return redirect()->route('services.index')->with('success', 'Service supprimé avec succès.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user