Files
Kol-Manager/app/Http/Controllers/NotificationSettingController.php
I-Fun 22f03ccca7 Add contract management and invoice settings pages
- Created a new Blade view for managing brand contracts with forms for adding and editing contracts, including client and category selection.
- Implemented success message notifications for contract actions.
- Added a new Blade view for invoice settings, allowing customization of invoice number format, header branding, and default values for new invoices.
- Introduced a new PDF view for generating rate cards with customizable styles based on user profile settings.
- Developed a profile management page for rate cards, enabling users to update their identity, appearance, and visibility settings.
2026-04-01 21:47:34 +07:00

98 lines
3.8 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' => 'nullable|string',
// WhatsApp (WAHA)
'config.waha_endpoint' => 'required_if:channel,whatsapp|nullable|url',
'config.waha_session' => 'nullable|string',
'config.api_key' => 'nullable|string',
]);
NotificationSetting::create([
'name' => $validated['name'],
'channel' => $validated['channel'],
'config' => $request->input('config', []), // pass raw config — not stripped by validation
'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' => $request->input('config', $setting->config), // use raw input so no keys are stripped
]);
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());
}
}
}