Öffne Katalog, Bibliothek und Testkauf für den MVP.
Deploy Test / test (push) Successful in 12s
Deploy Test / deploy (push) Successful in 21s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-11-15 09:04:08 +01:00
1 parent 463851d402
commit fd6860efda
51 files changed
+2331 -31

No files matched your search

+11 -3
View File
@@ -2,6 +2,7 @@
namespace App\Domain\Catalog;
use App\Jobs\StoreContentFile;
use App\Models\User;
use Illuminate\Support\Facades\DB;
@@ -29,10 +30,10 @@ public function publishElementVersion(
}
$number = ((int) $element->versions()->max('number')) + 1;
return $element->versions()->create([
$incoming = $metadata['incoming'] ?? null;
$version = $element->versions()->create([
'number' => $number,
'status' => ContentVersionStatus::Bereit,
'status' => is_string($incoming) && $incoming !== '' ? ContentVersionStatus::Entwurf : ContentVersionStatus::Bereit,
'content_hash' => hash('sha256', json_encode([
'body' => $body,
'metadata' => $metadata,
@@ -41,6 +42,13 @@ public function publishElementVersion(
'metadata' => $metadata === [] ? null : $metadata,
'change_note' => $changeNote,
]);
if (is_string($incoming) && $incoming !== '') {
StoreContentFile::dispatch($version->id, $incoming);
$version->refresh();
}
return $version;
}
/**
+187
View File
@@ -0,0 +1,187 @@
<?php
namespace App\Domain\Commerce;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\Offer;
use App\Domain\Catalog\OfferStatus;
use App\Domain\Entitlement\Acquisition;
use App\Domain\Entitlement\AcquisitionType;
use App\Domain\Entitlement\GrantIssuer;
use App\Domain\Entitlement\GrantStatus;
use App\Domain\Identity\AccountStatus;
use App\Mail\OrderConfirmed;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class Checkout
{
public function __construct(private readonly GrantIssuer $grants) {}
public function place(User $user, Offer $offer): Order
{
if ($user->status !== AccountStatus::Aktiv || $user->party_id === null) {
throw new DomainRuleException('Nur ein aktives Mitglied kann kaufen.');
}
$version = $offer->versions()->where('status', OfferStatus::Aktiv)->orderByDesc('number')->first();
if ($version === null || $version->grant_kind !== 'kauf' || $version->holder_kind !== 'person') {
throw new DomainRuleException('Dieses Angebot kann nicht als persönlicher Kauf bestellt werden.');
}
if ($version->price_cents === null) {
throw new DomainRuleException('Der Kauf braucht einen Preis an der Angebotsfassung.');
}
return DB::transaction(function () use ($user, $version): Order {
$order = Order::query()->create([
'user_id' => $user->id,
'party_id' => $user->party_id,
'status' => 'offen',
'holder_kind' => 'person',
]);
$line = $order->lines()->create([
'offer_version_id' => $version->id,
'price_cents' => $version->price_cents,
'term_rule' => $version->term_rule,
'update_promise' => $version->update_promise,
'holder_kind' => $version->holder_kind,
]);
$version->load('resolvedScopes.rights');
foreach ($version->resolvedScopes as $scope) {
$line->scopeItems()->create([
'license_article_id' => $scope->license_article_id,
'rights_profile_version_id' => $scope->rights_profile_version_id,
'rights_snapshot' => [
'purpose' => $scope->rights->purpose,
'capabilities' => $scope->rights->capabilities,
'options' => $scope->rights->options ?? [],
],
]);
}
return $order->load('lines.scopeItems');
});
}
public function confirmTestPayment(Order $order, string $reference): Order
{
$this->assertTestDriver();
$reference = trim($reference);
if ($reference === '') {
throw new DomainRuleException('Die Zahlungsreferenz fehlt.');
}
$paidNow = false;
$order = DB::transaction(function () use ($order, $reference, &$paidNow): Order {
$order = Order::query()->lockForUpdate()->findOrFail($order->id);
if ($order->status === 'bezahlt') {
return $order->load('lines', 'payments');
}
if ($order->status !== 'offen') {
throw new DomainRuleException('Diese Bestellung nimmt keine Zahlung mehr an.');
}
$order->payments()->create([
'provider' => 'test',
'status' => 'bestaetigt',
'provider_reference' => $reference,
]);
$order->status = 'bezahlt';
$order->save();
$paidNow = true;
$line = $order->lines()->firstOrFail();
$this->grants->issue(
$order->user,
$line->offerVersion,
AcquisitionType::Bestellposition,
'order_line',
$line->id,
);
return $order->load('lines', 'payments');
});
if ($paidNow) {
Mail::to($order->user)->send(new OrderConfirmed($order));
}
return $order;
}
public function failTestPayment(Order $order, string $reference): Order
{
$this->assertTestDriver();
return DB::transaction(function () use ($order, $reference): Order {
$order = Order::query()->lockForUpdate()->findOrFail($order->id);
if ($order->status !== 'offen') {
return $order;
}
$order->payments()->create([
'provider' => 'test',
'status' => 'fehlgeschlagen',
'provider_reference' => $reference,
]);
$order->status = 'fehlgeschlagen';
$order->save();
return $order;
});
}
public function refund(Order $order, string $reason): Order
{
$reason = trim($reason);
if ($reason === '') {
throw new DomainRuleException('Eine Erstattung braucht eine Begründung.');
}
return DB::transaction(function () use ($order, $reason): Order {
$order = Order::query()->lockForUpdate()->findOrFail($order->id);
if ($order->status !== 'bezahlt') {
throw new DomainRuleException('Nur eine bezahlte Bestellung kann erstattet werden.');
}
$line = $order->lines()->firstOrFail();
$acquisition = Acquisition::query()->where([
'type' => AcquisitionType::Bestellposition,
'reference_type' => 'order_line',
'reference_id' => $line->id,
])->first();
if ($acquisition !== null) {
foreach ($acquisition->grants as $grant) {
if ($grant->status === GrantStatus::Aktiv) {
$grant->status = GrantStatus::Widerrufen;
$grant->status_reason = $reason;
$grant->save();
}
}
}
$order->payments()->where('status', 'bestaetigt')->update(['status' => 'erstattet']);
$order->status = 'erstattet';
$order->save();
return $order->refresh();
});
}
private function assertTestDriver(): void
{
if (app()->isProduction() || config('services.payment.driver') !== 'test') {
throw new DomainRuleException('Der Testzahlungsweg ist nicht aktiv.');
}
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace App\Domain\Commerce;
use App\Domain\Identity\Party;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Order extends Model
{
protected $fillable = [
'user_id',
'party_id',
'status',
'holder_kind',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function party(): BelongsTo
{
return $this->belongsTo(Party::class);
}
public function lines(): HasMany
{
return $this->hasMany(OrderLine::class);
}
public function payments(): HasMany
{
return $this->hasMany(Payment::class);
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace App\Domain\Commerce;
use App\Domain\Catalog\OfferVersion;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class OrderLine extends Model
{
protected $fillable = [
'order_id',
'offer_version_id',
'price_cents',
'term_rule',
'update_promise',
'holder_kind',
];
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
public function offerVersion(): BelongsTo
{
return $this->belongsTo(OfferVersion::class);
}
public function scopeItems(): HasMany
{
return $this->hasMany(OrderScopeItem::class);
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace App\Domain\Commerce;
use App\Domain\Catalog\LicenseArticle;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class OrderScopeItem extends Model
{
protected $fillable = [
'order_line_id',
'license_article_id',
'rights_profile_version_id',
'rights_snapshot',
];
protected function casts(): array
{
return [
'rights_snapshot' => 'array',
];
}
public function line(): BelongsTo
{
return $this->belongsTo(OrderLine::class, 'order_line_id');
}
public function article(): BelongsTo
{
return $this->belongsTo(LicenseArticle::class, 'license_article_id');
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace App\Domain\Commerce;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Payment extends Model
{
protected $fillable = [
'order_id',
'provider',
'status',
'provider_reference',
];
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace App\Domain\Entitlement;
class AccessResult
{
public function __construct(
public readonly bool $allowed,
public readonly ?Grant $grant = null,
public readonly string $reason = '',
) {}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace App\Domain\Entitlement;
use App\Domain\Catalog\ArticleRelease;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\ReleaseStatus;
use App\Domain\Identity\AccountStatus;
use App\Models\User;
class DecideAccess
{
public function __invoke(
User $user,
LicenseArticle $article,
ArticleRelease $release,
ContentElementVersion $version,
string $action,
): AccessResult {
if ($user->status !== AccountStatus::Aktiv) {
return new AccessResult(false, null, 'Das Konto ist nicht aktiv.');
}
if ($release->license_article_id !== $article->id) {
return new AccessResult(false, null, 'Der Stand gehört zu einem anderen Artikel.');
}
if (! in_array($release->status, [ReleaseStatus::Veroeffentlicht, ReleaseStatus::AbgeloestVerfuegbar], true)) {
return new AccessResult(false, null, 'Dieser Stand ist nicht freigegeben.');
}
$contained = $release->contents()->where('content_element_version_id', $version->id)->exists();
if (! $contained) {
return new AccessResult(false, null, 'Die Fassung gehört nicht zum fixierten Inhalt dieses Stands.');
}
if ($version->status === ContentVersionStatus::Gesperrt) {
return new AccessResult(false, null, 'Die Fassung ist gesperrt.');
}
$version->loadMissing('element');
$outputClass = $version->element->output_class;
$now = now();
$candidates = Grant::query()
->where('license_article_id', $article->id)
->where('status', GrantStatus::Aktiv)
->whereHas('acquisition', fn ($query) => $query->where('party_id', $user->party_id))
->get()
->filter(function (Grant $grant) use ($now): bool {
if ($grant->valid_from !== null && $grant->valid_from->greaterThan($now)) {
return false;
}
return $grant->valid_until === null || $grant->valid_until->greaterThan($now);
})
->filter(function (Grant $grant) use ($outputClass, $action): bool {
$capabilities = $grant->rights_snapshot['capabilities'] ?? [];
$actions = $capabilities[$outputClass] ?? [];
return in_array($action, $actions, true);
})
->sort(function (Grant $left, Grant $right): int {
$leftUntil = $left->valid_until?->getTimestamp() ?? PHP_INT_MAX;
$rightUntil = $right->valid_until?->getTimestamp() ?? PHP_INT_MAX;
return $leftUntil === $rightUntil ? $right->id <=> $left->id : $leftUntil <=> $rightUntil;
})
->values();
$grant = $candidates->last();
if ($grant === null) {
return new AccessResult(false, null, 'Keine passende Berechtigung.');
}
return new AccessResult(true, $grant);
}
}
+132
View File
@@ -0,0 +1,132 @@
<?php
namespace App\Domain\Entitlement;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\Offer;
use App\Domain\Catalog\OfferStatus;
use App\Domain\Catalog\OfferVersion;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class GrantIssuer
{
public function claim(User $user, Offer $offer): Acquisition
{
$version = $this->activeVersion($offer);
if (! in_array($version->grant_kind, ['auf_abruf', 'bei_registrierung'], true)) {
throw new DomainRuleException('Dieses Angebot wird nicht frei vergeben.');
}
if ($version->holder_kind !== 'person') {
throw new DomainRuleException('Dieses Angebot richtet sich nicht an Personen.');
}
return $this->issue($user, $version, AcquisitionType::FreierAbruf, 'offer', $offer->id);
}
public function grantRegistrationOffers(User $user): void
{
OfferVersion::query()
->where('status', OfferStatus::Aktiv)
->where('grant_kind', 'bei_registrierung')
->where('holder_kind', 'person')
->with('offer')
->get()
->each(function (OfferVersion $version) use ($user): void {
$this->issue($user, $version, AcquisitionType::FreierAbruf, 'offer', $version->offer_id);
});
}
public function grantManually(User $user, Offer $offer, User $actor, string $reason): Acquisition
{
$reason = trim($reason);
if ($reason === '') {
throw new DomainRuleException('Eine manuelle Vergabe braucht eine Begründung.');
}
$version = $this->activeVersion($offer);
if ($version->holder_kind !== 'person') {
throw new DomainRuleException('Dieses Angebot richtet sich nicht an Personen.');
}
return DB::transaction(function () use ($user, $offer, $actor, $reason, $version): Acquisition {
$operation = ManualGrant::query()->create([
'actor_id' => $actor->id,
'party_id' => $user->party_id,
'offer_id' => $offer->id,
'reason' => $reason,
]);
return $this->issue($user, $version, AcquisitionType::ManuelleVergabe, 'manual_grant', $operation->id);
});
}
public function issue(
User $user,
OfferVersion $version,
AcquisitionType $type,
string $referenceType,
int $referenceId,
): Acquisition {
return DB::transaction(function () use ($user, $version, $type, $referenceType, $referenceId): Acquisition {
$existing = Acquisition::query()->where([
'party_id' => $user->party_id,
'type' => $type,
'reference_type' => $referenceType,
'reference_id' => $referenceId,
])->first();
if ($existing !== null) {
return $existing->load('grants');
}
$acquisition = Acquisition::query()->create([
'party_id' => $user->party_id,
'type' => $type,
'reference_type' => $referenceType,
'reference_id' => $referenceId,
'conditions' => [
'term_rule' => $version->term_rule,
'update_promise' => $version->update_promise,
'holder_kind' => $version->holder_kind,
'offer_version_id' => $version->id,
],
]);
$version->load('resolvedScopes.rights', 'resolvedScopes.article');
foreach ($version->resolvedScopes as $scope) {
$rights = $scope->rights;
$acquisition->grants()->create([
'license_article_id' => $scope->license_article_id,
'rights_profile_version_id' => $rights->id,
'rights_snapshot' => [
'purpose' => $rights->purpose,
'capabilities' => $rights->capabilities,
'options' => $rights->options ?? [],
],
'release_at_grant_id' => $scope->article->current_release_id,
'status' => GrantStatus::Aktiv,
'valid_from' => now(),
]);
}
return $acquisition->load('grants');
});
}
private function activeVersion(Offer $offer): OfferVersion
{
$version = $offer->versions()->where('status', OfferStatus::Aktiv)->orderByDesc('number')->first();
if ($version === null) {
throw new DomainRuleException('Das Angebot hat keine aktive Fassung.');
}
return $version;
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace App\Domain\Entitlement;
use App\Domain\Catalog\Offer;
use App\Domain\Identity\Party;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ManualGrant extends Model
{
protected $fillable = [
'actor_id',
'party_id',
'offer_id',
'reason',
];
public function actor(): BelongsTo
{
return $this->belongsTo(User::class, 'actor_id');
}
public function party(): BelongsTo
{
return $this->belongsTo(Party::class);
}
public function offer(): BelongsTo
{
return $this->belongsTo(Offer::class);
}
}