mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
PersonAddressMoveEvent on household move (wip)
This commit is contained in:
@@ -16,15 +16,20 @@ use Chill\MainBundle\Entity\Address;
|
||||
use Chill\MainBundle\Entity\AddressReference;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent;
|
||||
use Chill\PersonBundle\Repository\Household\HouseholdACLAwareRepositoryInterface;
|
||||
use Chill\PersonBundle\Repository\Household\HouseholdRepository;
|
||||
use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
use function array_filter;
|
||||
use function array_values;
|
||||
|
||||
@@ -34,10 +39,14 @@ class HouseholdApiController extends ApiController
|
||||
|
||||
private HouseholdRepository $householdRepository;
|
||||
|
||||
private EventDispatcherInterface $eventDispatcher;
|
||||
|
||||
public function __construct(
|
||||
EventDispatcherInterface $eventDispatcher,
|
||||
HouseholdRepository $householdRepository,
|
||||
HouseholdACLAwareRepositoryInterface $householdACLAwareRepository
|
||||
) {
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
$this->householdRepository = $householdRepository;
|
||||
$this->householdACLAwareRepository = $householdACLAwareRepository;
|
||||
}
|
||||
@@ -66,9 +75,51 @@ class HouseholdApiController extends ApiController
|
||||
]);
|
||||
}
|
||||
|
||||
public function householdAddressApi($id, Request $request, string $_format): Response
|
||||
/**
|
||||
* Add an address to a household
|
||||
*
|
||||
* @Route("/api/1.0/person/household/{id}/address.{_format}", name="chill_api_single_household_address",
|
||||
* methods={"POST"}, requirements={"_format": "json"})
|
||||
*/
|
||||
public function householdAddressApi(Household $household, Request $request, string $_format): Response
|
||||
{
|
||||
return $this->addRemoveSomething('address', $id, $request, $_format, 'address', Address::class, ['groups' => ['read']]);
|
||||
$this->denyAccessUnlessGranted(HouseholdVoter::EDIT, $household);
|
||||
|
||||
/** @var Address $address */
|
||||
$address = $this->getSerializer()->deserialize($request->getContent(), Address::class, $_format, [
|
||||
AbstractNormalizer::GROUPS => ['write']
|
||||
]);
|
||||
|
||||
$household->addAddress($address);
|
||||
|
||||
foreach ($household->getMembersOnRange(
|
||||
\DateTimeImmutable::createFromMutable($address->getValidFrom()),
|
||||
null === $address->getValidTo() ? null :
|
||||
\DateTimeImmutable::createFromMutable($address->getValidTo())
|
||||
) as $member) {
|
||||
/** @var HouseholdMember $member */
|
||||
$event = new PersonAddressMoveEvent($member->getPerson());
|
||||
$event
|
||||
->setPreviousAddress($household->getPreviousAddressOf($address))
|
||||
->setNextAddress($address)
|
||||
;
|
||||
$this->eventDispatcher->dispatch($event);
|
||||
}
|
||||
|
||||
$errors = $this->getValidator()->validate($household);
|
||||
|
||||
if ($errors->count() > 0) {
|
||||
return $this->json($errors, 422);
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
return $this->json(
|
||||
$address,
|
||||
Response::HTTP_OK,
|
||||
[],
|
||||
[AbstractNormalizer::GROUPS => ["read"]]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -24,7 +24,7 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
|
Reference in New Issue
Block a user