- Removed automatic email sending on docket entry creation - Added sendNotification() method to DocketEntryController - Added route for manual notification trigger - Added 'Send Notification' button to Show page - Email now only sends when admin explicitly clicks button - Still in testing mode (chris@deafgain.org only)
192 lines
6.3 KiB
PHP
192 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\DocketEntry;
|
|
use App\Models\Subscription;
|
|
use App\Mail\NewDocketEntryNotification;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
class DocketEntryController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(): Response
|
|
{
|
|
$paginated = DocketEntry::with('documents')
|
|
->orderBy('date', 'desc')
|
|
->paginate(20);
|
|
|
|
return Inertia::render('Admin/DocketEntries/Index', [
|
|
'entries' => $paginated->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(),
|
|
'pagination' => [
|
|
'current_page' => $paginated->currentPage(),
|
|
'last_page' => $paginated->lastPage(),
|
|
'per_page' => $paginated->perPage(),
|
|
'total' => $paginated->total(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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 subscribers.
|
|
* TESTING MODE: Only sends to chris@deafgain.org
|
|
*/
|
|
private function sendEmailNotifications(DocketEntry $entry): void
|
|
{
|
|
try {
|
|
// TESTING MODE: Only send to chris@deafgain.org
|
|
// In production, this would query all active subscribers
|
|
$testEmail = 'chris@deafgain.org';
|
|
$testToken = 'test-unsubscribe-token'; // Placeholder for testing
|
|
|
|
Log::info('Sending email notification for new docket entry', [
|
|
'entry_id' => $entry->id,
|
|
'entry_title' => $entry->title,
|
|
'recipient' => $testEmail,
|
|
]);
|
|
|
|
Mail::to($testEmail)->send(new NewDocketEntryNotification($entry, $testToken));
|
|
|
|
Log::info('Email notification sent successfully', [
|
|
'entry_id' => $entry->id,
|
|
'recipient' => $testEmail,
|
|
]);
|
|
|
|
// Production code (commented out for testing):
|
|
// $subscribers = Subscription::where('is_active', true)->get();
|
|
// foreach ($subscribers as $subscriber) {
|
|
// Mail::to($subscriber->email)->send(
|
|
// new NewDocketEntryNotification($entry, $subscriber->unsubscribe_token)
|
|
// );
|
|
// }
|
|
} catch (\Exception $e) {
|
|
Log::error('Failed to send email notification', [
|
|
'entry_id' => $entry->id,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
// Don't throw exception - we don't want email failures to break the entry creation
|
|
}
|
|
}
|
|
}
|