feat(ticket): implement ticket generation on purchase payment and add related tests
This commit is contained in:
14
app/Domains/Purchase/Events/PurchasePaid.php
Normal file
14
app/Domains/Purchase/Events/PurchasePaid.php
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Purchase\Events;
|
||||||
|
|
||||||
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class PurchasePaid
|
||||||
|
{
|
||||||
|
use Dispatchable, SerializesModels;
|
||||||
|
|
||||||
|
public function __construct(public readonly Purchase $purchase) {}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ namespace App\Domains\Purchase\Models;
|
|||||||
|
|
||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
use App\Domains\Cart\Models\Cart;
|
use App\Domains\Cart\Models\Cart;
|
||||||
|
use App\Domains\Purchase\Events\PurchasePaid;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
@@ -11,6 +12,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
#[Fillable([
|
#[Fillable([
|
||||||
'cart_id',
|
'cart_id',
|
||||||
@@ -137,8 +139,23 @@ class Purchase extends Model
|
|||||||
|
|
||||||
public function markAsPaid(): void
|
public function markAsPaid(): void
|
||||||
{
|
{
|
||||||
$this->update([
|
DB::transaction(function (): void {
|
||||||
'status' => self::STATUS_PAID,
|
$currentStatus = self::query()
|
||||||
]);
|
->whereKey($this->getKey())
|
||||||
|
->lockForUpdate()
|
||||||
|
->value('status');
|
||||||
|
|
||||||
|
if ($currentStatus === self::STATUS_PAID) {
|
||||||
|
$this->status = self::STATUS_PAID;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->update([
|
||||||
|
'status' => self::STATUS_PAID,
|
||||||
|
]);
|
||||||
|
|
||||||
|
PurchasePaid::dispatch($this);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
namespace App\Domains\Ticket\Exceptions;
|
namespace App\Domains\Ticket\Exceptions;
|
||||||
|
|
||||||
use App\Domains\Catalog\Models\CatalogItem;
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
|
use App\Domains\Purchase\Models\PurchaseItem;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
class TicketGenerationException extends RuntimeException
|
class TicketGenerationException extends RuntimeException
|
||||||
@@ -26,4 +28,14 @@ class TicketGenerationException extends RuntimeException
|
|||||||
{
|
{
|
||||||
return new self("El producto {$catalogItem->id} alcanzó su fecha máxima de uso.");
|
return new self("El producto {$catalogItem->id} alcanzó su fecha máxima de uso.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function purchaseWithoutUser(Purchase $purchase): self
|
||||||
|
{
|
||||||
|
return new self("La compra {$purchase->id} no tiene un usuario asociado.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function catalogItemNotFound(PurchaseItem $purchaseItem): self
|
||||||
|
{
|
||||||
|
return new self("No se encontró el producto de la línea de compra {$purchaseItem->id}.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Ticket\Listeners;
|
||||||
|
|
||||||
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
|
use App\Domains\Purchase\Events\PurchasePaid;
|
||||||
|
use App\Domains\Ticket\Exceptions\TicketGenerationException;
|
||||||
|
use App\Domains\Ticket\Services\TicketGeneratorService;
|
||||||
|
|
||||||
|
class GenerateTicketsForPaidPurchase
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly TicketGeneratorService $ticketGenerator,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function handle(PurchasePaid $event): void
|
||||||
|
{
|
||||||
|
$purchase = $event->purchase
|
||||||
|
->newQuery()
|
||||||
|
->with(['user', 'items'])
|
||||||
|
->findOrFail($event->purchase->getKey());
|
||||||
|
$user = $purchase->user;
|
||||||
|
|
||||||
|
foreach ($purchase->items as $purchaseItem) {
|
||||||
|
$catalogItem = CatalogItem::query()
|
||||||
|
->where('tenant_code', $purchase->tenant_codigo)
|
||||||
|
->find($purchaseItem->source_catalog_item_id);
|
||||||
|
|
||||||
|
if ($catalogItem === null) {
|
||||||
|
throw TicketGenerationException::catalogItemNotFound($purchaseItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $this->requiresTickets($catalogItem)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($user === null) {
|
||||||
|
throw TicketGenerationException::purchaseWithoutUser($purchase);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->ticketGenerator->generate(
|
||||||
|
$catalogItem,
|
||||||
|
$user,
|
||||||
|
$purchaseItem->cantidad,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function requiresTickets(CatalogItem $catalogItem): bool
|
||||||
|
{
|
||||||
|
if (! $catalogItem->isBundle()) {
|
||||||
|
return $catalogItem->has_tickets;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $catalogItem->bundleComponents()
|
||||||
|
->whereHas(
|
||||||
|
'catalogItem',
|
||||||
|
fn ($query) => $query->where('has_tickets', true),
|
||||||
|
)
|
||||||
|
->exists();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Domains\Purchase\Events\PurchasePaid;
|
||||||
|
use App\Domains\Ticket\Listeners\GenerateTicketsForPaidPurchase;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
@@ -20,6 +23,8 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function boot(): void
|
public function boot(): void
|
||||||
{
|
{
|
||||||
|
Event::listen(PurchasePaid::class, GenerateTicketsForPaidPurchase::class);
|
||||||
|
|
||||||
Builder::macro('paginateFromRequest', function (int $defaultPerPage = 15, int $maxPerPage = 100, ?int $page = null) {
|
Builder::macro('paginateFromRequest', function (int $defaultPerPage = 15, int $maxPerPage = 100, ?int $page = null) {
|
||||||
/** @var Builder $this */
|
/** @var Builder $this */
|
||||||
$perPage = (int) request()->query('per_page', $defaultPerPage);
|
$perPage = (int) request()->query('per_page', $defaultPerPage);
|
||||||
|
|||||||
@@ -7,9 +7,11 @@ use App\Domains\Attachable\Models\Attachment;
|
|||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
use App\Domains\Catalog\Enums\CatalogItemType;
|
use App\Domains\Catalog\Enums\CatalogItemType;
|
||||||
use App\Domains\Catalog\Models\CatalogItem;
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use App\Domains\Ticket\Exceptions\TicketGenerationException;
|
use App\Domains\Ticket\Exceptions\TicketGenerationException;
|
||||||
use App\Domains\Ticket\Services\TicketGeneratorService;
|
use App\Domains\Ticket\Services\TicketGeneratorService;
|
||||||
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
@@ -121,6 +123,48 @@ class TicketGeneratorServiceTest extends TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_marking_a_purchase_as_paid_generates_its_tickets_once(): void
|
||||||
|
{
|
||||||
|
$item = $this->createTicketableItem('paid-ticket');
|
||||||
|
$purchase = $this->createPurchase($item, 2);
|
||||||
|
$purchase->setRelation('items', new EloquentCollection);
|
||||||
|
|
||||||
|
$purchase->markAsPaid();
|
||||||
|
|
||||||
|
$this->assertSame(Purchase::STATUS_PAID, $purchase->status);
|
||||||
|
$this->assertDatabaseCount('tickets', 2);
|
||||||
|
|
||||||
|
$purchase->markAsPaid();
|
||||||
|
|
||||||
|
$this->assertDatabaseCount('tickets', 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_marking_a_purchase_as_paid_ignores_items_without_tickets(): void
|
||||||
|
{
|
||||||
|
$item = $this->createTicketableItem('regular-product');
|
||||||
|
$item->update(['has_tickets' => false]);
|
||||||
|
$purchase = $this->createPurchase($item->fresh(), 1);
|
||||||
|
|
||||||
|
$purchase->markAsPaid();
|
||||||
|
|
||||||
|
$this->assertSame(Purchase::STATUS_PAID, $purchase->status);
|
||||||
|
$this->assertDatabaseCount('tickets', 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_paid_status_is_rolled_back_when_ticket_generation_fails(): void
|
||||||
|
{
|
||||||
|
$item = $this->createTicketableItem('expired-paid-ticket', maximumUseDate: now());
|
||||||
|
$purchase = $this->createPurchase($item, 1);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$purchase->markAsPaid();
|
||||||
|
$this->fail('La generación debería haber fallado.');
|
||||||
|
} catch (TicketGenerationException) {
|
||||||
|
$this->assertSame(Purchase::STATUS_PENDING_PAYMENT, $purchase->fresh()->status);
|
||||||
|
$this->assertDatabaseCount('tickets', 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function createTicketableItem(
|
private function createTicketableItem(
|
||||||
string $slug,
|
string $slug,
|
||||||
mixed $minimumUseDate = null,
|
mixed $minimumUseDate = null,
|
||||||
@@ -151,6 +195,35 @@ class TicketGeneratorServiceTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function createPurchase(CatalogItem $catalogItem, int $quantity): Purchase
|
||||||
|
{
|
||||||
|
$purchase = Purchase::query()->create([
|
||||||
|
'tenant_codigo' => $this->tenant->codigo,
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
||||||
|
'payment_method' => 'transfer',
|
||||||
|
'total' => 10 * $quantity,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$purchase->items()->create([
|
||||||
|
'source_catalog_item_id' => $catalogItem->id,
|
||||||
|
'source_variant_id' => null,
|
||||||
|
'image_attachment_id' => null,
|
||||||
|
'nombre' => $catalogItem->nombre,
|
||||||
|
'descripcion' => $catalogItem->descripcion,
|
||||||
|
'slug' => $catalogItem->slug,
|
||||||
|
'item_nombre' => $catalogItem->nombre,
|
||||||
|
'variant_attributes' => [],
|
||||||
|
'cantidad' => $quantity,
|
||||||
|
'precio_unitario' => 10,
|
||||||
|
'discount_total' => null,
|
||||||
|
'tax_total' => null,
|
||||||
|
'total' => 10 * $quantity,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $purchase;
|
||||||
|
}
|
||||||
|
|
||||||
private function createTenant(): Tenant
|
private function createTenant(): Tenant
|
||||||
{
|
{
|
||||||
$header = Attachment::query()->create([
|
$header = Attachment::query()->create([
|
||||||
|
|||||||
Reference in New Issue
Block a user