Created
July 8, 2025 17:38
-
-
Save Anders87x/93014eb6db394a94214799b1929565c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use Illuminate\Support\Facades\Log; | |
| public function store(Request $request) | |
| { | |
| try { | |
| Log::debug('🔍 Iniciando validación de ticket.'); | |
| $validated = $request->validate([ | |
| 'subject' => 'required|string|max:255', | |
| 'description' => 'required|string', | |
| 'category_id' => 'required|exists:categories,id', | |
| 'files.*' => 'nullable|file|max:2048', | |
| ]); | |
| Log::debug('✅ Validación exitosa.'); | |
| } catch (\Illuminate\Validation\ValidationException $e) { | |
| Log::error('❌ Validación fallida', $e->errors()); | |
| return response()->json(['message' => 'Validación fallida', 'errors' => $e->errors()], 422); | |
| } | |
| try { | |
| DB::beginTransaction(); | |
| Log::debug('🚀 Iniciando creación de ticket.'); | |
| $ticket = Ticket::create([ | |
| 'user_id' => Auth::id(), | |
| 'subject' => $request->subject, | |
| 'description' => $request->description, | |
| 'category_id' => $request->category_id, | |
| 'status_id' => 1, | |
| 'priority_id' => 1, | |
| 'assigned_to' => null, | |
| ]); | |
| Log::debug('✅ Ticket creado', ['ticket_id' => $ticket->id]); | |
| if ($request->hasFile('files')) { | |
| Log::debug('📁 Archivos detectados en el request.'); | |
| $archivos = $request->file('files'); | |
| if (!is_array($archivos)) { | |
| $archivos = [$archivos]; | |
| } | |
| foreach ($archivos as $index => $file) { | |
| if ($file && $file->isValid()) { | |
| Log::debug("📝 Archivo válido detectado", [ | |
| 'nombre' => $file->getClientOriginalName(), | |
| 'tipo' => $file->getClientMimeType() | |
| ]); | |
| try { | |
| $path = $file->store("attachments/tickets/{$ticket->id}", 'public'); | |
| Log::debug('📂 Archivo almacenado correctamente', ['ruta' => $path]); | |
| AttachmentTicket::create([ | |
| 'ticket_id' => $ticket->id, | |
| 'file_path' => $path, | |
| 'file_type' => $file->getClientOriginalExtension(), | |
| ]); | |
| Log::debug('🗃️ Registro creado en attachments_ticket.'); | |
| } catch (\Exception $ex) { | |
| Log::error('❌ Error al guardar el archivo o registrar attachment', [ | |
| 'mensaje' => $ex->getMessage() | |
| ]); | |
| } | |
| } else { | |
| Log::warning("⚠️ Archivo no válido en índice $index."); | |
| } | |
| } | |
| } else { | |
| Log::info('ℹ️ No se adjuntaron archivos.'); | |
| } | |
| DB::commit(); | |
| Log::info("🎉 Ticket #{$ticket->id} creado correctamente."); | |
| try { | |
| $ticket->load(['creator.department', 'priority', 'category', 'status']); | |
| $destinatarios = explode(',', env('MAIL_TICKET_NOTIFICATION')); | |
| Mail::to($destinatarios)->send(new TicketCreatedMail($ticket)); | |
| Log::info('📧 Correo de notificación enviado.'); | |
| } catch (\Exception $e) { | |
| Log::error('❌ Error al enviar el correo.', ['mensaje' => $e->getMessage()]); | |
| } | |
| Log::register( | |
| $ticket->id, | |
| "El usuario " . auth()->user()->name . " creó el ticket '{$ticket->subject}' con ID #{$ticket->id}." | |
| ); | |
| return response()->json(['message' => 'Ticket creado con éxito'], 200); | |
| } catch (\Exception $e) { | |
| DB::rollBack(); | |
| Log::error('❌ Error general al crear el ticket.', ['mensaje' => $e->getMessage()]); | |
| return response()->json(['message' => 'Error al crear el ticket', 'error' => $e->getMessage()], 500); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment