Optimierung 2.5: Kontakt-Helfer + tagSuggestionsFor zentralisiert

Erkenntnis: Die Admin-Inline-Query (Contact::withoutGlobalScopes()->where
->orderBy) ist exakt der Contact::forCompany()-Scope -> alle vier
Kontakt-Helfer waren funktional identisch. companyContacts/companyContact/
defaultContactIdFor in neuen Concern InteractsWithPressReleaseContacts
(ueber forCompany) ausgelagert. tagSuggestionsFor() (4x identisch) in
InteractsWithPressReleaseTags verschoben. Ungenutzte Collection-Importe
in den Customer-Editoren entfernt; selectedCompany() bleibt je Kontext
(Admin: withoutGlobalScopes find; Customer: CustomerCompanyContext).

Tests: volle Suite gruen (794 passed, 1 skipped).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-06-19 08:59:38 +00:00
parent b41d34e39f
commit 8c53146548
6 changed files with 80 additions and 178 deletions

View file

@ -0,0 +1,47 @@
<?php
namespace App\Livewire\Concerns;
use App\Models\Contact;
use Illuminate\Database\Eloquent\Collection;
/**
* Pressekontakt-Helfer der PM-Editoren (Admin + Customer).
*
* Alle vier Editoren lesen die Kontakte einer Firma identisch über den
* `Contact::forCompany()`-Scope (ohne globale Scopes, sortiert nach Name).
* Single Source of Truth für die Kontaktauflösung im Editor.
*/
trait InteractsWithPressReleaseContacts
{
/**
* @return Collection<int, Contact>
*/
private function companyContacts(int $companyId): Collection
{
if ($companyId <= 0) {
return new Collection;
}
return Contact::forCompany($companyId)
->get(['id', 'company_id', 'first_name', 'last_name', 'responsibility', 'phone', 'email']);
}
private function companyContact(int $contactId, int $companyId): ?Contact
{
if ($contactId <= 0 || $companyId <= 0) {
return null;
}
return Contact::forCompany($companyId)->whereKey($contactId)->first();
}
private function defaultContactIdFor(int $companyId): ?int
{
if ($companyId <= 0) {
return null;
}
return $this->companyContacts($companyId)->first()?->id;
}
}

View file

@ -2,6 +2,8 @@
namespace App\Livewire\Concerns;
use App\Models\Company;
/**
* Themen-Tag-Verwaltung für die PM-Editoren (Erstellen/Bearbeiten).
*
@ -65,4 +67,31 @@ trait InteractsWithPressReleaseTags
->values()
->all();
}
/**
* Tag-Vorschläge: Portal-Label und Länderbezug der Firma plus generische
* Defaults. Identisch in allen vier PM-Editoren.
*
* @return list<string>
*/
private function tagSuggestionsFor(?Company $company): array
{
$defaults = [
__('Mittelstand'),
__('Unternehmen'),
__('Eröffnung'),
__('Innovation'),
__('Nachhaltigkeit'),
];
if (! $company) {
return $defaults;
}
return array_values(array_unique(array_filter([
$company->portal?->label(),
$company->country_code === 'DE' ? __('Deutschland') : null,
...$defaults,
])));
}
}