WS-8: N+1 entschaerfen und PM-Zugriffsmuster konsolidieren
- User::currentPlan() und hasUnlimitedPressReleaseQuota() pro Request memoisiert. Beide werden je Render mehrfach abgefragt (Status-Widget auf jeder Seite, Buchungsseite, Quota-Methoden) und loesten bisher jeweils eigene subscription-/Plan-/payment-option-Queries aus. fresh() setzt den Cache zurueck. - show.blade.php: getMyPR() war NICHT memoisiert und wurde pro Request bis zu 7x mit voller Eager-Load-Query aufgerufen -> jetzt memoisiert. Firmen- Kennzahlen direkt per withCount() eager geladen statt nachtraeglichem loadCount(). - Dupliziertes Zugriffsmuster (withoutGlobalScopes + user_id/company_id) aus edit/show/index in PressRelease::scopeAccessibleBy(User) zentralisiert (Single Source of Truth fuer Kunden-PM-Zugriff), mit Tests abgesichert. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
7de0db3547
commit
1ff1cc3626
6 changed files with 139 additions and 38 deletions
|
|
@ -684,16 +684,7 @@ new #[Layout('components.layouts.app'), Title('Pressemitteilung bearbeiten')] cl
|
|||
{
|
||||
// Pro Livewire-Request memoisiert: mount(), with() und save() greifen
|
||||
// sonst jeweils mit einer eigenen Query auf dieselbe PM zu.
|
||||
$user = auth()->user();
|
||||
|
||||
return $this->cachedPressRelease ??= PressRelease::withoutGlobalScopes()
|
||||
->where(function ($q) use ($user): void {
|
||||
$q->where('user_id', $user->id);
|
||||
$companyIds = $user->accessibleCompanyIds();
|
||||
if ($companyIds !== []) {
|
||||
$q->orWhereIn('company_id', $companyIds);
|
||||
}
|
||||
})
|
||||
return $this->cachedPressRelease ??= PressRelease::accessibleBy(auth()->user())
|
||||
->findOrFail($this->id);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -110,19 +110,11 @@ new #[Layout('components.layouts.app'), Title('Meine Pressemitteilungen')] class
|
|||
|
||||
public function with(): array
|
||||
{
|
||||
$userId = auth()->id();
|
||||
$context = app(CustomerCompanyContext::class);
|
||||
$selectedCompanyId = $context->selectedCompanyId(auth()->user());
|
||||
// Firmen-Scope: eigene PMs (Autor) ODER PMs der zugeordneten Firmen.
|
||||
$accessibleCompanyIds = auth()->user()->accessibleCompanyIds();
|
||||
|
||||
$base = PressRelease::withoutGlobalScopes()
|
||||
->where(function ($q) use ($userId, $accessibleCompanyIds): void {
|
||||
$q->where('user_id', $userId);
|
||||
if ($accessibleCompanyIds !== []) {
|
||||
$q->orWhereIn('company_id', $accessibleCompanyIds);
|
||||
}
|
||||
})
|
||||
// Firmen-Scope: eigene PMs (Autor) ODER PMs der zugeordneten Firmen.
|
||||
$base = PressRelease::accessibleBy(auth()->user())
|
||||
->when($selectedCompanyId !== null, fn($q) => $q->where('company_id', $selectedCompanyId))
|
||||
->when($selectedCompanyId === null && $this->companyFilter === 'assigned', fn($q) => $q->whereNotNull('company_id'))
|
||||
->when($selectedCompanyId === null && $this->companyFilter === 'unassigned', fn($q) => $q->whereNull('company_id'));
|
||||
|
|
|
|||
|
|
@ -158,7 +158,7 @@ new #[Layout('components.layouts.app'), Title('Pressemitteilung')] class extends
|
|||
$cover = app(PressReleaseCoverImage::class);
|
||||
$user = auth()->user();
|
||||
|
||||
$company = $pr->company?->loadCount(['pressReleases', 'contacts']);
|
||||
$company = $pr->company;
|
||||
|
||||
$boostService = app(BoostService::class);
|
||||
$proofService = app(ProofPdfService::class);
|
||||
|
|
@ -246,20 +246,19 @@ new #[Layout('components.layouts.app'), Title('Pressemitteilung')] class extends
|
|||
return implode(' · ', $parts);
|
||||
}
|
||||
|
||||
private ?PressRelease $cachedPressRelease = null;
|
||||
|
||||
private function getMyPR(): PressRelease
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
return PressRelease::withoutGlobalScopes()
|
||||
->where(function ($q) use ($user): void {
|
||||
$q->where('user_id', $user->id);
|
||||
$companyIds = $user->accessibleCompanyIds();
|
||||
if ($companyIds !== []) {
|
||||
$q->orWhereIn('company_id', $companyIds);
|
||||
}
|
||||
})
|
||||
// Pro Livewire-Request memoisiert: mount(), with() und die Add-on-
|
||||
// Aktionen rufen getMyPR() sonst jeweils mit der vollen Eager-Load-
|
||||
// Query erneut auf. Firmen-Kennzahlen direkt mit eager laden, statt
|
||||
// sie nachträglich per loadCount() nachzuziehen.
|
||||
return $this->cachedPressRelease ??= PressRelease::accessibleBy(auth()->user())
|
||||
->with([
|
||||
'company:id,name,email,phone,address,portal,logo_path,legacy_portal,is_active,type',
|
||||
'company' => fn ($query) => $query
|
||||
->select(['id', 'name', 'email', 'phone', 'address', 'portal', 'logo_path', 'legacy_portal', 'is_active', 'type'])
|
||||
->withCount(['pressReleases', 'contacts']),
|
||||
'category.translations',
|
||||
'contacts' => fn ($query) => $query
|
||||
->withoutGlobalScopes()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue