Files
Kol-Manager/app/Http/Controllers/NotificationSettingController.php
I-Fun d3c35ad730 feat: Add notification services for Email, Telegram, and WhatsApp
- Implement EmailNotifier for sending batch and test notifications via email.
- Implement TelegramNotifier for sending batch and test notifications via Telegram.
- Implement WahaNotifier for sending batch and test notifications via WhatsApp using WAHA API.
- Create migration for client_categories table and update clients table to include category_id.
- Create migration for notification_settings table to manage notification configurations.
- Add ClientCategorySeeder to populate default client categories.
- Create manifest.json for PWA support.
- Implement service worker (sw.js) for caching static assets and offline support.
- Add edit event view for modifying scheduled events.
- Create search results view for displaying search queries.
- Develop notification settings view for configuring notification channels and triggers.
2026-04-01 12:46:58 +07:00

98 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Models\NotificationSetting;
use App\Services\NotificationDispatcher;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class NotificationSettingController extends Controller
{
public function index(): View
{
$settings = NotificationSetting::orderBy('channel')->orderBy('name')->get();
return view('settings.notifications', compact('settings'));
}
public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'name' => 'required|string|max:100',
'channel' => 'required|in:email,telegram,whatsapp',
'triggers' => 'nullable|array',
'triggers.*' => 'in:upcoming_event,invoice_due,content_due',
'notify_before_hours' => 'nullable|integer|min:1|max:720',
// Email
'config.to_email' => 'required_if:channel,email|nullable|email',
// Telegram
'config.bot_token' => 'required_if:channel,telegram|nullable|string',
'config.chat_id' => 'required_if:channel,telegram|nullable|string',
// WhatsApp (WAHA)
'config.waha_url' => 'required_if:channel,whatsapp|nullable|url',
'config.waha_session' => 'required_if:channel,whatsapp|nullable|string',
'config.phone' => 'required_if:channel,whatsapp|nullable|string',
]);
NotificationSetting::create([
'name' => $validated['name'],
'channel' => $validated['channel'],
'config' => $validated['config'] ?? [],
'triggers' => $validated['triggers'] ?? [],
'notify_before_hours' => $validated['notify_before_hours'] ?? 24,
'is_enabled' => true,
]);
return back()->with('success', '🔔 Notification channel added!');
}
public function update(Request $request, NotificationSetting $setting): RedirectResponse
{
$validated = $request->validate([
'name' => 'required|string|max:100',
'triggers' => 'nullable|array',
'triggers.*' => 'in:upcoming_event,invoice_due,content_due',
'notify_before_hours' => 'nullable|integer|min:1|max:720',
'config' => 'nullable|array',
]);
$setting->update([
'name' => $validated['name'],
'triggers' => $validated['triggers'] ?? [],
'notify_before_hours' => $validated['notify_before_hours'] ?? $setting->notify_before_hours,
'config' => $validated['config'] ?? $setting->config,
]);
return back()->with('success', '🔔 Notification updated!');
}
public function destroy(NotificationSetting $setting): RedirectResponse
{
$setting->delete();
return back()->with('success', '🔔 Channel removed.');
}
public function toggle(NotificationSetting $setting): RedirectResponse
{
$setting->update(['is_enabled' => ! $setting->is_enabled]);
return back()->with('success', $setting->is_enabled ? '🔔 Enabled!' : '🔕 Disabled.');
}
public function test(NotificationSetting $setting): RedirectResponse
{
try {
app(NotificationDispatcher::class)->sendTest($setting);
return back()->with('success', '✅ Test notification sent!');
} catch (\Throwable $e) {
return back()->with('error', '❌ Failed: ' . $e->getMessage());
}
}
}