Make test personMove work for centers

This commit is contained in:
2023-09-07 16:07:23 +02:00
parent 256579af89
commit 94f26df81e
4 changed files with 58 additions and 40 deletions

View File

@@ -5,10 +5,14 @@ namespace Chill\PersonBundle\Actions\Remove\Handler;
use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\Person\PersonCenterHistoryRepository;
use Doctrine\ORM\EntityManagerInterface;
class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
{
public function __construct(private PersonCenterHistoryRepository $centerHistoryRepository)
public function __construct(
private PersonCenterHistoryRepository $centerHistoryRepository,
private EntityManagerInterface $em
)
{
}
@@ -23,20 +27,23 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
$oldestDateA = null;
$oldestDateB = null;
$oldestCenterHistoryB = null;
$oldestCenterHistoryA = null;
$centerHistoriesA = $this->centerHistoryRepository->findBy(['person' => $from]);
$datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA);
if (!count($datesArrayA) === 0) {
$oldestDateA = min($datesArrayA);
foreach ($centerHistoriesA as $ch) {
if ($oldestDateA === null || ($ch->getStartDate() < $oldestDateA)) {
$oldestDateA = $ch->getStartDate();
$oldestCenterHistoryA = $ch;
}
}
$centerHistoriesB = $this->centerHistoryRepository->findBy(['person' => $to]);
$datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB);
if (!count($datesArrayB) === 0) {
$oldestDateB = min($datesArrayB);
$indexOldestCenter = array_search(min($centerHistoriesB), $centerHistoriesB);
$oldestCenterHistoryB = $centerHistoriesB[$indexOldestCenter];
foreach ($centerHistoriesB as $ch) {
if ($oldestDateB === null || ($ch->getStartDate() < $oldestDateB)) {
$oldestDateB = $ch->getStartDate();
$oldestCenterHistoryB = $ch;
}
}
$sqlDelete = sprintf("delete FROM chill_person_person_center_history WHERE person_id = %d", $from->getId());
@@ -44,7 +51,7 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
$sqlStatements = [$sqlDelete];
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());
$sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s' WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId());
$sqlStatements = [$sqlInsert, $sqlDelete];
}