Bläddra i källkod

fix persons creation

Olivier Massot 1 år sedan
förälder
incheckning
ef2a4a4850

+ 3 - 1
src/ApiResources/Organization/OrganizationMemberCreationRequest.php

@@ -2,6 +2,7 @@
 
 namespace App\ApiResources\Organization;
 
+use App\Enum\Core\FileTypeEnum;
 use App\Enum\Person\GenderEnum;
 use Symfony\Component\Validator\Constraints as Assert;
 
@@ -10,7 +11,8 @@ use Symfony\Component\Validator\Constraints as Assert;
  */
 class OrganizationMemberCreationRequest
 {
-    private GenderEnum $gender;
+    #[Assert\Type(type: FileTypeEnum::class)]
+    private GenderEnum $gender = GenderEnum::MISTER;
 
     #[Assert\Regex(pattern: '/^[a-z0-9\-]{3,}$/')]
     private string $username;

+ 2 - 2
src/Entity/Person/Person.php

@@ -62,7 +62,7 @@ class Person implements UserInterface, PasswordAuthenticatedUserInterface
     #[Groups(['access_people_ref', 'access_address'])]
     private ?string $givenName = null;
 
-    #[ORM\ManyToMany(targetEntity: ContactPoint::class, mappedBy: 'person')]
+    #[ORM\ManyToMany(targetEntity: ContactPoint::class, mappedBy: 'person', cascade: ['persist'], orphanRemoval: true)]
     private Collection $contactPoints;
 
     #[ORM\ManyToMany(targetEntity: BankAccount::class, inversedBy: 'person', cascade: ['persist'], orphanRemoval: true)]
@@ -70,7 +70,7 @@ class Person implements UserInterface, PasswordAuthenticatedUserInterface
     #[ORM\InverseJoinColumn(name: 'bankAccount_id', referencedColumnName: 'id')]
     private Collection $bankAccount;
 
-    #[ORM\OneToMany(mappedBy: 'person', targetEntity: PersonAddressPostal::class, orphanRemoval: true)]
+    #[ORM\OneToMany(mappedBy: 'person', targetEntity: PersonAddressPostal::class, cascade: ['persist'], orphanRemoval: true)]
     #[Groups('access_address')]
     private Collection $personAddressPostal;
 

+ 6 - 2
src/Service/Organization/OrganizationFactory.php

@@ -587,6 +587,10 @@ class OrganizationFactory
         } else {
             $person = new Person();
 
+            if ($this->personRepository->findOneBy(['username' => $creationRequestData->getUsername()])) {
+                throw new \RuntimeException('Username already in use: ' . $creationRequestData->getUsername());
+            }
+
             $person->setUsername($creationRequestData->getUsername());
             $person->setPassword(ByteString::fromRandom(32)->toString());
             $person->setGender($creationRequestData->getGender());
@@ -657,12 +661,12 @@ class OrganizationFactory
     protected function makePersonContactPoint(OrganizationMemberCreationRequest $organizationMemberCreationRequest): ContactPoint
     {
         if (!$this->phoneNumberUtil->isPossibleNumber($organizationMemberCreationRequest->getPhone())) {
-            throw new \RuntimeException('Phone number is invalid or missing');
+            throw new \RuntimeException('Phone number is invalid or missing (person: ' . $organizationMemberCreationRequest->getUsername() . ')');
         }
         if (
             $organizationMemberCreationRequest->getMobile() !== null &&
             !$this->phoneNumberUtil->isPossibleNumber($organizationMemberCreationRequest->getMobile())) {
-            throw new \RuntimeException('Mobile phone number is invalid');
+            throw new \RuntimeException('Mobile phone number is invalid (person: ' . $organizationMemberCreationRequest->getUsername() . ')');
         }
 
         $phoneNumber = $this->phoneNumberUtil->parse($organizationMemberCreationRequest->getPhone());