<?php
namespace App\Controller\Controlpanel\Api;
use App\Application\DTO\ControlPanel\Api\JsonApi\JsonApiDocument;
use App\Application\DTO\ControlPanel\Api\QueryParameter\NotificationQueryParameter;
use App\Application\Service\ControlPanel\Api\Notification\MarkAsReadNotificationsService;
use App\Application\Service\ControlPanel\Api\Notification\NotificationConfigurationService;
use App\Application\Service\ControlPanel\Api\Notification\NotificationJsonApiPatcherService;
use App\Application\Service\ControlPanel\Api\Notification\NotificationJsonApiService;
use App\Application\Service\Session\SessionService;
use App\Entity\System\Customer;
use App\Exception\ControlPanel\ControlPanelNotFoundException;
use App\Model\Controlpanel\Response\JsonApiResponse;
use App\Model\Controlpanel\Response\WebJsonApiResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/controlpanel/api/notifications", name="api_controlpanel_notification_")
*/
class NotificationsController extends AbstractController
{
public function __construct(
private readonly NotificationConfigurationService $notificationService,
private readonly MarkAsReadNotificationsService $markAsReadNotificationsService,
private readonly SessionService $sessionService,
) {
}
/**
* @Route("/config", name="config_list", methods={"GET"})
*
* @return JsonApiResponse
*/
public function getNotificationsConfiguration(): JsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
$jsonApiDocument = new JsonApiDocument();
$jsonApiDocument->data = $this->notificationService->getNotifications($customer);
return new JsonApiResponse($jsonApiDocument);
}
/**
* @param Request $request
*
* @return JsonApiResponse
*
* @Route("/config", name="config_update", methods={"PATCH"})
*/
public function updateConfiguration(Request $request): JsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
$requestDocument = $request->attributes->get('json_api_document');
$newsletterActive = (bool)$requestDocument->data->attributes->newsletter;
$jsonApiDocument = new JsonApiDocument();
$jsonApiDocument->data = $this->notificationService->updateNotification($customer, $newsletterActive, $request->getClientIp());
return new JsonApiResponse($jsonApiDocument);
}
/**
* @param Request $request
* @param NotificationJsonApiService $notificationJsonApiService
*
* @return WebJsonApiResponse
*
* @throws \Exception
*
* @Route("", name="list", methods={"GET"})
*/
public function list(Request $request, NotificationJsonApiService $notificationJsonApiService): WebJsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
$parameters = new NotificationQueryParameter(
$request->query->all(),
$customer->getId(),
$this->sessionService->getLocaleId(),
$this->sessionService->getLocale()
);
$document = $notificationJsonApiService->getCollectionDocument($parameters);
return new WebJsonApiResponse($document);
}
/**
* @param int $customerNotificationId
* @param Request $request
* @param NotificationJsonApiPatcherService $service
*
* @return WebJsonApiResponse
*
* @throws ControlPanelNotFoundException
*
* @Route("/{customerNotificationId<\d+>}", name="update", methods={"PATCH"})
*/
public function patch(int $customerNotificationId, Request $request, NotificationJsonApiPatcherService $service): WebJsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
/** @var \App\Application\DTO\JsonApi\JsonApiDocument $document */
$document = $request->attributes->get('json_api_document');
$updatedDocument = $service->patchDocument(
$customerNotificationId,
$document,
$customer->getId(),
$this->sessionService->getLocaleId()
);
return new WebJsonApiResponse($updatedDocument);
}
/**
* @param Request $request
*
* @return WebJsonApiResponse
*
* @throws \Exception
*
* @Route("/mark-all-as-read", name="mark_all_as_read ", methods={"PATCH"})
*/
public function allRead(Request $request): WebJsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
$updatedNotificationsDocument = $this->markAsReadNotificationsService->markNotificationsAsRead(
$customer->getId(),
$this->sessionService->getLocaleId()
);
return new WebJsonApiResponse($updatedNotificationsDocument);
}
}