Compare commits

..

2 Commits

3 changed files with 114 additions and 11 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

@@ -54,7 +54,7 @@ readonly class PersonUpsertHandler
private ValidatorInterface $validator,
) {}
private function createAddressWithMessage(UpsertMessage $message): Address
private function createAddressWithMessage(UpsertMessage $message): ?Address
{
$newAddress = new Address();
if (null !== $message->addressStreet) {
@@ -65,9 +65,14 @@ readonly class PersonUpsertHandler
}
if (null !== $message->addressPostcode) {
$postalCode = $this->postalCodeRepository->findOneBy(['code' => $message->addressPostcode]);
if (null !== $postalCode) {
$newAddress->setPostcode($postalCode);
if (null === $postalCode) {
// Si aucun code postal trouvé, on ne gère pas l'adresse
return null;
}
$newAddress->setPostcode($postalCode);
} else {
// Si pas de postcode fourni, on ne gère pas l'adresse
return null;
}
if (null !== $message->addressExtra) {
$newAddress->setExtra($message->addressExtra);
@@ -111,15 +116,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
{
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
@@ -136,7 +178,9 @@ readonly class PersonUpsertHandler
$messageAddressMatch = $this->isMessageAddressMatch($lastCurrentAddress, $message);
if (!$messageAddressMatch) {
$newAddress = $this->createAddressWithMessage($message);
$currentHousehold->addAddress($newAddress);
if (null !== $newAddress) {
$currentHousehold->addAddress($newAddress);
}
}
}
} elseif (null === $currentHousehold && $hasAddressInfo) {
@@ -145,7 +189,9 @@ readonly class PersonUpsertHandler
// Create the new address
$newAddress = $this->createAddressWithMessage($message);
$newHousehold->addAddress($newAddress);
if (null !== $newAddress) {
$newHousehold->addAddress($newAddress);
}
// Create a MembersEditor with the new household and add the person
$membersEditor = $this->membersEditorFactory->createEditor($newHousehold);
@@ -198,8 +244,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);
}