<?php
namespace App\Controller\Api;
use App\Repository\AbsenceRepository;
use App\Repository\LateRepository;
use App\Repository\NoteRepository;
use App\Repository\PunishRepository;
use App\Repository\UserNotificationRepository;
use Doctrine\ORM\EntityManagerInterface;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerInterface;
use Knp\Component\Pager\PaginatorInterface;
use OpenApi\Annotations as OA;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class NotificationsController extends AbstractController
{
/**
* news show
* @OA\Tag(name="Users notifications")
*/
#[Route(path: '/api/user/notification/delete/{id}', name: 'api_user_notifications_delete', methods: ['GET'])]
public function userNotificationDelete(EntityManagerInterface $entityManager, $id, UserNotificationRepository $userNotificationsRepository, SerializerInterface $serializer):JsonResponse
{
$user = $this->getUser();
if (!$user){
return new JsonResponse(
$serializer->serialize(['message'=>'you must be logged'],'json'),
Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
true
);
}
$notifFind = $userNotificationsRepository->find($id);
if ($notifFind){
$entityManager->remove($notifFind);
$entityManager->flush();
}
return new JsonResponse(null);
}
/**
* news show
* @OA\Tag(name="Users notifications")
*/
#[Route(path: '/user/get/notifications', name: 'api_user_get_notifications', methods: ['GET'])]
public function userGetNotification(SerializerInterface $serializer, UserNotificationRepository $userNotificationsRepository):JsonResponse
{
$user = $this->getUser();
if (!$user){
return new JsonResponse(
$serializer->serialize(['message'=>'you must be logged'],'json'),
Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
true
);
}
$expoToken = $user->getUserToken();
if ($expoToken){
$notification = $userNotificationsRepository->findBy(['expoPushToken'=>$expoToken],[], 10);
//dd($notification);
$context = SerializationContext::create()->setGroups(["getUserNotification"]);
$jsonPost = $serializer->serialize($notification, 'json', $context);
return new JsonResponse($jsonPost, Response::HTTP_OK, ['accept' => 'application/json'], true);
}
return new JsonResponse(null);
// dd($expoToken);
}
/**
* news show
* @OA\Tag(name="Users notifications")
*/
#[Route(path: '/api/user/get/notifications/all', name: 'api_user_get_notifications_all', methods: ['GET'])]
public function userGetNotificationAll(Request $request, PaginatorInterface $paginator, SerializerInterface $serializer, UserNotificationRepository $userNotificationsRepository):JsonResponse
{
$user = $this->getUser();
if (!$user){
return new JsonResponse(
$serializer->serialize(['message'=>'you must be logged'],'json'),
Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
true
);
}
if ($user){
$notifications = $userNotificationsRepository->findBy(['user'=>$user],['createdAt' => 'DESC']);
$content = [];
foreach ($notifications as $value) {
$result = [
'id'=>$value->getId(),
'title'=>$value->getTitle(),
'content'=>$value->getContent(),
'userId'=> $value->getUser()->getId(),
'type'=>$value->getType(),
'data_id'=>$value->getDataId(),
'data' => $value->getData(),
'created_at'=>$value->getCreatedAt()
];
$content[]=$result;
}
return new JsonResponse($serializer->serialize($content,'json'), Response::HTTP_OK, ['accept' => 'application/json'], true);
}
return new JsonResponse(null);
}
/**
* news show
* @OA\Tag(name="Users notifications")
*/
#[Route(path: '/user/delete/notifications/{type}/', name: 'api_user_delete_notifications_all', methods: ['GET'])]
public function deleteNotification($type, EntityManagerInterface $entityManager, SerializerInterface $serializer, UserNotificationRepository $userNotificationsRepository):JsonResponse
{
$user = $this->getUser();
if (!$user){
return new JsonResponse(
$serializer->serialize(['message'=>'you must be logged'],'json'),
Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
true
);
}
$expoToken = $user->getUserToken();
if ($type == 'events'){
$userNotifs = $userNotificationsRepository->findBy(['type'=>'event', 'expoPushToken'=>$expoToken],[]);
foreach ($userNotifs as $value){
//dd($value);
$entityManager->remove($value);
}
$entityManager->flush();
}elseif ($type == 'notes'){
$userNotifs = $userNotificationsRepository->findBy(['type'=>'note', 'expoPushToken'=>$expoToken],[]);
foreach ($userNotifs as $value){
//dd($value);
$entityManager->remove($value);
}
$entityManager->flush();
}elseif ($type == 'punishes'){
$userNotifs = $userNotificationsRepository->findBy(['type'=>'punish', 'expoPushToken'=>$expoToken],[]);
foreach ($userNotifs as $value){
//dd($value);
$entityManager->remove($value);
}
$entityManager->flush();
}elseif ($type == 'absences'){
$userNotifs = $userNotificationsRepository->findBy(['type'=>'absence', 'expoPushToken'=>$expoToken],[]);
foreach ($userNotifs as $value){
//dd($value);
$entityManager->remove($value);
}
$entityManager->flush();
}elseif ($type == 'late'){
$userNotifs = $userNotificationsRepository->findBy(['type'=>'late', 'expoPushToken'=>$expoToken],[]);
foreach ($userNotifs as $value){
//dd($value);
$entityManager->remove($value);
}
$entityManager->flush();
}
return new JsonResponse(null);
}
}