diff --git a/app/Domain/Catalog/ArticleRelease.php b/app/Domain/Catalog/ArticleRelease.php index 4825b42..e2b54dd 100644 --- a/app/Domain/Catalog/ArticleRelease.php +++ b/app/Domain/Catalog/ArticleRelease.php @@ -18,6 +18,7 @@ class ArticleRelease extends Model 'block_reason', 'fallback_release_id', 'change_note', + 'impact_report', 'root_element_version_id', 'root_composition_version_id', ]; @@ -27,6 +28,7 @@ protected function casts(): array return [ 'important' => 'boolean', 'status' => ReleaseStatus::class, + 'impact_report' => 'array', ]; } @@ -34,24 +36,40 @@ protected static function booted(): void { static::updating(function (ArticleRelease $release): void { $original = StatusValue::of($release->getOriginal('status')); - - if (! in_array($original, [ + $next = StatusValue::of($release->status); + $locked = [ ReleaseStatus::Veroeffentlicht->value, ReleaseStatus::AbgeloestVerfuegbar->value, - ], true)) { + ReleaseStatus::Gesperrt->value, + ]; + + if (! in_array($original, $locked, true)) { return; } - $dirty = array_diff(array_keys($release->getDirty()), ['status', 'updated_at']); + $allowedFields = ['status', 'updated_at']; - if ($dirty !== []) { + if ($original === ReleaseStatus::Veroeffentlicht->value && $next === ReleaseStatus::Gesperrt->value) { + $allowedFields = ['status', 'updated_at', 'block_reason', 'fallback_release_id']; + } + + if (array_diff(array_keys($release->getDirty()), $allowedFields) !== []) { throw new DomainRuleException('Ein veröffentlichter Artikel-Stand ist unveränderlich.'); } - $next = StatusValue::of($release->status); + $allowed = match ($original) { + ReleaseStatus::Veroeffentlicht->value => [ + ReleaseStatus::AbgeloestVerfuegbar->value, + ReleaseStatus::Gesperrt->value, + ], + ReleaseStatus::AbgeloestVerfuegbar->value => [ + ReleaseStatus::Veroeffentlicht->value, + ], + default => [], + }; - if ($original === ReleaseStatus::Veroeffentlicht->value && $next !== ReleaseStatus::AbgeloestVerfuegbar->value) { - throw new DomainRuleException('Ein veröffentlichter Artikel-Stand kann nur abgelöst werden.'); + if (! in_array($next, $allowed, true)) { + throw new DomainRuleException('Dieser Statuswechsel eines Artikel-Stands ist nicht zulässig.'); } }); } diff --git a/app/Domain/Catalog/ArticleService.php b/app/Domain/Catalog/ArticleService.php index 20f00a7..4232704 100644 --- a/app/Domain/Catalog/ArticleService.php +++ b/app/Domain/Catalog/ArticleService.php @@ -2,6 +2,8 @@ namespace App\Domain\Catalog; +use App\Domain\Entitlement\Acquisition; +use App\Domain\Entitlement\GrantStatus; use Illuminate\Support\Facades\DB; class ArticleService @@ -34,22 +36,17 @@ public function publishRelease( throw new DomainRuleException('Ein neuer Stand braucht eine Einordnung, warum er zum selben Artikel gehört.'); } - if ($root->status !== ContentVersionStatus::Bereit) { - throw new DomainRuleException('Ein Artikel-Stand kann nur eine bereitgestellte Fassung festschreiben.'); - } + $this->checkedRows($root); - $rows = $this->resolver->expand($root); - - if ($rows === []) { - throw new DomainRuleException('Ein Artikel-Stand braucht mindestens eine Inhaltsfassung.'); - } - - return DB::transaction(function () use ($article, $root, $classification, $changeNote, $rows): ArticleRelease { + return DB::transaction(function () use ($article, $root, $classification, $changeNote): ArticleRelease { + $rows = $this->checkedRows($root); + $impact = $this->impact($article, count($rows)); $release = $article->releases()->create([ 'number' => ((int) $article->releases()->max('number')) + 1, 'classification_note' => $classification, 'status' => ReleaseStatus::Entwurf, 'change_note' => $changeNote, + 'impact_report' => $impact, 'root_element_version_id' => $root instanceof ContentElementVersion ? $root->id : null, 'root_composition_version_id' => $root instanceof CompositionVersion ? $root->id : null, ]); @@ -80,4 +77,115 @@ public function publishRelease( return $release->load('contents'); }); } + + /** + * @return array{fallback_number: int|null, holders: list} + */ + public function withdraw(LicenseArticle $article, string $reason): array + { + $reason = trim($reason); + + if ($reason === '') { + throw new DomainRuleException('Ein Rückzug braucht eine Begründung.'); + } + + return DB::transaction(function () use ($article, $reason): array { + $article->refresh(); + $current = $article->currentRelease; + + if ($current === null || $current->status !== ReleaseStatus::Veroeffentlicht) { + throw new DomainRuleException('Zurückziehen lässt sich nur der aktuell veröffentlichte Stand.'); + } + + $fallback = ArticleRelease::query() + ->where('license_article_id', $article->id) + ->where('status', ReleaseStatus::AbgeloestVerfuegbar) + ->orderByDesc('number') + ->first(); + + $current->status = ReleaseStatus::Gesperrt; + $current->block_reason = $reason; + $current->fallback_release_id = $fallback?->id; + $current->save(); + + if ($fallback instanceof ArticleRelease) { + $fallback->status = ReleaseStatus::Veroeffentlicht; + $fallback->save(); + $article->current_release_id = $fallback->id; + } else { + $article->current_release_id = null; + } + + $article->save(); + + return [ + 'fallback_number' => $fallback?->number, + 'holders' => Acquisition::query() + ->whereHas('grants', fn ($query) => $query + ->where('license_article_id', $article->id) + ->where('status', GrantStatus::Aktiv)) + ->with('party') + ->get() + ->map(fn (Acquisition $acquisition): string => $acquisition->party->display_name) + ->unique() + ->values() + ->all(), + ]; + }); + } + + /** + * @return list + */ + private function checkedRows(ContentElementVersion|CompositionVersion $root): array + { + $root->refresh(); + + if ($root->status !== ContentVersionStatus::Bereit) { + throw new DomainRuleException('Ein Artikel-Stand kann nur eine bereitgestellte Fassung festschreiben.'); + } + + $rows = $this->resolver->expand($root); + + if ($rows === []) { + throw new DomainRuleException('Ein Artikel-Stand braucht mindestens eine Inhaltsfassung.'); + } + + $blocked = ContentElementVersion::query() + ->whereIn('id', array_column($rows, 'version_id')) + ->where('status', '!=', ContentVersionStatus::Bereit->value) + ->exists(); + + if ($blocked) { + throw new DomainRuleException('Eine gesperrte Inhaltsfassung kann nicht veröffentlicht werden.'); + } + + return $rows; + } + + /** + * @return array{holders: int, offers: list, contents: int, replaces_number: int|null} + */ + private function impact(LicenseArticle $article, int $contents): array + { + $article->loadMissing('currentRelease'); + + return [ + 'holders' => Acquisition::query() + ->whereHas('grants', fn ($query) => $query + ->where('license_article_id', $article->id) + ->where('status', GrantStatus::Aktiv)) + ->distinct() + ->count('party_id'), + 'offers' => Offer::query() + ->whereHas('versions', fn ($query) => $query + ->where('status', OfferStatus::Aktiv) + ->whereHas('resolvedScopes', fn ($scope) => $scope->where('license_article_id', $article->id))) + ->orderBy('name') + ->pluck('name') + ->all(), + 'contents' => $contents, + 'replaces_number' => $article->currentRelease?->number, + ]; + } } diff --git a/app/Domain/Catalog/CompositionVersion.php b/app/Domain/Catalog/CompositionVersion.php index f79a615..879c819 100644 --- a/app/Domain/Catalog/CompositionVersion.php +++ b/app/Domain/Catalog/CompositionVersion.php @@ -26,9 +26,24 @@ protected function casts(): array protected static function booted(): void { static::updating(function (CompositionVersion $version): void { - if (StatusValue::of($version->getOriginal('status')) === ContentVersionStatus::Bereit->value) { + $original = StatusValue::of($version->getOriginal('status')); + + if (! in_array($original, [ + ContentVersionStatus::Bereit->value, + ContentVersionStatus::Gesperrt->value, + ], true)) { + return; + } + + $dirty = array_diff(array_keys($version->getDirty()), ['status', 'updated_at']); + + if ($dirty !== [] || $original === ContentVersionStatus::Gesperrt->value) { throw new DomainRuleException('Eine bestätigte Kompositionsfassung ist unveränderlich.'); } + + if (StatusValue::of($version->status) !== ContentVersionStatus::Gesperrt->value) { + throw new DomainRuleException('Eine bestätigte Kompositionsfassung kann nur gesperrt werden.'); + } }); } diff --git a/app/Domain/Catalog/ContentElementVersion.php b/app/Domain/Catalog/ContentElementVersion.php index 0878a63..f4020eb 100644 --- a/app/Domain/Catalog/ContentElementVersion.php +++ b/app/Domain/Catalog/ContentElementVersion.php @@ -28,12 +28,24 @@ protected function casts(): array protected static function booted(): void { static::updating(function (ContentElementVersion $version): void { - if (in_array(StatusValue::of($version->getOriginal('status')), [ + $original = StatusValue::of($version->getOriginal('status')); + + if (! in_array($original, [ ContentVersionStatus::Bereit->value, ContentVersionStatus::Gesperrt->value, ], true)) { + return; + } + + $dirty = array_diff(array_keys($version->getDirty()), ['status', 'updated_at']); + + if ($dirty !== [] || $original === ContentVersionStatus::Gesperrt->value) { throw new DomainRuleException('Eine bereitgestellte Inhaltsfassung ist unveränderlich.'); } + + if (StatusValue::of($version->status) !== ContentVersionStatus::Gesperrt->value) { + throw new DomainRuleException('Eine bereitgestellte Inhaltsfassung kann nur gesperrt werden.'); + } }); } diff --git a/app/Domain/Catalog/ContentService.php b/app/Domain/Catalog/ContentService.php index 5da5979..69f4a20 100644 --- a/app/Domain/Catalog/ContentService.php +++ b/app/Domain/Catalog/ContentService.php @@ -19,6 +19,20 @@ public function createElement(string $kind, string $outputClass, string $working ]); } + public function block(ContentElementVersion|CompositionVersion $version, string $reason): void + { + if (trim($reason) === '') { + throw new DomainRuleException('Eine Sperre braucht eine Begründung.'); + } + + if ($version->status !== ContentVersionStatus::Bereit) { + throw new DomainRuleException('Nur eine bereitgestellte Fassung kann gesperrt werden.'); + } + + $version->status = ContentVersionStatus::Gesperrt; + $version->save(); + } + public function publishElementVersion( ContentElement $element, ?string $body = null, diff --git a/app/Domain/Catalog/ReleaseStatus.php b/app/Domain/Catalog/ReleaseStatus.php index baaefc6..dd3ad93 100644 --- a/app/Domain/Catalog/ReleaseStatus.php +++ b/app/Domain/Catalog/ReleaseStatus.php @@ -7,4 +7,5 @@ enum ReleaseStatus: string case Entwurf = 'entwurf'; case Veroeffentlicht = 'veroeffentlicht'; case AbgeloestVerfuegbar = 'abgeloest_verfuegbar'; + case Gesperrt = 'gesperrt'; } diff --git a/app/Filament/Resources/LicenseArticles/Pages/ListLicenseArticles.php b/app/Filament/Resources/LicenseArticles/Pages/ListLicenseArticles.php index 695633d..daf55b5 100644 --- a/app/Filament/Resources/LicenseArticles/Pages/ListLicenseArticles.php +++ b/app/Filament/Resources/LicenseArticles/Pages/ListLicenseArticles.php @@ -44,19 +44,45 @@ protected function getHeaderActions(): array TextInput::make('change_note')->label('Änderungshinweis'), ]) ->action(function (array $data): void { - $this->report(function () use ($data): void { + $this->report(function () use ($data): string { [$type, $id] = explode(':', $data['root'], 2); $root = $type === 'composition' ? CompositionVersion::query()->findOrFail($id) : ContentElementVersion::query()->findOrFail($id); - app(ArticleService::class)->publishRelease( + $release = app(ArticleService::class)->publishRelease( LicenseArticle::query()->findOrFail($data['article_id']), $root, $data['classification'], $data['change_note'] ?? null, ); + $report = $release->impact_report ?? []; + $offers = $report['offers'] ?? []; + + return 'Stand '.$release->number.' ist veröffentlicht. Inhalte: '.($report['contents'] ?? 0) + .', Inhaber: '.($report['holders'] ?? 0) + .', Angebote: '.(implode(', ', $offers) !== '' ? implode(', ', $offers) : 'keins').'.'; }, 'Der Artikel-Stand ist veröffentlicht.'); }), + Action::make('withdraw') + ->label('Stand zurückziehen') + ->schema([ + Select::make('article_id')->label('Artikel')->options(fn (): array => CatalogOptions::articles())->required(), + Textarea::make('reason')->label('Begründung')->required(), + ]) + ->action(function (array $data): void { + $this->report(function () use ($data): string { + $result = app(ArticleService::class)->withdraw( + LicenseArticle::query()->findOrFail($data['article_id']), + $data['reason'], + ); + $current = $result['fallback_number'] === null + ? 'Es gibt keinen früheren Stand.' + : 'Aktuell ist wieder Stand '.$result['fallback_number'].'.'; + $holders = $result['holders'] === [] ? 'niemand' : implode(', ', $result['holders']); + + return 'Der Stand ist zurückgezogen. '.$current.' Betroffen: '.$holders.'.'; + }, 'Der Stand ist zurückgezogen.'); + }), ]; } } diff --git a/app/Filament/Support/ReportsDomainRules.php b/app/Filament/Support/ReportsDomainRules.php index 6fcd340..161b3ca 100644 --- a/app/Filament/Support/ReportsDomainRules.php +++ b/app/Filament/Support/ReportsDomainRules.php @@ -10,8 +10,8 @@ trait ReportsDomainRules protected function report(callable $callback, string $success): void { try { - $callback(); - Notification::make()->title($success)->success()->send(); + $detail = $callback(); + Notification::make()->title(is_string($detail) && $detail !== '' ? $detail : $success)->success()->send(); } catch (DomainRuleException $exception) { Notification::make()->title($exception->getMessage())->danger()->send(); } diff --git a/database/migrations/2026_11_15_130000_add_impact_report_to_article_releases.php b/database/migrations/2026_11_15_130000_add_impact_report_to_article_releases.php new file mode 100644 index 0000000..9373ff5 --- /dev/null +++ b/database/migrations/2026_11_15_130000_add_impact_report_to_article_releases.php @@ -0,0 +1,22 @@ +json('impact_report')->nullable(); + }); + } + + public function down(): void + { + Schema::table('article_releases', function (Blueprint $table) { + $table->dropColumn('impact_report'); + }); + } +}; diff --git a/docs/betrieb/MVP-Stand.md b/docs/betrieb/MVP-Stand.md index 1bd1b7f..bd89901 100644 --- a/docs/betrieb/MVP-Stand.md +++ b/docs/betrieb/MVP-Stand.md @@ -6,7 +6,7 @@ ## Bedienbar - Öffentliche Seiten unter `/verwaltung` als Redakteur: Entwurf, Veröffentlichung, Wiederherstellung. - Inhaltselemente, Fassungen und Kompositionen als Redakteur. Eine Datei wird von der Queue nach `derived/` kopiert. -- Lizenzartikel, verschachtelte Pakete, Rechteprofile und Angebote als Katalogverwalter. +- Lizenzartikel, verschachtelte Pakete, Rechteprofile und Angebote als Katalogverwalter. Ein neuer Stand zeigt Inhaber, Angebote und Inhaltszahl; ein Rückzug stellt den vorherigen Stand wieder her und nennt die Betroffenen. - Manuelle Vergabe als Kundenbetreuung, am Benutzer in der Verwaltung. - Registrierung, Anmeldung, kostenloser Abruf, Testkauf, Ablehnung und Widerruf im Mitgliederbereich. - Bibliothek entscheidet je Abruf erneut. Filme mit Datei werden im Player geladen, Texte und PDF über einen kurz signierten Abruf. diff --git a/tests/Feature/CatalogRulesTest.php b/tests/Feature/CatalogRulesTest.php index 9e1aa92..86e180c 100644 --- a/tests/Feature/CatalogRulesTest.php +++ b/tests/Feature/CatalogRulesTest.php @@ -189,6 +189,63 @@ public function test_h_a_bundle_change_does_not_change_an_activated_offer(): voi $offer->save(); } + public function test_publication_records_impact_and_withdrawal_restores_the_previous_release(): void + { + $content = app(ContentService::class); + $articles = app(ArticleService::class); + $firstVersion = $this->element('Blatt', 'Fassung 1'); + $article = $this->article('blatt'); + $first = $articles->publishRelease($article, $firstVersion, 'Erste Einordnung.'); + $article = $article->fresh(); + $rights = $this->rights(); + app(OfferService::class)->activate( + app(OfferService::class)->create('Blattangebot'), + [['target' => $article, 'rights' => $rights]], + ); + $party = Party::query()->create([ + 'type' => PartyType::Person, + 'display_name' => 'Mitglied Probe', + ]); + $acquisition = Acquisition::query()->create([ + 'party_id' => $party->id, + 'type' => AcquisitionType::FreierAbruf, + 'reference_type' => 'offer', + 'reference_id' => 1, + ]); + Grant::query()->create([ + 'acquisition_id' => $acquisition->id, + 'license_article_id' => $article->id, + 'rights_profile_version_id' => $rights->id, + 'rights_snapshot' => $rights->capabilities, + 'release_at_grant_id' => $first->id, + 'status' => GrantStatus::Aktiv, + ]); + + $secondVersion = $content->publishElementVersion($firstVersion->element->fresh(), 'Fassung 2'); + $second = $articles->publishRelease($article->fresh(), $secondVersion, 'Überarbeitung desselben Artikels.'); + + $this->assertSame(1, $second->impact_report['holders']); + $this->assertSame(['Blattangebot'], $second->impact_report['offers']); + $this->assertSame(1, $second->impact_report['replaces_number']); + + $content->block($secondVersion, 'Fehlerhafte Fassung.'); + + try { + $articles->publishRelease($article->fresh(), $secondVersion, 'Darf nicht erscheinen.'); + $this->fail('Eine gesperrte Fassung darf nicht veröffentlicht werden.'); + } catch (DomainRuleException) { + } + + $this->assertSame($second->id, $article->fresh()->current_release_id); + + $result = $articles->withdraw($article->fresh(), 'Die zweite Fassung ist fehlerhaft.'); + + $this->assertSame(1, $result['fallback_number']); + $this->assertSame(['Mitglied Probe'], $result['holders']); + $this->assertSame($first->id, $article->fresh()->current_release_id); + $this->assertSame('gesperrt', $second->fresh()->status->value); + } + public function test_each_holder_gets_an_own_acquisition_and_one_grant_per_article(): void { $article = $this->publishedArticle('frei', 'Kostenloses Blatt');