Ver Fonte

✨ feat(dashboard): adicionar endpoint de dashboard com estatísticas do sistema

Fase: dev | Origin: melhoria-interna
Gustavo Zanatta há 1 semana atrás
pai
commit
fe5fbd538b

+ 17 - 0
app/Http/Controllers/DashboardController.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Services\DashboardService;
+use Illuminate\Http\JsonResponse;
+
+class DashboardController extends Controller
+{
+    public function __construct(protected DashboardService $service) {}
+
+    public function stats(): JsonResponse
+    {
+        $stats = $this->service->getStats();
+        return $this->successResponse(payload: $stats);
+    }
+}

+ 25 - 0
app/Services/DashboardService.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Services;
+
+use App\Enums\UserStatusEnum;
+use App\Enums\UserTypeEnum;
+use App\Models\PartnerAgreement;
+use App\Models\User;
+
+class DashboardService
+{
+  public function getStats(): array
+  {
+    return [
+      'total_associados'    => User::where('type', UserTypeEnum::ASSOCIADO)->count(),
+      'associados_ativos'   => User::where('type', UserTypeEnum::ASSOCIADO)->where('status', UserStatusEnum::ACTIVE)->count(),
+      'parceiros'           => PartnerAgreement::count(),
+      'contratos_a_vencer'  => PartnerAgreement::whereNotNull('contract_end')
+        ->whereBetween('contract_end', [now()->toDateString(), now()->addDays(30)->toDateString()])
+        ->count(),
+      'novos_mes'           => PartnerAgreement::whereMonth('created_at', now()->month)->whereYear('created_at', now()->year)->count(),
+      'associados_pendentes' => User::where('type', UserTypeEnum::ASSOCIADO)->where('status', UserStatusEnum::PENDING)->count(),
+    ];
+  }
+}

+ 8 - 0
routes/authRoutes/dashboard.php

@@ -0,0 +1,8 @@
+<?php
+
+use App\Http\Controllers\DashboardController;
+use Illuminate\Support\Facades\Route;
+
+Route::controller(DashboardController::class)->prefix('dashboard')->group(function () {
+    Route::get('/stats', 'stats')->middleware('permission:dashboard,view');
+});