Add externalId to Center entity and adapt personupertHandler

This commit is contained in:
Boris Waaub
2026-03-25 11:21:41 +01:00
parent bd4c5adfa6
commit 222777f88c
3 changed files with 100 additions and 6 deletions

View File

@@ -39,6 +39,9 @@ class Center implements HasCenterInterface, \Stringable
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN, options: ['default' => true])]
private bool $isActive = true;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, unique: true, nullable: true)]
private ?string $externalId = null;
/**
* @var Collection<int, Regroupment>
*/
@@ -124,4 +127,16 @@ class Center implements HasCenterInterface, \Stringable
return $this;
}
public function getExternalId(): ?string
{
return $this->externalId;
}
public function setExternalId(?string $externalId): self
{
$this->externalId = $externalId;
return $this;
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20260325095546 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE centers ADD externalId VARCHAR(255) DEFAULT NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_274DDB31A770AC6E ON centers (externalId)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE centers DROP externalId');
}
}

View File

@@ -111,15 +111,52 @@ readonly class PersonUpsertHandler
return count($genders) > 0 ? $genders[0] : null;
}
private function findCenterByName(?string $centerName): ?Center
/**
* Recherche ou crée un Center selon externalId (HelpIt) et nom.
* Si externalId existe, retourne le Center associé.
* Sinon, cherche par nom, sinon crée un nouveau Center.
*/
private function findOrCreateCenter(?string $externalId, ?string $centerName): object | null
{
if (null === $centerName) {
if (null === $externalId && null === $centerName) {
return null;
}
$centers = $this->centerRepository->findBy(['name' => $centerName]);
// Recherche par externalId
if (null !== $externalId) {
$center = $this->centerRepository->findOneBy(['externalId' => $externalId]);
if ($center) {
return $center;
}
}
return count($centers) > 0 ? $centers[0] : null;
// Recherche par nom
if (null !== $centerName) {
$center = $this->centerRepository->findOneBy(['name' => $centerName]);
if ($center) {
// On met à jour l'externalId si besoin
if (null !== $externalId && $center->getExternalId() !== $externalId) {
$center->setExternalId($externalId);
$this->entityManager->persist($center);
}
return $center;
}
}
// Création si rien trouvé
if (null !== $centerName) {
$center = new Center();
$center->setName($centerName);
if (null !== $externalId) {
$center->setExternalId($externalId);
}
$this->entityManager->persist($center);
return $center;
}
return null;
}
private function handlePersonMessage(UpsertMessage $message, Person $person): Person
@@ -198,8 +235,12 @@ readonly class PersonUpsertHandler
$person->setGender($gender);
}
// Handle center
$center = $this->findCenterByName($message->center);
// Handle center (territoire)
// On suppose que UpsertMessage contient $centerExternalId et $center (nom)
$center = $this->findOrCreateCenter(
property_exists($message, 'centerExternalId') ? $message->centerExternalId : null,
$message->center
);
if (null !== $center) {
$person->setCenter($center);
}