Öffne Katalog, Bibliothek und Testkauf für den MVP.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
463851d402
commit
fd6860efda
51 files changed
+2331
-31
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user