orderBy('date', 'desc') ->get(); return Inertia::render('Admin/DocketEntries/Index', [ 'entries' => $entries->map(fn ($entry) => [ 'id' => $entry->id, 'date' => $entry->date->format('m/d/Y'), 'title' => $entry->title, 'summary' => $entry->summary, 'documents_count' => $entry->documents->count(), ])->toArray(), ]); } /** * Show the form for creating a new resource. */ public function create(): Response { return Inertia::render('Admin/DocketEntries/Create'); } /** * Store a newly created resource in storage. */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'date' => 'required|date', 'title' => 'required|string|max:500', 'summary' => 'required|string', 'notes' => 'nullable|string', ]); $entry = DocketEntry::create($validated); return redirect()->route('admin.docket-entries.show', $entry->id) ->with('success', 'Docket entry created successfully.'); } /** * Display the specified resource. */ public function show(DocketEntry $docketEntry): Response { $docketEntry->load('documents'); return Inertia::render('Admin/DocketEntries/Show', [ 'entry' => [ 'id' => $docketEntry->id, 'date' => $docketEntry->date->format('m/d/Y'), 'title' => $docketEntry->title, 'summary' => $docketEntry->summary, 'notes' => $docketEntry->notes, 'documents' => $docketEntry->documents->map(fn ($doc) => [ 'id' => $doc->id, 'title' => $doc->title, 'original_filename' => $doc->original_filename, 'file_size' => $doc->file_size, 'summary' => $doc->summary, 'display_order' => $doc->display_order, ]), ], ]); } /** * Show the form for editing the specified resource. */ public function edit(DocketEntry $docketEntry): Response { return Inertia::render('Admin/DocketEntries/Edit', [ 'entry' => [ 'id' => $docketEntry->id, 'date' => $docketEntry->date->format('Y-m-d'), 'title' => $docketEntry->title, 'summary' => $docketEntry->summary, 'notes' => $docketEntry->notes, ], ]); } /** * Update the specified resource in storage. */ public function update(Request $request, DocketEntry $docketEntry): RedirectResponse { $validated = $request->validate([ 'date' => 'required|date', 'title' => 'required|string|max:500', 'summary' => 'required|string', 'notes' => 'nullable|string', ]); $docketEntry->update($validated); return redirect()->route('admin.docket-entries.show', $docketEntry->id) ->with('success', 'Docket entry updated successfully.'); } /** * Remove the specified resource from storage. */ public function destroy(DocketEntry $docketEntry): RedirectResponse { $docketEntry->delete(); return redirect()->route('admin.docket-entries.index') ->with('success', 'Docket entry deleted successfully.'); } /** * Send email notification to subscribers for this docket entry. */ public function sendNotification(DocketEntry $docketEntry): RedirectResponse { $this->sendEmailNotifications($docketEntry); return redirect()->route('admin.docket-entries.show', $docketEntry->id) ->with('success', 'Email notifications sent successfully to subscribers.'); } /** * Send email notifications to all active subscribers. */ private function sendEmailNotifications(DocketEntry $entry): void { try { // Get all active subscribers $subscribers = Subscription::where('is_active', true)->get(); if ($subscribers->isEmpty()) { Log::info('No active subscribers to notify', [ 'entry_id' => $entry->id, ]); return; } Log::info('Sending email notifications for new docket entry', [ 'entry_id' => $entry->id, 'entry_title' => $entry->title, 'subscriber_count' => $subscribers->count(), ]); $successCount = 0; $failureCount = 0; foreach ($subscribers as $subscriber) { try { Mail::to($subscriber->email)->send( new NewDocketEntryNotification($entry, $subscriber->unsubscribe_token) ); $successCount++; Log::debug('Email sent to subscriber', [ 'entry_id' => $entry->id, 'subscriber_email' => $subscriber->email, ]); } catch (\Exception $e) { $failureCount++; Log::error('Failed to send email to subscriber', [ 'entry_id' => $entry->id, 'subscriber_email' => $subscriber->email, 'error' => $e->getMessage(), ]); } } Log::info('Email notification batch complete', [ 'entry_id' => $entry->id, 'success_count' => $successCount, 'failure_count' => $failureCount, 'total_subscribers' => $subscribers->count(), ]); } catch (\Exception $e) { Log::error('Failed to send email notifications', [ 'entry_id' => $entry->id, 'error' => $e->getMessage(), ]); // Don't throw exception - we don't want email failures to break the entry creation } } }