update normalizers to take into account referrerHistory logic for accompanying period work

This commit is contained in:
Julie Lenaerts 2024-01-24 19:30:09 +01:00
parent d91b1a70bf
commit bc683b28d6
5 changed files with 68 additions and 30 deletions

View File

@ -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(),
];

View File

@ -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);

View File

@ -394,10 +394,6 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues
/**
* @return ReadableCollection<int, User>
*
* @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<int, AccompanyingPeriodWorkReferrerHistory>
*/
public function getReferrersHistory(): Collection
{
return $this->referrersHistory;

View File

@ -34,7 +34,7 @@
<li>
<span class="item-key">{{ 'Referrers'|trans ~ ' : ' }}</span>
{% for rh in w.referrers %}
<span class="badge-user">{{ rh.user|chill_entity_render_box({'at_date': rh.startDate}) }}</span>
<span class="badge-user">{{ rh|chill_entity_render_box }}</span>
{% endfor %}
{% if w.referrers|length == 0 %}
<span class="chill-no-data-statement">{{ 'Not given'|trans }}</span>

View File

@ -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);
}
}