diff --git a/src/Bundle/ChillPersonBundle/Actions/Upsert/Handler/PersonUpsertHandler.php b/src/Bundle/ChillPersonBundle/Actions/Upsert/Handler/PersonUpsertHandler.php index d34c6935d..fa099cff9 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Upsert/Handler/PersonUpsertHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Upsert/Handler/PersonUpsertHandler.php @@ -12,10 +12,12 @@ declare(strict_types=1); namespace Chill\PersonBundle\Actions\Upsert\Handler; use Chill\MainBundle\Entity\Address; +use Chill\MainBundle\Entity\GenderEnum; +use Chill\MainBundle\Repository\CenterRepository; +use Chill\MainBundle\Repository\GenderRepository; use Chill\MainBundle\Repository\PostalCodeRepositoryInterface; use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Household\MembersEditorFactory; -use Chill\PersonBundle\Repository\Household\PositionRepository; use Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository; use Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository; use Chill\PersonBundle\Entity\Person; @@ -34,7 +36,8 @@ readonly class PersonUpsertHandler private EntityManagerInterface $entityManager, private MembersEditorFactory $membersEditorFactory, private PostalCodeRepositoryInterface $postalCodeRepository, - private PositionRepository $positionRepository, + private CenterRepository $centerRepository, + private GenderRepository $genderRepository, private ClockInterface $clock, ) {} @@ -102,25 +105,18 @@ readonly class PersonUpsertHandler $newAddress = $this->createAddressWithMessage($message); $newHousehold->addAddress($newAddress); - // Find a default position (first one ordered by ordering) - $positions = $this->positionRepository->findByActiveOrdered(); - $defaultPosition = count($positions) > 0 ? $positions[0] : null; - // Create a MembersEditor with the new household and add the person $membersEditor = $this->membersEditorFactory->createEditor($newHousehold); $membersEditor->addMovement( $this->clock->now(), $person, - $defaultPosition, + null, true ); // Validate the membership $violations = $membersEditor->validate(); - if ($violations->count() > 0) { - // Handle validation errors - for now we'll just skip household creation - // In production, you might want to log this or throw an exception - } else { + if (0 === $violations->count()) { // Persist the household and trigger events $this->entityManager->persist($newHousehold); foreach ($membersEditor->getPersistable() as $persistable) { @@ -138,7 +134,36 @@ readonly class PersonUpsertHandler $person->setLastName($message->lastName); } - // @todo add gender and center + // Handle gender + $gender = null; + if (null !== $message->gender) { + // Try to find gender by the provided value + foreach (GenderEnum::cases() as $case) { + if ($case->value === $message->gender) { + $genders = $this->genderRepository->findByGenderTranslation($case); + $gender = count($genders) > 0 ? $genders[0] : null; + break; + } + } + } + + // If no gender found or provided, use neutral as default + if (null === $gender) { + $genders = $this->genderRepository->findByGenderTranslation(GenderEnum::NEUTRAL); + $gender = count($genders) > 0 ? $genders[0] : null; + } else { + $person->setGender($gender); + } + + // Handle center + if (null !== $message->center) { + $centers = $this->centerRepository->findBy(['name' => $message->center]); + $center = count($centers) > 0 ? $centers[0] : null; + if (null !== $center) { + $person->setCenter($center); + } + } + return $person; }