HEX
Server: Apache
System: Linux server1.ariadata.co 4.18.0-553.120.1.el8_10.x86_64 #1 SMP Mon Apr 20 18:04:27 EDT 2026 x86_64
User: nepi (1051)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system,show_source,popen,proc_open
Upload Files
File: /home/nepi/file.nepi.ir/common/Notifications/NotificationSubscriptionsController.php
<?php

namespace Common\Notifications;

use App\User;
use Common\Core\BaseController;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\JsonResponse;

class NotificationSubscriptionsController extends BaseController
{
    public function __construct()
    {
        $this->middleware(['auth']);
    }

    public function index(User $user): JsonResponse
    {
        $response = $this->getConfig();
        $subs = $user->notificationSubscriptions;
        $response['user_selections'] = $subs;

        return $this->success($response);
    }

    public function update(User $user): JsonResponse
    {
        $this->validate(request(), [
            'selections' => 'present|array',
            'selections.*.notif_id' => 'required|string',
            'selections.*.channels' => 'required|array',
        ]);

        foreach (request()->get('selections') as $selection) {
            $subscription = $user
                ->notificationSubscriptions()
                ->firstOrNew(['notif_id' => $selection['notif_id']]);
            $newChannels = $subscription['channels'];
            // can update state of all channels at once or only a single channel
            foreach ($selection['channels'] as $newChannel => $isSubscribed) {
                $newChannels[$newChannel] = $isSubscribed;
            }
            $subscription->fill(['channels' => $newChannels])->save();
        }

        return $this->success();
    }

    private function getConfig()
    {
        return app(Filesystem::class)->getRequire(
            resource_path('defaults/notification-settings.php'),
        );
    }
}