Add tests for ticket validity and event date formatting
- Create TicketValiditySchemaTest to verify database schema for ticket validity. - Update CatalogModelsTest to include tests for event date attributes and selection options. - Introduce EventDateTextFormatterTest for formatting event dates in Spanish. - Refactor EventModelsTest to include validity time relationships. - Add SaleDetailResourceTest to ensure correct serialization of purchase items. - Enhance TicketTest with validity time checks and status management. - Implement ValidityTimeResourceTest to validate resource output for different validity types. - Add ValidityTimeTest to verify casting and validity checks for validity time types.
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,8 +24,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
'source_purchase_id',
|
||||
'source_catalog_item_id',
|
||||
'source_variant_id',
|
||||
'starts_at',
|
||||
'expires_at',
|
||||
'used_at',
|
||||
'scanner_user_id',
|
||||
'user_id',
|
||||
@@ -31,12 +32,19 @@ class Ticket extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const STATUS_ACTIVE = 'active';
|
||||
|
||||
public const STATUS_EXPIRED = 'expired';
|
||||
|
||||
public const STATUS_USED = 'used';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $appends = [
|
||||
'is_valid',
|
||||
'is_expired',
|
||||
'is_used',
|
||||
'status',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
@@ -45,8 +53,6 @@ class Ticket extends Model
|
||||
'source_catalog_item_id' => 'integer',
|
||||
'source_variant_id' => 'integer',
|
||||
'source_purchase_id' => 'integer',
|
||||
'starts_at' => 'datetime',
|
||||
'expires_at' => 'datetime',
|
||||
'used_at' => 'datetime',
|
||||
'scanner_user_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
@@ -89,15 +95,24 @@ class Ticket extends Model
|
||||
return $this->belongsTo(Variant::class, 'source_variant_id');
|
||||
}
|
||||
|
||||
/** @return HasMany<TicketValidityGroup, $this> */
|
||||
public function validityGroups(): HasMany
|
||||
{
|
||||
return $this->hasMany(TicketValidityGroup::class);
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
$now = now();
|
||||
$startsAt = $this->getEffectiveStartsAt();
|
||||
$expiresAt = $this->getEffectiveExpiresAt();
|
||||
if ($this->used_at !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->used_at === null
|
||||
&& ($startsAt === null || $startsAt->lessThanOrEqualTo($now))
|
||||
&& ($expiresAt === null || $expiresAt->greaterThan($now));
|
||||
$validityGroups = $this->resolvedValidityGroups();
|
||||
|
||||
return $validityGroups->isEmpty()
|
||||
|| $validityGroups->contains(
|
||||
fn (TicketValidityGroup $group): bool => $group->isValid()
|
||||
);
|
||||
}
|
||||
|
||||
public function getIsValidAttribute(): bool
|
||||
@@ -107,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
|
||||
@@ -119,15 +136,65 @@ class Ticket extends Model
|
||||
return $this->used_at !== null;
|
||||
}
|
||||
|
||||
public function getStatusAttribute(): string
|
||||
{
|
||||
if ($this->is_used) {
|
||||
return self::STATUS_USED;
|
||||
}
|
||||
|
||||
if ($this->is_expired) {
|
||||
return self::STATUS_EXPIRED;
|
||||
}
|
||||
|
||||
return self::STATUS_ACTIVE;
|
||||
}
|
||||
|
||||
public function getEffectiveStartsAt(): ?CarbonInterface
|
||||
{
|
||||
return $this->sourceVariant?->getMinimumUseDate()
|
||||
?? $this->starts_at;
|
||||
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->sourceVariant?->getMaximumUseDate()
|
||||
?? $this->expires_at;
|
||||
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