51 lines
2.3 KiB
PHP
51 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\InterestRequest;
|
|
use App\Models\Opportunity;
|
|
use App\Models\Sector;
|
|
use App\Services\InvestorPath;
|
|
use App\Services\PortalProfileAccess;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function __invoke(Request $request): Response
|
|
{
|
|
if (! $request->user()->can('manage-portal')) {
|
|
$path = app(InvestorPath::class);
|
|
$contexts = $path->contexts($request->user());
|
|
$current = $contexts ? $path->view($request->user(), $contexts[0]['id']) : null;
|
|
|
|
return Inertia::render('dashboard', ['summary' => null, 'investorPath' => $path->available() ? [
|
|
'contexts' => $contexts,
|
|
'current' => $current ? ['id' => $current['case']['id'], 'next' => $current['next'],
|
|
'pending' => count(array_filter($current['steps'], fn ($step) => ! in_array($step['state'], ['completed', 'approved', 'dispensed'], true))),
|
|
'documents' => count($current['documents']), 'results' => count($current['results']), 'tasks' => count($current['tasks'])] : null,
|
|
] : null]);
|
|
}
|
|
|
|
$counts = InterestRequest::query()->selectRaw('status, count(*) as total')
|
|
->groupBy('status')->pluck('total', 'status');
|
|
$drafts = Opportunity::where('status', 'draft');
|
|
if (Schema::hasColumn('opportunities', 'owner_user_id')) {
|
|
$drafts = app(PortalProfileAccess::class)->scope($drafts, $request->user());
|
|
}
|
|
|
|
return Inertia::render('dashboard', ['summary' => [
|
|
'canCreateOpportunity' => app(PortalProfileAccess::class)->legacyWriteAllowed($request->user()),
|
|
'requestsByStatus' => collect(['received', 'in_review', 'contacted', 'closed'])
|
|
->mapWithKeys(fn ($status) => [$status => (int) ($counts[$status] ?? 0)]),
|
|
'publishedOpportunities' => Opportunity::published()->count(),
|
|
'draftOpportunities' => $drafts->count(),
|
|
'activeSectors' => Sector::catalog()->count(),
|
|
'recentRequests' => InterestRequest::orderByDesc('id')->limit(5)
|
|
->get(['id', 'reference', 'sector_interest', 'status', 'created_at']),
|
|
]]);
|
|
}
|
|
}
|