mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
list of unread notificaiton api endpoint
This commit is contained in:
parent
eccc75aecf
commit
e23ef35b75
@ -13,13 +13,17 @@ namespace Chill\MainBundle\Controller;
|
|||||||
|
|
||||||
use Chill\MainBundle\Entity\Notification;
|
use Chill\MainBundle\Entity\Notification;
|
||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
|
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||||
|
use Chill\MainBundle\Repository\NotificationRepository;
|
||||||
use Chill\MainBundle\Security\Authorization\NotificationVoter;
|
use Chill\MainBundle\Security\Authorization\NotificationVoter;
|
||||||
|
use Chill\MainBundle\Serializer\Model\Collection;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||||
use Symfony\Component\Security\Core\Security;
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use UnexpectedValueException;
|
use UnexpectedValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,12 +33,26 @@ class NotificationApiController
|
|||||||
{
|
{
|
||||||
private EntityManagerInterface $entityManager;
|
private EntityManagerInterface $entityManager;
|
||||||
|
|
||||||
|
private NotificationRepository $notificationRepository;
|
||||||
|
|
||||||
|
private PaginatorFactory $paginatorFactory;
|
||||||
|
|
||||||
private Security $security;
|
private Security $security;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $entityManager, Security $security)
|
private SerializerInterface $serializer;
|
||||||
{
|
|
||||||
|
public function __construct(
|
||||||
|
EntityManagerInterface $entityManager,
|
||||||
|
NotificationRepository $notificationRepository,
|
||||||
|
PaginatorFactory $paginatorFactory,
|
||||||
|
Security $security,
|
||||||
|
SerializerInterface $serializer
|
||||||
|
) {
|
||||||
$this->entityManager = $entityManager;
|
$this->entityManager = $entityManager;
|
||||||
|
$this->notificationRepository = $notificationRepository;
|
||||||
|
$this->paginatorFactory = $paginatorFactory;
|
||||||
$this->security = $security;
|
$this->security = $security;
|
||||||
|
$this->serializer = $serializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,6 +71,29 @@ class NotificationApiController
|
|||||||
return $this->markAs('unread', $notification);
|
return $this->markAs('unread', $notification);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/my/unread")
|
||||||
|
*/
|
||||||
|
public function myUnreadNotifications(): JsonResponse
|
||||||
|
{
|
||||||
|
$total = $this->notificationRepository->countUnreadByUser($this->security->getUser());
|
||||||
|
$paginator = $this->paginatorFactory->create($total);
|
||||||
|
$notifications = $this->notificationRepository->findUnreadByUser(
|
||||||
|
$this->security->getUser(),
|
||||||
|
$paginator->getItemsPerPage(),
|
||||||
|
$paginator->getCurrentPageFirstItemNumber()
|
||||||
|
);
|
||||||
|
dump($notifications);
|
||||||
|
$collection = new Collection($notifications, $paginator);
|
||||||
|
|
||||||
|
return new JsonResponse(
|
||||||
|
$this->serializer->serialize($collection, 'json', ['groups' => ['read']]),
|
||||||
|
JsonResponse::HTTP_OK,
|
||||||
|
[],
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private function markAs(string $target, Notification $notification): JsonResponse
|
private function markAs(string $target, Notification $notification): JsonResponse
|
||||||
{
|
{
|
||||||
if (!$this->security->isGranted(NotificationVoter::NOTIFICATION_TOGGLE_READ_STATUS, $notification)) {
|
if (!$this->security->isGranted(NotificationVoter::NOTIFICATION_TOGGLE_READ_STATUS, $notification)) {
|
||||||
|
@ -179,6 +179,29 @@ final class NotificationRepository implements ObjectRepository
|
|||||||
return $this->repository->findOneBy($criteria, $orderBy);
|
return $this->repository->findOneBy($criteria, $orderBy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array|Notification[]
|
||||||
|
*/
|
||||||
|
public function findUnreadByUser(User $user, int $limit = 20, int $offset = 0): array
|
||||||
|
{
|
||||||
|
$rsm = new Query\ResultSetMappingBuilder($this->em);
|
||||||
|
$rsm->addRootEntityFromClassMetadata(Notification::class, 'cmn');
|
||||||
|
|
||||||
|
$sql = 'SELECT ' . $rsm->generateSelectClause(['cmn' => 'cmn']) . ' ' .
|
||||||
|
'FROM chill_main_notification cmn ' .
|
||||||
|
'WHERE ' .
|
||||||
|
'EXISTS (select 1 FROM chill_main_notification_addresses_unread cmnau WHERE cmnau.user_id = :userId and cmnau.notification_id = cmn.id) ' .
|
||||||
|
'ORDER BY cmn.date DESC ' .
|
||||||
|
'LIMIT :limit OFFSET :offset';
|
||||||
|
|
||||||
|
$nq = $this->em->createNativeQuery($sql, $rsm)
|
||||||
|
->setParameter('userId', $user->getId())
|
||||||
|
->setParameter('limit', $limit)
|
||||||
|
->setParameter('offset', $offset);
|
||||||
|
|
||||||
|
return $nq->getResult();
|
||||||
|
}
|
||||||
|
|
||||||
public function getClassName()
|
public function getClassName()
|
||||||
{
|
{
|
||||||
return Notification::class;
|
return Notification::class;
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||||
|
|
||||||
|
use ArrayObject;
|
||||||
|
use Chill\MainBundle\Entity\Notification;
|
||||||
|
use Chill\MainBundle\Notification\NotificationHandlerManager;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
|
|
||||||
|
class NotificationNormalizer implements NormalizerAwareInterface, NormalizerInterface
|
||||||
|
{
|
||||||
|
use NormalizerAwareTrait;
|
||||||
|
|
||||||
|
private EntityManagerInterface $entityManager;
|
||||||
|
|
||||||
|
private NotificationHandlerManager $notificationHandlerManager;
|
||||||
|
|
||||||
|
private Security $security;
|
||||||
|
|
||||||
|
public function __construct(NotificationHandlerManager $notificationHandlerManager, EntityManagerInterface $entityManager, Security $security)
|
||||||
|
{
|
||||||
|
$this->notificationHandlerManager = $notificationHandlerManager;
|
||||||
|
$this->entityManager = $entityManager;
|
||||||
|
$this->security = $security;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Notification $object
|
||||||
|
*
|
||||||
|
* @return array|ArrayObject|bool|float|int|string|void|null
|
||||||
|
*/
|
||||||
|
public function normalize($object, ?string $format = null, array $context = [])
|
||||||
|
{
|
||||||
|
dump($object);
|
||||||
|
$entity = $this->entityManager
|
||||||
|
->getRepository($object->getRelatedEntityClass())
|
||||||
|
->find($object->getRelatedEntityId());
|
||||||
|
|
||||||
|
return [
|
||||||
|
'type' => 'notification',
|
||||||
|
'addressees' => $this->normalizer->normalize($object->getAddressees(), $format, $context),
|
||||||
|
'date' => $this->normalizer->normalize($object->getDate(), $format, $context),
|
||||||
|
'isRead' => $object->isReadBy($this->security->getUser()),
|
||||||
|
'message' => $object->getMessage(),
|
||||||
|
'relatedEntityClass' => $object->getRelatedEntityClass(),
|
||||||
|
'relatedEntityId' => $object->getRelatedEntityId(),
|
||||||
|
'sender' => $this->normalizer->normalize($object->getSender(), $format, $context),
|
||||||
|
'title' => $object->getTitle(),
|
||||||
|
'entity' => null !== $entity ? $this->normalizer->normalize($entity, $format, $context) : null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsNormalization($data, ?string $format = null)
|
||||||
|
{
|
||||||
|
return $data instanceof Notification && 'json' === $format;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user