Files
fewo-stommer/app/Filament/Operator/Concerns/EditsTenantServices.php
T
Lars StommerandClaude Opus 5 e6d4d89141 Mandanten Phase 3: Einstellungen, Texte und Dienste gehören dem Mandanten
Vermieterdaten, Bank, Steuerangabe, Adressen, Betriebsart und Fristen sind
Spalten von `tenants`. `App\Tenancy\TenantConfig` legt sie über
`config('fewo.*')` und `mail.from`, sobald der Mandantenkontext wechselt –
so lesen Vorlagen, Mails und der Mietvertrag weiter dieselben Schlüssel und
`resources/themes/**` bleibt unangetastet.

Die `.env` ist die Grundlinie: Rückfall, solange kein Mandant feststeht, und
Grenze für Löschfristen und Bot-Schutz (nur verschärfen, nicht lockern). Die
Flutgrenze bleibt global. Bank, Steuerangabe und eigene Dienst-Schlüssel
liegen verschlüsselt.

Die Seite System › Betrieb schreibt jetzt den Mandanten und enthält
zusätzlich Vermieterdaten, Bank und die Fristen. Tabelle `settings` und
Modell `Setting` entfallen; die Migration übernimmt die bisherigen Werte
aus `settings` und der Konfiguration in den Mandanten Stommer.

Neu: `tenant_services` (Freischaltung je Dienst mit optionalem eigenem
API-Schlüssel, nur im Betreiber-Bereich schaltbar) und `service_usages`.
Es zählt noch nichts, weil kein Dienst aufruft.

235 Feature-Tests grün. Der Umbauweg wurde zusätzlich auf einer Kopie der
lokalen Datenbank durchgespielt.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-23 20:49:22 +02:00

101 lines
2.5 KiB
PHP

<?php
namespace App\Filament\Operator\Concerns;
use App\Enums\TenantServiceKey;
use App\Models\Tenant;
use App\Models\TenantService;
/**
* The service switches of the tenant form. They live in their own table, so the form state is read and
* written separately from the landlord's own columns.
*/
trait EditsTenantServices
{
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function mutateFormDataBeforeFill(array $data): array
{
$tenant = $this->record;
if (! $tenant instanceof Tenant) {
return $data; // creating: there is nothing to read yet
}
$data['services'] = $tenant->services
->mapWithKeys(fn (TenantService $service): array => [$service->service => [
'is_enabled' => $service->is_enabled,
'api_key' => $service->api_key,
]])
->all();
return $data;
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function mutateFormDataBeforeCreate(array $data): array
{
return $this->withoutServices($data);
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function mutateFormDataBeforeSave(array $data): array
{
return $this->withoutServices($data);
}
protected function afterCreate(): void
{
$this->saveServices();
}
protected function afterSave(): void
{
$this->saveServices();
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
private function withoutServices(array $data): array
{
$this->services = is_array($data['services'] ?? null) ? $data['services'] : [];
unset($data['services']);
return $data;
}
private function saveServices(): void
{
$tenant = $this->record;
if (! $tenant instanceof Tenant) {
return;
}
foreach (TenantServiceKey::cases() as $service) {
$state = $this->services[$service->value] ?? [];
$tenant->services()->updateOrCreate(
['service' => $service->value],
[
'is_enabled' => (bool) ($state['is_enabled'] ?? false),
'api_key' => filled($state['api_key'] ?? null) ? $state['api_key'] : null,
],
);
}
$tenant->unsetRelation('services');
}
}