diff --git a/.changes/unreleased/Fixed-20230712-111206.yaml b/.changes/unreleased/Fixed-20230712-111206.yaml new file mode 100644 index 000000000..7bec6095e --- /dev/null +++ b/.changes/unreleased/Fixed-20230712-111206.yaml @@ -0,0 +1,5 @@ +kind: Fixed +body: reinstate the fusion of duplicate persons +time: 2023-07-12T11:12:06.673925762+02:00 +custom: + Issue: "107" diff --git a/.changes/unreleased/Fixed-20230914-115451.yaml b/.changes/unreleased/Fixed-20230914-115451.yaml new file mode 100644 index 000000000..ecee3cd32 --- /dev/null +++ b/.changes/unreleased/Fixed-20230914-115451.yaml @@ -0,0 +1,5 @@ +kind: Fixed +body: Fix gestion doublon functionality to work with chill bundles v2 +time: 2023-09-14T11:54:51.09060399+02:00 +custom: + Issue: "107" diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php new file mode 100644 index 000000000..1b4c07469 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -0,0 +1,46 @@ +getId(), $from->getId(), $to->getId()); + + $deleteSql = sprintf(<<<'SQL' + DELETE FROM chill_person_accompanying_period_participation WHERE person_id = %d; + SQL, $from->getId()); + + return [$insertSql, $deleteSql]; + } +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php new file mode 100644 index 000000000..3bf8da3d3 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -0,0 +1,73 @@ +centerHistoryRepository->findBy(['person' => $from]); + foreach ($centerHistoriesA as $ch) { + if ($oldestDateA === null || ($ch->getStartDate() < $oldestDateA)) { + $oldestDateA = $ch->getStartDate(); + $oldestCenterHistoryA = $ch; + } + } + + $centerHistoriesB = $this->centerHistoryRepository->findBy(['person' => $to]); + foreach ($centerHistoriesB as $ch) { + if ($oldestDateB === null || ($ch->getStartDate() < $oldestDateB)) { + $oldestDateB = $ch->getStartDate(); + $oldestCenterHistoryB = $ch; + } + } + + $sqlDelete = sprintf(<<<'SQL' + DELETE FROM chill_person_person_center_history WHERE person_id = %d; + SQL, $from->getId()); + + $sqlStatements = [$sqlDelete]; + + if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) { + + $sqlInsert = sprintf(<<<'SQL' + UPDATE chill_person_person_center_history SET startDate = '%s' WHERE id = %d; + SQL, $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); + + $sqlStatements = [$sqlInsert, $sqlDelete]; + } + + return $sqlStatements; + + } + +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php new file mode 100644 index 000000000..d617e12ed --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php @@ -0,0 +1,47 @@ +getId(), $from->getId(), $to->getId()); + + $deleteSql = sprintf(<<<'SQL' + DELETE FROM chill_person_household_members WHERE person_id = %d; + SQL, $from->getId()); + + return [$sqlInsert, $deleteSql]; + } + +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php new file mode 100644 index 000000000..2c2f68ca9 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php @@ -0,0 +1,47 @@ +getId(), $from->getId(), $to->getId(), $from->getId(), $from->getId(), $to->getId()); + + $deleteSql = [ + sprintf("DELETE FROM chill_person_relationships WHERE fromperson_id = %d", $from->getId()), + sprintf("DELETE FROM chill_person_relationships WHERE toperson_id = %d", $from->getId()) + ]; + + return [$insertSql, ...$deleteSql]; + } +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index 4e2f7468e..80fde1367 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -35,23 +35,11 @@ use function in_array; */ class PersonMove { - /** - * @var EntityManagerInterface - */ - protected $em; - - /** - * @var EventDispatcherInterface - */ - protected $eventDispatcher; - public function __construct( - EntityManagerInterface $em, - EventDispatcherInterface $eventDispatcher - ) { - $this->em = $em; - $this->eventDispatcher = $eventDispatcher; - } + private EntityManagerInterface $em, + private PersonMoveManager $personMoveManager, + private EventDispatcherInterface $eventDispatcher + ) {} /** * Return the sql used to move or delete entities associated to a person to @@ -88,9 +76,16 @@ class PersonMove } foreach ($metadata->getAssociationMappings() as $field => $mapping) { + + if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) { + $sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to)); + continue; + } + if (in_array($mapping['sourceEntity'], $this->getIgnoredEntities(), true)) { continue; } + if (Person::class === $mapping['targetEntity'] and true === $mapping['isOwningSide']) { if (in_array($mapping['sourceEntity'], $toDelete, true)) { $sql = $this->createDeleteSQL($metadata, $from, $field); @@ -101,25 +96,17 @@ class PersonMove ['to' => $to->getId(), 'original_action' => 'move'] ); $this->eventDispatcher->dispatch(ActionEvent::DELETE, $event); + $sqls = array_merge($sqls, $event->getPreSql(), [$event->getSqlStatement()], $event->getPostSql()); } else { - $sql = $this->createMoveSQL($metadata, $from, $to, $field); - $event = new ActionEvent( - $from->getId(), - $metadata->getName(), - $sql, - ['to' => $to->getId(), 'original_action' => 'move'] - ); - $this->eventDispatcher->dispatch(ActionEvent::MOVE, $event); + $sqls = array_merge($sqls, $this->createMoveSQLs($metadata, $from, $to, $field)); } - - $sqls = array_merge($sqls, $event->getPreSql(), [$event->getSqlStatement()], $event->getPostSql()); } } } $personMetadata = $this->em->getClassMetadata(Person::class); $sqls[] = sprintf( - 'DELETE FROM %s WHERE id = %d', + 'DELETE FROM %s WHERE id = %d;', $this->getTableName($personMetadata), $from->getId() ); @@ -133,18 +120,24 @@ class PersonMove $conditions = []; - foreach ($mapping['joinColumns'] as $columns) { - $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + if (array_key_exists('joinTable', $mapping)) { + foreach ($mapping['joinTable']['joinColumns'] as $columns) { + $conditions[] = sprintf('%s = %d', $columns['referencedColumnName'], $from->getId()); + } + } elseif (array_key_exists('joinColumns', $mapping)) { + foreach ($mapping['joinColumns'] as $columns) { + $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + } } return sprintf( - 'DELETE FROM %s WHERE %s', + 'DELETE FROM %s WHERE %s;', $this->getTableName($metadata), implode(' AND ', $conditions) ); } - private function createMoveSQL(ClassMetadata $metadata, Person $from, Person $to, $field): string + private function createMoveSQLs($metadata, Person $from, Person $to, $field): array { $mapping = $metadata->getAssociationMapping($field); @@ -154,9 +147,29 @@ class PersonMove $tableName = ''; if (array_key_exists('joinTable', $mapping)) { + // there is a join_table: we have to find conflict $tableName = (null !== ($mapping['joinTable']['schema'] ?? null) ? $mapping['joinTable']['schema'] . '.' : '') . $mapping['joinTable']['name']; + $sqlInsert = sprintf( + "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING;", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $to->getId(), + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + ); + + $deleteSql = sprintf( + "DELETE FROM %s WHERE %s = %d;", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + ); + foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); } @@ -164,24 +177,29 @@ class PersonMove foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); } - } elseif (array_key_exists('joinColumns', $mapping)) { + + return [ + $sqlInsert, $deleteSql + ]; + + } + if (array_key_exists('joinColumns', $mapping)) { $tableName = $this->getTableName($metadata); foreach ($mapping['joinColumns'] as $columns) { $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); } - foreach ($mapping['joinColumns'] as $columns) { $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); } } - return sprintf( - 'UPDATE %s SET %s WHERE %s', + return [sprintf( + 'UPDATE %s SET %s WHERE %s;', $tableName, implode(' ', $sets), implode(' AND ', $conditions) - ); + )]; } /** @@ -191,9 +209,6 @@ class PersonMove private function getDeleteEntities(): array { return [ - Person\PersonCenterHistory::class, - HouseholdMember::class, - AccompanyingPeriodParticipation::class, AccompanyingPeriod\AccompanyingPeriodWork::class, Relationship::class ]; diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php new file mode 100644 index 000000000..854b9e99e --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php @@ -0,0 +1,55 @@ + + */ + private iterable $handlers, + ) {} + + /** + * @param class-string $className + * @param string $field + * @return bool + */ + public function hasHandler(string $className, string $field): bool + { + foreach ($this->handlers as $handler) { + if ($handler->supports($className, $field)) { + return true; + } + } + + return false; + } + + /** + * @param class-string $className + * @return array + */ + public function getSqls(string $className, string $field, Person $from, Person $to): array + { + foreach ($this->handlers as $handler) { + if ($handler->supports($className, $field)) { + return $handler->getSqls($className, $field, $from, $to); + } + } + return []; + } + +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php new file mode 100644 index 000000000..c1b47127d --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php @@ -0,0 +1,28 @@ + + */ + public function getSqls(string $className, string $field, Person $from, Person $to): array; +} diff --git a/src/Bundle/ChillPersonBundle/ChillPersonBundle.php b/src/Bundle/ChillPersonBundle/ChillPersonBundle.php index 4233a7914..43c38aa36 100644 --- a/src/Bundle/ChillPersonBundle/ChillPersonBundle.php +++ b/src/Bundle/ChillPersonBundle/ChillPersonBundle.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle; +use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface; use Chill\PersonBundle\DependencyInjection\CompilerPass\AccompanyingPeriodTimelineCompilerPass; use Chill\PersonBundle\Service\EntityInfo\AccompanyingPeriodInfoUnionQueryPartInterface; use Chill\PersonBundle\Widget\PersonListWidgetFactory; @@ -29,5 +30,7 @@ class ChillPersonBundle extends Bundle $container->addCompilerPass(new AccompanyingPeriodTimelineCompilerPass()); $container->registerForAutoconfiguration(AccompanyingPeriodInfoUnionQueryPartInterface::class) ->addTag('chill_person.accompanying_period_info_part'); + $container->registerForAutoconfiguration(PersonMoveSqlHandlerInterface::class) + ->addTag('chill_person.person_move_handler'); } } diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php index 7ade542e0..31ec67a40 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php @@ -120,6 +120,7 @@ class PersonDuplicateController extends Controller $connection->beginTransaction(); foreach ($sqls as $sql) { + dump($sql); $connection->executeQuery($sql); } $connection->commit(); diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index af8e7f92b..a45498e56 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -203,6 +203,7 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI /** * @ORM\OneToMany(targetEntity=PersonCenterHistory::class, mappedBy="person", cascade={"persist"}) + * @ORM\OrderBy({"startDate": "ASC", "id": "ASC"}) * * @var Collection|PersonCenterHistory[] */ @@ -1606,6 +1607,15 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI return $this; } + public function addCenterHistory(PersonCenterHistory $newCenterHistory): self + { + if (!$this->centerHistory->contains($newCenterHistory)) { + $this->centerHistory[] = $newCenterHistory; + $newCenterHistory->setPerson($this); + } + return $this; + } + /** * @return Person */ diff --git a/src/Bundle/ChillPersonBundle/Form/PersonFindManuallyDuplicateType.php b/src/Bundle/ChillPersonBundle/Form/PersonFindManuallyDuplicateType.php index b9dfad240..1d7697332 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonFindManuallyDuplicateType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonFindManuallyDuplicateType.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Form; -use Chill\PersonBundle\Form\Type\PickPersonType; +use Chill\PersonBundle\Form\Type\PickPersonDynamicType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\FormBuilderInterface; @@ -21,7 +21,7 @@ class PersonFindManuallyDuplicateType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('person', PickPersonType::class, [ + ->add('person', PickPersonDynamicType::class, [ 'label' => 'Find duplicate', 'mapped' => false, ]) diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig index 1e770062a..b8edcc437 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig @@ -4,7 +4,7 @@
  • {{ 'gender'|trans }}: {{ person.gender|trans }}
  • {{ 'maritalStatus'|trans }}: - {% if person.maritalStatus.name %}{{ person.maritalStatus.name|localize_translatable_string }}{% endif %}
  • + {% if person.maritalStatus %}{{ person.maritalStatus.name|localize_translatable_string }}{% endif %}
  • {{ 'birthdate'|trans }}: {% if person.birthdate is not null %}{{ person.birthdate|format_date('short') }}{% endif %}
  • {{ 'placeOfBirth'|trans }}: diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/find_manually.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/find_manually.html.twig index 8769ae6da..982690f00 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/find_manually.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/find_manually.html.twig @@ -8,9 +8,9 @@ {% block content %}
    - +

    {{ 'Désigner un dossier doublon'|trans }}

    - + {{ form_start(form) }} {{ form_rest(form) }} @@ -29,3 +29,11 @@
    {% endblock %} + +{% block js %} + {{ encore_entry_script_tags('mod_pickentity_type') }} +{% endblock %} + +{% block css %} + {{ encore_entry_link_tags('mod_pickentity_type') }} +{% endblock %} diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php new file mode 100644 index 000000000..0b2201b37 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -0,0 +1,275 @@ + + */ + private static $entitiesToDelete = []; + + public function setUp(): void + { + self::bootKernel(); + $this->em = self::$container->get(EntityManagerInterface::class); + $this->personMoveManager = self::$container->get(PersonMoveManager::class); + $this->eventDispatcher = self::$container->get(EventDispatcherInterface::class); + $this->centerRepository = self::$container->get(CenterRepositoryInterface::class); + } + + public static function tearDownAfterClass(): void + { + self::bootKernel(); + $em = self::$container->get(EntityManagerInterface::class); + + foreach (self::$entitiesToDelete as [$class, $id]) { + $entity = $em->find($class, $id); + + if (null !== $entity) { + $em->remove($entity); + } + } + + $em->flush(); + } + + /** + * @dataProvider dataProviderMovePerson + */ + public function testMovePersonSimple(Person $personA, Person $personB, string $message): void + { + $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); + $sqls = $move->getSQL($personA, $personB); + $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + foreach ($sqls as $sql) { + $conn->executeStatement($sql); + } + }); + + $personsByIdOfA = $this->em->createQuery("SELECT p FROM " . Person::class . " p WHERE p.id = :id") + ->setParameter('id', $personA->getId()) + ->getResult(); + $personB = $this->em->find(Person::class, $personB->getId()); + + self::assertCount(0, $personsByIdOfA); + self::assertNotNull($personB?->getId(), $message); + } + + public function testMovePersonCenterHistory(): void + { + $personA = new Person(); + $personB = new Person(); + [$centerA, $centerB] = $this->centerRepository->findAll(); + + $this->em->persist($personA); + $this->em->persist($personB); + + $personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setCenter($centerA) + ->setStartDate(new \DateTimeImmutable('2023-01-01')) + ->setEndDate(new \DateTimeImmutable('2023-06-30')); + $personCenterHistoryASecond = (new Person\PersonCenterHistory())->setCenter($centerB) + ->setStartDate(new \DateTimeImmutable('2023-06-30')) + ->setEndDate(new \DateTimeImmutable('2023-09-30')); + $personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setCenter($centerA) + ->setStartDate(new \DateTimeImmutable('2023-03-01')) + ->setEndDate(new \DateTimeImmutable('2023-07-15')); + $personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setCenter($centerB) + ->setStartDate(new \DateTimeImmutable('2023-07-15')) + ->setEndDate(new \DateTimeImmutable('2023-09-30')); + + $this->em->persist($personCenterHistoryAFirst); + $this->em->persist($personCenterHistoryASecond); + $this->em->persist($personCenterHistoryBFirst); + $this->em->persist($personCenterHistoryBSecond); + + $personA->addCenterHistory($personCenterHistoryAFirst); + $personA->addCenterHistory($personCenterHistoryASecond); + $personB->addCenterHistory($personCenterHistoryBFirst); + $personB->addCenterHistory($personCenterHistoryBSecond); + + $this->em->flush(); + $this->em->clear(); + + $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); + $sqls = $move->getSQL($personA, $personB); + $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + foreach ($sqls as $sql) { + $conn->executeStatement($sql); + } + }); + + $personsByIdOfA = $this->em->createQuery("SELECT p FROM " . Person::class . " p WHERE p.id = :id") + ->setParameter('id', $personA->getId()) + ->getResult(); + /** @var Person $personB */ + $personB = $this->em->find(Person::class, $personB->getId()); + $message = 'Move persons with overlapping center histories'; + + $this->em->refresh($personB); + + self::assertCount(0, $personsByIdOfA); + self::assertNotNull($personB?->getId(), $message); + + $centerHistoriesB = $personB->getCenterHistory(); + $oldestDate = new \DateTimeImmutable('2023-01-01'); + + $this->em->refresh($centerHistoriesB->first()); + + self::assertCount(2, $centerHistoriesB); + self::assertEquals($oldestDate, $centerHistoriesB->first()->getStartDate()); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryAFirst]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryASecond]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryBFirst]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryBSecond]; + } + + public function dataProviderMovePerson(): iterable + { + $this->setUp(); + + $personA = new Person(); + $personB = new Person(); + + $this->em->persist($personA); + $this->em->persist($personB); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + + yield [$personA, $personB, "move 2 people without any associated data"]; + + $personA = new Person(); + $personB = new Person(); + + $activity = new Activity(); + $activity->setDate(new \DateTime('today')); + $activity->addPerson($personA); + $activity->addPerson($personB); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($activity); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [Activity::class, $activity]; + + yield [$personA, $personB, "move 2 people having an activity"]; + + $personA = new Person(); + $personB = new Person(); + $household = new Household(); + $household->addMember( + $memberA = (new HouseholdMember())->setPerson($personA)->setShareHousehold(true) + ->setStartDate(new \DateTimeImmutable('2023-01-01')) + ); + $household->addMember( + $memberB = (new HouseholdMember())->setPerson($personB)->setShareHousehold(true) + ->setStartDate(new \DateTimeImmutable('2023-01-01')) + ); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($household); + $this->em->persist($memberA); + $this->em->persist($memberB); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [HouseholdMember::class, $memberA]; + self::$entitiesToDelete[] = [HouseholdMember::class, $memberB]; + self::$entitiesToDelete[] = [Household::class, $household]; + + yield [$personA, $personB, "move 2 people having the same household at the same time"]; + + $personA = new Person(); + $personB = new Person(); + $parcours = new AccompanyingPeriod(); + + $parcours->addPerson($personA); + $parcours->addPerson($personB); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($parcours); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [AccompanyingPeriod::class, $parcours]; + + yield [$personA, $personB, "move 2 people participating to the same parcours"]; + + $personA = new Person(); + $personB = new Person(); + $relationship = new Relationship(); + $relation = new Relation(); + $user = (new User())->setUsername(uniqid())->setEmail(uniqid() . '@foo.com'); + + $relationship->setRelation($relation); + $relationship->setToPerson($personA); + $relationship->setFromPerson($personB); + $relationship->setReverse(false); + $relationship->setCreatedBy($user); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($relation); + $this->em->persist($user); + $this->em->persist($relationship); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [Relation::class, $relation]; + self::$entitiesToDelete[] = [User::class, $user]; + self::$entitiesToDelete[] = [Relationship::class, $relationship]; + + yield [$personA, $personB, "move 2 people with a relationship"]; + + $this->em->flush(); + $this->em->clear(); + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/actions.yaml b/src/Bundle/ChillPersonBundle/config/services/actions.yaml index e4c6c6621..d6e2c80a5 100644 --- a/src/Bundle/ChillPersonBundle/config/services/actions.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/actions.yaml @@ -1,5 +1,13 @@ services: - Chill\PersonBundle\Actions\Remove\PersonMove: + _defaults: + autowire: true + autoconfigure: true + + Chill\PersonBundle\Actions\Remove\PersonMove: ~ + + Chill\PersonBundle\Actions\Remove\PersonMoveManager: arguments: - $em: '@Doctrine\ORM\EntityManagerInterface' - $eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface' \ No newline at end of file + $handlers: !tagged_iterator chill_person.person_move_handler + + Chill\PersonBundle\Actions\Remove\Handler\: + resource: '../../Actions/Remove/Handler'