mad-lawsuit/app/Http/Controllers/SubscriptionController.php
TheMaddax 06a620406c Phase 1: Email subscription and document download features
- Created SubscriptionController with subscribe/unsubscribe endpoints
- Added subscription API routes (POST /api/subscribe, GET /api/unsubscribe/{token})
- Updated Vue Home component with working subscription form
- Created DocumentController with download endpoint
- Added document download route (GET /api/documents/{id}/download)
- Updated Vue component to link PDF buttons to download route
- Created DocketSeeder with 5 sample docket entries and documents
- Seeded database with test data for development

Features working:
- Email subscription with validation and duplicate checking
- Subscription reactivation for previously unsubscribed emails
- Document download functionality (ready for actual PDFs)
- Sample data for testing UI

Next: Admin dashboard for CRUD operations
2025-12-17 15:51:47 -07:00

86 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Subscription;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Validator;
class SubscriptionController extends Controller
{
/**
* Subscribe an email address to notifications.
*/
public function subscribe(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'email' => 'required|email|max:255',
]);
if ($validator->fails()) {
return response()->json([
'success' => false,
'message' => 'Please provide a valid email address.',
'errors' => $validator->errors(),
], 422);
}
$email = $request->input('email');
// Check if email already exists
$existing = Subscription::where('email', $email)->first();
if ($existing) {
if ($existing->is_active) {
return response()->json([
'success' => false,
'message' => 'This email is already subscribed to notifications.',
], 409);
} else {
// Reactivate subscription
$existing->is_active = true;
$existing->save();
return response()->json([
'success' => true,
'message' => 'Your subscription has been reactivated! You will receive email notifications for new docket entries.',
]);
}
}
// Create new subscription
Subscription::create([
'email' => $email,
'is_active' => true,
]);
return response()->json([
'success' => true,
'message' => 'Successfully subscribed! You will receive email notifications for new docket entries.',
]);
}
/**
* Unsubscribe using token.
*/
public function unsubscribe(Request $request, string $token): JsonResponse
{
$subscription = Subscription::where('unsubscribe_token', $token)->first();
if (!$subscription) {
return response()->json([
'success' => false,
'message' => 'Invalid unsubscribe token.',
], 404);
}
$subscription->is_active = false;
$subscription->save();
return response()->json([
'success' => true,
'message' => 'You have been successfully unsubscribed from notifications.',
]);
}
}