do not launch PersonMoveEvent when moving to the same household

This commit is contained in:
Julien Fastré 2022-03-30 16:42:57 +02:00
parent dae9d48574
commit 36b1f05524
3 changed files with 84 additions and 4 deletions

View File

@ -328,6 +328,8 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
* targetEntity=HouseholdMember::class, * targetEntity=HouseholdMember::class,
* mappedBy="person" * mappedBy="person"
* ) * )
*
* @var Collection|HouseholdMember[]
*/ */
private Collection $householdParticipations; private Collection $householdParticipations;
@ -1117,6 +1119,9 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
return $this->householdAddresses; return $this->householdAddresses;
} }
/**
* @return Collection|HouseholdMember[]
*/
public function getHouseholdParticipations(): Collection public function getHouseholdParticipations(): Collection
{ {
return $this->householdParticipations; return $this->householdParticipations;
@ -1126,6 +1131,8 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
* Get participation where the person does not share the household. * Get participation where the person does not share the household.
* *
* Order by startDate, desc * Order by startDate, desc
*
* @return HouseholdMember[]
*/ */
public function getHouseholdParticipationsNotShareHousehold(): Collection public function getHouseholdParticipationsNotShareHousehold(): Collection
{ {
@ -1146,6 +1153,8 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
* Get participation where the person does share the household. * Get participation where the person does share the household.
* *
* Order by startDate, desc * Order by startDate, desc
*
* @return Collection|HouseholdMember[]
*/ */
public function getHouseholdParticipationsShareHousehold(): Collection public function getHouseholdParticipationsShareHousehold(): Collection
{ {

View File

@ -70,10 +70,12 @@ class MembersEditor
$this->household->addMember($membership); $this->household->addMember($membership);
if ($position->getShareHousehold()) { if ($position->getShareHousehold()) {
// launch event only if moving to a "share household" position // launch event only if moving to a "share household" position,
// and if the destination household is different than the previous one
$event = new PersonAddressMoveEvent($person); $event = new PersonAddressMoveEvent($person);
$event->setNextMembership($membership); $event->setNextMembership($membership);
$this->events[] = $event;
$counter = 0;
foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) { foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) {
if ($participation === $membership) { if ($participation === $membership) {
@ -84,14 +86,25 @@ class MembersEditor
continue; continue;
} }
++$counter;
if ($participation->getEndDate() === null || $participation->getEndDate() > $date) { if ($participation->getEndDate() === null || $participation->getEndDate() > $date) {
$event->setPreviousMembership($participation);
$participation->setEndDate($date); $participation->setEndDate($date);
$this->membershipsAffected[] = $participation; $this->membershipsAffected[] = $participation;
$this->oldMembershipsHashes[] = spl_object_hash($participation); $this->oldMembershipsHashes[] = spl_object_hash($participation);
if ($participation->getHousehold() !== $this->household) {
$event->setPreviousMembership($participation);
$this->events[] = $event;
}
} }
} }
// send also the event if there was no participation before
if (0 === $counter) {
$this->events[] = $event;
}
foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) { foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) {
if ($participation->getHousehold() === $this->household if ($participation->getHousehold() === $this->household
&& $participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate() && $participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate()

View File

@ -231,13 +231,71 @@ final class MembersEditorTest extends TestCase
$editor->postMove(); $editor->postMove();
} }
public function testPostMoveToAPositionSharingHousehold() public function testPostMoveToAPositionSharingHouseholdAndSameHousehold()
{
$person = new Person();
$position = (new Position())
->setShareHousehold(true);
$position2 = (new Position())
->setShareHousehold(true);
$household1 = new Household();
// set into the first household
$editor = $this->buildMembersEditorFactory()
->createEditor($household1);
$editor->addMovement(new DateTimeImmutable('1 year ago'), $person, $position);
// prepare for next move
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher
->dispatch(Argument::type(PersonAddressMoveEvent::class))
->shouldNotBeCalled();
$factory = $this->buildMembersEditorFactory(
$eventDispatcher->reveal(),
null
);
$editor = $factory->createEditor($household1);
$editor->addMovement(new DateTimeImmutable('now'), $person, $position2);
$editor->postMove();
}
public function testPostMoveToAPositionSharingHouseholdFromDifferentHousehold()
{ {
$person = new Person(); $person = new Person();
$position = (new Position()) $position = (new Position())
->setShareHousehold(true); ->setShareHousehold(true);
$household1 = new Household(); $household1 = new Household();
$household2 = new Household(); $household2 = new Household();
// set into the first household
$editor = $this->buildMembersEditorFactory()
->createEditor($household1);
$editor->addMovement(new DateTimeImmutable('1 year ago'), $person, $position);
// perform now the movement
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher
->dispatch(Argument::type(PersonAddressMoveEvent::class))
->shouldBeCalled();
$factory = $this->buildMembersEditorFactory(
$eventDispatcher->reveal(),
null
);
$editor = $factory->createEditor($household2);
$editor->addMovement(new DateTimeImmutable('now'), $person, $position);
$editor->postMove();
}
public function testPostMoveToAPositionSharingHouseholdFromNoHousehold()
{
$person = new Person();
$position = (new Position())
->setShareHousehold(true);
$household1 = new Household();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher $eventDispatcher
->dispatch(Argument::type(PersonAddressMoveEvent::class)) ->dispatch(Argument::type(PersonAddressMoveEvent::class))