Boost-Geschaeftslogik (Decision-Update 2.2)
Bezahlte Platzierung gruener, veroeffentlichter PMs ueber die Credit-Wallet: - boosts Tabelle (Zeitraum starts_at/ends_at, days, credits_charged) - BoostService: Gate (nur Published + Green), Preis nach Laufzeit (7/14/30 -> 12/20/35 Credits), Mehrfachkauf verlaengert vom laufenden Ende, Wallet-Belastung referenziert den Boost im Ledger - PressRelease::boosts()/isBoosted()/scopeBoosted() als Basis fuer die Featured-Platzierung (Frontend-Anbindung bleibt der Web-Strecke ueberlassen) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
3e8844245d
commit
c7a4c8bfd4
7 changed files with 355 additions and 0 deletions
17
app/Exceptions/BoostNotAllowedException.php
Normal file
17
app/Exceptions/BoostNotAllowedException.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Wird geworfen, wenn eine PM nicht geboostet werden darf — nur
|
||||
* veröffentlichte, grün klassifizierte Meldungen sind boostbar (§2.2).
|
||||
*/
|
||||
class BoostNotAllowedException extends RuntimeException
|
||||
{
|
||||
public static function notBoostable(): self
|
||||
{
|
||||
return new self('Nur veröffentlichte, grün klassifizierte Pressemitteilungen sind boostbar.');
|
||||
}
|
||||
}
|
||||
57
app/Models/Boost.php
Normal file
57
app/Models/Boost.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Services\PressRelease\BoostService;
|
||||
use Database\Factories\BoostFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* Ein gekaufter Boost-Zeitraum. Anlage ausschließlich über
|
||||
* {@see BoostService} (Gate + Wallet-Belastung).
|
||||
*/
|
||||
class Boost extends Model
|
||||
{
|
||||
/** @use HasFactory<BoostFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'press_release_id',
|
||||
'user_id',
|
||||
'days',
|
||||
'credits_charged',
|
||||
'starts_at',
|
||||
'ends_at',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'days' => 'integer',
|
||||
'credits_charged' => 'integer',
|
||||
'starts_at' => 'datetime',
|
||||
'ends_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function pressRelease(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(PressRelease::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Boosts, deren Platzierung gerade aktiv ist (läuft noch).
|
||||
*/
|
||||
public function scopeActive(Builder $query): Builder
|
||||
{
|
||||
return $query->where('ends_at', '>', now());
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ use App\Models\Concerns\HasUniqueSlug;
|
|||
use App\Scopes\PortalScope;
|
||||
use App\Services\PressRelease\PressReleaseHtmlSanitizer;
|
||||
use Database\Factories\PressReleaseFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
|
@ -167,6 +168,28 @@ class PressRelease extends Model
|
|||
return $this->hasMany(KiAudit::class)->orderByDesc('created_at');
|
||||
}
|
||||
|
||||
public function boosts(): HasMany
|
||||
{
|
||||
return $this->hasMany(Boost::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktuell geboostet (mindestens ein laufender Boost-Zeitraum)?
|
||||
*/
|
||||
public function isBoosted(): bool
|
||||
{
|
||||
return $this->boosts()->active()->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Beschränkt auf PMs mit aktuell laufendem Boost — Basis für die
|
||||
* Featured-Platzierung auf Start-/Branchenseite.
|
||||
*/
|
||||
public function scopeBoosted(Builder $query): Builder
|
||||
{
|
||||
return $query->whereHas('boosts', fn (Builder $q) => $q->active());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display-ready text. Returns sanitized HTML for Phase-7+ PMs and
|
||||
* <p>/<br>-wrapped legacy plain text for older imports.
|
||||
|
|
|
|||
87
app/Services/PressRelease/BoostService.php
Normal file
87
app/Services/PressRelease/BoostService.php
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\PressRelease;
|
||||
|
||||
use App\Enums\PressReleaseClassification;
|
||||
use App\Enums\PressReleaseStatus;
|
||||
use App\Exceptions\BoostNotAllowedException;
|
||||
use App\Models\Boost;
|
||||
use App\Models\PressRelease;
|
||||
use App\Models\User;
|
||||
use App\Services\Billing\CreditPricingService;
|
||||
use App\Services\Billing\CreditWalletService;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Boost-Kauf laut Decision-Update §2.2. Gate: nur veröffentlichte, grüne PMs.
|
||||
* Bezahlung über die Credit-Wallet, Preis nach Laufzeit (7/14/30 Tage).
|
||||
* Mehrfachkauf verlängert: der neue Zeitraum schließt an ein laufendes Ende an.
|
||||
*/
|
||||
class BoostService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CreditPricingService $pricing,
|
||||
private readonly CreditWalletService $wallet,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Boostbar = veröffentlicht UND grün klassifiziert (gelb/rot nicht).
|
||||
*/
|
||||
public function canBoost(PressRelease $pressRelease): bool
|
||||
{
|
||||
return $pressRelease->status === PressReleaseStatus::Published
|
||||
&& $pressRelease->classification === PressReleaseClassification::Green;
|
||||
}
|
||||
|
||||
public function isBoosted(PressRelease $pressRelease): bool
|
||||
{
|
||||
return $pressRelease->boosts()->active()->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ende des aktuell laufenden Boosts (das späteste zukünftige `ends_at`)
|
||||
* oder null, wenn die PM gerade nicht geboostet ist.
|
||||
*/
|
||||
public function activeUntil(PressRelease $pressRelease): ?Carbon
|
||||
{
|
||||
$endsAt = $pressRelease->boosts()->active()->max('ends_at');
|
||||
|
||||
return $endsAt ? Carbon::parse($endsAt) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bucht einen Boost. Wirft BoostNotAllowedException am Gate und
|
||||
* InvalidArgumentException bei unbekannter Laufzeit; die Wallet-Belastung
|
||||
* wirft InsufficientCreditsException, falls das Guthaben nicht reicht.
|
||||
*/
|
||||
public function boost(User $user, PressRelease $pressRelease, int $days): Boost
|
||||
{
|
||||
if (! $this->canBoost($pressRelease)) {
|
||||
throw BoostNotAllowedException::notBoostable();
|
||||
}
|
||||
|
||||
$credits = $this->pricing->boostCredits($days);
|
||||
|
||||
return DB::transaction(function () use ($user, $pressRelease, $days, $credits): Boost {
|
||||
$startsAt = $this->activeUntil($pressRelease) ?? now();
|
||||
|
||||
$boost = $pressRelease->boosts()->create([
|
||||
'user_id' => $user->id,
|
||||
'days' => $days,
|
||||
'credits_charged' => $credits,
|
||||
'starts_at' => $startsAt,
|
||||
'ends_at' => $startsAt->copy()->addDays($days),
|
||||
]);
|
||||
|
||||
$this->wallet->debit(
|
||||
$user,
|
||||
$credits,
|
||||
"Boost {$days} Tage – PM #{$pressRelease->id}",
|
||||
$boost,
|
||||
);
|
||||
|
||||
return $boost;
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue