From 2921073da3ed8dea09ba7d6a2e5348f707a0a629 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 27 Jul 2023 10:34:19 +0200 Subject: [PATCH] [centerHistory] add conditions to be sure at least one center history exists per person --- .../Remove/Handler/PersonMoveCenterHistoryHandler.php | 7 ++++--- .../Tests/Action/Remove/PersonMoveTest.php | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index 1c1b2f8f0..53ec57843 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -20,10 +20,11 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface public function getSqls(string $className, string $field, Person $from, Person $to): array { $sqlStatements = []; + $oldestDateA = null; + $oldestDateB = null; $centerHistoriesA = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $from->getId()]); // $centerHistoriesA = $from->getCenterHistory()->toArray(); -// var_dump($centerHistoriesA); $datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA); if (!count($datesArrayA) === 0) { $oldestDateA = min($datesArrayA); @@ -31,7 +32,6 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $centerHistoriesB = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $to->getId()]); // $centerHistoriesB = $to->getCenterHistory()->toArray(); -// var_dump($centerHistoriesB); $datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB); if (!count($datesArrayB) === 0) { @@ -45,12 +45,13 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $sqlStatements = [$sqlDelete]; - if ($oldestDateA <= $oldestDateB) { + if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) { $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); $sqlStatements = [$sqlInsert, $sqlDelete]; } + var_dump($sqlStatements); return $sqlStatements; } diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index fa6c5faa9..613baaf44 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -61,11 +61,11 @@ class PersonMoveTest extends KernelTestCase $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); $conn = $this->em->getConnection(); -// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { $conn->executeStatement($sql); } -// }); + }); $personA = $this->em->find(Person::class, $personA->getId()); $personB = $this->em->find(Person::class, $personB->getId()); @@ -100,11 +100,11 @@ class PersonMoveTest extends KernelTestCase $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); $conn = $this->em->getConnection(); -// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { $conn->executeStatement($sql); } -// }); + }); $personA = $this->em->find(Person::class, $personA->getId()); $personB = $this->em->find(Person::class, $personB->getId());