src/Controller/Api/NotificationsController.php line 88

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Repository\AbsenceRepository;
  4. use App\Repository\LateRepository;
  5. use App\Repository\NoteRepository;
  6. use App\Repository\PunishRepository;
  7. use App\Repository\UserNotificationRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use JMS\Serializer\SerializationContext;
  10. use JMS\Serializer\Serializer;
  11. use JMS\Serializer\SerializerInterface;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use OpenApi\Annotations as OA;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class NotificationsController extends AbstractController
  20. {
  21.     /**
  22.      * news show
  23.      * @OA\Tag(name="Users notifications")
  24.      */
  25.     #[Route(path'/api/user/notification/delete/{id}'name'api_user_notifications_delete'methods: ['GET'])]
  26.     public function userNotificationDelete(EntityManagerInterface $entityManager$idUserNotificationRepository $userNotificationsRepositorySerializerInterface $serializer):JsonResponse
  27.     {
  28.         $user $this->getUser();
  29.         if (!$user){
  30.             return new JsonResponse(
  31.                 $serializer->serialize(['message'=>'you must be logged'],'json'),
  32.                 Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
  33.                 true
  34.             );
  35.         }
  36.         $notifFind $userNotificationsRepository->find($id);
  37.         if ($notifFind){
  38.             $entityManager->remove($notifFind);
  39.             $entityManager->flush();
  40.         }
  41.         return new JsonResponse(null);
  42.     }
  43.     /**
  44.      * news show
  45.      * @OA\Tag(name="Users notifications")
  46.      */
  47.     #[Route(path'/user/get/notifications'name'api_user_get_notifications'methods: ['GET'])]
  48.     public function userGetNotification(SerializerInterface $serializerUserNotificationRepository $userNotificationsRepository):JsonResponse
  49.     {
  50.         $user $this->getUser();
  51.         if (!$user){
  52.             return new JsonResponse(
  53.                 $serializer->serialize(['message'=>'you must be logged'],'json'),
  54.                 Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
  55.                 true
  56.             );
  57.         }
  58.         $expoToken $user->getUserToken();
  59.         if ($expoToken){
  60.             $notification $userNotificationsRepository->findBy(['expoPushToken'=>$expoToken],[], 10);
  61.             //dd($notification);
  62.             $context SerializationContext::create()->setGroups(["getUserNotification"]);
  63.             $jsonPost $serializer->serialize($notification'json'$context);
  64.             return new JsonResponse($jsonPostResponse::HTTP_OK, ['accept' => 'application/json'], true);
  65.         }
  66.         return new JsonResponse(null);
  67.         // dd($expoToken);
  68.     }
  69.     /**
  70.      * news show
  71.      * @OA\Tag(name="Users notifications")
  72.      */
  73.     #[Route(path'/api/user/get/notifications/all'name'api_user_get_notifications_all'methods: ['GET'])]
  74.     public function userGetNotificationAll(Request $requestPaginatorInterface $paginatorSerializerInterface $serializerUserNotificationRepository $userNotificationsRepository):JsonResponse
  75.     {
  76.         $user $this->getUser();
  77.         if (!$user){
  78.             return new JsonResponse(
  79.                 $serializer->serialize(['message'=>'you must be logged'],'json'),
  80.                 Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
  81.                 true
  82.             );
  83.         }
  84.         if ($user){
  85.             $notifications $userNotificationsRepository->findBy(['user'=>$user],['createdAt' => 'DESC']);
  86.             $content = [];
  87.             foreach ($notifications as $value) {
  88.                 $result = [
  89.                     'id'=>$value->getId(),
  90.                     'title'=>$value->getTitle(),
  91.                     'content'=>$value->getContent(),
  92.                     'userId'=> $value->getUser()->getId(),
  93.                     'type'=>$value->getType(),
  94.                     'data_id'=>$value->getDataId(),
  95.                     'data' => $value->getData(),
  96.                     'created_at'=>$value->getCreatedAt()
  97.                 ];
  98.                $content[]=$result;
  99.             }
  100.             return new JsonResponse($serializer->serialize($content,'json'), Response::HTTP_OK, ['accept' => 'application/json'], true);
  101.         }
  102.         return new JsonResponse(null);
  103.     }
  104.     /**
  105.      * news show
  106.      * @OA\Tag(name="Users notifications")
  107.      */
  108.     #[Route(path'/user/delete/notifications/{type}/'name'api_user_delete_notifications_all'methods: ['GET'])]
  109.     public function deleteNotification($type,  EntityManagerInterface $entityManagerSerializerInterface $serializerUserNotificationRepository $userNotificationsRepository):JsonResponse
  110.     {
  111.         $user $this->getUser();
  112.         if (!$user){
  113.             return new JsonResponse(
  114.                 $serializer->serialize(['message'=>'you must be logged'],'json'),
  115.                 Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
  116.                 true
  117.             );
  118.         }
  119.         $expoToken $user->getUserToken();
  120.         if ($type == 'events'){
  121.             $userNotifs $userNotificationsRepository->findBy(['type'=>'event''expoPushToken'=>$expoToken],[]);
  122.             foreach ($userNotifs as $value){
  123.                 //dd($value);
  124.                 $entityManager->remove($value);
  125.             }
  126.             $entityManager->flush();
  127.         }elseif ($type == 'notes'){
  128.             $userNotifs $userNotificationsRepository->findBy(['type'=>'note''expoPushToken'=>$expoToken],[]);
  129.             foreach ($userNotifs as $value){
  130.                 //dd($value);
  131.                 $entityManager->remove($value);
  132.             }
  133.             $entityManager->flush();
  134.         }elseif ($type == 'punishes'){
  135.             $userNotifs $userNotificationsRepository->findBy(['type'=>'punish''expoPushToken'=>$expoToken],[]);
  136.             foreach ($userNotifs as $value){
  137.                 //dd($value);
  138.                 $entityManager->remove($value);
  139.             }
  140.             $entityManager->flush();
  141.         }elseif ($type == 'absences'){
  142.             $userNotifs $userNotificationsRepository->findBy(['type'=>'absence''expoPushToken'=>$expoToken],[]);
  143.             foreach ($userNotifs as $value){
  144.                 //dd($value);
  145.                 $entityManager->remove($value);
  146.             }
  147.             $entityManager->flush();
  148.         }elseif ($type == 'late'){
  149.             $userNotifs $userNotificationsRepository->findBy(['type'=>'late''expoPushToken'=>$expoToken],[]);
  150.             foreach ($userNotifs as $value){
  151.                 //dd($value);
  152.                 $entityManager->remove($value);
  153.             }
  154.             $entityManager->flush();
  155.         }
  156.         return new JsonResponse(null);
  157.     }
  158. }