feat(ticket): add source IDs to tickets and update related services and tests
This commit is contained in:
@@ -42,6 +42,7 @@ class GenerateTicketsForPaidPurchase
|
|||||||
$catalogItem,
|
$catalogItem,
|
||||||
$user,
|
$user,
|
||||||
$purchaseItem->cantidad,
|
$purchaseItem->cantidad,
|
||||||
|
$purchaseItem->source_variant_id,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|||||||
'ticket',
|
'ticket',
|
||||||
'name',
|
'name',
|
||||||
'description',
|
'description',
|
||||||
|
'source_catalog_item_id',
|
||||||
|
'source_variant_id',
|
||||||
'starts_at',
|
'starts_at',
|
||||||
'expires_at',
|
'expires_at',
|
||||||
'used_at',
|
'used_at',
|
||||||
@@ -34,6 +36,8 @@ class Ticket extends Model
|
|||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
'source_catalog_item_id' => 'integer',
|
||||||
|
'source_variant_id' => 'integer',
|
||||||
'starts_at' => 'datetime',
|
'starts_at' => 'datetime',
|
||||||
'expires_at' => 'datetime',
|
'expires_at' => 'datetime',
|
||||||
'used_at' => 'datetime',
|
'used_at' => 'datetime',
|
||||||
|
|||||||
@@ -16,15 +16,19 @@ class TicketGeneratorService
|
|||||||
/**
|
/**
|
||||||
* @return Collection<int, Ticket>
|
* @return Collection<int, Ticket>
|
||||||
*/
|
*/
|
||||||
public function generate(CatalogItem $catalogItem, User $user, int $quantity = 1): Collection
|
public function generate(
|
||||||
{
|
CatalogItem $catalogItem,
|
||||||
|
User $user,
|
||||||
|
int $quantity = 1,
|
||||||
|
?int $sourceVariantId = null,
|
||||||
|
): Collection {
|
||||||
if ($quantity < 1) {
|
if ($quantity < 1) {
|
||||||
throw TicketGenerationException::invalidQuantity();
|
throw TicketGenerationException::invalidQuantity();
|
||||||
}
|
}
|
||||||
|
|
||||||
$now = now();
|
$now = now();
|
||||||
|
|
||||||
return DB::transaction(function () use ($catalogItem, $user, $quantity, $now): Collection {
|
return DB::transaction(function () use ($catalogItem, $user, $quantity, $sourceVariantId, $now): Collection {
|
||||||
$catalogItems = $this->resolveCatalogItems($catalogItem, $quantity, $now);
|
$catalogItems = $this->resolveCatalogItems($catalogItem, $quantity, $now);
|
||||||
|
|
||||||
return $catalogItems->map(fn (CatalogItem $item): Ticket => Ticket::query()->create([
|
return $catalogItems->map(fn (CatalogItem $item): Ticket => Ticket::query()->create([
|
||||||
@@ -32,6 +36,8 @@ class TicketGeneratorService
|
|||||||
'ticket' => (string) Str::uuid(),
|
'ticket' => (string) Str::uuid(),
|
||||||
'name' => $item->nombre,
|
'name' => $item->nombre,
|
||||||
'description' => (string) ($item->descripcion ?? ''),
|
'description' => (string) ($item->descripcion ?? ''),
|
||||||
|
'source_catalog_item_id' => $catalogItem->getKey(),
|
||||||
|
'source_variant_id' => $sourceVariantId,
|
||||||
'starts_at' => $item->minimum_use_date,
|
'starts_at' => $item->minimum_use_date,
|
||||||
'expires_at' => $item->maximum_use_date,
|
'expires_at' => $item->maximum_use_date,
|
||||||
'used_at' => null,
|
'used_at' => null,
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?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('tickets', function (Blueprint $table): void {
|
||||||
|
$table->unsignedBigInteger('source_catalog_item_id')->nullable()->after('description');
|
||||||
|
$table->unsignedBigInteger('source_variant_id')->nullable()->after('source_catalog_item_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('tickets', function (Blueprint $table): void {
|
||||||
|
$table->dropColumn(['source_catalog_item_id', 'source_variant_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -61,6 +61,8 @@ class TicketGeneratorServiceTest extends TestCase
|
|||||||
$this->assertSame($this->user->id, $ticket->user_id);
|
$this->assertSame($this->user->id, $ticket->user_id);
|
||||||
$this->assertSame($item->nombre, $ticket->name);
|
$this->assertSame($item->nombre, $ticket->name);
|
||||||
$this->assertSame($item->descripcion, $ticket->description);
|
$this->assertSame($item->descripcion, $ticket->description);
|
||||||
|
$this->assertSame($item->id, $ticket->source_catalog_item_id);
|
||||||
|
$this->assertNull($ticket->source_variant_id);
|
||||||
$this->assertTrue($ticket->starts_at->equalTo($item->minimum_use_date));
|
$this->assertTrue($ticket->starts_at->equalTo($item->minimum_use_date));
|
||||||
$this->assertTrue($ticket->expires_at->equalTo($item->maximum_use_date));
|
$this->assertTrue($ticket->expires_at->equalTo($item->maximum_use_date));
|
||||||
}
|
}
|
||||||
@@ -100,6 +102,7 @@ class TicketGeneratorServiceTest extends TestCase
|
|||||||
$tickets = $this->service->generate($bundle, $this->user, 2);
|
$tickets = $this->service->generate($bundle, $this->user, 2);
|
||||||
|
|
||||||
$this->assertCount(6, $tickets);
|
$this->assertCount(6, $tickets);
|
||||||
|
$this->assertCount(6, $tickets->where('source_catalog_item_id', $bundle->id));
|
||||||
$this->assertCount(4, $tickets->where('name', $first->nombre));
|
$this->assertCount(4, $tickets->where('name', $first->nombre));
|
||||||
$this->assertCount(2, $tickets->where('name', $second->nombre));
|
$this->assertCount(2, $tickets->where('name', $second->nombre));
|
||||||
}
|
}
|
||||||
@@ -139,6 +142,19 @@ class TicketGeneratorServiceTest extends TestCase
|
|||||||
$this->assertDatabaseCount('tickets', 2);
|
$this->assertDatabaseCount('tickets', 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_a_ticket_generated_from_a_purchase_keeps_its_source_ids(): void
|
||||||
|
{
|
||||||
|
$item = $this->createTicketableItem('sourced-ticket');
|
||||||
|
$purchase = $this->createPurchase($item, 1, 1234);
|
||||||
|
|
||||||
|
$purchase->markAsPaid();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('tickets', [
|
||||||
|
'source_catalog_item_id' => $item->id,
|
||||||
|
'source_variant_id' => 1234,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_marking_a_purchase_as_paid_ignores_items_without_tickets(): void
|
public function test_marking_a_purchase_as_paid_ignores_items_without_tickets(): void
|
||||||
{
|
{
|
||||||
$item = $this->createTicketableItem('regular-product');
|
$item = $this->createTicketableItem('regular-product');
|
||||||
@@ -195,8 +211,11 @@ class TicketGeneratorServiceTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createPurchase(CatalogItem $catalogItem, int $quantity): Purchase
|
private function createPurchase(
|
||||||
{
|
CatalogItem $catalogItem,
|
||||||
|
int $quantity,
|
||||||
|
?int $sourceVariantId = null,
|
||||||
|
): Purchase {
|
||||||
$purchase = Purchase::query()->create([
|
$purchase = Purchase::query()->create([
|
||||||
'tenant_codigo' => $this->tenant->codigo,
|
'tenant_codigo' => $this->tenant->codigo,
|
||||||
'user_id' => $this->user->id,
|
'user_id' => $this->user->id,
|
||||||
@@ -207,7 +226,7 @@ class TicketGeneratorServiceTest extends TestCase
|
|||||||
|
|
||||||
$purchase->items()->create([
|
$purchase->items()->create([
|
||||||
'source_catalog_item_id' => $catalogItem->id,
|
'source_catalog_item_id' => $catalogItem->id,
|
||||||
'source_variant_id' => null,
|
'source_variant_id' => $sourceVariantId,
|
||||||
'image_attachment_id' => null,
|
'image_attachment_id' => null,
|
||||||
'nombre' => $catalogItem->nombre,
|
'nombre' => $catalogItem->nombre,
|
||||||
'descripcion' => $catalogItem->descripcion,
|
'descripcion' => $catalogItem->descripcion,
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ class TicketTest extends TestCase
|
|||||||
{
|
{
|
||||||
$ticket = new Ticket;
|
$ticket = new Ticket;
|
||||||
$ticket->setRawAttributes([
|
$ticket->setRawAttributes([
|
||||||
|
'source_catalog_item_id' => '20',
|
||||||
|
'source_variant_id' => '30',
|
||||||
'starts_at' => '2026-07-21 10:00:00',
|
'starts_at' => '2026-07-21 10:00:00',
|
||||||
'expires_at' => '2026-07-22 10:00:00',
|
'expires_at' => '2026-07-22 10:00:00',
|
||||||
'used_at' => null,
|
'used_at' => null,
|
||||||
@@ -29,6 +31,8 @@ class TicketTest extends TestCase
|
|||||||
|
|
||||||
$this->assertSame('tickets', $ticket->getTable());
|
$this->assertSame('tickets', $ticket->getTable());
|
||||||
$this->assertFalse($ticket->usesTimestamps());
|
$this->assertFalse($ticket->usesTimestamps());
|
||||||
|
$this->assertSame(20, $ticket->source_catalog_item_id);
|
||||||
|
$this->assertSame(30, $ticket->source_variant_id);
|
||||||
$this->assertInstanceOf(Carbon::class, $ticket->starts_at);
|
$this->assertInstanceOf(Carbon::class, $ticket->starts_at);
|
||||||
$this->assertInstanceOf(Carbon::class, $ticket->expires_at);
|
$this->assertInstanceOf(Carbon::class, $ticket->expires_at);
|
||||||
$this->assertNull($ticket->used_at);
|
$this->assertNull($ticket->used_at);
|
||||||
|
|||||||
Reference in New Issue
Block a user