From a992c45720b521c97ce80cb0f0080f6d5909a986 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 23 Mar 2023 10:14:40 +0100 Subject: [PATCH] Feature: create a new API endpoint for my workflows in Cc --- .../Controller/WorkflowApiController.php | 39 +++++++++++++++++++ .../Workflow/EntityWorkflowRepository.php | 39 +++++++++++++++++++ .../ChillMainBundle/chill.api.specs.yaml | 16 ++++++++ 3 files changed, 94 insertions(+) diff --git a/src/Bundle/ChillMainBundle/Controller/WorkflowApiController.php b/src/Bundle/ChillMainBundle/Controller/WorkflowApiController.php index b451e9209..f0e728252 100644 --- a/src/Bundle/ChillMainBundle/Controller/WorkflowApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/WorkflowApiController.php @@ -93,6 +93,45 @@ class WorkflowApiController ); } + /** + * Return a list of workflow which are waiting an action for the user. + * + * @Route("/api/1.0/main/workflow/my-cc", methods={"GET"}) + */ + public function myWorkflowCc(Request $request): JsonResponse + { + if (!$this->security->isGranted('ROLE_USER') || !$this->security->getUser() instanceof User) { + throw new AccessDeniedException(); + } + + $total = $this->entityWorkflowRepository->countByCc($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->findByCc( + $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 + ); + } + /** * @Route("/api/1.0/main/workflow/{id}/subscribe", methods={"POST"}) */ diff --git a/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php b/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php index 1fc309d6e..a304ff6d7 100644 --- a/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php @@ -27,6 +27,13 @@ class EntityWorkflowRepository implements ObjectRepository $this->repository = $entityManager->getRepository(EntityWorkflow::class); } + public function countByCc(User $user): int + { + $qb = $this->buildQueryByCc($user)->select('count(ew)'); + + return (int) $qb->getQuery()->getSingleScalarResult(); + } + public function countByDest(User $user): int { $qb = $this->buildQueryByDest($user)->select('count(ew)'); @@ -103,6 +110,19 @@ class EntityWorkflowRepository implements ObjectRepository return $this->repository->findBy($criteria, $orderBy, $limit, $offset); } + public function findByCc(User $user, ?array $orderBy = null, $limit = null, $offset = null): array + { + $qb = $this->buildQueryByCc($user)->select('ew'); + + foreach ($orderBy as $key => $sort) { + $qb->addOrderBy('ew.' . $key, $sort); + } + + $qb->setMaxResults($limit)->setFirstResult($offset); + + return $qb->getQuery()->getResult(); + } + public function findByDest(User $user, ?array $orderBy = null, $limit = null, $offset = null): array { $qb = $this->buildQueryByDest($user)->select('ew'); @@ -165,6 +185,25 @@ class EntityWorkflowRepository implements ObjectRepository return EntityWorkflow::class; } + private function buildQueryByCc(User $user): QueryBuilder + { + $qb = $this->repository->createQueryBuilder('ew'); + + $qb->join('ew.steps', 'step'); + + $qb->where( + $qb->expr()->andX( + $qb->expr()->isMemberOf(':user', 'step.ccUser'), + $qb->expr()->isNull('step.transitionAfter'), + $qb->expr()->eq('step.isFinal', "'FALSE'") + ) + ); + + $qb->setParameter('user', $user); + + return $qb; + } + private function buildQueryByDest(User $user): QueryBuilder { $qb = $this->repository->createQueryBuilder('ew'); diff --git a/src/Bundle/ChillMainBundle/chill.api.specs.yaml b/src/Bundle/ChillMainBundle/chill.api.specs.yaml index 133345a6f..98e0e915e 100644 --- a/src/Bundle/ChillMainBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillMainBundle/chill.api.specs.yaml @@ -826,4 +826,20 @@ paths: $ref: '#/components/schemas/Workflow' 403: description: "Unauthorized" + /1.0/main/workflow/my-cc: + get: + tags: + - workflow + summary: Return a list of workflows for which user was notified in Cc + responses: + 200: + description: "ok" + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Workflow' + 403: + description: "Unauthorized"