refactor(ticket): remove service_date field and update validity time logic
This commit is contained in:
@@ -21,7 +21,6 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'codigo',
|
||||
'nombre',
|
||||
'dominio',
|
||||
'timezone',
|
||||
'primary_color',
|
||||
'secondary_color',
|
||||
'danger_color',
|
||||
|
||||
@@ -22,7 +22,7 @@ class TicketController extends Controller
|
||||
$tickets = Ticket::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('user_id', $request->user()->getKey())
|
||||
->with('validityTime', 'tenant', 'sourceVariant.eventDate', 'sourceVariant.catalogItem')
|
||||
->with('validityTime', 'sourceVariant.eventDate', 'sourceVariant.catalogItem')
|
||||
->orderByDesc('id')
|
||||
->get();
|
||||
|
||||
@@ -36,7 +36,7 @@ class TicketController extends Controller
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('user_id', $request->user()->getKey())
|
||||
->whereIn('id', $ticketIds)
|
||||
->with('validityTime', 'tenant', 'sourceVariant.eventDate', 'sourceVariant.catalogItem')
|
||||
->with('validityTime', 'sourceVariant.eventDate', 'sourceVariant.catalogItem')
|
||||
->orderByDesc('id')
|
||||
->get();
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace App\Domains\Ticket\Enums;
|
||||
|
||||
enum ValidityTimeType: string
|
||||
{
|
||||
case ServiceDateWindow = 'service_date_window';
|
||||
case TimeWindow = 'time_window';
|
||||
case FixedWindow = 'fixed_window';
|
||||
|
||||
/** @return list<string> */
|
||||
|
||||
@@ -22,7 +22,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
'source_catalog_item_id',
|
||||
'source_variant_id',
|
||||
'validity_time_id',
|
||||
'service_date',
|
||||
'used_at',
|
||||
'scanner_user_id',
|
||||
'user_id',
|
||||
@@ -46,7 +45,6 @@ class Ticket extends Model
|
||||
'source_variant_id' => 'integer',
|
||||
'source_purchase_id' => 'integer',
|
||||
'validity_time_id' => 'integer',
|
||||
'service_date' => 'date',
|
||||
'used_at' => 'datetime',
|
||||
'scanner_user_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
@@ -97,13 +95,11 @@ class Ticket extends Model
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
$now = now();
|
||||
$startsAt = $this->getEffectiveStartsAt();
|
||||
$expiresAt = $this->getEffectiveExpiresAt();
|
||||
|
||||
return $this->used_at === null
|
||||
&& ($startsAt === null || $startsAt->lessThanOrEqualTo($now))
|
||||
&& ($expiresAt === null || $expiresAt->greaterThan($now));
|
||||
&& (
|
||||
$this->validityTime === null
|
||||
|| $this->validityTime->isValid()
|
||||
);
|
||||
}
|
||||
|
||||
public function getIsValidAttribute(): bool
|
||||
@@ -127,17 +123,11 @@ class Ticket extends Model
|
||||
|
||||
public function getEffectiveStartsAt(): ?CarbonInterface
|
||||
{
|
||||
return $this->validityTime?->startsAt(
|
||||
$this->service_date,
|
||||
$this->tenant?->timezone ?? config('app.timezone'),
|
||||
);
|
||||
return $this->validityTime?->startsAt();
|
||||
}
|
||||
|
||||
public function getEffectiveExpiresAt(): ?CarbonInterface
|
||||
{
|
||||
return $this->validityTime?->expiresAt(
|
||||
$this->service_date,
|
||||
$this->tenant?->timezone ?? config('app.timezone'),
|
||||
);
|
||||
return $this->validityTime?->expiresAt();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,36 +52,49 @@ class ValidityTime extends Model
|
||||
return $this->hasMany(Ticket::class);
|
||||
}
|
||||
|
||||
public function startsAt(?CarbonInterface $serviceDate, string $timezone): ?CarbonInterface
|
||||
{
|
||||
public function startsAt(
|
||||
?CarbonInterface $at = null,
|
||||
): ?CarbonInterface {
|
||||
if ($this->type === ValidityTimeType::FixedWindow) {
|
||||
return $this->fixed_starts_at;
|
||||
}
|
||||
|
||||
return $this->atServiceDate($serviceDate, $this->start_time, $timezone);
|
||||
return $this->atCurrentDate($this->start_time, $at);
|
||||
}
|
||||
|
||||
public function expiresAt(?CarbonInterface $serviceDate, string $timezone): ?CarbonInterface
|
||||
{
|
||||
public function expiresAt(
|
||||
?CarbonInterface $at = null,
|
||||
): ?CarbonInterface {
|
||||
if ($this->type === ValidityTimeType::FixedWindow) {
|
||||
return $this->fixed_expires_at;
|
||||
}
|
||||
|
||||
return $this->atServiceDate($serviceDate, $this->end_time, $timezone);
|
||||
return $this->atCurrentDate($this->end_time, $at);
|
||||
}
|
||||
|
||||
private function atServiceDate(
|
||||
?CarbonInterface $serviceDate,
|
||||
public function isValid(
|
||||
?CarbonInterface $at = null,
|
||||
): bool {
|
||||
$at ??= now();
|
||||
$startsAt = $this->startsAt($at);
|
||||
$expiresAt = $this->expiresAt($at);
|
||||
|
||||
return ($startsAt === null || $startsAt->lessThanOrEqualTo($at))
|
||||
&& ($expiresAt === null || $expiresAt->greaterThan($at));
|
||||
}
|
||||
|
||||
private function atCurrentDate(
|
||||
?string $time,
|
||||
string $timezone,
|
||||
?CarbonInterface $at,
|
||||
): ?CarbonInterface {
|
||||
if ($serviceDate === null || $time === null) {
|
||||
if ($time === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return CarbonImmutable::parse(
|
||||
$serviceDate->format('Y-m-d').' '.$time,
|
||||
$timezone,
|
||||
)->utc();
|
||||
$at ??= now();
|
||||
$localDate = CarbonImmutable::instance($at)
|
||||
->format('Y-m-d');
|
||||
|
||||
return CarbonImmutable::parse($localDate.' '.$time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace App\Domains\Ticket\Services;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Ticket\Enums\ValidityTimeType;
|
||||
use App\Domains\Ticket\Exceptions\TicketGenerationException;
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use App\Domains\Ticket\Models\ValidityTime;
|
||||
@@ -53,9 +52,6 @@ class TicketGeneratorService
|
||||
'source_catalog_item_id' => $item->getKey(),
|
||||
'source_variant_id' => $variant?->getKey(),
|
||||
'validity_time_id' => $validityTime?->getKey(),
|
||||
'service_date' => $validityTime?->type === ValidityTimeType::ServiceDateWindow
|
||||
? ($variant?->eventDate?->date ?? now($item->tenant->timezone)->toDateString())
|
||||
: null,
|
||||
'used_at' => null,
|
||||
'user_id' => $user->getKey(),
|
||||
]);
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->string('timezone')->default('UTC')->after('dominio');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->dropColumn('timezone');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -15,7 +15,6 @@ return new class extends Migration
|
||||
->constrained('validity_times')
|
||||
->cascadeOnUpdate()
|
||||
->restrictOnDelete();
|
||||
$table->date('service_date')->nullable()->after('validity_time_id');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,7 +22,6 @@ return new class extends Migration
|
||||
{
|
||||
Schema::table('tickets', function (Blueprint $table): void {
|
||||
$table->dropConstrainedForeignId('validity_time_id');
|
||||
$table->dropColumn('service_date');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -65,7 +65,6 @@ class TicketGeneratorServiceTest extends TestCase
|
||||
$this->assertSame($item->id, $ticket->source_catalog_item_id);
|
||||
$this->assertNull($ticket->source_variant_id);
|
||||
$this->assertNull($ticket->validity_time_id);
|
||||
$this->assertNull($ticket->service_date);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,8 +26,8 @@ class TicketValiditySchemaTest extends TestCase
|
||||
|
||||
$this->assertTrue(Schema::hasColumns('tickets', [
|
||||
'validity_time_id',
|
||||
'service_date',
|
||||
]));
|
||||
$this->assertFalse(Schema::hasColumn('tickets', 'service_date'));
|
||||
$this->assertFalse(Schema::hasColumn('tickets', 'starts_at'));
|
||||
$this->assertFalse(Schema::hasColumn('tickets', 'expires_at'));
|
||||
|
||||
@@ -38,6 +38,5 @@ class TicketValiditySchemaTest extends TestCase
|
||||
$this->assertFalse(Schema::hasColumn('variantes', 'minimum_use_date'));
|
||||
$this->assertFalse(Schema::hasColumn('variantes', 'maximum_use_date'));
|
||||
|
||||
$this->assertTrue(Schema::hasColumn('tenants', 'timezone'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ class TicketTest extends TestCase
|
||||
'source_catalog_item_id' => '20',
|
||||
'source_variant_id' => '30',
|
||||
'validity_time_id' => '40',
|
||||
'service_date' => '2026-07-21',
|
||||
'used_at' => null,
|
||||
'scanner_user_id' => '15',
|
||||
'user_id' => '10',
|
||||
@@ -39,7 +38,6 @@ class TicketTest extends TestCase
|
||||
$this->assertSame(20, $ticket->source_catalog_item_id);
|
||||
$this->assertSame(30, $ticket->source_variant_id);
|
||||
$this->assertSame(40, $ticket->validity_time_id);
|
||||
$this->assertInstanceOf(Carbon::class, $ticket->service_date);
|
||||
$this->assertNull($ticket->used_at);
|
||||
$this->assertSame(15, $ticket->scanner_user_id);
|
||||
$this->assertSame(10, $ticket->user_id);
|
||||
@@ -69,19 +67,18 @@ class TicketTest extends TestCase
|
||||
$this->assertFalse($ticket->is_expired);
|
||||
}
|
||||
|
||||
public function test_service_date_window_is_resolved_in_tenant_timezone(): void
|
||||
public function test_time_window_is_resolved_for_current_date(): void
|
||||
{
|
||||
Carbon::setTestNow('2026-07-21 14:00:00');
|
||||
$ticket = new Ticket(['service_date' => '2026-07-21']);
|
||||
Carbon::setTestNow('2026-07-21 12:00:00');
|
||||
$ticket = new Ticket;
|
||||
$ticket->setRelation('validityTime', new ValidityTime([
|
||||
'type' => ValidityTimeType::ServiceDateWindow,
|
||||
'type' => ValidityTimeType::TimeWindow,
|
||||
'start_time' => '10:00:00',
|
||||
'end_time' => '12:00:00',
|
||||
'end_time' => '14:00:00',
|
||||
]));
|
||||
$ticket->setRelation('tenant', new Tenant(['timezone' => 'America/Argentina/Buenos_Aires']));
|
||||
|
||||
$this->assertSame('2026-07-21 13:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
|
||||
$this->assertSame('2026-07-21 15:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
|
||||
$this->assertSame('2026-07-21 10:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
|
||||
$this->assertSame('2026-07-21 14:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
|
||||
$this->assertTrue($ticket->isValid());
|
||||
}
|
||||
|
||||
@@ -114,7 +111,6 @@ class TicketTest extends TestCase
|
||||
{
|
||||
$ticket = new Ticket;
|
||||
$ticket->setRelation('validityTime', $validityTime);
|
||||
$ticket->setRelation('tenant', new Tenant(['timezone' => 'UTC']));
|
||||
|
||||
return $ticket;
|
||||
}
|
||||
|
||||
@@ -34,8 +34,36 @@ class ValidityTimeTest extends TestCase
|
||||
public function test_it_exposes_supported_type_values(): void
|
||||
{
|
||||
$this->assertSame([
|
||||
'service_date_window',
|
||||
'time_window',
|
||||
'fixed_window',
|
||||
], ValidityTimeType::values());
|
||||
}
|
||||
|
||||
public function test_fixed_window_is_valid_between_its_limits(): void
|
||||
{
|
||||
$validityTime = new ValidityTime([
|
||||
'type' => ValidityTimeType::FixedWindow,
|
||||
'fixed_starts_at' => '2026-08-20 10:00:00',
|
||||
'fixed_expires_at' => '2026-08-20 12:00:00',
|
||||
]);
|
||||
|
||||
$this->assertTrue($validityTime->isValid(at: Carbon::parse('2026-08-20 10:00:00')));
|
||||
$this->assertTrue($validityTime->isValid(at: Carbon::parse('2026-08-20 11:00:00')));
|
||||
$this->assertFalse($validityTime->isValid(at: Carbon::parse('2026-08-20 12:00:00')));
|
||||
}
|
||||
|
||||
public function test_time_window_is_valid_between_its_limits(): void
|
||||
{
|
||||
$validityTime = new ValidityTime([
|
||||
'type' => ValidityTimeType::TimeWindow,
|
||||
'start_time' => '11:00:00',
|
||||
'end_time' => '14:00:00',
|
||||
]);
|
||||
$this->assertTrue($validityTime->isValid(
|
||||
Carbon::parse('2026-08-20 12:00:00'),
|
||||
));
|
||||
$this->assertFalse($validityTime->isValid(
|
||||
Carbon::parse('2026-08-20 14:00:00'),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user