press-releases: Publish-Rechte, Quota-Races & Slug vereinheitlichen

- Publish/Reject/Archive und changeStatus erzwingen konsistent
  press-releases:publish; changeStatus schloss die Lücke, über die das
  Status-Modal die Prüfung umging (Fix#1/H2).
- publish()/changeStatusFromAdmin sperren PM- und User-Zeile
  transaktional; QuotaExceededException wird in Admin-Publish und im
  Auto-Publish-Job abgefangen (Warnung statt 500 / bleibt in Review, Fix#3/M2).
- Slug-Erzeugung nur noch über HasUniqueSlug (generateUniqueSlug +
  saveWithUniqueSlugRetry), redundanter Controller-Helper entfernt (M3).
- API-/Legacy-Text wird vor der Persistenz sanitisiert (M6);
  Scheduler/Legacy-Tracking ignorieren soft-gelöschte PMs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-07-14 13:47:51 +00:00
parent 7043ea8ad5
commit 0b35c3b04d
19 changed files with 536 additions and 91 deletions

View file

@ -4,6 +4,7 @@ namespace App\Models\Concerns;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\Support\Str;
/**
@ -49,6 +50,37 @@ trait HasUniqueSlug
return $slug;
}
/**
* Persist the model and recover from a concurrent slug insert by
* regenerating the next available suffix.
*
* @param array<string, mixed> $scope
* @param array<string, mixed> $options
*/
public function saveWithUniqueSlugRetry(
string $source,
array $scope = [],
array $options = [],
int $maxAttempts = 3,
): bool {
for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) {
try {
return $this->save($options);
} catch (QueryException $exception) {
if ($attempt === $maxAttempts || ! $this->isSlugUniqueConstraintViolation($exception)) {
throw $exception;
}
$this->setAttribute(
$this->slugColumn(),
$this->generateUniqueSlug($source, $scope),
);
}
}
return false;
}
/**
* @param array<string, mixed> $scope
*/
@ -98,6 +130,15 @@ trait HasUniqueSlug
return 'item';
}
private function isSlugUniqueConstraintViolation(QueryException $exception): bool
{
$message = Str::lower($exception->getMessage());
$isUniqueViolation = in_array((string) $exception->getCode(), ['23000', '23505'], true)
|| Str::contains($message, ['unique constraint', 'duplicate entry', 'duplicate key']);
return $isUniqueViolation && Str::contains($message, $this->slugColumn());
}
/**
* Hook for models to add additional WHERE constraints (e.g. soft-deleted
* records). Default: no additional constraints.

View file

@ -7,6 +7,7 @@ use App\Enums\BoostPlacement;
use App\Enums\Portal;
use App\Enums\PressReleaseClassification;
use App\Enums\PressReleaseContentTier;
use App\Enums\PressReleaseEventType;
use App\Enums\PressReleasePlaceholder;
use App\Enums\PressReleaseStatus;
use App\Http\Middleware\SetEdition;
@ -264,6 +265,25 @@ class PressRelease extends Model
return $this->hasMany(PressReleaseEvent::class);
}
/**
* Zählt einen Aufruf atomar (increment `hits`) und schreibt ein View-Event.
* Zentral, weil sowohl die Web-Detailseite als auch die Legacy-API dies
* per `defer()` nach der Response ausführen. Das Event wird direkt über die
* bekannte ID angelegt (kein erneutes Laden der PM).
*/
public static function recordDeferredView(int $pressReleaseId): void
{
static::query()
->withoutGlobalScope(PortalScope::class)
->whereKey($pressReleaseId)
->increment('hits');
PressReleaseEvent::create([
'press_release_id' => $pressReleaseId,
'type' => PressReleaseEventType::View,
]);
}
/**
* Versionierter Audit-Trail der KI-Deklarationen (Art. 50 KI-VO) —
* jeder Submit erzeugt einen neuen, unveränderlichen Eintrag.
@ -373,6 +393,18 @@ class PressRelease extends Model
return $query->published()->where('published_at', '<=', now());
}
/**
* Instanz-Pendant zu scopeVisibleInArchive(): wertet die öffentliche
* Sichtbarkeit aus den bereits geladenen Attributen aus, ohne erneute
* DB-Abfrage (für die hochfrequenten Medienrouten).
*/
public function isVisibleInArchive(): bool
{
return $this->status === PressReleaseStatus::Published
&& $this->published_at !== null
&& $this->published_at->lessThanOrEqualTo(now());
}
/**
* @param list<string> $portalValues
*/