From c8762d2bc2a617a90af61bec89c1539906dca707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 20 Oct 2021 13:10:28 +0200 Subject: [PATCH] list referral for an accompanying period --- .../Repository/UserRepository.php | 18 +++++++ .../Suggestion/ReferralAvailable.php | 31 ++++++++++++ .../Suggestion/ReferralAvailableInterface.php | 20 ++++++++ .../AccompanyingCourseApiController.php | 40 ++++++++++++++-- .../AccompanyingCourseApiControllerTest.php | 13 +++++ .../ChillPersonBundle/chill.api.specs.yaml | 48 +++++++++++++------ .../ChillPersonBundle/chill.webpack.config.js | 2 +- .../config/services/accompanyingPeriod.yaml | 7 +++ .../config/services/controller.yaml | 6 +-- 9 files changed, 161 insertions(+), 24 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/AccompanyingPeriod/Suggestion/ReferralAvailable.php create mode 100644 src/Bundle/ChillPersonBundle/AccompanyingPeriod/Suggestion/ReferralAvailableInterface.php diff --git a/src/Bundle/ChillMainBundle/Repository/UserRepository.php b/src/Bundle/ChillMainBundle/Repository/UserRepository.php index e5427325a..825a1600e 100644 --- a/src/Bundle/ChillMainBundle/Repository/UserRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/UserRepository.php @@ -48,6 +48,24 @@ final class UserRepository implements ObjectRepository return $this->repository->findBy($criteria, $orderBy, $limit, $offset); } + public function countBy(array $criteria): int + { + return $this->repository->count($criteria); + } + + public function countByActive(): int + { + return $this->countBy(['enabled' => true]); + } + + /** + * @return User[]|array + */ + public function findByActive(array $orderBy = null, int $limit = null, int $offset = null): array + { + return $this->findBy(['enabled' => true], $orderBy, $limit, $offset); + } + public function getClassName() { return User::class; } diff --git a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Suggestion/ReferralAvailable.php b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Suggestion/ReferralAvailable.php new file mode 100644 index 000000000..d70342610 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Suggestion/ReferralAvailable.php @@ -0,0 +1,31 @@ +userRepository = $userRepository; + } + + public function countReferralAvailable(AccompanyingPeriod $period, ?array $options = []): int + { + return $this->userRepository->countByActive(); + } + + /** + * @param AccompanyingPeriod $period + * @return array|User[] + */ + public function findReferralAvailable(AccompanyingPeriod $period, int $limit = 50, int $start = 0): array + { + return $this->userRepository->findByActive(); + } +} diff --git a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Suggestion/ReferralAvailableInterface.php b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Suggestion/ReferralAvailableInterface.php new file mode 100644 index 000000000..f064fff99 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Suggestion/ReferralAvailableInterface.php @@ -0,0 +1,20 @@ +eventDispatcher = $eventDispatcher; $this->validator = $validator; $this->registry = $registry; + $this->referralAvailable = $referralAvailable; } public function confirmApi($id, Request $request, $_format): Response { - /** @var AccompanyingPeriod $accompanyingPeriod */ + /** @var AccompanyingPeriod $accompanyingPeriod */ $accompanyingPeriod = $this->getEntity('participation', $id, $request); $this->checkACL('confirm', $request, $_format, $accompanyingPeriod); @@ -58,10 +68,10 @@ $workflow = $this->registry->get($accompanyingPeriod); 'groups' => [ 'read' ] ]); } - + public function participationApi($id, Request $request, $_format) { - /** @var AccompanyingPeriod $accompanyingPeriod */ + /** @var AccompanyingPeriod $accompanyingPeriod */ $accompanyingPeriod = $this->getEntity('participation', $id, $request); $person = $this->getSerializer() ->deserialize($request->getContent(), Person::class, $_format, []); @@ -152,7 +162,7 @@ $workflow = $this->registry->get($accompanyingPeriod); ->deserialize($request->getContent(), $class, $_format, []); } catch (RuntimeException $e) { $exceptions[] = $e; - } + } } if ($requestor === null) { throw new BadRequestException('Could not find any person or requestor', 0, $exceptions[0]); @@ -187,4 +197,24 @@ $workflow = $this->registry->get($accompanyingPeriod); return null; } + + /** + * @Route("/api/1.0/person/accompanying-course/{id}/referral-availables.{_format}", + * requirements={ "_format"="json"}, + * name="chill_api_person_accompanying_period_referral_available") + * @param AccompanyingPeriod $period + * @return JsonResponse + */ + public function userReferral(AccompanyingPeriod $period, string $_format = 'json'): JsonResponse + { + $this->denyAccessUnlessGranted(AccompanyingPeriodVoter::EDIT, $period); + + $total = $this->referralAvailable->countReferralAvailable($period); + $paginator = $this->getPaginatorFactory()->create($total); + $users = $this->referralAvailable->findReferralAvailable($period, $paginator->getItemsPerPage(), + $paginator->getCurrentPageFirstItemNumber()); + + return $this->json(new Collection($users, $paginator), Response::HTTP_OK, + [], [ AbstractNormalizer::GROUPS => [ 'read' ]]); + } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseApiControllerTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseApiControllerTest.php index a24e94bfa..80bb1d7cc 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseApiControllerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseApiControllerTest.php @@ -325,6 +325,19 @@ class AccompanyingCourseApiControllerTest extends WebTestCase $this->period = $period; } + /** + * @dataProvider dataGenerateRandomAccompanyingCourse + */ + public function testReferralAvailable(int $personId, int $periodId) + { + $this->client->request( + Request::METHOD_POST, + sprintf('/api/1.0/person/accompanying-course/%d/referral-availables.json', $periodId) + ); + + $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); + } + /** * * @dataProvider dataGenerateRandomAccompanyingCourse diff --git a/src/Bundle/ChillPersonBundle/chill.api.specs.yaml b/src/Bundle/ChillPersonBundle/chill.api.specs.yaml index 4041c52b7..a3621dac1 100644 --- a/src/Bundle/ChillPersonBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillPersonBundle/chill.api.specs.yaml @@ -445,7 +445,7 @@ paths: /1.0/person/accompanying-course/{id}.json: get: tags: - - person + - accompanying-course summary: "Return the description for an accompanying course (accompanying period)" parameters: - name: id @@ -532,7 +532,7 @@ paths: /1.0/person/accompanying-course/{id}/requestor.json: post: tags: - - person + - accompanying-course summary: "Add a requestor to the accompanying course" parameters: - name: id @@ -574,7 +574,7 @@ paths: description: "object with validation errors" delete: tags: - - person + - accompanying-course summary: "Remove the requestor for the accompanying course" parameters: - name: id @@ -598,7 +598,7 @@ paths: /1.0/person/accompanying-course/{id}/participation.json: post: tags: - - person + - accompanying-course summary: "Add a participant to the accompanying course" parameters: - name: id @@ -627,7 +627,7 @@ paths: description: "object with validation errors" delete: tags: - - person + - accompanying-course summary: "Remove the participant for the accompanying course" parameters: - name: id @@ -658,7 +658,7 @@ paths: /1.0/person/accompanying-course/{id}/resource.json: post: tags: - - person + - accompanying-course summary: "Add a resource to the accompanying course" parameters: - name: id @@ -703,7 +703,7 @@ paths: description: "object with validation errors" delete: tags: - - person + - accompanying-course summary: "Remove the resource" parameters: - name: id @@ -734,7 +734,7 @@ paths: /1.0/person/accompanying-course/{id}/comment.json: post: tags: - - person + - accompanying-course summary: "Add a comment to the accompanying course" parameters: - name: id @@ -772,7 +772,7 @@ paths: description: "object with validation errors" delete: tags: - - person + - accompanying-course summary: "Remove the comment" parameters: - name: id @@ -803,7 +803,7 @@ paths: /1.0/person/accompanying-course/{id}/scope.json: post: tags: - - person + - accompanying-course summary: "Add a scope to the accompanying course" parameters: - name: id @@ -837,7 +837,7 @@ paths: description: "object with validation errors" delete: tags: - - person + - accompanying-course summary: "Remove the scope" parameters: - name: id @@ -868,7 +868,7 @@ paths: /1.0/person/accompanying-course/{id}/socialissue.json: post: tags: - - person + - accompanying-course summary: "Add a social issue to the accompanying course" parameters: - name: id @@ -902,7 +902,7 @@ paths: description: "object with validation errors" delete: tags: - - person + - accompanying-course summary: "Remove the social issue" parameters: - name: id @@ -929,11 +929,31 @@ paths: description: "OK" 422: description: "object with validation errors" + /1.0/person/accompanying-course/{id}/referral-availables.json: + get: + tags: + - accompanying-course + summary: "get a list of available referral for a given accompanying cours" + parameters: + - name: id + in: path + required: true + description: The accompanying period's id + schema: + type: integer + format: integer + minimum: 1 + responses: + 401: + description: "Unauthorized" + 404: + description: "Not found" + 200: + description: "OK" /1.0/person/accompanying-course/{id}/work.json: post: tags: - - person - accompanying-course-work summary: "Add a work (AccompanyingPeriodwork) to the accompanying course" parameters: diff --git a/src/Bundle/ChillPersonBundle/chill.webpack.config.js b/src/Bundle/ChillPersonBundle/chill.webpack.config.js index 925a5ccf8..e8194b648 100644 --- a/src/Bundle/ChillPersonBundle/chill.webpack.config.js +++ b/src/Bundle/ChillPersonBundle/chill.webpack.config.js @@ -16,5 +16,5 @@ module.exports = function(encore, entries) encore.addEntry('page_household_edit_metadata', __dirname + '/Resources/public/page/household_edit_metadata/index.js'); encore.addEntry('page_person', __dirname + '/Resources/public/page/person/index.js'); encore.addEntry('page_accompanying_course_index_person_locate', __dirname + '/Resources/public/page/accompanying_course_index/person_locate.js'); - encore.addEntry('page_vis', __dirname + '/Resources/public/page/vis/index.js'); + //encore.addEntry('page_vis', __dirname + '/Resources/public/page/vis/index.js'); }; diff --git a/src/Bundle/ChillPersonBundle/config/services/accompanyingPeriod.yaml b/src/Bundle/ChillPersonBundle/config/services/accompanyingPeriod.yaml index 88e70c9a8..5b8639146 100644 --- a/src/Bundle/ChillPersonBundle/config/services/accompanyingPeriod.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/accompanyingPeriod.yaml @@ -13,3 +13,10 @@ services: entity: 'Chill\PersonBundle\Entity\AccompanyingPeriod' lazy: true method: preUpdateAccompanyingPeriod + + Chill\PersonBundle\AccompanyingPeriod\Suggestion\ReferralAvailable: + autowire: true + autoconfigure: true + + Chill\PersonBundle\AccompanyingPeriod\Suggestion\ReferralAvailableInterface: '@Chill\PersonBundle\AccompanyingPeriod\Suggestion\ReferralAvailable' + diff --git a/src/Bundle/ChillPersonBundle/config/services/controller.yaml b/src/Bundle/ChillPersonBundle/config/services/controller.yaml index 489168425..098313286 100644 --- a/src/Bundle/ChillPersonBundle/config/services/controller.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/controller.yaml @@ -41,10 +41,8 @@ services: tags: ['controller.service_arguments'] Chill\PersonBundle\Controller\AccompanyingCourseApiController: - arguments: - $eventDispatcher: '@Symfony\Contracts\EventDispatcher\EventDispatcherInterface' - $validator: '@Symfony\Component\Validator\Validator\ValidatorInterface' - $registry: '@Symfony\Component\Workflow\Registry' + autoconfigure: true + autowire: true tags: ['controller.service_arguments'] Chill\PersonBundle\Controller\PersonApiController: