Fix package limits in tenant overview
This commit is contained in:
@@ -29,23 +29,34 @@ class TenantPackageController extends Controller
|
|||||||
->orderBy('created_at', 'desc')
|
->orderBy('created_at', 'desc')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$packages->each(function ($package) {
|
$packages->each(fn (TenantPackage $package) => $this->hydratePackageSnapshot($package));
|
||||||
$pkg = $package->package;
|
|
||||||
$package->remaining_events = $pkg->max_events_per_year - $package->used_events;
|
$activePackage = $tenant->activeResellerPackage?->load('package');
|
||||||
$package->package_limits = array_merge(
|
|
||||||
$pkg->limits,
|
if ($activePackage instanceof TenantPackage) {
|
||||||
[
|
$this->hydratePackageSnapshot($activePackage);
|
||||||
'branding_allowed' => $pkg->branding_allowed,
|
}
|
||||||
'watermark_allowed' => $pkg->watermark_allowed,
|
|
||||||
'features' => $pkg->features,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'data' => $packages,
|
'data' => $packages,
|
||||||
'active_package' => $tenant->activeResellerPackage ? $tenant->activeResellerPackage->load('package') : null,
|
'active_package' => $activePackage,
|
||||||
'message' => 'Tenant packages loaded successfully.',
|
'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 ?? [],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -505,9 +505,6 @@ function PackageSummarySheet({
|
|||||||
const { t } = useTranslation('management');
|
const { t } = useTranslation('management');
|
||||||
const { textStrong, muted, border, surface, accentSoft, primary } = useAdminTheme();
|
const { textStrong, muted, border, surface, accentSoft, primary } = useAdminTheme();
|
||||||
const text = textStrong;
|
const text = textStrong;
|
||||||
const maxPhotos = (limits as Record<string, number | null> | null)?.max_photos ?? null;
|
|
||||||
const maxGuests = (limits as Record<string, number | null> | null)?.max_guests ?? null;
|
|
||||||
const galleryDays = (limits as Record<string, number | null> | null)?.gallery_days ?? null;
|
|
||||||
const resolvedFeatures = collectPackageFeatures({
|
const resolvedFeatures = collectPackageFeatures({
|
||||||
features,
|
features,
|
||||||
package_limits: limits,
|
package_limits: limits,
|
||||||
@@ -532,9 +529,6 @@ function PackageSummarySheet({
|
|||||||
</Text>
|
</Text>
|
||||||
</YStack>
|
</YStack>
|
||||||
<YStack space="$2" marginTop="$2">
|
<YStack space="$2" marginTop="$2">
|
||||||
<SummaryRow label={t('mobileDashboard.packageSummary.limitPhotos', 'Photos')} value={formatPackageLimit(maxPhotos, t)} />
|
|
||||||
<SummaryRow label={t('mobileDashboard.packageSummary.limitGuests', 'Guests')} value={formatPackageLimit(maxGuests, t)} />
|
|
||||||
<SummaryRow label={t('mobileDashboard.packageSummary.limitDays', 'Gallery days')} value={formatPackageLimit(galleryDays, t)} />
|
|
||||||
<SummaryRow
|
<SummaryRow
|
||||||
label={t('mobileDashboard.packageSummary.remaining', 'Remaining events')}
|
label={t('mobileDashboard.packageSummary.remaining', 'Remaining events')}
|
||||||
value={formatPackageLimit(remainingEvents, t)}
|
value={formatPackageLimit(remainingEvents, t)}
|
||||||
|
|||||||
48
tests/Feature/Tenant/TenantPackageOverviewTest.php
Normal file
48
tests/Feature/Tenant/TenantPackageOverviewTest.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Tenant;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Api\TenantPackageController;
|
||||||
|
use App\Models\Package;
|
||||||
|
use App\Models\TenantPackage;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class TenantPackageOverviewTest extends TenantTestCase
|
||||||
|
{
|
||||||
|
public function test_active_package_contains_limits_and_features(): void
|
||||||
|
{
|
||||||
|
$package = Package::factory()->reseller()->create([
|
||||||
|
'max_photos' => 1500,
|
||||||
|
'max_guests' => 250,
|
||||||
|
'gallery_days' => 30,
|
||||||
|
'max_tasks' => 40,
|
||||||
|
'max_events_per_year' => 12,
|
||||||
|
'features' => ['custom_branding', 'reseller_dashboard'],
|
||||||
|
'branding_allowed' => true,
|
||||||
|
'watermark_allowed' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
TenantPackage::factory()->create([
|
||||||
|
'tenant_id' => $this->tenant->id,
|
||||||
|
'package_id' => $package->id,
|
||||||
|
'active' => true,
|
||||||
|
'used_events' => 3,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$request = Request::create('/api/v1/tenant/packages', 'GET');
|
||||||
|
$request->attributes->set('tenant', $this->tenant);
|
||||||
|
|
||||||
|
$response = app(TenantPackageController::class)->index($request);
|
||||||
|
$payload = $response->getData(true);
|
||||||
|
|
||||||
|
$this->assertSame(1500, $payload['active_package']['package_limits']['max_photos']);
|
||||||
|
$this->assertSame(250, $payload['active_package']['package_limits']['max_guests']);
|
||||||
|
$this->assertSame(30, $payload['active_package']['package_limits']['gallery_days']);
|
||||||
|
$this->assertSame(40, $payload['active_package']['package_limits']['max_tasks']);
|
||||||
|
$this->assertSame(12, $payload['active_package']['package_limits']['max_events_per_year']);
|
||||||
|
$this->assertTrue($payload['active_package']['package_limits']['branding_allowed']);
|
||||||
|
$this->assertFalse($payload['active_package']['package_limits']['watermark_allowed']);
|
||||||
|
$this->assertSame(['custom_branding', 'reseller_dashboard'], $payload['active_package']['package_limits']['features']);
|
||||||
|
$this->assertSame(9, $payload['active_package']['remaining_events']);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user