diff --git a/app/Domains/Ticket/Services/AdminAppTicketService.php b/app/Domains/Ticket/Services/AdminAppTicketService.php index 4f54759..55d3b80 100644 --- a/app/Domains/Ticket/Services/AdminAppTicketService.php +++ b/app/Domains/Ticket/Services/AdminAppTicketService.php @@ -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);