Halte Auswirkung und Rückzug beim Veröffentlichen eines Artikel-Stands fest.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
fd6860efda
commit
fef0ea2ac2
11 files changed
+298
-25
No files matched your search
@@ -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.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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<string>}
|
||||
*/
|
||||
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<array{version_id: int, role: string, path: string}>
|
||||
*/
|
||||
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<string>, 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -7,4 +7,5 @@ enum ReleaseStatus: string
|
||||
case Entwurf = 'entwurf';
|
||||
case Veroeffentlicht = 'veroeffentlicht';
|
||||
case AbgeloestVerfuegbar = 'abgeloest_verfuegbar';
|
||||
case Gesperrt = 'gesperrt';
|
||||
}
|
||||
@@ -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.');
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user