117 lines
3.5 KiB
PHP
117 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Alert;
|
|
use App\Models\Website;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class AlertController extends Controller
|
|
{
|
|
public function index(Website $website): View
|
|
{
|
|
$this->authorize('view', $website);
|
|
|
|
$alerts = $website->alerts()->orderBy('name')->get();
|
|
$unreadCount = $website->alerts()
|
|
->whereHas('notifications', fn ($q) => $q->where('is_read', false))
|
|
->count();
|
|
|
|
return view('alerts.index', [
|
|
'website' => $website,
|
|
'alerts' => $alerts,
|
|
'unreadCount' => $unreadCount,
|
|
]);
|
|
}
|
|
|
|
public function create(Website $website): View
|
|
{
|
|
$this->authorize('view', $website);
|
|
|
|
return view('alerts.create', ['website' => $website]);
|
|
}
|
|
|
|
public function store(Request $request, Website $website): RedirectResponse
|
|
{
|
|
$this->authorize('view', $website);
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'metric' => 'required|in:' . implode(',', array_keys(Alert::$metrics)),
|
|
'condition' => 'required|in:' . implode(',', array_keys(Alert::$conditions)),
|
|
'threshold' => 'required|numeric|min:0',
|
|
'period' => 'required|in:daily,hourly',
|
|
'channel' => 'required|in:email,in_app,both',
|
|
]);
|
|
|
|
$website->alerts()->create($validated);
|
|
|
|
return redirect()
|
|
->route('alerts.index', $website)
|
|
->with('success', 'Alert created successfully.');
|
|
}
|
|
|
|
public function edit(Website $website, Alert $alert): View
|
|
{
|
|
$this->authorize('view', $website);
|
|
|
|
return view('alerts.edit', [
|
|
'website' => $website,
|
|
'alert' => $alert,
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, Website $website, Alert $alert): RedirectResponse
|
|
{
|
|
$this->authorize('view', $website);
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'metric' => 'required|in:' . implode(',', array_keys(Alert::$metrics)),
|
|
'condition' => 'required|in:' . implode(',', array_keys(Alert::$conditions)),
|
|
'threshold' => 'required|numeric|min:0',
|
|
'period' => 'required|in:daily,hourly',
|
|
'channel' => 'required|in:email,in_app,both',
|
|
]);
|
|
|
|
$alert->update($validated);
|
|
|
|
return redirect()
|
|
->route('alerts.index', $website)
|
|
->with('success', 'Alert updated successfully.');
|
|
}
|
|
|
|
public function destroy(Website $website, Alert $alert): RedirectResponse
|
|
{
|
|
$this->authorize('view', $website);
|
|
|
|
$alert->delete();
|
|
|
|
return redirect()
|
|
->route('alerts.index', $website)
|
|
->with('success', 'Alert deleted.');
|
|
}
|
|
|
|
public function notifications(Website $website): View
|
|
{
|
|
$this->authorize('view', $website);
|
|
|
|
$notifications = \App\Models\AlertNotification::whereHas('alert', fn ($q) => $q->where('website_id', $website->id))
|
|
->with('alert')
|
|
->orderByDesc('created_at')
|
|
->paginate(20);
|
|
|
|
// Mark as read
|
|
\App\Models\AlertNotification::whereHas('alert', fn ($q) => $q->where('website_id', $website->id))
|
|
->where('is_read', false)
|
|
->update(['is_read' => true]);
|
|
|
|
return view('alerts.notifications', [
|
|
'website' => $website,
|
|
'notifications' => $notifications,
|
|
]);
|
|
}
|
|
}
|