feat(ticket): validate ticket capability status and use transaction in cancel and refund

This commit is contained in:
2026-09-11 09:25:32 -03:00
parent 2ddb046c26
commit 6384c0046d

View File

@@ -80,14 +80,23 @@ class AdminAppTicketService
public function cancel(Tenant $tenant, int $ticketId): Ticket
{
$ticket = Ticket::query()
->where('tenant_code', $tenant->codigo)
->findOrFail($ticketId);
return DB::transaction(function () use ($tenant, $ticketId): Ticket {
$ticket = Ticket::query()
->where('tenant_code', $tenant->codigo)
->lockForUpdate()
->findOrFail($ticketId);
$ticket->markAsCancelled();
$ticket->save();
if (! $ticket->can_cancel()) {
throw ValidationException::withMessages([
'status' => 'El ticket debe estar activo para poder cancelarlo.',
]);
}
return $ticket->refresh()->load(self::RELATIONS);
$ticket->markAsCancelled();
$ticket->save();
return $ticket->refresh()->load(self::RELATIONS);
});
}
public function refund(Tenant $tenant, int $ticketId, string $refundType): Ticket
@@ -100,6 +109,18 @@ class AdminAppTicketService
->lockForUpdate()
->findOrFail($ticketId);
if (! $ticket->can_refund()) {
if ($ticket->status !== Ticket::STATUS_ACTIVE) {
throw ValidationException::withMessages([
'status' => 'El ticket debe estar activo para poder reembolsarlo.',
]);
}
throw ValidationException::withMessages([
'refund' => 'El reembolso no está disponible para este ticket.',
]);
}
$purchaseItem = PurchaseItem::query()
->lockForUpdate()
->find($ticket->source_purchase_item_id);