Add tenant lifecycle view and limit controls
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled

This commit is contained in:
Codex Agent
2026-01-01 19:36:51 +01:00
parent 117250879b
commit da06db2d3b
22 changed files with 1312 additions and 148 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Services\Tenant;
use App\Models\Tenant;
use App\Models\TenantLifecycleEvent;
use App\Models\User;
use Carbon\CarbonInterface;
class TenantLifecycleLogger
{
public function record(
Tenant $tenant,
string $type,
array $payload = [],
?User $actor = null,
?CarbonInterface $occurredAt = null
): TenantLifecycleEvent {
return TenantLifecycleEvent::create([
'tenant_id' => $tenant->getKey(),
'actor_id' => $actor?->getKey(),
'type' => $type,
'payload' => $payload,
'occurred_at' => $occurredAt ?? now(),
]);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Services\Tenant;
use App\Models\EventMediaAsset;
use App\Models\Tenant;
class TenantUsageService
{
public function storageSummary(Tenant $tenant): array
{
$limitBytes = $this->storageLimitBytes($tenant);
$usedBytes = $this->storageUsedBytes($tenant);
$remainingBytes = $limitBytes !== null
? max(0, $limitBytes - $usedBytes)
: null;
return [
'used_bytes' => $usedBytes,
'limit_bytes' => $limitBytes,
'remaining_bytes' => $remainingBytes,
'used_mb' => $this->bytesToMegabytes($usedBytes),
'limit_mb' => $limitBytes !== null ? $this->bytesToMegabytes($limitBytes) : null,
'remaining_mb' => $remainingBytes !== null ? $this->bytesToMegabytes($remainingBytes) : null,
'percentage' => $limitBytes !== null && $limitBytes > 0
? round(min(1, $usedBytes / $limitBytes) * 100, 1)
: null,
];
}
public function storageLimitBytes(Tenant $tenant): ?int
{
$limitMb = $tenant->max_storage_mb;
if ($limitMb === null) {
return null;
}
$limitMb = (int) $limitMb;
if ($limitMb <= 0) {
return null;
}
return $limitMb * 1024 * 1024;
}
public function storageUsedBytes(Tenant $tenant): int
{
return (int) EventMediaAsset::query()
->whereHas('event', fn ($query) => $query->where('tenant_id', $tenant->getKey()))
->sum('size_bytes');
}
private function bytesToMegabytes(int $bytes): float
{
return round($bytes / (1024 * 1024), 2);
}
}