63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\TenantPackage;
|
|
use App\Support\ApiError;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class TenantPackageController extends Controller
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$tenant = $request->attributes->get('tenant');
|
|
|
|
if (! $tenant) {
|
|
return ApiError::response(
|
|
'tenant_not_found',
|
|
'Tenant Not Found',
|
|
'The authenticated tenant context could not be resolved.',
|
|
Response::HTTP_NOT_FOUND
|
|
);
|
|
}
|
|
|
|
$packages = TenantPackage::where('tenant_id', $tenant->id)
|
|
->with('package')
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
$packages->each(fn (TenantPackage $package) => $this->hydratePackageSnapshot($package));
|
|
|
|
$activePackage = $tenant->activeResellerPackage?->load('package');
|
|
|
|
if ($activePackage instanceof TenantPackage) {
|
|
$this->hydratePackageSnapshot($activePackage);
|
|
}
|
|
|
|
return response()->json([
|
|
'data' => $packages,
|
|
'active_package' => $activePackage,
|
|
'message' => 'Tenant packages loaded successfully.',
|
|
]);
|
|
}
|
|
|
|
private function hydratePackageSnapshot(TenantPackage $package): void
|
|
{
|
|
$pkg = $package->package;
|
|
|
|
$maxEvents = $pkg?->max_events_per_year;
|
|
$package->remaining_events = $maxEvents === null ? null : max($maxEvents - $package->used_events, 0);
|
|
$package->package_limits = array_merge(
|
|
$pkg?->limits ?? [],
|
|
[
|
|
'branding_allowed' => $pkg?->branding_allowed,
|
|
'watermark_allowed' => $pkg?->watermark_allowed,
|
|
'features' => $pkg?->features ?? [],
|
|
]
|
|
);
|
|
}
|
|
}
|