137 lines
4.7 KiB
PHP
137 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Event;
|
|
use App\Models\PackagePurchase;
|
|
use App\Models\Tenant;
|
|
use App\Services\Tenant\DashboardSummaryService;
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function __invoke(Request $request, DashboardSummaryService $summaryService): Response
|
|
{
|
|
$user = $request->user();
|
|
$tenant = $user?->tenant;
|
|
|
|
$summary = $tenant instanceof Tenant
|
|
? $summaryService->build($tenant)
|
|
: null;
|
|
|
|
$events = $tenant instanceof Tenant
|
|
? $this->collectUpcomingEvents($tenant)
|
|
: collect();
|
|
|
|
$purchases = $tenant instanceof Tenant
|
|
? $this->collectRecentPurchases($tenant)
|
|
: collect();
|
|
|
|
$activePackage = $summary['active_package'] ?? null;
|
|
|
|
return Inertia::render('dashboard', [
|
|
'metrics' => $summary,
|
|
'upcomingEvents' => $events->values()->all(),
|
|
'recentPurchases' => $purchases->values()->all(),
|
|
'latestPurchase' => $purchases->first(),
|
|
'tenant' => $tenant ? [
|
|
'id' => $tenant->id,
|
|
'name' => $tenant->name,
|
|
'eventCreditsBalance' => $tenant->event_credits_balance,
|
|
'subscriptionStatus' => $tenant->subscription_status,
|
|
'subscriptionExpiresAt' => optional($tenant->subscription_expires_at)->toIso8601String(),
|
|
'activePackage' => $activePackage ? [
|
|
'name' => $activePackage['name'] ?? '',
|
|
'price' => $activePackage['price'] ?? null,
|
|
'expiresAt' => $activePackage['expires_at'] ?? null,
|
|
'remainingEvents' => $activePackage['remaining_events'] ?? null,
|
|
] : null,
|
|
] : null,
|
|
'emailVerification' => [
|
|
'mustVerify' => $user instanceof MustVerifyEmail,
|
|
'verified' => $user?->hasVerifiedEmail() ?? false,
|
|
],
|
|
]);
|
|
}
|
|
|
|
private function collectUpcomingEvents(Tenant $tenant): Collection
|
|
{
|
|
return Event::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->withCount(['photos', 'tasks', 'joinTokens'])
|
|
->orderByRaw('date IS NULL')
|
|
->orderBy('date')
|
|
->limit(5)
|
|
->get()
|
|
->map(function (Event $event): array {
|
|
return [
|
|
'id' => $event->id,
|
|
'name' => $this->resolveEventName($event),
|
|
'slug' => $event->slug,
|
|
'status' => $event->status,
|
|
'isActive' => (bool) $event->is_active,
|
|
'date' => optional($event->date)->toIso8601String(),
|
|
'photosCount' => (int) ($event->photos_count ?? 0),
|
|
'tasksCount' => (int) ($event->tasks_count ?? 0),
|
|
'joinTokensCount' => (int) ($event->join_tokens_count ?? 0),
|
|
];
|
|
});
|
|
}
|
|
|
|
private function collectRecentPurchases(Tenant $tenant): Collection
|
|
{
|
|
return $tenant->purchases()
|
|
->with('package')
|
|
->latest('purchased_at')
|
|
->limit(6)
|
|
->get()
|
|
->map(function (PackagePurchase $purchase): array {
|
|
return [
|
|
'id' => $purchase->id,
|
|
'packageName' => $purchase->package?->getNameForLocale(app()->getLocale())
|
|
?? $purchase->package?->name
|
|
?? __('Unknown package'),
|
|
'price' => $purchase->price !== null ? (float) $purchase->price : null,
|
|
'purchasedAt' => optional($purchase->purchased_at)->toIso8601String(),
|
|
'type' => $purchase->type,
|
|
'provider' => $purchase->provider,
|
|
];
|
|
});
|
|
}
|
|
|
|
private function resolveEventName(Event $event): string
|
|
{
|
|
$name = $event->name;
|
|
|
|
if (is_array($name)) {
|
|
$locale = app()->getLocale();
|
|
|
|
if (! empty($name[$locale])) {
|
|
return (string) $name[$locale];
|
|
}
|
|
|
|
foreach (['de', 'en'] as $fallback) {
|
|
if (! empty($name[$fallback])) {
|
|
return (string) $name[$fallback];
|
|
}
|
|
}
|
|
|
|
$firstTranslated = reset($name);
|
|
|
|
if (is_string($firstTranslated) && $firstTranslated !== '') {
|
|
return $firstTranslated;
|
|
}
|
|
}
|
|
|
|
if (is_string($name) && $name !== '') {
|
|
return $name;
|
|
}
|
|
|
|
return __('Untitled event');
|
|
}
|
|
}
|