feat: Phase 2 - admin layout, dashboard, page CRUD, subscribers, user management
This commit is contained in:
41
app/Services/DashboardStatisticsService.php
Normal file
41
app/Services/DashboardStatisticsService.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\PreregistrationPage;
|
||||
use App\Models\Subscriber;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
final class DashboardStatisticsService
|
||||
{
|
||||
/**
|
||||
* @return array{total_pages: int, total_subscribers: int, active_pages: int}
|
||||
*/
|
||||
public function forUser(User $user): array
|
||||
{
|
||||
$pagesQuery = PreregistrationPage::query();
|
||||
if (! $user->isSuperadmin()) {
|
||||
$pagesQuery->where('user_id', $user->id);
|
||||
}
|
||||
|
||||
$now = Carbon::now();
|
||||
$totalPages = (clone $pagesQuery)->count();
|
||||
$pageIds = (clone $pagesQuery)->pluck('id');
|
||||
$totalSubscribers = $pageIds->isEmpty()
|
||||
? 0
|
||||
: Subscriber::whereIn('preregistration_page_id', $pageIds)->count();
|
||||
$activePages = (clone $pagesQuery)
|
||||
->where('start_date', '<=', $now)
|
||||
->where('end_date', '>=', $now)
|
||||
->count();
|
||||
|
||||
return [
|
||||
'total_pages' => $totalPages,
|
||||
'total_subscribers' => $totalSubscribers,
|
||||
'active_pages' => $activePages,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user