Refactor ticket validity handling and improve tests
- Updated tests for Accommodation, Entry, Food, Merchandise, and Sale controllers to use soft deletes for variants and ensure proper inventory counts. - Enhanced ticket generation logic to resolve validity from soft-deleted catalog sources. - Introduced a new TicketValidityResolver service to manage ticket validity based on event dates and variant definitions. - Removed unnecessary database assertions and improved the clarity of validity checks in tests. - Added comprehensive tests for the new TicketValidityResolver service, ensuring correct handling of event dates and multi-select options. - Cleaned up unused code and assertions in existing tests for better maintainability.
This commit is contained in:
@@ -7,20 +7,20 @@ use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticket\Services\ResolvedTicketValidity;
|
||||
use App\Domains\Ticket\Services\ResolvedValidityGroup;
|
||||
use App\Domains\Ticket\Services\TicketPresentationResolver;
|
||||
use App\Domains\Ticket\Services\TicketValidityResolver;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
#[Fillable([
|
||||
'tenant_code',
|
||||
'ticket',
|
||||
'name',
|
||||
'description',
|
||||
'source_purchase_id',
|
||||
'source_catalog_item_id',
|
||||
'source_variant_id',
|
||||
@@ -32,6 +32,8 @@ class Ticket extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
private ?ResolvedTicketValidity $resolvedValidity = null;
|
||||
|
||||
public const STATUS_ACTIVE = 'active';
|
||||
|
||||
public const STATUS_EXPIRED = 'expired';
|
||||
@@ -41,6 +43,8 @@ class Ticket extends Model
|
||||
public $timestamps = false;
|
||||
|
||||
protected $appends = [
|
||||
'name',
|
||||
'description',
|
||||
'is_valid',
|
||||
'is_expired',
|
||||
'is_used',
|
||||
@@ -86,19 +90,13 @@ class Ticket extends Model
|
||||
/** @return BelongsTo<CatalogItem, $this> */
|
||||
public function sourceCatalogItem(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CatalogItem::class, 'source_catalog_item_id');
|
||||
return $this->belongsTo(CatalogItem::class, 'source_catalog_item_id')->withTrashed();
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Variant, $this> */
|
||||
public function sourceVariant(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Variant::class, 'source_variant_id');
|
||||
}
|
||||
|
||||
/** @return HasMany<TicketValidityGroup, $this> */
|
||||
public function validityGroups(): HasMany
|
||||
{
|
||||
return $this->hasMany(TicketValidityGroup::class);
|
||||
return $this->belongsTo(Variant::class, 'source_variant_id')->withTrashed();
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
@@ -107,12 +105,7 @@ class Ticket extends Model
|
||||
return false;
|
||||
}
|
||||
|
||||
$validityGroups = $this->resolvedValidityGroups();
|
||||
|
||||
return $validityGroups->isEmpty()
|
||||
|| $validityGroups->contains(
|
||||
fn (TicketValidityGroup $group): bool => $group->isValid()
|
||||
);
|
||||
return $this->resolvedValidity()->isValid();
|
||||
}
|
||||
|
||||
public function getIsValidAttribute(): bool
|
||||
@@ -122,13 +115,7 @@ class Ticket extends Model
|
||||
|
||||
public function getIsExpiredAttribute(): bool
|
||||
{
|
||||
$validityGroups = $this->resolvedValidityGroups();
|
||||
|
||||
return $this->used_at === null
|
||||
&& $validityGroups->isNotEmpty()
|
||||
&& $validityGroups->every(
|
||||
fn (TicketValidityGroup $group): bool => $group->isExpired()
|
||||
);
|
||||
return $this->used_at === null && $this->resolvedValidity()->isExpired();
|
||||
}
|
||||
|
||||
public function getIsUsedAttribute(): bool
|
||||
@@ -149,52 +136,34 @@ class Ticket extends Model
|
||||
return self::STATUS_ACTIVE;
|
||||
}
|
||||
|
||||
public function getNameAttribute(): string
|
||||
{
|
||||
return app(TicketPresentationResolver::class)->name($this);
|
||||
}
|
||||
|
||||
public function getDescriptionAttribute(): string
|
||||
{
|
||||
return app(TicketPresentationResolver::class)->description($this);
|
||||
}
|
||||
|
||||
public function getEffectiveStartsAt(): ?CarbonInterface
|
||||
{
|
||||
return $this->resolvedValidityGroups()
|
||||
->map(fn (TicketValidityGroup $group): ?CarbonInterface => $group->effectiveStartsAt())
|
||||
->filter()
|
||||
->sortBy(fn (CarbonInterface $startsAt): int => $startsAt->getTimestamp())
|
||||
->first();
|
||||
return $this->resolvedValidity()->effectiveStartsAt();
|
||||
}
|
||||
|
||||
public function getEffectiveExpiresAt(): ?CarbonInterface
|
||||
{
|
||||
return $this->resolvedValidityGroups()
|
||||
->map(fn (TicketValidityGroup $group): ?CarbonInterface => $group->effectiveExpiresAt())
|
||||
->filter()
|
||||
->sortByDesc(fn (CarbonInterface $expiresAt): int => $expiresAt->getTimestamp())
|
||||
->first();
|
||||
return $this->resolvedValidity()->effectiveExpiresAt();
|
||||
}
|
||||
|
||||
/** @return Collection<int, ValidityTime> */
|
||||
public function allValidityTimes(): Collection
|
||||
/** @return Collection<int, ResolvedValidityGroup> */
|
||||
public function resolvedValidityGroups(): Collection
|
||||
{
|
||||
return $this->resolvedValidityGroups()
|
||||
->flatMap(fn (TicketValidityGroup $group): EloquentCollection => $group->resolvedValidityTimes())
|
||||
->unique(
|
||||
fn (ValidityTime $validityTime): int => $validityTime->getKey()
|
||||
?? spl_object_id($validityTime)
|
||||
)
|
||||
->values();
|
||||
return $this->resolvedValidity()->groups;
|
||||
}
|
||||
|
||||
/** @return EloquentCollection<int, TicketValidityGroup> */
|
||||
public function resolvedValidityGroups(): EloquentCollection
|
||||
public function resolvedValidity(): ResolvedTicketValidity
|
||||
{
|
||||
if ($this->relationLoaded('validityGroups')) {
|
||||
return $this->getRelation('validityGroups');
|
||||
}
|
||||
|
||||
if (! $this->exists) {
|
||||
return new EloquentCollection;
|
||||
}
|
||||
|
||||
$validityGroups = $this->validityGroups()
|
||||
->with('validityTimes')
|
||||
->get();
|
||||
$this->setRelation('validityGroups', $validityGroups);
|
||||
|
||||
return $validityGroups;
|
||||
return $this->resolvedValidity ??= app(TicketValidityResolver::class)->resolveTicket($this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Ticket\Models;
|
||||
|
||||
use App\Domains\Ticket\Enums\ValidityTimeType;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
#[Fillable(['ticket_id'])]
|
||||
class TicketValidityGroup extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
/** @return BelongsTo<Ticket, $this> */
|
||||
public function ticket(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Ticket::class);
|
||||
}
|
||||
|
||||
/** @return BelongsToMany<ValidityTime, $this> */
|
||||
public function validityTimes(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
ValidityTime::class,
|
||||
'ticket_validity_group_times',
|
||||
'ticket_validity_group_id',
|
||||
'validity_time_id',
|
||||
);
|
||||
}
|
||||
|
||||
public function isValid(?CarbonInterface $at = null): bool
|
||||
{
|
||||
$at ??= now();
|
||||
$validityTimes = $this->resolvedValidityTimes();
|
||||
|
||||
return $validityTimes->isNotEmpty()
|
||||
&& $validityTimes->every(
|
||||
fn (ValidityTime $validityTime): bool => $validityTime->isValid($at)
|
||||
);
|
||||
}
|
||||
|
||||
public function isExpired(?CarbonInterface $at = null): bool
|
||||
{
|
||||
$at ??= now();
|
||||
$expiresAt = $this->effectiveExpiresAt($at);
|
||||
|
||||
return $expiresAt !== null && $expiresAt->lessThanOrEqualTo($at);
|
||||
}
|
||||
|
||||
public function effectiveStartsAt(?CarbonInterface $at = null): ?CarbonInterface
|
||||
{
|
||||
$at ??= now();
|
||||
$anchor = $this->dateAnchor() ?? $at;
|
||||
|
||||
return $this->resolvedValidityTimes()
|
||||
->map(fn (ValidityTime $validityTime): ?CarbonInterface => $validityTime->startsAt($anchor))
|
||||
->filter()
|
||||
->sortByDesc(fn (CarbonInterface $startsAt): int => $startsAt->getTimestamp())
|
||||
->first();
|
||||
}
|
||||
|
||||
public function effectiveExpiresAt(?CarbonInterface $at = null): ?CarbonInterface
|
||||
{
|
||||
$at ??= now();
|
||||
$anchor = $this->dateAnchor() ?? $at;
|
||||
|
||||
return $this->resolvedValidityTimes()
|
||||
->map(function (ValidityTime $validityTime) use ($anchor): ?CarbonInterface {
|
||||
$startsAt = $validityTime->startsAt($anchor);
|
||||
$expiresAt = $validityTime->expiresAt($anchor);
|
||||
|
||||
if (
|
||||
$startsAt !== null
|
||||
&& $expiresAt !== null
|
||||
&& $expiresAt->lessThanOrEqualTo($startsAt)
|
||||
) {
|
||||
return $expiresAt->addDay();
|
||||
}
|
||||
|
||||
return $expiresAt;
|
||||
})
|
||||
->filter()
|
||||
->sortBy(fn (CarbonInterface $expiresAt): int => $expiresAt->getTimestamp())
|
||||
->first();
|
||||
}
|
||||
|
||||
/** @return EloquentCollection<int, ValidityTime> */
|
||||
public function resolvedValidityTimes(): EloquentCollection
|
||||
{
|
||||
if ($this->relationLoaded('validityTimes')) {
|
||||
return $this->getRelation('validityTimes');
|
||||
}
|
||||
|
||||
if (! $this->exists) {
|
||||
return new EloquentCollection;
|
||||
}
|
||||
|
||||
$validityTimes = $this->validityTimes()->get();
|
||||
$this->setRelation('validityTimes', $validityTimes);
|
||||
|
||||
return $validityTimes;
|
||||
}
|
||||
|
||||
private function dateAnchor(): ?CarbonInterface
|
||||
{
|
||||
return $this->resolvedValidityTimes()
|
||||
->filter(fn (ValidityTime $validityTime): bool => $validityTime->type === ValidityTimeType::FixedWindow)
|
||||
->map(fn (ValidityTime $validityTime): ?CarbonInterface => $validityTime->fixed_starts_at)
|
||||
->filter()
|
||||
->sortByDesc(fn (CarbonInterface $startsAt): int => $startsAt->getTimestamp())
|
||||
->first();
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Domains\Ticket\Models;
|
||||
|
||||
use App\Domains\Catalog\Models\AttributeOption;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Event\Models\EventDate;
|
||||
use App\Domains\Ticket\Enums\ValidityTimeType;
|
||||
use Carbon\CarbonImmutable;
|
||||
@@ -11,7 +10,6 @@ use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
@@ -35,12 +33,6 @@ class ValidityTime extends Model
|
||||
];
|
||||
}
|
||||
|
||||
/** @return HasMany<CatalogItem, $this> */
|
||||
public function catalogItems(): HasMany
|
||||
{
|
||||
return $this->hasMany(CatalogItem::class);
|
||||
}
|
||||
|
||||
/** @return HasMany<AttributeOption, $this> */
|
||||
public function attributeOptions(): HasMany
|
||||
{
|
||||
@@ -53,17 +45,6 @@ class ValidityTime extends Model
|
||||
return $this->hasOne(EventDate::class);
|
||||
}
|
||||
|
||||
/** @return BelongsToMany<TicketValidityGroup, $this> */
|
||||
public function ticketValidityGroups(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
TicketValidityGroup::class,
|
||||
'ticket_validity_group_times',
|
||||
'validity_time_id',
|
||||
'ticket_validity_group_id',
|
||||
);
|
||||
}
|
||||
|
||||
public function startsAt(
|
||||
?CarbonInterface $at = null,
|
||||
): ?CarbonInterface {
|
||||
|
||||
Reference in New Issue
Block a user