From bc683b28d61fbcf74f6e485bd4146036f36178d1 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 24 Jan 2024 19:30:09 +0100 Subject: [PATCH] update normalizers to take into account referrerHistory logic for accompanying period work --- .../Serializer/Normalizer/UserNormalizer.php | 9 +++- .../Templating/Entity/UserRender.php | 27 ++++++++-- .../AccompanyingPeriodWork.php | 7 ++- ...st_recent_by_accompanying_period.html.twig | 2 +- .../AccompanyingPeriodWorkNormalizer.php | 53 ++++++++++++------- 5 files changed, 68 insertions(+), 30 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php index 9843c5dd5..92f5ea4d8 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php @@ -19,6 +19,7 @@ use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\UserJob; use Chill\MainBundle\Templating\Entity\UserRender; use libphonenumber\PhoneNumber; +use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; @@ -27,6 +28,8 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware { use NormalizerAwareTrait; + final public const AT_DATE = 'chill:user:at_date'; + final public const NULL_USER = [ 'type' => 'user', 'id' => '', @@ -74,6 +77,8 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware return [...self::NULL_USER, 'phonenumber' => $this->normalizer->normalize(null, $format, $phonenumberContext), 'civility' => $this->normalizer->normalize(null, $format, $civilityContext), 'user_job' => $this->normalizer->normalize(null, $format, $userJobContext), 'main_center' => $this->normalizer->normalize(null, $format, $centerContext), 'main_scope' => $this->normalizer->normalize(null, $format, $scopeContext), 'current_location' => $this->normalizer->normalize(null, $format, $locationContext), 'main_location' => $this->normalizer->normalize(null, $format, $locationContext)]; } + $at = $context[self::AT_DATE] ?? $this->clock->now(); + $data = [ 'type' => 'user', 'id' => $object->getId(), @@ -83,9 +88,9 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware 'label' => $object->getLabel(), 'email' => (string) $object->getEmail(), 'phonenumber' => $this->normalizer->normalize($object->getPhonenumber(), $format, $phonenumberContext), - 'user_job' => $this->normalizer->normalize($object->getUserJob(), $format, $userJobContext), + 'user_job' => $this->normalizer->normalize($object->getUserJob($at), $format, $userJobContext), 'main_center' => $this->normalizer->normalize($object->getMainCenter(), $format, $centerContext), - 'main_scope' => $this->normalizer->normalize($object->getMainScope(), $format, $scopeContext), + 'main_scope' => $this->normalizer->normalize($object->getMainScope($at), $format, $scopeContext), 'isAbsent' => $object->isAbsent(), ]; diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/UserRender.php b/src/Bundle/ChillMainBundle/Templating/Entity/UserRender.php index a03acb476..45aad79ee 100644 --- a/src/Bundle/ChillMainBundle/Templating/Entity/UserRender.php +++ b/src/Bundle/ChillMainBundle/Templating/Entity/UserRender.php @@ -14,6 +14,7 @@ namespace Chill\MainBundle\Templating\Entity; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Templating\TranslatableStringHelper; use Monolog\DateTimeImmutable; +use Symfony\Component\Clock\ClockInterface; use Symfony\Contracts\Translation\TranslatorInterface; /** @@ -25,18 +26,29 @@ class UserRender implements ChillEntityRenderInterface 'main_scope' => true, 'user_job' => true, 'absence' => true, - 'at_date' => null, + 'at_date' => null, // instanceof DateTimeInterface ]; - public function __construct(private readonly TranslatableStringHelper $translatableStringHelper, private readonly \Twig\Environment $engine, private readonly TranslatorInterface $translator) - { - } + public function __construct( + private readonly TranslatableStringHelper $translatableStringHelper, + private readonly \Twig\Environment $engine, private readonly TranslatorInterface $translator, + private readonly ClockInterface $clock, + ) {} + /** + * @param $entity + * @param array{main_scope?: bool, user_job?: bool, absence?: bool, at_date?: null|DateTimeImmutable|DateTimeMutable} $options + * @return string + */ public function renderBox($entity, array $options): string { $opts = \array_merge(self::DEFAULT_OPTIONS, $options); - $opts['at_date'] = $opts['at_date'] instanceOf \DateTime ? DateTimeImmutable::createFromMutable($opts['at_date']) : $opts['at_date']; + if (null === $opts['at_date']) { + $opts['at_date'] = $this->clock->now(); + } elseif ($opts['at_date'] instanceof \DateTime) { + $opts['at_date'] = DateTimeImmutable::createFromMutable($opts['at_date']); + } return $this->engine->render('@ChillMain/Entity/user.html.twig', [ 'user' => $entity, @@ -44,6 +56,11 @@ class UserRender implements ChillEntityRenderInterface ]); } + /** + * @param $entity + * @param array{main_scope?: bool, user_job?: bool, absence?: bool, at_date?: null|DateTimeImmutable|DateTimeMutable} $options + * @return string + */ public function renderString($entity, array $options): string { $opts = \array_merge(self::DEFAULT_OPTIONS, $options); diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php index 8326fc43b..62f7e868f 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php @@ -394,10 +394,6 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues /** * @return ReadableCollection - * - * @Serializer\Groups({"read", "docgen:read", "read:accompanyingPeriodWork:light"}) - * @Serializer\Groups({"accompanying_period_work:edit"}) - * @Serializer\Groups({"accompanying_period_work:create"}) */ public function getReferrers(): ReadableCollection { @@ -409,6 +405,9 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues return new ArrayCollection(array_values($users)); } + /** + * @return Collection + */ public function getReferrersHistory(): Collection { return $this->referrersHistory; diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig index 9772641f7..21552e44c 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig @@ -34,7 +34,7 @@
  • {{ 'Referrers'|trans ~ ' : ' }} {% for rh in w.referrers %} - {{ rh.user|chill_entity_render_box({'at_date': rh.startDate}) }} + {{ rh|chill_entity_render_box }} {% endfor %} {% if w.referrers|length == 0 %} {{ 'Not given'|trans }} diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodWorkNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodWorkNormalizer.php index 438e89093..040576fa6 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodWorkNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodWorkNormalizer.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Serializer\Normalizer; use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository; +use Chill\MainBundle\Serializer\Normalizer\UserNormalizer; use Chill\MainBundle\Workflow\Helper\MetadataExtractor; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; @@ -53,35 +54,51 @@ class AccompanyingPeriodWorkNormalizer implements ContextAwareNormalizerInterfac $context ); - // then, we add normalization for things which are not into the entity + // add the referrers + $initial['referrers'] = []; - $initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor( - AccompanyingPeriodWork::class, - $object->getId() - ); + if ($object instanceof AccompanyingPeriodWork) { + foreach ($object->getReferrersHistory() as $referrerHistory) { + if (null !== $referrerHistory->getEndDate()) { + continue; + } - $initial['workflows_availables_evaluation'] = $this->metadataExtractor->availableWorkflowFor( - AccompanyingPeriodWorkEvaluation::class - ); + $initial['referrers'][] = $this->normalizer->normalize($referrerHistory->getUser(), + $format, [...$context, UserNormalizer::AT_DATE => $referrerHistory->getStartDate()]); + } + } - $initial['workflows_availables_evaluation_documents'] = $this->metadataExtractor->availableWorkflowFor( - AccompanyingPeriodWorkEvaluationDocument::class - ); + if ($format === 'json') { - $workflows = $this->entityWorkflowRepository->findBy([ - 'relatedEntityClass' => AccompanyingPeriodWork::class, - 'relatedEntityId' => $object->getId(), - ]); + // then, we add normalization for things which are not into the entity + $initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor( + AccompanyingPeriodWork::class, + $object->getId() + ); - $initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $context); + $initial['workflows_availables_evaluation'] = $this->metadataExtractor->availableWorkflowFor( + AccompanyingPeriodWorkEvaluation::class + ); + + $initial['workflows_availables_evaluation_documents'] = $this->metadataExtractor->availableWorkflowFor( + AccompanyingPeriodWorkEvaluationDocument::class + ); + + $workflows = $this->entityWorkflowRepository->findBy([ + 'relatedEntityClass' => AccompanyingPeriodWork::class, + 'relatedEntityId' => $object->getId(), + ]); + + $initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $context); + } return $initial; } public function supportsNormalization($data, ?string $format = null, array $context = []): bool { - return 'json' === $format - && $data instanceof AccompanyingPeriodWork + return ('json' === $format || 'docgen' === $format) + && ($data instanceof AccompanyingPeriodWork || ('docgen' === $format && $data === null && ($context['docgen:expects'] ?? null) === AccompanyingPeriodWork::class)) && !\array_key_exists(self::IGNORE_WORK, $context); } }