src/Controller/ActionTraceController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ActionTrace;
  4. use App\Entity\Prospect;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Security;
  11. class ActionTraceController extends AbstractController
  12. {
  13.     private $entityManager;
  14.     private $security;
  15.     public function __construct(EntityManagerInterface $entityManagerSecurity $security)
  16.     {
  17.         $this->entityManager $entityManager;
  18.         $this->security $security;
  19.     }
  20.     /**
  21.      * @Route("/action/trace", name="create_action_trace", methods={"POST"})
  22.      */
  23.     public function createActionTrace(Request $request): JsonResponse
  24.     {
  25.         $data $request->toArray();
  26.         $prospectId $data['prospectId'] ?? null;
  27.         $type $data['type'] ?? null;
  28.         $description $data['description'] ?? null;
  29.         $date $data['date'] ?? null;
  30.         $user $this->security->getUser();
  31.         if (!$prospectId || !$type || !$user || !$date) {
  32.             return new JsonResponse(['error' => 'Missing parameters'], 400);
  33.         }
  34.         $prospect $this->entityManager->getRepository(Prospect::class)->find($prospectId);
  35.         if (!$prospect) {
  36.             return new JsonResponse(['error' => 'Prospect not found'], 404);
  37.         }
  38.         try {
  39.             $createdAt = new \DateTimeImmutable($date);
  40.         } catch (\Exception $e) {
  41.             return new JsonResponse(['error' => 'Invalid date format'], 400);
  42.         }
  43.         $actionTrace = new ActionTrace();
  44.         $actionTrace->setProspect($prospect);
  45.         $actionTrace->setType($type);
  46.         $actionTrace->setDescription($description);
  47.         $actionTrace->setCreatedAt($createdAt);
  48.         $actionTrace->setUser($user);
  49.         $this->entityManager->persist($actionTrace);
  50.         $this->entityManager->flush();
  51.         return new JsonResponse(['success' => 'Action trace created']);
  52.     }
  53.     /**
  54.      * @Route("/action/trace/list/{prospectId}", name="list_action_trace", methods={"GET"})
  55.      */
  56.     public function listActionTrace(int $prospectId): JsonResponse
  57.     {
  58.         $prospect $this->entityManager->getRepository(Prospect::class)->find($prospectId);
  59.         if (!$prospect) {
  60.             return new JsonResponse(['error' => 'Prospect not found'], 404);
  61.         }
  62.         $actionTraces $this->entityManager->getRepository(ActionTrace::class)
  63.             ->findBy(['prospect' => $prospect], ['createdAt' => 'DESC']);
  64.         $data array_map(function ($actionTrace) {
  65.             $user $actionTrace->getUser();
  66.             return [
  67.                 'id' => $actionTrace->getId(),
  68.                 'type' => $actionTrace->getType(),
  69.                 'description' => $actionTrace->getDescription(),
  70.                 'createdAt' => $actionTrace->getCreatedAt()->format('Y-m-d\TH:i'), // Adjusted format for datetime-local
  71.                 'user' => [
  72.                     'id' => $user->getId(),
  73.                     'prenom' => $user->getPrenom(),
  74.                     'nom' => $user->getNom(),
  75.                     'email' => $user->getEmail(),
  76.                     'couleur' => $user->getCouleur(),
  77.                 ],
  78.             ];
  79.         }, $actionTraces);
  80.         return new JsonResponse(['actionTraces' => $data]);
  81.     }
  82.     /**
  83.      * @Route("/action/trace/{actionId}/update", name="update_action_trace", methods={"POST"})
  84.      */
  85.     public function updateActionTrace(int $actionIdRequest $request): JsonResponse
  86.     {
  87.         $data $request->toArray();
  88.         $type $data['type'] ?? null;
  89.         $description $data['description'] ?? null;
  90.         $date $data['date'] ?? null;
  91.         $user $this->security->getUser();
  92.         if (!$type || !$user || !$date) {
  93.             return new JsonResponse(['error' => 'Missing parameters'], 400);
  94.         }
  95.         $actionTrace $this->entityManager->getRepository(ActionTrace::class)->find($actionId);
  96.         if (!$actionTrace) {
  97.             return new JsonResponse(['error' => 'Action trace not found'], 404);
  98.         }
  99.         // Optionally, check if the current user has permission to edit this action trace
  100.         // For example:
  101.         // if ($actionTrace->getUser()->getId() !== $user->getId() && !$this->isGranted('ROLE_ADMIN')) {
  102.         //     return new JsonResponse(['error' => 'Unauthorized'], 403);
  103.         // }
  104.         try {
  105.             $createdAt = new \DateTimeImmutable($date);
  106.         } catch (\Exception $e) {
  107.             return new JsonResponse(['error' => 'Invalid date format'], 400);
  108.         }
  109.         $actionTrace->setType($type);
  110.         $actionTrace->setDescription($description);
  111.         $actionTrace->setCreatedAt($createdAt);
  112.         // Optionally update the user if needed
  113.         // $actionTrace->setUser($user);
  114.         $this->entityManager->flush();
  115.         return new JsonResponse(['success' => 'Action trace updated']);
  116.     }
  117.     /**
  118.      * @Route("/action/trace/{actionId}/delete", name="delete_action_trace", methods={"DELETE"})
  119.      */
  120.     public function deleteActionTrace(int $actionId): JsonResponse
  121.     {
  122.         $actionTrace $this->entityManager->getRepository(ActionTrace::class)->find($actionId);
  123.         if (!$actionTrace) {
  124.             return new JsonResponse(['error' => 'Action trace not found'], 404);
  125.         }
  126.         $this->entityManager->remove($actionTrace);
  127.         $this->entityManager->flush();
  128.         return new JsonResponse(['success' => 'Action trace deleted']);
  129.     }
  130. }