Improve package usage visibility
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Support\ApiError;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -29,12 +30,17 @@ class TenantPackageController extends Controller
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
$packages->each(fn (TenantPackage $package) => $this->hydratePackageSnapshot($package));
|
||||
$usageEventPackage = $this->resolveUsageEventPackage($tenant->id);
|
||||
|
||||
$packages->each(function (TenantPackage $package) use ($usageEventPackage): void {
|
||||
$eventPackage = $package->active ? $usageEventPackage : null;
|
||||
$this->hydratePackageSnapshot($package, $eventPackage);
|
||||
});
|
||||
|
||||
$activePackage = $tenant->activeResellerPackage?->load('package');
|
||||
|
||||
if ($activePackage instanceof TenantPackage) {
|
||||
$this->hydratePackageSnapshot($activePackage);
|
||||
$this->hydratePackageSnapshot($activePackage, $usageEventPackage);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
@@ -44,7 +50,7 @@ class TenantPackageController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
private function hydratePackageSnapshot(TenantPackage $package): void
|
||||
private function hydratePackageSnapshot(TenantPackage $package, ?EventPackage $eventPackage = null): void
|
||||
{
|
||||
$pkg = $package->package;
|
||||
|
||||
@@ -52,6 +58,7 @@ class TenantPackageController extends Controller
|
||||
$package->remaining_events = $maxEvents === null ? null : max($maxEvents - $package->used_events, 0);
|
||||
$package->package_limits = array_merge(
|
||||
$pkg?->limits ?? [],
|
||||
$this->buildUsageSnapshot($eventPackage),
|
||||
[
|
||||
'branding_allowed' => $pkg?->branding_allowed,
|
||||
'watermark_allowed' => $pkg?->watermark_allowed,
|
||||
@@ -59,4 +66,72 @@ class TenantPackageController extends Controller
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, EventPackage>
|
||||
*/
|
||||
private function resolveUsageEventPackage(int $tenantId): ?EventPackage
|
||||
{
|
||||
$baseQuery = EventPackage::query()
|
||||
->whereHas('event', fn ($query) => $query->where('tenant_id', $tenantId))
|
||||
->with('package')
|
||||
->orderByDesc('purchased_at')
|
||||
->orderByDesc('created_at');
|
||||
|
||||
$activeEventPackage = (clone $baseQuery)
|
||||
->whereNotNull('gallery_expires_at')
|
||||
->where('gallery_expires_at', '>=', now())
|
||||
->first();
|
||||
|
||||
return $activeEventPackage ?? $baseQuery->first();
|
||||
}
|
||||
|
||||
private function buildUsageSnapshot(?EventPackage $eventPackage): array
|
||||
{
|
||||
if (! $eventPackage) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$limits = $eventPackage->effectiveLimits();
|
||||
$maxPhotos = $this->normalizeLimit($limits['max_photos'] ?? null);
|
||||
$maxGuests = $this->normalizeLimit($limits['max_guests'] ?? null);
|
||||
$galleryDays = $this->normalizeLimit($limits['gallery_days'] ?? null);
|
||||
|
||||
$usedPhotos = (int) $eventPackage->used_photos;
|
||||
$usedGuests = (int) $eventPackage->used_guests;
|
||||
|
||||
$remainingPhotos = $maxPhotos === null ? null : max(0, $maxPhotos - $usedPhotos);
|
||||
$remainingGuests = $maxGuests === null ? null : max(0, $maxGuests - $usedGuests);
|
||||
|
||||
$remainingGalleryDays = null;
|
||||
$usedGalleryDays = null;
|
||||
if ($galleryDays !== null && $eventPackage->gallery_expires_at) {
|
||||
$remainingGalleryDays = max(0, now()->diffInDays($eventPackage->gallery_expires_at, false));
|
||||
$usedGalleryDays = max(0, $galleryDays - $remainingGalleryDays);
|
||||
}
|
||||
|
||||
return array_filter([
|
||||
'used_photos' => $maxPhotos === null ? null : $usedPhotos,
|
||||
'remaining_photos' => $remainingPhotos,
|
||||
'used_guests' => $maxGuests === null ? null : $usedGuests,
|
||||
'remaining_guests' => $remainingGuests,
|
||||
'used_gallery_days' => $usedGalleryDays,
|
||||
'remaining_gallery_days' => $remainingGalleryDays,
|
||||
], static fn ($value) => $value !== null);
|
||||
}
|
||||
|
||||
private function normalizeLimit(?int $value): ?int
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = (int) $value;
|
||||
|
||||
if ($value <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user