PersonAddressMoveEvent on household move (wip)

This commit is contained in:
2022-02-19 14:04:51 +01:00
parent 8675bb65c1
commit caa63ea97a
11 changed files with 237 additions and 48 deletions

View File

@@ -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"]]
);
}
/**