src/Controller/AreaController.php line 264

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Constants\AppConstants;
  4. use App\DataTransformer\Heimdall\AreaOutputDataTransformer;
  5. use App\Exception\Heimdall\ContractNotFoundException;
  6. use App\Exception\Import\GeoJsonImportException;
  7. use App\Manager\Admin\AdminImportManagerInterface;
  8. use App\Manager\Admin\AdminManagerInterface;
  9. use App\Manager\AreaManagerInterface;
  10. use App\Manager\OrganizationManagerInterface;
  11. use App\Manager\UserManagerInterface;
  12. use App\Manager\HeimdallManagerInterface;
  13. use App\Models\Heimdall\Portal;
  14. use App\Models\Heimdall\Profile;
  15. use Psr\EventDispatcher\EventDispatcherInterface;
  16. use Swagger\Annotations as SWG;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  23. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  24. use App\Repository\HeimdallRepository;
  25. use App\Models\Heimdall\Contract as HeimdallContract;
  26. /**
  27.  * @SWG\Tag(name="Area")
  28.  */
  29. class AreaController extends AbstractController
  30. {
  31.     protected AreaManagerInterface $areaManager;
  32.     protected EventDispatcherInterface $eventDispatcher;
  33.     protected OrganizationManagerInterface $organizationManager;
  34.     protected HeimdallRepository $repository;
  35.     protected HeimdallManagerInterface $heimdallManager;
  36.     public const HEIMDALL_CONTRACT_URL '/api/contract/%s';
  37.     /**
  38.      * @param AreaManagerInterface $areaManager
  39.      * @param EventDispatcherInterface $eventDispatcher
  40.      * @param OrganizationManagerInterface $organizationManager
  41.      * @param HeimdallRepository $repository
  42.      */
  43.     public function __construct(
  44.         AreaManagerInterface $areaManager,
  45.         EventDispatcherInterface $eventDispatcher,
  46.         OrganizationManagerInterface $organizationManager,
  47.         HeimdallRepository $repository,
  48.         HeimdallManagerInterface $heimdallManager
  49.     )
  50.     {
  51.         $this->areaManager $areaManager;
  52.         $this->eventDispatcher $eventDispatcher;
  53.         $this->organizationManager $organizationManager;
  54.         $this->repository $repository;
  55.         $this->heimdallManager $heimdallManager;
  56.     }
  57.     /**
  58.      * @Route(
  59.      *     "/api/contract/{contractId}/areas",
  60.      *     name="get_contract_areas",
  61.      *     methods={"GET"}
  62.      * )
  63.      *
  64.      * @SWG\Get(
  65.      *     path="/api/contract/{contractId}/areas",
  66.      *     summary="Get contract areas",
  67.      *     tags={"Area"},
  68.      *
  69.      *     @SWG\Parameter(
  70.      *         name="contractId",
  71.      *         in="path",
  72.      *         required=true,
  73.      *         type="string",
  74.      *         description="Contract ID"
  75.      *     ),
  76.      *     @SWG\Parameter(
  77.      *         name="withGeoJson",
  78.      *         in="query",
  79.      *         description="If true, returns a GeoJSON FeatureCollection",
  80.      *         required=false,
  81.      *         default=false,
  82.      *         type="boolean"
  83.      *     ),
  84.      *     @SWG\Parameter(
  85.      *         name="currentPage",
  86.      *         in="query",
  87.      *         description="current page for pagination offset",
  88.      *         required=false,
  89.      *         default=1,
  90.      *         type="number"
  91.      *     ),
  92.      *     @SWG\Parameter(
  93.      *         name="itemsPerPage",
  94.      *         in="query",
  95.      *         description="Nb items per page for pagination",
  96.      *         required=false,
  97.      *         default=20,
  98.      *         type="number"
  99.      *     ),
  100.      *     @SWG\Parameter(
  101.      *         name="labelFilter",
  102.      *         in="query",
  103.      *         description="Filter on area label name",
  104.      *         required=false,
  105.      *         type="string"
  106.      *     ),
  107.      *      @SWG\Response(
  108.      *          response=200,
  109.      *          description="Everything is OK."
  110.      *      ),
  111.      *      @SWG\Response(
  112.      *          response=500,
  113.      *          description="Error."
  114.      *      )
  115.      *  )
  116.      *
  117.      * @throws ExceptionInterface
  118.      */
  119.     public function getClientAreas(Request $requestAdminManagerInterface $adminManagerstring $contractIdNormalizerInterface $normalizer): JsonResponse
  120.     {
  121.         $this->denyAccessUnlessGranted(Profile::PROFILE_SUPER_ADMIN$contractId);
  122.         $currentPage  $request->get('currentPage'1);
  123.         $itemsPerPage $request->get('itemsPerPage'20);
  124.         $labelFilter $request->get('labelFilter');
  125.         $withGeoJson  $request->query->getBoolean('withGeoJson'false);
  126.         $transformer  = new AreaOutputDataTransformer();
  127.         if ($withGeoJson) {
  128.             $urlHeimdall sprintf(static::HEIMDALL_CONTRACT_URL$contractId);
  129.             $heimdallContract $this->repository->getEntity($urlHeimdallHeimdallContract::class);
  130.             $organizations $this->heimdallManager->getOrganizationsByContract($heimdallContract);
  131.             $featureCollection $this->areaManager->getOrganizationsAreasGeoJson($organizationstrue);
  132.             return new JsonResponse($normalizer->normalize($featureCollection), Response::HTTP_OK);
  133.         }
  134.         list($areas$pagination) = $adminManager->getClientAreas($contractId$currentPage$itemsPerPage$labelFilter);
  135.         $areas array_map(
  136.             function ($area) use ($transformer) {
  137.                 return $transformer->transform($area);
  138.             },
  139.             $areas
  140.         );
  141.         return new JsonResponse($normalizer->normalize($areas), Response::HTTP_OK$pagination);
  142.     }
  143.     /**
  144.      * @Route("/api/contract/{contractId}/area/{areaId}", name="get_contract_area", methods={"Get"})
  145.      *
  146.      * @SWG\Get (
  147.      *      path="/api/contract/{contractId}/area/{areaId}",
  148.      *      summary="Get contract area by id",
  149.      *      tags={"Area"},
  150.      *      @SWG\Response(
  151.      *          response=200,
  152.      *          description="Everything is OK."
  153.      *      ),
  154.      *      @SWG\Response(
  155.      *          response=500,
  156.      *          description="Error."
  157.      *      )
  158.      *  )
  159.      *
  160.      * @throws ExceptionInterface
  161.      */
  162.     public function getContractArea(AreaManagerInterface $areaManagerNormalizerInterface $normalizerstring $contractIdstring $areaId): JsonResponse
  163.     {
  164.         $this->denyAccessUnlessGranted(Profile::PROFILE_SUPER_ADMIN$contractId);
  165.         $contract $areaManager->getArea($areaId);
  166.         return new JsonResponse($normalizer->normalize($contract'json', ['groups' => 'front']));
  167.     }
  168.     /**
  169.      * @Route("/api/contract/{contractId}/areas/skillset", name="get_areas_skillsets", methods={"Get"})
  170.      *
  171.      * @SWG\Get (
  172.      *      path="/api/contract/{contractId}/areas/skillset",
  173.      *      summary="Get area skillset for multiple areas",
  174.      *      tags={"Area"},
  175.      *      @SWG\Parameter(
  176.      *          name="countOnly",
  177.      *          in="query",
  178.      *          description="if true, will return only the number of skillsets for each Area instead of the full list.",
  179.      *          type="boolean"
  180.      *     ),
  181.      *      @SWG\Parameter(
  182.      *          name="areas[]",
  183.      *          in="query",
  184.      *          description="list of all the areas id we want SkillSets for",
  185.      *          type="array",
  186.      *          @SWG\Items(type="string"),
  187.      *          collectionFormat="multi"
  188.      *     ),
  189.      *      @SWG\Response(
  190.      *          response=200,
  191.      *          description="Everything is OK."
  192.      *      ),
  193.      *      @SWG\Response(
  194.      *          response=500,
  195.      *          description="Error."
  196.      *      )
  197.      *  )
  198.      */
  199.     public function getAreasSkillSets(Request $requestAreaManagerInterface $areaManagerstring $contractId): JsonResponse
  200.     {
  201.         $this->denyAccessUnlessGranted(Profile::PROFILE_SUPER_ADMIN$contractId);
  202.         $areas     $request->get('areas', []);
  203.         $countOnly filter_var($request->get('countOnly'false), FILTER_VALIDATE_BOOLEAN);
  204.         $categories $areaManager->getAreasCategories($areas$countOnly);
  205.         return new JsonResponse($categories);
  206.     }
  207.     /**
  208.      * @Route("/api/contract/{contractId}/areas/organization", name="get_areas_organizations", methods={"Get"})
  209.      *
  210.      * @SWG\Get (
  211.      *      path="/api/contract/{contractId}/areas/organization",
  212.      *      summary="Get area organizations for multiple areas",
  213.      *      tags={"Area"},
  214.      *      @SWG\Parameter(
  215.      *          name="countOnly",
  216.      *          in="query",
  217.      *          description="if true, will return only the number of organizations for each Area instead of the full list.",
  218.      *          type="boolean"
  219.      *     ),
  220.      *      @SWG\Parameter(
  221.      *          name="areas[]",
  222.      *          in="query",
  223.      *          description="list of all the areas id we want Organizations for",
  224.      *          type="array",
  225.      *          @SWG\Items(type="string"),
  226.      *          collectionFormat="multi"
  227.      *     ),
  228.      *     @SWG\Parameter(
  229.      *         name="organizationType",
  230.      *         in="query",
  231.      *         description="Filter by organization type",
  232.      *         required=false,
  233.      *         type="string",
  234.      *         enum={"Community", "ExternalEntity", "Operator"}
  235.      *     ),
  236.      *      @SWG\Response(
  237.      *          response=200,
  238.      *          description="Everything is OK."
  239.      *      ),
  240.      *      @SWG\Response(
  241.      *          response=500,
  242.      *          description="Error."
  243.      *      )
  244.      *  )
  245.      */
  246.     public function getAreasOrganizations(Request $requestAreaManagerInterface $areaManagerstring $contractId): JsonResponse
  247.     {
  248.         $this->denyAccessUnlessGranted(Profile::PROFILE_SUPER_ADMIN$contractId);
  249.         $areas            $request->get('areas', []);
  250.         $organizationType $request->get('organizationType');
  251.         $countOnly        filter_var($request->get('countOnly'false), FILTER_VALIDATE_BOOLEAN);
  252.         $organizations $areaManager->getAreasOrganizations($areas$organizationType$countOnly);
  253.         return new JsonResponse($organizations);
  254.     }
  255.     /**
  256.      * @Route("/api/contract/{contractId}/contractArea", name="create_contract_area_from_administrative_divisions", methods={"POST"})
  257.      *
  258.      * @SWG\Post (
  259.      *      path="/api/contract/{contractId}/contractArea",
  260.      *      tags={"Area"},
  261.      *      summary="Create multiple areas on mdm and heimdall + contractAreas on Heimdall based on a list of administratives divisions",
  262.      *     @SWG\Parameter(
  263.      *         name="administrativeDivisions",
  264.      *         in="body",
  265.      *     @SWG\Schema(
  266.      *           type="array",
  267.      *           @SWG\Items(type="string")
  268.      *         )
  269.      *     ),
  270.      *      @SWG\Response(
  271.      *          response=200,
  272.      *          description="location in the header"
  273.      *      )
  274.      *  )
  275.      */
  276.     public function createAreasFromAdministrativeDivision(
  277.         Request $request,
  278.         AdminManagerInterface $adminManager,
  279.         string $contractId
  280.     ): JsonResponse {
  281.         $this->denyAccessUnlessGranted(Profile::PROFILE_SUPER_ADMINPortal::PORTAL_VILLAGILE);
  282.         $administrativeDivisions json_decode($request->getContent());
  283.         $adminManager->createAreaFromAdministrativeDivisions($contractId$administrativeDivisions);
  284.         return new JsonResponse(nullResponse::HTTP_OK);
  285.     }
  286.     /**
  287.      * @Route("/api/contract/{contractId}/contractArea/import", name="create_contract_area_from_geojson", methods={"POST"})
  288.      *
  289.      * @SWG\Post (
  290.      *      path="/api/contract/{contractId}/contractArea/import",
  291.      *      tags={"Area"},
  292.      *      summary="Create multiple areas on mdm and heimdall + contractAreas on Heimdall based on a GeoJSON file upload",
  293.      *      @SWG\Parameter(
  294.      *          name="file",
  295.      *          in="formData",
  296.      *          type="file",
  297.      *          description="GeoJSON file",
  298.      *          required=true
  299.      *      ),
  300.      *      @SWG\Parameter(
  301.      *          name="areasPropertiesName",
  302.      *          in="formData",
  303.      *          type="string",
  304.      *          description="Name of attribute in GeoJSON properties to use as Area name",
  305.      *          required=true
  306.      *      ),
  307.      *      @SWG\Parameter(
  308.      *          name="postalCodePropertiesName",
  309.      *          in="formData",
  310.      *          type="string",
  311.      *          description="Name of attribute in GeoJSON properties to use as Postal Code value",
  312.      *          required=false
  313.      *      ),
  314.      *      @SWG\Response(
  315.      *          response=200,
  316.      *          description="location in the header"
  317.      *      )
  318.      *  )
  319.      */
  320.     public function createAreasFromGeoJsonFile(
  321.         Request $request,
  322.         AdminImportManagerInterface $adminImportManager,
  323.         string $contractId
  324.     ): JsonResponse {
  325.         $this->denyAccessUnlessGranted(Profile::PROFILE_SUPER_ADMINPortal::PORTAL_VILLAGILE);
  326.         $file $request->files->get('file');
  327.         if (!$file) {
  328.             return new JsonResponse(['error' => 'No file provided'], Response::HTTP_BAD_REQUEST);
  329.         }
  330.         $areasPropertiesName $request->request->get('areasPropertiesName');
  331.         if (!$areasPropertiesName) {
  332.             return new JsonResponse(['error' => 'No areasPropertiesName provided'], Response::HTTP_BAD_REQUEST);
  333.         }
  334.         $postalCodePropertiesName $request->request->get('postalCodePropertiesName');
  335.         try {
  336.             $adminImportManager->importGeoJsonFile($contractId$file->getPathname(), $areasPropertiesName$postalCodePropertiesName);
  337.         } catch (ContractNotFoundException $e) {
  338.             return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_NOT_FOUND);
  339.         } catch (GeoJsonImportException $e) {
  340.             return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
  341.         } catch (\Exception $e) {
  342.             return new JsonResponse(['error' => 'An unexpected error occurred'], Response::HTTP_INTERNAL_SERVER_ERROR);
  343.         }
  344.         return new JsonResponse("Success created"Response::HTTP_CREATED);
  345.     }
  346.     /**
  347.      * @Route("/api/organization/{organizationId}/area", name="get_organization_areas", methods={"Get"})
  348.      *
  349.      * @SWG\Get (
  350.      *      path="/api/organization/{organizationId}/area",
  351.      *      summary="Get organization areas",
  352.      *      tags={"Area"},
  353.      *     @SWG\Parameter(
  354.      *         name="currentPage",
  355.      *         in="query",
  356.      *         description="current page for pagination offset",
  357.      *         required=false,
  358.      *         default=1,
  359.      *         type="number"
  360.      *     ),
  361.      *     @SWG\Parameter(
  362.      *         name="itemsPerPage",
  363.      *         in="query",
  364.      *         description="Nb items per page for pagination",
  365.      *         required=false,
  366.      *         default=20,
  367.      *         type="number"
  368.      *     ),
  369.      *      @SWG\Response(
  370.      *          response=200,
  371.      *          description="Everything is OK."
  372.      *      ),
  373.      *      @SWG\Response(
  374.      *          response=500,
  375.      *          description="Error."
  376.      *      )
  377.      *  )
  378.      *
  379.      * @throws ExceptionInterface
  380.      */
  381.     public function getAreasByOrganization(Request $requestAreaManagerInterface $areaManagerstring $organizationIdNormalizerInterface $normalizer): JsonResponse
  382.     {
  383.         $this->denyAccessUnlessGranted(Profile::PROFILE_SUPER_ADMIN$organizationId);
  384.         $currentPage  $request->get('currentPage'1);
  385.         $itemsPerPage $request->get('itemsPerPage'20);
  386.         list($areas$pagination) = $areaManager->getOrganizationAreas($organizationId$currentPage$itemsPerPage);
  387.         return new JsonResponse(
  388.             $normalizer->normalize($areas'json', ['groups' => 'front']),
  389.             Response::HTTP_OK,
  390.             $pagination
  391.         );
  392.     }
  393.     /**
  394.      * @Route("/api/user/{userId}/area", name="get_user_areas", methods={"Get"})
  395.      *
  396.      * @SWG\Get (
  397.      *      path="/api/user/{userId}/area",
  398.      *      summary="Get areas of all user Organizations",
  399.      *      tags={"Area"},
  400.      *      @SWG\Response(
  401.      *          response=200,
  402.      *          description="Everything is OK."
  403.      *      ),
  404.      *      @SWG\Response(
  405.      *          response=500,
  406.      *          description="Error."
  407.      *      )
  408.      *  )
  409.      *
  410.      * @throws ExceptionInterface
  411.      */
  412.     public function getUserAreas(AreaManagerInterface $areaManagerstring $userIdNormalizerInterface $normalizer): JsonResponse
  413.     {
  414.         $areas $areaManager->getUserAreas($userId);
  415.         return new JsonResponse($normalizer->normalize($areas'json', ['groups' => 'front']), Response::HTTP_OK);
  416.     }
  417.         /**
  418.      * @Route("/api/user/{userId}/contract/{contractId}/area", name="get_user_areas_by_contract", methods={"Get"})
  419.      *
  420.      * @SWG\Get (
  421.      *      path="/api/user/{userId}/contract/{contractId}/area",
  422.      *      summary="Get areas of all user Organizations by contract",
  423.      *      tags={"Area"},
  424.      *      @SWG\Response(
  425.      *          response=200,
  426.      *          description="Everything is OK."
  427.      *      ),
  428.      *      @SWG\Response(
  429.      *          response=500,
  430.      *          description="Error."
  431.      *      )
  432.      *  )
  433.      *
  434.      * @throws ExceptionInterface
  435.      */
  436.     public function getUserAreasByContract(string $userIdstring $contractIdNormalizerInterface $normalizer): JsonResponse
  437.     {
  438.         $userOrganizations $this->organizationManager->getUsersOrganizationsByContract($contractId$userId);
  439.         $areas = [];
  440.         foreach ($userOrganizations as $organizationId) {
  441.             list($organizationAreas$pagination) = $this->areaManager->getOrganizationAreas($organizationIdAppConstants::DEFAULT_CURRENT_PAGEAppConstants::DEFAULT_ALL_ITEMS_PER_PAGE);
  442.             $areas array_merge($areas$organizationAreas);
  443.         }
  444.         return new JsonResponse($normalizer->normalize($areas'json', ['groups' => 'front']), Response::HTTP_OK);
  445.     }
  446.     /**
  447.      * @Route("/api/user/{userId}/geoshape", name="get_user_geoshapes", methods={"Get"})
  448.      *
  449.      * @SWG\Get (
  450.      *     path="/api/user/{userId}/geoshape",
  451.      *     summary="Get user organizations geoshape",
  452.      *     tags={"Area"},
  453.      *     @SWG\Parameter(
  454.      *         name="serviceCode",
  455.      *         in="query",
  456.      *         description="Filter by service activated on Organization",
  457.      *         type="string"
  458.      *     ),
  459.      *     @SWG\Parameter(
  460.      *          name="contractId",
  461.      *          in="query",
  462.      *          description="User contract id",
  463.      *          type="string"
  464.      *     ),
  465.      *     @SWG\Parameter(
  466.      *          name="merged",
  467.      *          in="query",
  468.      *          description="Merge in multipolygon",
  469.      *          type="boolean"
  470.      *     ),
  471.      *     @SWG\Response(
  472.      *          response=200,
  473.      *          description="Everything is OK."
  474.      *     ),
  475.      *     @SWG\Response(
  476.      *          response=500,
  477.      *          description="Error."
  478.      *     )
  479.      *  )
  480.      *
  481.      * @throws ExceptionInterface
  482.      */
  483.     public function getUserGeoShapes(
  484.         string $userId,
  485.         AreaManagerInterface $areaManager,
  486.         NormalizerInterface $normalizer,
  487.         Request $request,
  488.         UserManagerInterface $userManager
  489.     ): JsonResponse {
  490.         $serviceCode $request->query->get('serviceCode''');
  491.         $merged      $request->query->getBoolean('merged');
  492.         $contractId  $request->query->get('contractId');
  493.         $user $userManager->getUser($userId);
  494.         $geoShapes $areaManager->getUserGeoJsonByService($user, [$serviceCode], $contractId);
  495.         if ($merged) {
  496.             $data $geoShapes json_decode($geoShapestrue): [];
  497.         } else {
  498.             $data $normalizer->normalize($geoShapes'json', ['groups' => 'front']);
  499.         }
  500.         return new JsonResponse($dataResponse::HTTP_OK);
  501.     }
  502.     /**
  503.      * @Route("/api/contract/{contractId}/area/{areaId}", name="delete_contract_area_id", methods={"DELETE"})
  504.      *
  505.      * @SWG\Delete (
  506.      *      path="/api/contract/{contractId}/area/{areaId}",
  507.      *      summary="delete contract area id",
  508.      *      tags={"Area"},
  509.      *      @SWG\Response(
  510.      *          response=204,
  511.      *          description="Area is deleted."
  512.      *      ),
  513.      *      @SWG\Response(
  514.      *          response=500,
  515.      *          description="Error server."
  516.      *      )
  517.      * )
  518.      *
  519.      * @throws ExceptionInterface
  520.      * @return Response
  521.      */
  522.     public function deleteArea(Request $requeststring $contractIdstring $areaId): Response
  523.     {
  524.         $this->areaManager->deleteArea($areaId);
  525.         return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  526.     }
  527. }