try to fix some things

This commit is contained in:
Julien Fastré 2022-02-28 16:09:52 +01:00
parent cdd21c94c6
commit 1e146f542e
5 changed files with 82 additions and 29 deletions

View File

@ -19,6 +19,7 @@ use Chill\ThirdPartyBundle\Entity\ThirdParty;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Context;
/**
* @ORM\Entity(repositoryClass=ResidentialAddressRepository::class)
@ -48,6 +49,7 @@ class ResidentialAddress
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=true)
* @Groups({"read"})
* @Context(normalizationContext={"groups"={"minimal"}})
*/
private ?Person $hostPerson = null;

View File

@ -15,6 +15,8 @@ use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Person\ResidentialAddress;
use DateTimeImmutable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
/**
@ -30,18 +32,37 @@ class ResidentialAddressRepository extends ServiceEntityRepository
parent::__construct($registry, ResidentialAddress::class);
}
public function findCurrentResidentialAddressByPerson(Person $person, ?DateTimeImmutable $at = null): ?ResidentialAddress
/**
* @param Person $person
* @param DateTimeImmutable|null $at
* @return array|ResidentialAddress[]|null
*/
public function findCurrentResidentialAddressByPerson(Person $person, ?DateTimeImmutable $at = null): array
{
return $this->buildQueryFindCurrentResidentialAddresses($person, $at)
->select('ra')
->getQuery()
->getResult();
}
public function buildQueryFindCurrentResidentialAddresses(Person $person, ?DateTimeImmutable $at = null): QueryBuilder
{
$addresses = $this->findBy(['person' => $person], ['startDate' => 'DESC']);
$date = null === $at ? new DateTimeImmutable('today') : $at;
$qb = $this->createQueryBuilder('ra');
foreach ($addresses as $a) {
if ($a->getStartDate() < $date && $a->getEndDate() > $date) {
return $a;
}
}
$dateFilter = $qb->expr()->andX(
$qb->expr()->lte('ra.startDate', ':dateIn'),
$qb->expr()->orX(
$qb->expr()->isNull('ra.endDate'),
$qb->expr()->gte('ra.endDate', ':dateIn')
)
);
return null;
$qb
->where($dateFilter)
->setParameter('dateIn', $date, Types::DATE_IMMUTABLE);
return $qb;
}
// /**

View File

@ -128,42 +128,42 @@
</div>
</div>
<div class="item-col mx-3">
<div class="item-col mx-3" v-if="this.showResidentialAddresses && (person.current_residential_addresses || []).length > 0">
<div class="float-button bottom">
<div class="box">
<div class="box" >
<ul class="list-content fa-ul">
<li v-if="person.current_residential_address">
<li v-for="addr in person.current_residential_addresses">
<i class="fa fa-li fa-map-marker"></i>
<div v-if="person.current_residential_address.address">
<div v-if="addr.address">
<address-render-box
:address="person.current_residential_address.address"
:address="addr.address"
:isMultiline="isMultiline">
</address-render-box>
<p>({{ $t('renderbox.residential_address') }})</p>
</div>
<div v-else-if="person.current_residential_address.hostPerson" class="mt-3">
<div v-else-if="addr.hostPerson" class="mt-3">
<p>{{ $t('renderbox.located_at') }}:</p>
<span class="chill-entity entity-person badge-person">
<person-text
v-if="person.current_residential_address.hostPerson"
:person="person.current_residential_address.hostPerson"
v-if="addr.hostPerson"
:person="addr.hostPerson"
></person-text>
</span>
<address-render-box v-if="person.current_residential_address.hostPerson.address"
:address="person.current_residential_address.hostPerson.address"
<address-render-box v-if="addr.hostPerson.address"
:address="addr.hostPerson.address"
:isMultiline="isMultiline">
</address-render-box>
</div>
<div v-else-if="person.current_residential_address.hostThirdParty" class="mt-3">
<div v-else-if="addr.hostThirdParty" class="mt-3">
<p>{{ $t('renderbox.located_at') }}:</p>
<span class="chill-entity entity-person badge-thirdparty">
<third-party-text
v-if="person.current_residential_address.hostThirdParty"
:thirdparty="person.current_residential_address.hostThirdParty"
v-if="addr.hostThirdParty"
:thirdparty="addr.hostThirdParty"
></third-party-text>
</span>
<address-render-box v-if="person.current_residential_address.hostThirdParty.address"
:address="person.current_residential_address.hostThirdParty.address"
<address-render-box v-if="addr.hostThirdParty.address"
:address="addr.hostThirdParty.address"
:isMultiline="isMultiline">
</address-render-box>
</div>
@ -216,7 +216,25 @@ export default {
PersonText,
ThirdPartyText
},
props: ['person', 'options', 'render', 'returnPath'],
props: {
person: {
required: true,
},
options: {
type: Object,
required: false,
},
render: {
type: String,
},
returnPath: {
type: String,
},
showResidentialAddresses: {
type: Boolean,
default: false,
}
},
computed: {
isMultiline: function() {
if(this.options.isMultiline){

View File

@ -15,6 +15,7 @@
addNoData: true,
isMultiline: true
}"
:show-residential-addresses="true"
></person-render-box>
</div>
</div>

View File

@ -22,6 +22,7 @@ use DateTime;
use DateTimeImmutable;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
@ -35,7 +36,9 @@ use function array_key_exists;
class PersonJsonNormalizer implements DenormalizerAwareInterface, NormalizerAwareInterface, PersonJsonNormalizerInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
use ObjectToPopulateTrait;
private CenterResolverManagerInterface $centerResolverManager;
@ -177,29 +180,37 @@ class PersonJsonNormalizer implements DenormalizerAwareInterface, NormalizerAwar
*/
public function normalize($person, $format = null, array $context = [])
{
$groups = $context[AbstractNormalizer::GROUPS] ?? [];
$household = $person->getCurrentHousehold();
$currentResidentialAddress = $this->residentialAddressRepository->findCurrentResidentialAddressByPerson($person);
$currentResidentialAddresses = $this->residentialAddressRepository->findCurrentResidentialAddressByPerson($person);
return [
$data = [
'type' => 'person',
'id' => $person->getId(),
'text' => $this->render->renderString($person, ['addAge' => false]),
'textAge' => $this->render->renderString($person, ['addAge' => true]),
'firstName' => $person->getFirstName(),
'lastName' => $person->getLastName(),
'current_household_address' => $this->normalizer->normalize($person->getCurrentHouseholdAddress(), $format, $context),
'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $context),
'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $context),
'age' => $this->normalizer->normalize($person->getAge(), $format, $context),
];
if (in_array("minimal", $groups) && 1 === count($context)) {
return $data;
}
return array_merge($data, [
'centers' => $this->normalizer->normalize($this->centerResolverManager->resolveCenters($person), $format, $context),
'phonenumber' => $person->getPhonenumber(),
'mobilenumber' => $person->getMobilenumber(),
'email' => $person->getEmail(),
'altNames' => $this->normalizeAltNames($person->getAltNames()),
'gender' => $person->getGender(),
'current_household_address' => $this->normalizer->normalize($person->getCurrentHouseholdAddress(), $format, $context),
'current_household_id' => $household ? $this->normalizer->normalize($household->getId(), $format, $context) : null,
'current_residential_address' => $currentResidentialAddress ? $this->normalizer->normalize($currentResidentialAddress, $format, $context) : null,
];
'current_residential_addresses' => $currentResidentialAddresses ? $this->normalizer->normalize($currentResidentialAddresses, $format, $context) : null,
]);
}
public function supportsDenormalization($data, $type, $format = null)