File: /home/xedaptot/be.naniguide.com/app/Http/Controllers/Refactor/Admin/DashboardController.php
<?php
namespace App\Http\Controllers\Refactor\Admin;
use App\Http\Controllers\Controller;
use App\Jobs\RefreshCacheableJob;
use App\Library\Cache\AppCache;
use App\Services\Admin\AdminDashboardCache;
use App\Services\Admin\DashboardService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
private const ALLOWED_DAYS = [7, 30, 90];
private const DEFAULT_DAYS = 7;
public function index(Request $request)
{
$admin = $request->user()->admin;
$service = new DashboardService($admin);
$stats = $service->getStatCounts();
$emailMetrics = $service->getEmailMetrics();
$sendingTrend = $service->getSendingTrend(7);
$campaignPerformance = $service->getCampaignPerformance(self::DEFAULT_DAYS);
$campaignPerformanceComputedAt = AppCache::for(AdminDashboardCache::instance())
->lastUpdatedAt('SentByDay_' . self::DEFAULT_DAYS);
$customerGrowth = $service->getCustomerGrowth();
$planDistribution = $service->getPlanDistribution();
$topCampaigns = $service->getTopCampaigns(5);
$topLists = $service->getTopLists(5);
$recent = $service->getRecentData();
$setupChecklist = $service->getSetupChecklist();
$currentTimezone = $admin->getTimezone();
return view('refactor.pages.admin.dashboard', compact(
'admin',
'stats',
'emailMetrics',
'sendingTrend',
'campaignPerformance',
'campaignPerformanceComputedAt',
'customerGrowth',
'planDistribution',
'topCampaigns',
'topLists',
'recent',
'setupChecklist',
'currentTimezone'
));
}
public function chartData(Request $request): JsonResponse
{
$days = $this->resolveDays($request);
$admin = $request->user()->admin;
$service = new DashboardService($admin);
$data = $service->getCampaignPerformance($days);
$computedAt = AppCache::for(AdminDashboardCache::instance())
->lastUpdatedAt('SentByDay_' . $days);
return response()->json([
'data' => $data,
'computed_at' => $computedAt ? $admin->formatDateTime($computedAt, 'datetime_full') : null,
'computed_at_relative' => $computedAt?->diffForHumans(),
'computed_at_iso' => $computedAt?->toIso8601String(),
]);
}
public function refreshChart(Request $request): JsonResponse
{
$days = $this->resolveDays($request);
RefreshCacheableJob::dispatch(
AdminDashboardCache::instance(),
'SentByDay_' . $days
);
return response()->json([
'message' => trans('refactor/admin_dashboard.performance.refresh_dispatched'),
], 202);
}
private function resolveDays(Request $request): int
{
$days = (int) $request->input('days', self::DEFAULT_DAYS);
return in_array($days, self::ALLOWED_DAYS, true) ? $days : self::DEFAULT_DAYS;
}
}