Eigene Compat-Controller (Api/Legacy) fuer die 5 real genutzten
/service-Endpoints (Prod-Log-Beleg): Company-/Kontakt-Create (liefen
410!), PM-Create, Bild-Upload, published-only-GET — verschachtelte
Alt-Params, 201+Location+{}, Alt-Fehlerformat, Hit-Zaehlung,
Legacy-Land-Mapping.
- Anlegen = Einreichen (Alt-Verhalten); Sofort-Publish via neuer
Permission api:immediate-publication (WS-7: connektar zuweisen)
- Admin-Schalter 'KI-Pruefung Legacy-API' (app_settings-Tabelle,
Toggle auf /admin/press-releases): an = review + KI-Routing
- Admin-PM-Liste: KI-Spalte (Ampel-Badge) + manueller Pruef-Button
ohne Routing fuer ungepruefte PMs
- Mails bei API-Einlieferern an Pressekontakt, sonst Firmen-Mail,
Fallback Einlieferer (User-Entscheidung 10.07.)
- Tests: LegacyServiceParityTest (connektar-Flow end-to-end);
Suite 1182 gruen
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
187 lines
7.7 KiB
PHP
187 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Legacy;
|
|
|
|
use App\Enums\AiTextDeclaration;
|
|
use App\Enums\PressReleaseEventType;
|
|
use App\Enums\PressReleaseStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AppSetting;
|
|
use App\Models\Category;
|
|
use App\Models\Company;
|
|
use App\Models\PressRelease;
|
|
use App\Services\PressRelease\BlacklistViolationException;
|
|
use App\Services\PressRelease\BookingRequiredException;
|
|
use App\Services\PressRelease\PressReleaseService;
|
|
use App\Services\PressRelease\QuotaExceededException;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* Alt-Vertrag POST/GET /service/pressreleases (Paritäts-Doku §1/§4):
|
|
*
|
|
* POST: Felder unter press_release[...]; Anlegen IST Einreichen. Kunden mit
|
|
* Permission api:immediate-publication (connektar) veröffentlichen sofort —
|
|
* außer der Admin-Schalter „KI-Prüfung Legacy-API" ist an, dann Status review
|
|
* und die KI routet (Gelb/Grün auto-publish, Rot Ablehnung). KI-Deklaration
|
|
* defaulted auf „ohne generative KI" (Entscheidung 10.07.).
|
|
*
|
|
* GET {id}: nur published (404 sonst — Alt-Clients pollen darüber den
|
|
* Veröffentlichungsstatus), zählt Hits, flaches Alt-Objekt.
|
|
*/
|
|
class LegacyPressReleaseController extends Controller
|
|
{
|
|
use RespondsLikeLegacyApi;
|
|
|
|
public const SETTING_AI_REVIEW = 'legacy_api_ai_review';
|
|
|
|
public function store(Request $request, PressReleaseService $service): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'press_release' => ['required', 'array'],
|
|
'press_release.company_id' => ['required', 'integer'],
|
|
'press_release.category_id' => ['required', 'integer'],
|
|
'press_release.language' => ['nullable', 'string', 'max:5'],
|
|
'press_release.title' => ['required', 'string', 'max:255'],
|
|
'press_release.text' => ['required', 'string'],
|
|
'press_release.backlink_url' => ['nullable', 'url', 'max:255'],
|
|
'press_release.keywords' => ['nullable', 'string', 'max:255'],
|
|
'press_release.contacts_list' => ['nullable', 'array'],
|
|
'press_release.contacts_list.*' => ['integer'],
|
|
])['press_release'];
|
|
} catch (ValidationException $exception) {
|
|
return $this->legacyValidationError($exception);
|
|
}
|
|
|
|
$user = $request->user();
|
|
|
|
$company = Company::withoutGlobalScopes()
|
|
->whereKey((int) $validated['company_id'])
|
|
->accessibleBy($user)
|
|
->first();
|
|
|
|
if (! $company) {
|
|
return $this->legacyError(403, ['You may only access companies which are related to your user-account']);
|
|
}
|
|
|
|
// Alt-Clients senden Legacy-Kategorie-IDs; nach der Migration sind
|
|
// id und legacy_id identisch — legacy_id zuerst, id als Fallback.
|
|
$category = Category::withoutGlobalScopes()->where('legacy_id', (int) $validated['category_id'])->first()
|
|
?? Category::withoutGlobalScopes()->find((int) $validated['category_id']);
|
|
|
|
if (! $category) {
|
|
return $this->legacyError(422, ['Unknown category.']);
|
|
}
|
|
|
|
$language = strtolower(substr((string) ($validated['language'] ?? 'de'), 0, 2)) ?: 'de';
|
|
|
|
$pressRelease = new PressRelease([
|
|
'company_id' => $company->id,
|
|
'category_id' => $category->id,
|
|
'language' => $language,
|
|
'title' => $validated['title'],
|
|
'text' => $validated['text'],
|
|
'backlink_url' => $validated['backlink_url'] ?? null,
|
|
'keywords' => $validated['keywords'] ?? null,
|
|
'status' => PressReleaseStatus::Draft->value,
|
|
// Kompatibilitäts-Default (Entscheidung 10.07.): Alt-Clients
|
|
// kennen die KI-Kennzeichnung nicht.
|
|
'ai_text_declaration' => AiTextDeclaration::None->value,
|
|
]);
|
|
$pressRelease->uuid = (string) Str::uuid();
|
|
$pressRelease->user_id = $user->id;
|
|
$pressRelease->portal = $company->portal?->value;
|
|
$pressRelease->ai_declaration_confirmed_at = now();
|
|
$pressRelease->slug = $pressRelease->generateUniqueSlug($pressRelease->title, [
|
|
'portal' => $pressRelease->portal,
|
|
'language' => $language,
|
|
]);
|
|
$pressRelease->save();
|
|
|
|
if (! empty($validated['contacts_list'])) {
|
|
$ownContactIds = $company->contacts()->whereIn('contacts.id', $validated['contacts_list'])->pluck('contacts.id');
|
|
$pressRelease->contacts()->sync($ownContactIds);
|
|
}
|
|
|
|
// Anlegen ist Einreichen (Alt-Verhalten): Gates + KI-Jobs laufen
|
|
// über denselben Funnel wie Panel/v1.
|
|
try {
|
|
$service->submitForReview($pressRelease);
|
|
} catch (BookingRequiredException|QuotaExceededException $exception) {
|
|
// Gates greifen vor jeder Statusänderung -> Draft rückstandslos
|
|
// entfernen (Alt-Verhalten: ungültiger Request legt nichts an).
|
|
$pressRelease->delete();
|
|
|
|
return $this->legacyError(422, [$exception->getMessage()]);
|
|
} catch (BlacklistViolationException $exception) {
|
|
// PM ist bereits automatisch abgelehnt + protokolliert -- stehen
|
|
// lassen (wie v1), dem Client den Alt-Fehler melden.
|
|
return $this->legacyError(422, [$exception->getMessage()]);
|
|
}
|
|
|
|
$aiReviewEnabled = AppSetting::getBool(self::SETTING_AI_REVIEW);
|
|
|
|
if (! $aiReviewEnabled && $user->can('api:immediate-publication')) {
|
|
try {
|
|
$service->publish($pressRelease, 'api');
|
|
} catch (BlacklistViolationException $exception) {
|
|
return $this->legacyError(422, [$exception->getMessage()]);
|
|
}
|
|
}
|
|
|
|
return $this->legacyCreated(url("/service/pressreleases/{$pressRelease->id}"));
|
|
}
|
|
|
|
public function show(Request $request, int $pressRelease): JsonResponse
|
|
{
|
|
$pressRelease = PressRelease::withoutGlobalScopes()
|
|
->whereKey($pressRelease)
|
|
->with(['company'])
|
|
->first();
|
|
|
|
if (
|
|
! $pressRelease
|
|
|| $pressRelease->status !== PressReleaseStatus::Published
|
|
|| ($pressRelease->company && ! $pressRelease->company->is_active)
|
|
) {
|
|
return $this->legacyError(404, ['Not found.']);
|
|
}
|
|
|
|
// Hit-Zählung wie im Altsystem (dort auch für API-Abrufe).
|
|
PressRelease::withoutGlobalScopes()
|
|
->whereKey($pressRelease->getKey())
|
|
->update(['hits' => ($pressRelease->hits ?? 0) + 1]);
|
|
$pressRelease->events()->create(['type' => PressReleaseEventType::View]);
|
|
|
|
$spokeUrl = $this->spokeBaseUrl($pressRelease->portal?->value);
|
|
|
|
$payload = [
|
|
'id' => $pressRelease->id,
|
|
'title' => $pressRelease->title,
|
|
'text' => $pressRelease->text,
|
|
'teaser_end' => $pressRelease->teaser_end,
|
|
'language' => $pressRelease->language,
|
|
'category_id' => $pressRelease->category_id,
|
|
'view_url' => "{$spokeUrl}/de/press-release/{$pressRelease->slug}",
|
|
];
|
|
|
|
if ($pressRelease->company) {
|
|
$payload['company'] = [
|
|
'viewUrl' => "{$spokeUrl}/de/newsroom/{$pressRelease->company->slug}",
|
|
'name' => $pressRelease->company->name,
|
|
];
|
|
}
|
|
|
|
return response()->json($payload);
|
|
}
|
|
|
|
private function spokeBaseUrl(?string $portal): string
|
|
{
|
|
$key = in_array($portal, ['presseecho', 'businessportal24'], true) ? $portal : 'businessportal24';
|
|
|
|
return rtrim((string) config("domains.domains.{$key}.url", config('app.url')), '/');
|
|
}
|
|
}
|