From 222777f88c61700fedacbf6242a47e3dcd1fbd62 Mon Sep 17 00:00:00 2001 From: Boris Waaub Date: Wed, 25 Mar 2026 11:21:41 +0100 Subject: [PATCH] Add externalId to Center entity and adapt personupertHandler --- src/Bundle/ChillMainBundle/Entity/Center.php | 15 ++++++ .../migrations/Version20260325095546.php | 38 +++++++++++++ .../Upsert/Handler/PersonUpsertHandler.php | 53 ++++++++++++++++--- 3 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/migrations/Version20260325095546.php diff --git a/src/Bundle/ChillMainBundle/Entity/Center.php b/src/Bundle/ChillMainBundle/Entity/Center.php index 1ac7f5977..2b289fc9e 100644 --- a/src/Bundle/ChillMainBundle/Entity/Center.php +++ b/src/Bundle/ChillMainBundle/Entity/Center.php @@ -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 */ @@ -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; + } } diff --git a/src/Bundle/ChillMainBundle/migrations/Version20260325095546.php b/src/Bundle/ChillMainBundle/migrations/Version20260325095546.php new file mode 100644 index 000000000..d089a56a2 --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20260325095546.php @@ -0,0 +1,38 @@ +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'); + } +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Upsert/Handler/PersonUpsertHandler.php b/src/Bundle/ChillPersonBundle/Actions/Upsert/Handler/PersonUpsertHandler.php index 6629cd63b..ab8be0101 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Upsert/Handler/PersonUpsertHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Upsert/Handler/PersonUpsertHandler.php @@ -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); }