From 89d3ab38f043d119b62166451d4ce74c4fbde48d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 14 Feb 2022 20:02:07 +0100 Subject: [PATCH] add endpoint to list workflows --- .../Controller/WorkflowApiController.php | 63 ++++++++++++++++++- .../ChillMainBundle/chill.api.specs.yaml | 21 +++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/WorkflowApiController.php b/src/Bundle/ChillMainBundle/Controller/WorkflowApiController.php index dda787708..afb85658b 100644 --- a/src/Bundle/ChillMainBundle/Controller/WorkflowApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/WorkflowApiController.php @@ -11,7 +11,12 @@ declare(strict_types=1); namespace Chill\MainBundle\Controller; +use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\Workflow\EntityWorkflow; +use Chill\MainBundle\Pagination\PaginatorFactory; +use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository; +use Chill\MainBundle\Serializer\Model\Collection; +use Chill\MainBundle\Serializer\Model\Counter; use Doctrine\ORM\EntityManagerInterface; use LogicException; use Symfony\Component\HttpFoundation\JsonResponse; @@ -21,17 +26,71 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\Security\Core\Security; +use Symfony\Component\Serializer\SerializerInterface; class WorkflowApiController { private EntityManagerInterface $entityManager; + private EntityWorkflowRepository $entityWorkflowRepository; + + private PaginatorFactory $paginatorFactory; + private Security $security; - public function __construct(Security $security, EntityManagerInterface $entityManager) - { + private SerializerInterface $serializer; + + public function __construct( + EntityManagerInterface $entityManager, + EntityWorkflowRepository $entityWorkflowRepository, + PaginatorFactory $paginatorFactory, + Security $security, + SerializerInterface $serializer + ) { $this->entityManager = $entityManager; + $this->entityWorkflowRepository = $entityWorkflowRepository; + $this->paginatorFactory = $paginatorFactory; $this->security = $security; + $this->serializer = $serializer; + } + + /** + * Return a list of workflow which are waiting an action for the user. + * + * @Route("/api/1.0/main/workflow/my", methods={"GET"}) + */ + public function myWorkflow(Request $request): JsonResponse + { + if (!$this->security->isGranted('ROLE_USER') || !$this->security->getUser() instanceof User) { + throw new AccessDeniedException(); + } + + $total = $this->entityWorkflowRepository->countByDest($this->security->getUser()); + + if ($request->query->getBoolean('countOnly', false)) { + return new JsonResponse( + $this->serializer->serialize(new Counter($total), 'json'), + JsonResponse::HTTP_OK, + [], + true + ); + } + + $paginator = $this->paginatorFactory->create($total); + + $workflows = $this->entityWorkflowRepository->findByDest( + $this->security->getUser(), + ['id' => 'DESC'], + $paginator->getItemsPerPage(), + $paginator->getCurrentPageFirstItemNumber() + ); + + return new JsonResponse( + $this->serializer->serialize(new Collection($workflows, $paginator), 'json', ['groups' => ['read']]), + JsonResponse::HTTP_OK, + [], + true + ); } /** diff --git a/src/Bundle/ChillMainBundle/chill.api.specs.yaml b/src/Bundle/ChillMainBundle/chill.api.specs.yaml index 034dfe74b..8129748a3 100644 --- a/src/Bundle/ChillMainBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillMainBundle/chill.api.specs.yaml @@ -125,6 +125,11 @@ components: type: object type: type: string + Workflow: + type: object + properties: + id: + type: integer paths: /1.0/search.json: @@ -793,4 +798,20 @@ paths: application/json: schema: $ref: '#/components/schemas/UserJob' + /1.0/main/workflow/my: + get: + tags: + - workflow + summary: Return a list of workflows awaiting for user's action + responses: + 200: + description: "ok" + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Workflow' + 403: + description: "Unauthorized"