75 lines
3.0 KiB
PHP
75 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class ProfileController extends Controller
|
|
{
|
|
public function index(Request $request): Response
|
|
{
|
|
$user = $request->user()
|
|
->load(['tenant' => function ($query) {
|
|
$query->with([
|
|
'purchases' => fn ($purchases) => $purchases
|
|
->with('package')
|
|
->latest('purchased_at')
|
|
->limit(10),
|
|
'tenantPackages' => fn ($packages) => $packages
|
|
->with('package')
|
|
->orderByDesc('active')
|
|
->orderByDesc('purchased_at'),
|
|
]);
|
|
}]);
|
|
|
|
$tenant = $user->tenant;
|
|
$activePackage = $tenant?->tenantPackages
|
|
?->first(fn ($package) => (bool) $package->active);
|
|
|
|
$purchases = $tenant?->purchases
|
|
?->map(fn ($purchase) => [
|
|
'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,
|
|
])
|
|
->values()
|
|
->all();
|
|
|
|
return Inertia::render('Profile/Index', [
|
|
'userData' => [
|
|
'id' => $user->id,
|
|
'name' => $user->name,
|
|
'email' => $user->email,
|
|
'username' => $user->username,
|
|
'preferredLocale' => $user->preferred_locale,
|
|
'emailVerifiedAt' => optional($user->email_verified_at)->toIso8601String(),
|
|
'mustVerifyEmail' => $user instanceof MustVerifyEmail,
|
|
],
|
|
'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->package?->getNameForLocale(app()->getLocale())
|
|
?? $activePackage->package?->name
|
|
?? __('Unknown package'),
|
|
'price' => $activePackage->price !== null ? (float) $activePackage->price : null,
|
|
'expiresAt' => optional($activePackage->expires_at)->toIso8601String(),
|
|
'remainingEvents' => $activePackage->remaining_events ?? null,
|
|
] : null,
|
|
] : null,
|
|
'purchases' => $purchases,
|
|
]);
|
|
}
|
|
}
|