feat(ticket): implement ticket generation policies and validity time management
- Added `ticket_generation_policy` column to `catalog_items` table to manage ticket generation strategies. - Created `ticket_validity_times` table to allow multiple validity times per ticket. - Introduced `validity_time_id` column in `event_dates` table to associate event dates with validity times. - Established `ticket_validity_groups` and `ticket_validity_group_times` tables to group validity times for tickets. - Updated seeder to set default ticket generation policy for specific tenant. - Enhanced tests to cover new functionality, including ticket generation based on event dates and validity times. - Refactored ticket validity checks to accommodate multiple validity times in a group.
This commit is contained in:
@@ -9,9 +9,12 @@ use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
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',
|
||||
@@ -21,7 +24,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
'source_purchase_id',
|
||||
'source_catalog_item_id',
|
||||
'source_variant_id',
|
||||
'validity_time_id',
|
||||
'used_at',
|
||||
'scanner_user_id',
|
||||
'user_id',
|
||||
@@ -51,7 +53,6 @@ class Ticket extends Model
|
||||
'source_catalog_item_id' => 'integer',
|
||||
'source_variant_id' => 'integer',
|
||||
'source_purchase_id' => 'integer',
|
||||
'validity_time_id' => 'integer',
|
||||
'used_at' => 'datetime',
|
||||
'scanner_user_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
@@ -94,18 +95,23 @@ class Ticket extends Model
|
||||
return $this->belongsTo(Variant::class, 'source_variant_id');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<ValidityTime, $this> */
|
||||
public function validityTime(): BelongsTo
|
||||
/** @return HasMany<TicketValidityGroup, $this> */
|
||||
public function validityGroups(): HasMany
|
||||
{
|
||||
return $this->belongsTo(ValidityTime::class);
|
||||
return $this->hasMany(TicketValidityGroup::class);
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
return $this->used_at === null
|
||||
&& (
|
||||
$this->validityTime === null
|
||||
|| $this->validityTime->isValid()
|
||||
if ($this->used_at !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$validityGroups = $this->resolvedValidityGroups();
|
||||
|
||||
return $validityGroups->isEmpty()
|
||||
|| $validityGroups->contains(
|
||||
fn (TicketValidityGroup $group): bool => $group->isValid()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -116,11 +122,13 @@ class Ticket extends Model
|
||||
|
||||
public function getIsExpiredAttribute(): bool
|
||||
{
|
||||
$expiresAt = $this->getEffectiveExpiresAt();
|
||||
$validityGroups = $this->resolvedValidityGroups();
|
||||
|
||||
return $this->used_at === null
|
||||
&& $expiresAt !== null
|
||||
&& $expiresAt->lessThanOrEqualTo(now());
|
||||
&& $validityGroups->isNotEmpty()
|
||||
&& $validityGroups->every(
|
||||
fn (TicketValidityGroup $group): bool => $group->isExpired()
|
||||
);
|
||||
}
|
||||
|
||||
public function getIsUsedAttribute(): bool
|
||||
@@ -143,11 +151,50 @@ class Ticket extends Model
|
||||
|
||||
public function getEffectiveStartsAt(): ?CarbonInterface
|
||||
{
|
||||
return $this->validityTime?->startsAt();
|
||||
return $this->resolvedValidityGroups()
|
||||
->map(fn (TicketValidityGroup $group): ?CarbonInterface => $group->effectiveStartsAt())
|
||||
->filter()
|
||||
->sortBy(fn (CarbonInterface $startsAt): int => $startsAt->getTimestamp())
|
||||
->first();
|
||||
}
|
||||
|
||||
public function getEffectiveExpiresAt(): ?CarbonInterface
|
||||
{
|
||||
return $this->validityTime?->expiresAt();
|
||||
return $this->resolvedValidityGroups()
|
||||
->map(fn (TicketValidityGroup $group): ?CarbonInterface => $group->effectiveExpiresAt())
|
||||
->filter()
|
||||
->sortByDesc(fn (CarbonInterface $expiresAt): int => $expiresAt->getTimestamp())
|
||||
->first();
|
||||
}
|
||||
|
||||
/** @return Collection<int, ValidityTime> */
|
||||
public function allValidityTimes(): Collection
|
||||
{
|
||||
return $this->resolvedValidityGroups()
|
||||
->flatMap(fn (TicketValidityGroup $group): EloquentCollection => $group->resolvedValidityTimes())
|
||||
->unique(
|
||||
fn (ValidityTime $validityTime): int => $validityTime->getKey()
|
||||
?? spl_object_id($validityTime)
|
||||
)
|
||||
->values();
|
||||
}
|
||||
|
||||
/** @return EloquentCollection<int, TicketValidityGroup> */
|
||||
public function resolvedValidityGroups(): EloquentCollection
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user