From 45c608cd4832931c29cd7913a83099ee1c5aa3e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 18:55:25 +0100 Subject: [PATCH 01/11] AddPerson: optimize request while searching * wait for a person to finish to type: delay before launching request; * cancel previous requests, if any --- .../Resources/public/vuejs/_api/AddPersons.js | 38 ++++++++++++------- .../public/vuejs/_components/AddPersons.vue | 32 ++++++++++------ 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_api/AddPersons.js b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_api/AddPersons.js index cf2404288..d72a42b15 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_api/AddPersons.js +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_api/AddPersons.js @@ -3,44 +3,54 @@ */ const parametersToString = ({ query, options }) => { let types =''; - options.type.forEach(function(type) { + options.type.forEach(function(type) { types += '&type[]=' + type; - }); + }); return 'q=' + query + types; }; -/* +/* * Endpoint chill_person_search * method GET, get a list of persons -* +* * @query string - the query to search for +* @deprecated */ -const searchPersons = ({ query, options }) => { +const searchPersons = ({ query, options }, signal) => { + console.err('deprecated'); let queryStr = parametersToString({ query, options }); let url = `/fr/search.json?name=person_regular&${queryStr}`; - return fetch(url) + let fetchOpts = { + method: 'GET', + headers: { + 'Content-Type': 'application/json;charset=utf-8' + }, + signal, + }; + + return fetch(url, fetchOpts) .then(response => { if (response.ok) { return response.json(); } - throw Error('Error with request resource response'); + throw Error('Error with request resource response'); }); }; -/* +/* * Endpoint v.2 chill_main_search_global * method GET, get a list of persons and thirdparty -* -* NOTE: this is a temporary WIP endpoint, return inconsistent random results -* @query string - the query to search for +* +* @param query string - the query to search for +* */ -const searchPersons_2 = ({ query, options }) => { +const searchEntities = ({ query, options }, signal) => { let queryStr = parametersToString({ query, options }); let url = `/api/1.0/search.json?${queryStr}`; return fetch(url) .then(response => { if (response.ok) { return response.json(); } - throw Error('Error with request resource response'); + throw Error('Error with request resource response'); }); }; -export { searchPersons, searchPersons_2 }; +export { searchPersons, searchEntities }; diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/AddPersons.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/AddPersons.vue index e2415d1e4..f118671ed 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/AddPersons.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/AddPersons.vue @@ -90,7 +90,7 @@ import Modal from 'ChillMainAssets/vuejs/_components/Modal'; import OnTheFly from 'ChillMainAssets/vuejs/OnTheFly/components/OnTheFly.vue'; import PersonSuggestion from './AddPersons/PersonSuggestion'; -import { searchPersons, searchPersons_2 } from 'ChillPersonAssets/vuejs/_api/AddPersons'; +import { searchEntities } from 'ChillPersonAssets/vuejs/_api/AddPersons'; import { postPerson } from "ChillPersonAssets/vuejs/_api/OnTheFly"; import { postThirdparty } from "ChillThirdPartyAssets/vuejs/_api/OnTheFly"; @@ -115,6 +115,8 @@ export default { }, search: { query: "", + previousQuery: "", + currentSearchQueryController: null, suggested: [], selected: [], priorSuggestion: {} @@ -189,16 +191,24 @@ export default { }, setQuery(query) { this.search.query = query; - if (query.length >= 3) { - searchPersons_2({ query, options: this.options }) - .then(suggested => new Promise((resolve, reject) => { - //console.log('suggested', suggested); - this.loadSuggestions(suggested.results); - resolve(); - })); - } else { - this.loadSuggestions([]); - } + + setTimeout(function() { + if (query === "") { + this.loadSuggestions([]); + return; + } + if (query === this.search.query) { + if (this.currentSearchQueryController !== undefined) { + this.currentSearchQueryController.abort() + } + this.currentSearchQueryController = new AbortController(); + searchEntities({ query, options: this.options }, this.currentSearchQueryController) + .then(suggested => new Promise((resolve, reject) => { + this.loadSuggestions(suggested.results); + resolve(); + })); + } + }.bind(this), query.length > 3 ? 300 : 700); }, loadSuggestions(suggested) { this.search.suggested = suggested; From 46ecab24c2dfd36249b08ce78ebe81b420ec8b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 19:34:12 +0100 Subject: [PATCH 02/11] update changelog --- CHANGELOG.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3637de1b5..db1a07606 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ and this project adheres to ## Unreleased +* AddAddress: optimize loading: wait for the user finish typing; +* UserPicker: fix bug with deprecated role + +## Test releases + +### test release 2021-12-14 + * [asideactivity] creation of aside activity category fixed (https://gitlab.com/champs-libres/departement-de-la-vendee/accent-suivi-developpement/-/issues/262) * [vendee/person] fix typo "situation professionelle" => "situation professionnelle" * [main] add availableForUsers condition from locationType in the location API endpoint (champs-libres/departement-de-la-vendee/accent-suivi-developpement#248) @@ -23,8 +30,6 @@ and this project adheres to * [acompanyingCourse] add initial comment on Resume page * [person] create button full width (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/330) -## Test releases - ### test release 2021-12-11 * [main] add order field to civility From 75dda5b999735ecc9c95afe3cf1d444f75b941df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 22:05:55 +0100 Subject: [PATCH 03/11] userpicker: fix role type: adapt on both allowed types --- .../Form/Type/UserPickerType.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/Type/UserPickerType.php b/src/Bundle/ChillMainBundle/Form/Type/UserPickerType.php index 0141438f6..c9a644aa9 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/UserPickerType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/UserPickerType.php @@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\User; use Chill\MainBundle\Repository\UserACLAwareRepositoryInterface; use Chill\MainBundle\Repository\UserRepository; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; +use Chill\MainBundle\Templating\Entity\UserRender; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\Options; @@ -47,16 +48,20 @@ class UserPickerType extends AbstractType protected UserRepository $userRepository; + private UserRender $userRender; + public function __construct( AuthorizationHelper $authorizationHelper, TokenStorageInterface $tokenStorage, UserRepository $userRepository, - UserACLAwareRepositoryInterface $userACLAwareRepository + UserACLAwareRepositoryInterface $userACLAwareRepository, + UserRender $userRender ) { $this->authorizationHelper = $authorizationHelper; $this->tokenStorage = $tokenStorage; $this->userRepository = $userRepository; $this->userACLAwareRepository = $userACLAwareRepository; + $this->userRender = $userRender; } public function configureOptions(OptionsResolver $resolver) @@ -74,14 +79,19 @@ class UserPickerType extends AbstractType ->setAllowedTypes('having_permissions_group_flag', ['string', 'null']) ->setDefault('class', User::class) ->setDefault('placeholder', 'Choose an user') - ->setDefault('choice_label', static function (User $u) { - return $u->getUsername(); + ->setDefault('choice_label', function (User $u) { + return $this->userRender->renderString($u, []); }) ->setDefault('scope', null) ->setAllowedTypes('scope', [Scope::class, 'array', 'null']) ->setNormalizer('choices', function (Options $options) { + if ($options['role'] instanceof Role) { + $role = $options['role']->getRole(); + } else { + $role = $options['role']; + } $users = $this->userACLAwareRepository - ->findUsersByReachedACL($options['role']->getRole(), $options['center'], $options['scope'], true); + ->findUsersByReachedACL($role, $options['center'], $options['scope'], true); if (null !== $options['having_permissions_group_flag']) { return $this->userRepository From 44d38bcef5f744e6303b0053d65d57935db83dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 22:06:18 +0100 Subject: [PATCH 04/11] fix typing for email --- src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 06b0dddbc..8b2dc6aca 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -409,7 +409,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface * * @return string|null */ - public function getEmail() + public function getEmail(): ?string { return $this->email; } From 6501a0148e9fa4ccdc48a3b7ef87ddef42debab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 22:14:13 +0100 Subject: [PATCH 05/11] fix downloading document --- .../public/module/async_upload/downloader.js | 37 ++++++------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/src/Bundle/ChillDocStoreBundle/Resources/public/module/async_upload/downloader.js b/src/Bundle/ChillDocStoreBundle/Resources/public/module/async_upload/downloader.js index b8abcae62..4c82a8e4b 100644 --- a/src/Bundle/ChillDocStoreBundle/Resources/public/module/async_upload/downloader.js +++ b/src/Bundle/ChillDocStoreBundle/Resources/public/module/async_upload/downloader.js @@ -34,9 +34,6 @@ var download = (button) => { key, url ; - console.log('keyData', keyData); - console.log('ivData', ivData); - button.textContent = labelPreparing; window.fetch(urlGenerator) @@ -48,36 +45,25 @@ var download = (button) => { } }) .then(data => { - url = data.url; - - if (keyData.length > 0) { - return window.crypto.subtle.importKey('jwk', keyData, { name: algo, iv: iv}, false, ['decrypt']); - } - return Promise.resolve(undefined); + return window.fetch(data.url); }) - .then(nKey => { - key = nKey; - - return window.fetch(url); - }) - .then(r => { - console.log('r', r); - if (r.ok) { - return r.arrayBuffer(); - } else { - throw new Error(r.status + r.statusText); + .then(response => { + if (response.ok) { + return response.arrayBuffer(); } + throw new Error(response.status + response.statusText); }) .then(buffer => { - console.log('buffer', buffer); - if (keyData.length > 0) { - return window.crypto.subtle.decrypt({ name: algo, iv: iv }, key, buffer); + if (keyData.alg !== undefined) { + return window.crypto.subtle + .importKey('jwk', keyData, { name: algo, iv: iv}, false, ['decrypt']) + .then(key => { + return window.crypto.subtle.decrypt({ name: algo, iv: iv }, key, buffer); + }); } - return Promise.resolve(buffer); }) .then(decrypted => { - console.log('decrypted', decrypted); var blob = new Blob([decrypted], { type: mimeType }), url = window.URL.createObjectURL(blob) @@ -96,7 +82,6 @@ var download = (button) => { button.click(); }) .catch(error => { - console.log(error); button.textContent = ""; button.appendChild(document.createTextNode("error while handling decrypted file")); }) From 17a81d7e666723b42b54df7a7c6022339eeb50b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 23:05:25 +0100 Subject: [PATCH 06/11] build base context data --- .../Service/Context/BaseContextData.php | 53 +++++++++++++++ .../config/services.yaml | 5 ++ .../Service/Context/BaseContextDataTest.php | 66 +++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php new file mode 100644 index 000000000..0194b8d08 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php @@ -0,0 +1,53 @@ +security = $security; + $this->normalizer = $normalizer; + } + + public function getData(): array + { + $data = []; + $user = $this->security->getUser(); + + $data['creator'] = $this->normalizer->normalize( + $user instanceof User ? $user : null, + 'docgen', + ['docgen:expects' => User::class, 'groups' => ['docgen:read']] + ); + $data['createdAt'] = $this->normalizer->normalize(new DateTimeImmutable(), 'docgen', [ + 'docgen:expects' => DateTimeImmutable::class, 'groups' => ['docgen:read'], + ]); + $data['location'] = $this->normalizer->normalize( + $user instanceof User ? $user->getCurrentLocation() : null, + 'docgen', + ['docgen:expects' => Location::class, 'groups' => ['docgen:expects']] + ); + + return $data; + } +} diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 54bf5a697..5bdfe2a11 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -34,6 +34,11 @@ services: autowire: true autoconfigure: true + Chill\DocGeneratorBundle\Service\Context\: + resource: "../Service/Context/" + autowire: true + autoconfigure: true + Chill\DocGeneratorBundle\GeneratorDriver\: resource: "../GeneratorDriver/" autowire: true diff --git a/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php new file mode 100644 index 000000000..8496bdda1 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php @@ -0,0 +1,66 @@ +buildBaseContext(); + + $actual = $context->getData(); + + $this->assertIsArray($actual); + $this->assertArrayHasKey('creator', $actual); + $this->assertArrayHasKey('createdAt', $actual); + } + + public function testGenerateWithUser() + { + $security = $this->prophesize(Security::class); + $security->getUser()->willReturn(new User()); + + $context = $this->buildBaseContext($security->reveal()); + + $actual = $context->getData(); + + $this->assertIsArray($actual); + $this->assertArrayHasKey('creator', $actual); + $this->assertArrayHasKey('createdAt', $actual); + } + + private function buildBaseContext( + ?Security $security = null, + ?NormalizerInterface $normalizer = null + ): BaseContextData { + return new BaseContextData( + $security ?? self::$container->get(Security::class), + $normalizer ?? self::$container->get(NormalizerInterface::class) + ); + } +} From 689f61238202d94fe672cc9502a8887755abbe70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 23:05:37 +0100 Subject: [PATCH 07/11] fix cs --- .../Controller/LocationApiController.php | 8 ++++---- src/Bundle/ChillMainBundle/Entity/Location.php | 12 ++++++------ src/Bundle/ChillMainBundle/Entity/LocationType.php | 5 +++-- .../ChillThirdPartyBundle/Entity/ThirdParty.php | 2 -- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/LocationApiController.php b/src/Bundle/ChillMainBundle/Controller/LocationApiController.php index aa5f46f1a..250d92980 100644 --- a/src/Bundle/ChillMainBundle/Controller/LocationApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/LocationApiController.php @@ -25,10 +25,10 @@ class LocationApiController extends ApiController ->leftJoin('e.locationType', 'lt') ->andWhere( $query->expr()->andX( - $query->expr()->eq('e.availableForUsers', "'TRUE'"), - $query->expr()->eq('lt.availableForUsers', "'TRUE'"), - $query->expr()->eq('e.active', "'TRUE'"), - ) + $query->expr()->eq('e.availableForUsers', "'TRUE'"), + $query->expr()->eq('lt.availableForUsers', "'TRUE'"), + $query->expr()->eq('e.active', "'TRUE'"), + ) ); } } diff --git a/src/Bundle/ChillMainBundle/Entity/Location.php b/src/Bundle/ChillMainBundle/Entity/Location.php index ba1ad8fb0..8496374d8 100644 --- a/src/Bundle/ChillMainBundle/Entity/Location.php +++ b/src/Bundle/ChillMainBundle/Entity/Location.php @@ -40,7 +40,7 @@ class Location implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\ManyToOne(targetEntity=Address::class, cascade={"persist"}) * @ORM\JoinColumn(nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?Address $address = null; @@ -72,26 +72,26 @@ class Location implements TrackCreationInterface, TrackUpdateInterface * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id = null; /** * @ORM\ManyToOne(targetEntity=LocationType::class) * @ORM\JoinColumn(nullable=false) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?LocationType $locationType = null; /** * @ORM\Column(type="string", length=255, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?string $name = null; /** * @ORM\Column(type="string", length=64, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) * @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/") * @PhonenumberConstraint(type="any") */ @@ -99,7 +99,7 @@ class Location implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\Column(type="string", length=64, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) * @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/") * @PhonenumberConstraint(type="any") */ diff --git a/src/Bundle/ChillMainBundle/Entity/LocationType.php b/src/Bundle/ChillMainBundle/Entity/LocationType.php index cda7e073f..9081304c8 100644 --- a/src/Bundle/ChillMainBundle/Entity/LocationType.php +++ b/src/Bundle/ChillMainBundle/Entity/LocationType.php @@ -71,13 +71,14 @@ class LocationType * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id = null; /** * @ORM\Column(type="json") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) + * @Serializer\Context({"is-translatable": true}, groups={"docgen:read"}) */ private array $title = []; diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 8b2dc6aca..eb9a3849b 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -406,8 +406,6 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface /** * Get email. - * - * @return string|null */ public function getEmail(): ?string { From 9453fc2dd5c704e6b6baeb462580432efbc6a2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 23:42:37 +0100 Subject: [PATCH 08/11] docgen: add age and base context --- CHANGELOG.md | 2 ++ .../Service/Context/BaseContextData.php | 2 +- .../tests/Service/Context/BaseContextDataTest.php | 2 ++ .../Serializer/Normalizer/PersonDocGenNormalizer.php | 3 ++- .../Service/DocGenerator/AccompanyingPeriodContext.php | 10 +++++++--- .../Normalizer/PersonDocGenNormalizerTest.php | 1 + 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db1a07606..67a26b73c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to * AddAddress: optimize loading: wait for the user finish typing; * UserPicker: fix bug with deprecated role +* docgen: add base context + tests +* docgen: add age for person ## Test releases diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php index 0194b8d08..0e2da164f 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php @@ -45,7 +45,7 @@ class BaseContextData $data['location'] = $this->normalizer->normalize( $user instanceof User ? $user->getCurrentLocation() : null, 'docgen', - ['docgen:expects' => Location::class, 'groups' => ['docgen:expects']] + ['docgen:expects' => Location::class, 'groups' => ['docgen:read']] ); return $data; diff --git a/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php index 8496bdda1..1960fbcb9 100644 --- a/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php +++ b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php @@ -38,6 +38,7 @@ final class BaseContextDataTest extends KernelTestCase $this->assertIsArray($actual); $this->assertArrayHasKey('creator', $actual); $this->assertArrayHasKey('createdAt', $actual); + $this->assertArrayHasKey('location', $actual); } public function testGenerateWithUser() @@ -52,6 +53,7 @@ final class BaseContextDataTest extends KernelTestCase $this->assertIsArray($actual); $this->assertArrayHasKey('creator', $actual); $this->assertArrayHasKey('createdAt', $actual); + $this->assertArrayHasKey('location', $actual); } private function buildBaseContext( diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index a1f8b1f83..d6c9e0aac 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -74,7 +74,7 @@ class PersonDocGenNormalizer implements $data = [ 'type' => 'person', 'isNull' => false, - 'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expect' => Civility::class])), + 'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expects' => Civility::class])), 'firstname' => $person->getFirstName(), 'lastname' => $person->getLastName(), 'altNames' => implode( @@ -87,6 +87,7 @@ class PersonDocGenNormalizer implements ) ), 'text' => $this->personRender->renderString($person, []), + 'age' => $person->getAge(), 'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext), 'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext), 'gender' => $this->translator->trans($person->getGender()), diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 345fa2556..60406c3d5 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -15,6 +15,7 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocGeneratorBundle\Service\Context\BaseContextData; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; @@ -38,6 +39,8 @@ class AccompanyingPeriodContext implements DocGeneratorContextWithAdminFormInterface, DocGeneratorContextWithPublicFormInterface { + private BaseContextData $baseContextData; + private DocumentCategoryRepository $documentCategoryRepository; private EntityManagerInterface $em; @@ -56,7 +59,8 @@ class AccompanyingPeriodContext implements TranslatableStringHelperInterface $translatableStringHelper, EntityManagerInterface $em, PersonRender $personRender, - TranslatorInterface $translator + TranslatorInterface $translator, + BaseContextData $baseContextData ) { $this->documentCategoryRepository = $documentCategoryRepository; $this->normalizer = $normalizer; @@ -64,12 +68,11 @@ class AccompanyingPeriodContext implements $this->em = $em; $this->personRender = $personRender; $this->translator = $translator; + $this->baseContextData = $baseContextData; } public function adminFormReverseTransform(array $data): array { - dump($data); - if (array_key_exists('category', $data)) { $data['category'] = [ 'idInsideBundle' => $data['category']->getIdInsideBundle(), @@ -171,6 +174,7 @@ class AccompanyingPeriodContext implements $options = $template->getOptions(); $data = []; + $data = array_merge($data, $this->baseContextData->getData()); $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); foreach (['mainPerson', 'person1', 'person2'] as $k) { diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php index 6c9d8de5c..b0341105f 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php @@ -56,6 +56,7 @@ final class PersonDocGenNormalizerTest extends KernelTestCase 'placeOfBirth' => '', 'memo' => '', 'numberOfChildren' => '', + 'age' => '@ignored', ]; private NormalizerInterface $normalizer; From d4a5735e1562251b0f67d189a6f748b74f5c0ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 22:43:06 +0000 Subject: [PATCH 09/11] Docgen/add base context --- CHANGELOG.md | 2 + .../Service/Context/BaseContextData.php | 53 +++++++++++++++ .../config/services.yaml | 5 ++ .../Service/Context/BaseContextDataTest.php | 68 +++++++++++++++++++ .../Controller/LocationApiController.php | 8 +-- .../ChillMainBundle/Entity/Location.php | 12 ++-- .../ChillMainBundle/Entity/LocationType.php | 5 +- .../Normalizer/PersonDocGenNormalizer.php | 3 +- .../AccompanyingPeriodContext.php | 10 ++- .../Normalizer/PersonDocGenNormalizerTest.php | 1 + .../Entity/ThirdParty.php | 2 - 11 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index db1a07606..67a26b73c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to * AddAddress: optimize loading: wait for the user finish typing; * UserPicker: fix bug with deprecated role +* docgen: add base context + tests +* docgen: add age for person ## Test releases diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php new file mode 100644 index 000000000..0e2da164f --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php @@ -0,0 +1,53 @@ +security = $security; + $this->normalizer = $normalizer; + } + + public function getData(): array + { + $data = []; + $user = $this->security->getUser(); + + $data['creator'] = $this->normalizer->normalize( + $user instanceof User ? $user : null, + 'docgen', + ['docgen:expects' => User::class, 'groups' => ['docgen:read']] + ); + $data['createdAt'] = $this->normalizer->normalize(new DateTimeImmutable(), 'docgen', [ + 'docgen:expects' => DateTimeImmutable::class, 'groups' => ['docgen:read'], + ]); + $data['location'] = $this->normalizer->normalize( + $user instanceof User ? $user->getCurrentLocation() : null, + 'docgen', + ['docgen:expects' => Location::class, 'groups' => ['docgen:read']] + ); + + return $data; + } +} diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 54bf5a697..5bdfe2a11 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -34,6 +34,11 @@ services: autowire: true autoconfigure: true + Chill\DocGeneratorBundle\Service\Context\: + resource: "../Service/Context/" + autowire: true + autoconfigure: true + Chill\DocGeneratorBundle\GeneratorDriver\: resource: "../GeneratorDriver/" autowire: true diff --git a/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php new file mode 100644 index 000000000..1960fbcb9 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php @@ -0,0 +1,68 @@ +buildBaseContext(); + + $actual = $context->getData(); + + $this->assertIsArray($actual); + $this->assertArrayHasKey('creator', $actual); + $this->assertArrayHasKey('createdAt', $actual); + $this->assertArrayHasKey('location', $actual); + } + + public function testGenerateWithUser() + { + $security = $this->prophesize(Security::class); + $security->getUser()->willReturn(new User()); + + $context = $this->buildBaseContext($security->reveal()); + + $actual = $context->getData(); + + $this->assertIsArray($actual); + $this->assertArrayHasKey('creator', $actual); + $this->assertArrayHasKey('createdAt', $actual); + $this->assertArrayHasKey('location', $actual); + } + + private function buildBaseContext( + ?Security $security = null, + ?NormalizerInterface $normalizer = null + ): BaseContextData { + return new BaseContextData( + $security ?? self::$container->get(Security::class), + $normalizer ?? self::$container->get(NormalizerInterface::class) + ); + } +} diff --git a/src/Bundle/ChillMainBundle/Controller/LocationApiController.php b/src/Bundle/ChillMainBundle/Controller/LocationApiController.php index aa5f46f1a..250d92980 100644 --- a/src/Bundle/ChillMainBundle/Controller/LocationApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/LocationApiController.php @@ -25,10 +25,10 @@ class LocationApiController extends ApiController ->leftJoin('e.locationType', 'lt') ->andWhere( $query->expr()->andX( - $query->expr()->eq('e.availableForUsers', "'TRUE'"), - $query->expr()->eq('lt.availableForUsers', "'TRUE'"), - $query->expr()->eq('e.active', "'TRUE'"), - ) + $query->expr()->eq('e.availableForUsers', "'TRUE'"), + $query->expr()->eq('lt.availableForUsers', "'TRUE'"), + $query->expr()->eq('e.active', "'TRUE'"), + ) ); } } diff --git a/src/Bundle/ChillMainBundle/Entity/Location.php b/src/Bundle/ChillMainBundle/Entity/Location.php index ba1ad8fb0..8496374d8 100644 --- a/src/Bundle/ChillMainBundle/Entity/Location.php +++ b/src/Bundle/ChillMainBundle/Entity/Location.php @@ -40,7 +40,7 @@ class Location implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\ManyToOne(targetEntity=Address::class, cascade={"persist"}) * @ORM\JoinColumn(nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?Address $address = null; @@ -72,26 +72,26 @@ class Location implements TrackCreationInterface, TrackUpdateInterface * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id = null; /** * @ORM\ManyToOne(targetEntity=LocationType::class) * @ORM\JoinColumn(nullable=false) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?LocationType $locationType = null; /** * @ORM\Column(type="string", length=255, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?string $name = null; /** * @ORM\Column(type="string", length=64, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) * @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/") * @PhonenumberConstraint(type="any") */ @@ -99,7 +99,7 @@ class Location implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\Column(type="string", length=64, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) * @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/") * @PhonenumberConstraint(type="any") */ diff --git a/src/Bundle/ChillMainBundle/Entity/LocationType.php b/src/Bundle/ChillMainBundle/Entity/LocationType.php index cda7e073f..9081304c8 100644 --- a/src/Bundle/ChillMainBundle/Entity/LocationType.php +++ b/src/Bundle/ChillMainBundle/Entity/LocationType.php @@ -71,13 +71,14 @@ class LocationType * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id = null; /** * @ORM\Column(type="json") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) + * @Serializer\Context({"is-translatable": true}, groups={"docgen:read"}) */ private array $title = []; diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index a1f8b1f83..d6c9e0aac 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -74,7 +74,7 @@ class PersonDocGenNormalizer implements $data = [ 'type' => 'person', 'isNull' => false, - 'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expect' => Civility::class])), + 'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expects' => Civility::class])), 'firstname' => $person->getFirstName(), 'lastname' => $person->getLastName(), 'altNames' => implode( @@ -87,6 +87,7 @@ class PersonDocGenNormalizer implements ) ), 'text' => $this->personRender->renderString($person, []), + 'age' => $person->getAge(), 'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext), 'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext), 'gender' => $this->translator->trans($person->getGender()), diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 345fa2556..60406c3d5 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -15,6 +15,7 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocGeneratorBundle\Service\Context\BaseContextData; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; @@ -38,6 +39,8 @@ class AccompanyingPeriodContext implements DocGeneratorContextWithAdminFormInterface, DocGeneratorContextWithPublicFormInterface { + private BaseContextData $baseContextData; + private DocumentCategoryRepository $documentCategoryRepository; private EntityManagerInterface $em; @@ -56,7 +59,8 @@ class AccompanyingPeriodContext implements TranslatableStringHelperInterface $translatableStringHelper, EntityManagerInterface $em, PersonRender $personRender, - TranslatorInterface $translator + TranslatorInterface $translator, + BaseContextData $baseContextData ) { $this->documentCategoryRepository = $documentCategoryRepository; $this->normalizer = $normalizer; @@ -64,12 +68,11 @@ class AccompanyingPeriodContext implements $this->em = $em; $this->personRender = $personRender; $this->translator = $translator; + $this->baseContextData = $baseContextData; } public function adminFormReverseTransform(array $data): array { - dump($data); - if (array_key_exists('category', $data)) { $data['category'] = [ 'idInsideBundle' => $data['category']->getIdInsideBundle(), @@ -171,6 +174,7 @@ class AccompanyingPeriodContext implements $options = $template->getOptions(); $data = []; + $data = array_merge($data, $this->baseContextData->getData()); $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); foreach (['mainPerson', 'person1', 'person2'] as $k) { diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php index 6c9d8de5c..b0341105f 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php @@ -56,6 +56,7 @@ final class PersonDocGenNormalizerTest extends KernelTestCase 'placeOfBirth' => '', 'memo' => '', 'numberOfChildren' => '', + 'age' => '@ignored', ]; private NormalizerInterface $normalizer; diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 8b2dc6aca..eb9a3849b 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -406,8 +406,6 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface /** * Get email. - * - * @return string|null */ public function getEmail(): ?string { From efdd96b69f73a43947ac9fad6366a066767de131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 23:51:41 +0100 Subject: [PATCH 10/11] docgen: person normalization: fix typing of age --- .../Serializer/Normalizer/PersonDocGenNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index d6c9e0aac..3f7fd5d9e 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -87,7 +87,7 @@ class PersonDocGenNormalizer implements ) ), 'text' => $this->personRender->renderString($person, []), - 'age' => $person->getAge(), + 'age' => (int) $person->getAge(), 'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext), 'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext), 'gender' => $this->translator->trans($person->getGender()), From 66f407ab8c7d139dd00a858bd9b3245fde6fcd1c Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Fri, 17 Dec 2021 16:18:36 +0100 Subject: [PATCH 11/11] filiation: fix 265 https://gitlab.com/champs-libres/departement-de-la-vendee/accent-suivi-developpement/-/issues/265 --- CHANGELOG.md | 1 + src/Bundle/ChillPersonBundle/Menu/HouseholdMenuBuilder.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67a26b73c..c9a6669b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to * UserPicker: fix bug with deprecated role * docgen: add base context + tests * docgen: add age for person +* [household menu] fix filiation order https://gitlab.com/champs-libres/departement-de-la-vendee/accent-suivi-developpement/-/issues/265 ## Test releases diff --git a/src/Bundle/ChillPersonBundle/Menu/HouseholdMenuBuilder.php b/src/Bundle/ChillPersonBundle/Menu/HouseholdMenuBuilder.php index feb465f8d..a144bf65b 100644 --- a/src/Bundle/ChillPersonBundle/Menu/HouseholdMenuBuilder.php +++ b/src/Bundle/ChillPersonBundle/Menu/HouseholdMenuBuilder.php @@ -57,7 +57,7 @@ class HouseholdMenuBuilder implements LocalMenuBuilderInterface 'routeParameters' => [ 'household_id' => $household->getId(), ], ]) - ->setExtras(['order' => 40]); + ->setExtras(['order' => 15]); } public static function getMenuIds(): array