src/Controller/Api/DocumentController.php line 140

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Enum\DocumentTypeOrganizationEnum;
  4. use App\Enum\DocumentTypeUserEnum;
  5. use App\Enum\OrganizationStatusEnum;
  6. use App\Enum\PappersDocumentTypeOrganizationEnum;
  7. use App\Service\DocumentUtils;
  8. use App\Service\EncryptorDataUtils;
  9. use App\Service\Organization\GetOrganizationFromUserAccess;
  10. use App\Service\OrganizationUtils;
  11. use App\Service\UserChecker;
  12. use App\Traits\SentryNotifyTrait;
  13. use App\Utils\UserUtils;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Evo\Domain\Core\SignatureServiceInterface;
  16. use Evo\Infrastructure\MappingORM\Document;
  17. use Evo\Infrastructure\MappingORM\Organization;
  18. use Evo\Infrastructure\MappingORM\User;
  19. use GuzzleHttp\Exception\GuzzleException;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpKernel\KernelInterface;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. /**
  28.  * @Route("/document")
  29.  */
  30. class DocumentController extends AbstractController
  31. {
  32.     use SentryNotifyTrait;
  33.     private SignatureServiceInterface $yousignAPI;
  34.     private EncryptorDataUtils $encryptor;
  35.     private KernelInterface $kernel;
  36.     private EntityManagerInterface $entityManager;
  37.     private UserChecker $userChecker;
  38.     private UserUtils $userUtils;
  39.     private DocumentUtils $documentUtils;
  40.     private OrganizationUtils $organizationUtils;
  41.     public function __construct(
  42.         SignatureServiceInterface $yousignAPI,
  43.         EncryptorDataUtils $encryptor,
  44.         EntityManagerInterface $entityManager,
  45.         UserChecker $userChecker,
  46.         UserUtils $userUtils,
  47.         DocumentUtils $documentUtils,
  48.         OrganizationUtils $organizationUtils
  49.     ) {
  50.         $this->yousignAPI $yousignAPI;
  51.         $this->encryptor $encryptor;
  52.         $this->entityManager $entityManager;
  53.         $this->userChecker $userChecker;
  54.         $this->userUtils $userUtils;
  55.         $this->documentUtils $documentUtils;
  56.         $this->organizationUtils $organizationUtils;
  57.     }
  58.     /**
  59.      * @Route("/get-all-types", name="app_document_get_all_types", methods={"GET"})
  60.      */
  61.     public function getAllTypes(TranslatorInterface $translator)
  62.     {
  63.         $documentTypes = [];
  64.         $documentUserEnums DocumentTypeUserEnum::getChoices();
  65.         $documentOrgaEnums DocumentTypeOrganizationEnum::getChoices();
  66.         $documentEnums array_merge($documentUserEnums$documentOrgaEnums);
  67.         foreach ($documentEnums as $key => $value) {
  68.             $documentTypes[$translator->trans($key)] = $value;
  69.         }
  70.         return $this->json($documentTypes);
  71.     }
  72.     /**
  73.      * @Route("/checked", name="app_document_checked", methods={"GET"})
  74.      */
  75.     public function checked(EntityManagerInterface $em)
  76.     {
  77.         $repository $em->getRepository(Document::class);
  78.         $repositoryOrganization $em->getRepository(Organization::class);
  79.         $aDocuments $repository->findBy(['organization' => null]);
  80.         $aDocsType DocumentTypeUserEnum::DOCUMENT_WITH_VALIDITY;
  81.         $aOrganizationStatus = [
  82.             OrganizationStatusEnum::NEW,
  83.             OrganizationStatusEnum::PRE_CANCELLATION,
  84.             OrganizationStatusEnum::CANCELLED,
  85.             OrganizationStatusEnum::LOST,
  86.         ];
  87.         $now strtotime(date('Y-m-d H:s:i'));
  88.         $count 0;
  89.         /** @var Document $document */
  90.         foreach ($aDocuments as $document) {
  91.             $aOrganizations $document->getPerson()->getLegalRepresentative()->getOrganizations()->toArray();
  92.             /** @var Organization $organization */
  93.             foreach ($aOrganizations as $organization) {
  94.                 if (in_array($document->getType(), $aDocsType) && !in_array($organization->getStatus(), $aOrganizationStatustrue)) {
  95.                     $expirationDate $document->getExpirationDate();
  96.                     if (null !== $expirationDate) {
  97.                         $expirationDate $expirationDate->getTimestamp();
  98.                         if ($expirationDate $now) {
  99.                             $organization $repositoryOrganization->find($organization->getId());
  100.                             $organization->setStatus(OrganizationStatusEnum::DOCS_EXPIRED);
  101.                             $em->persist($organization);
  102.                             ++$count;
  103.                         }
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         $em->flush();
  109.         return $this->json(['result' => 'successs''count' => $count]);
  110.     }
  111.     /**
  112.      * @Route("/view/{id}/{token}", name="app_document_view", methods={"GET"})
  113.      */
  114.     public function viewDocument(string $id, ?string $token null): Response
  115.     {
  116.         $repository $this->entityManager->getRepository(Document::class);
  117.         /** @var ?User $user */
  118.         $user $this->userChecker->checkUserByToken($token);
  119.         $decryptedId $this->encryptor->decrypt($id);
  120.         /** @var ?Document $document */
  121.         $document $repository->find($decryptedId);
  122.         if (!$user || !$document) {
  123.             return $this->json(['result' => 'Document not found'], Response::HTTP_NOT_FOUND);
  124.         }
  125.         if (in_array('ROLE_ADMIN'$user->getRoles(), true)) {
  126.             $this->documentUtils->viewDocument($document);
  127.         }
  128.         $organization $document->getOrganization();
  129.         $person $document->getPerson();
  130.         if (!$organization && null !== $person) {
  131.             $organization GetOrganizationFromUserAccess::process($person$user);
  132.             if (!$organization instanceof Organization) {
  133.                 return $this->json(['result' => 'Document not found'], Response::HTTP_NOT_FOUND);
  134.             }
  135.         }
  136.         $prescriberOrganizations $this->userUtils->getOrganizationPrescribersID($user);
  137.         if ($organization ||
  138.             ($organization->getPrescriber() && in_array($organization->getPrescriber()->getId(), $prescriberOrganizationstrue))) {
  139.             $this->documentUtils->viewDocument($document);
  140.         }
  141.         return $this->json(['message' => 'Document not found'], Response::HTTP_NOT_FOUND);
  142.     }
  143.     /**
  144.      * @Route("/download/{id}/{token}", name="app_document_dowload", methods={"GET"})
  145.      */
  146.     public function downloadDocument(string $id, ?string $token null): Response
  147.     {
  148.         $repository $this->entityManager->getRepository(Document::class);
  149.         /** @var ?User $user */
  150.         $user $this->userChecker->checkUserByToken($token);
  151.         $decryptedId $this->encryptor->decrypt($id);
  152.         /** @var ?Document $document */
  153.         $document $repository->find($decryptedId);
  154.         if (!$user || !$document) {
  155.             return $this->json(['result' => 'Document not found'], Response::HTTP_NOT_FOUND);
  156.         }
  157.         if (in_array('ROLE_ADMIN'$user->getRoles(), true)) {
  158.             $this->documentUtils->downloadDocument($document);
  159.         }
  160.         $organization $document->getOrganization();
  161.         $person $document->getPerson();
  162.         if (!$organization && null !== $person) {
  163.             $organization GetOrganizationFromUserAccess::process($person$user);
  164.             if (!$organization instanceof Organization) {
  165.                 return $this->json(['result' => 'Document not found'], Response::HTTP_NOT_FOUND);
  166.             }
  167.         }
  168.         $prescriberOrganizations $this->userUtils->getOrganizationPrescribersID($user);
  169.         if ($organization ||
  170.             ($organization->getPrescriber() && in_array($organization->getPrescriber()->getId(), $prescriberOrganizationstrue))) {
  171.             $this->documentUtils->downloadDocument($document);
  172.         }
  173.         return $this->json(['message' => 'Document not found'], Response::HTTP_NOT_FOUND);
  174.     }
  175.     /**
  176.      * @Route("/download/{id}", name="app_document_download_aws_file", methods={"GET"})
  177.      */
  178.     public function downloadAWSFile($idEntityManagerInterface $emDocumentUtils $documentUtils): JsonResponse
  179.     {
  180.         // @TODO: use flysystem and dynamic path
  181.         $repository $em->getRepository(Document::class);
  182.         $id $this->encryptor->decrypt($id);
  183.         $document $repository->find($id);
  184.         if (null !== $document) {
  185.             return $this->json(['result' => $documentUtils->downloadAWSDocument($document)], Response::HTTP_OK);
  186.         }
  187.         return $this->json(['result' => 'Document not found'], Response::HTTP_NOT_FOUND);
  188.     }
  189.     /**
  190.      * Check if is api can generate document.
  191.      *
  192.      * @Route("/{id}/is-able-to-generate", name="document_is_able_to_generate", methods={"GET"})
  193.      */
  194.     public function isAbleToGenerateDocument(Request $requestint $id): JsonResponse
  195.     {
  196.         $documentToGenerate $request->query->get('documentToGenerate');
  197.         $organization $this->entityManager->getRepository(Organization::class)->find($id);
  198.         $isAble false;
  199.         if ($organization && $documentToGenerate) {
  200.             $generateContract DocumentTypeOrganizationEnum::DOMICILIATION_CONTRACT === $documentToGenerate;
  201.             $documentsOtherToGenerate $this->organizationUtils->getDocsToGenerate($organization$generateContract);
  202.             if ($this->documentUtils->checkGenerateStatus($organization)) {
  203.                 $documentsOtherToGenerate[] = DocumentTypeOrganizationEnum::STATUS;
  204.             }
  205.             $isAble in_array($documentToGenerate$documentsOtherToGenerate);
  206.         }
  207.         return new JsonResponse(['isAble' => $isAble], Response::HTTP_OK);
  208.     }
  209.     /**
  210.      * Check if is PAPPERS can generate document.
  211.      *
  212.      * @Route("/{id}/is-pappers-able-to-generate", name="document_is_pappers_able_to_generate", methods={"GET"})
  213.      *
  214.      * @throws GuzzleException
  215.      */
  216.     public function isPappersAbleToGenerateDocument(Request $requestint $id): JsonResponse
  217.     {
  218.         $documentToGenerate $request->query->get('documentToGenerate');
  219.         $organization $this->entityManager->getRepository(Organization::class)->find($id);
  220.         $isDocumentCanBeGenerated false;
  221.         if ($organization && $organization->getSIRET() && $documentToGenerate) {
  222.             $isDocumentCanBeGenerated array_search($documentToGeneratePappersDocumentTypeOrganizationEnum::DOCUMENT_PAPPERS_ABLE_TO_GENERATEtrue);
  223.         }
  224.         return new JsonResponse(
  225.             [
  226.                 'isAble' => (bool) $isDocumentCanBeGenerated,
  227.             ],
  228.             Response::HTTP_OK
  229.         );
  230.     }
  231.     /**
  232.      * @Route("/{documentId}/get-link-sign", name="document_get_link_sign", methods={"GET"})
  233.      */
  234.     public function getLinkSign(string $documentId): JsonResponse
  235.     {
  236.         $docRepo $this->entityManager->getRepository(Document::class);
  237.         $document $docRepo->find($documentId);
  238.         if (!$document) {
  239.             return $this->json(['message' => 'Document not found'], Response::HTTP_NOT_FOUND);
  240.         }
  241.         $organization $document->getOrganization();
  242.         if (!$organization) {
  243.             return $this->json(['message' => 'Organization not found'], Response::HTTP_NOT_FOUND);
  244.         }
  245.         if (!$organization->getProcessYousign()) {
  246.             return $this->json(['message' => 'Process not found'], Response::HTTP_NOT_FOUND);
  247.         }
  248.         $signatureRequested $this->yousignAPI->getSignatureRequest($document->getProcessYousigns()->first()->getUniqueKey());
  249.         if (!$this->yousignAPI->isResponseValid($signatureRequested)) {
  250.             return $this->json(['message' => 'Link not found'], Response::HTTP_NOT_FOUND);
  251.         }
  252.         $signer $this->yousignAPI->getSigner($signatureRequested['id'], $signatureRequested['signers'][1]['id'] ?? $signatureRequested['signers'][0]['id']);
  253.         if (!$this->yousignAPI->isResponseValid($signer)) {
  254.             return $this->json(['message' => 'Link not found'], Response::HTTP_NOT_FOUND);
  255.         }
  256.         return $this->json(['link' => $signer['signature_link']]);
  257.     }
  258.     /**
  259.      * @Route("/{id}/is-exist", name="app_document_is_exist", methods={"GET"})
  260.      */
  261.     public function isDocumentExist(string $id): JsonResponse
  262.     {
  263.         $repository $this->entityManager->getRepository(Document::class);
  264.         $document $repository->find($id);
  265.         if (!$document) {
  266.             return $this->json(['result' => 'Document not found'], Response::HTTP_NOT_FOUND);
  267.         }
  268.         if (!$this->documentUtils->isDocumentExist($document)) {
  269.             return $this->json(['result' => 'Document not found'], Response::HTTP_NOT_FOUND);
  270.         }
  271.         return $this->json(['result' => true], Response::HTTP_OK);
  272.     }
  273. }