45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\UpdateNotificationPreferencesRequest;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class NotificationPreferenceController extends Controller
|
|
{
|
|
public function show(Request $request): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'data' => $this->preferences($request),
|
|
]);
|
|
}
|
|
|
|
public function update(UpdateNotificationPreferencesRequest $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
$validated = $request->validated();
|
|
|
|
$user->forceFill([
|
|
'social_notifications_enabled' => $validated['socialNotificationsEnabled']
|
|
?? $user->social_notifications_enabled,
|
|
'engagement_reminders_enabled' => $validated['engagementRemindersEnabled']
|
|
?? $user->engagement_reminders_enabled,
|
|
])->save();
|
|
|
|
return response()->json([
|
|
'data' => $this->preferences($request),
|
|
'code' => 'NOTIFICATION_PREFERENCES_UPDATED',
|
|
]);
|
|
}
|
|
|
|
/** @return array{socialNotificationsEnabled: bool, engagementRemindersEnabled: bool} */
|
|
private function preferences(Request $request): array
|
|
{
|
|
return [
|
|
'socialNotificationsEnabled' => (bool) $request->user()->social_notifications_enabled,
|
|
'engagementRemindersEnabled' => (bool) $request->user()->engagement_reminders_enabled,
|
|
];
|
|
}
|
|
}
|