Browse Source

add the identifier field to the organization creation request

Olivier Massot 1 year ago
parent
commit
29ebb20207

+ 17 - 0
src/ApiResources/Organization/OrganizationCreationRequest.php

@@ -43,6 +43,12 @@ class OrganizationCreationRequest
 
     private string $name;
 
+    /**
+     * Matricule (obligatoire dans le réseau CMF)
+     * @var string
+     */
+    private string $identifier = '';
+
     private ?LegalEnum $legalStatus = null;
 
     private SettingsProductEnum $product;
@@ -158,6 +164,17 @@ class OrganizationCreationRequest
         return $this;
     }
 
+    public function getIdentifier(): string
+    {
+        return $this->identifier;
+    }
+
+    public function setIdentifier(string $identifier): self
+    {
+        $this->identifier = $identifier;
+        return $this;
+    }
+
     public function getLegalStatus(): LegalEnum
     {
         return $this->legalStatus;

+ 10 - 0
src/Service/Organization/OrganizationFactory.php

@@ -18,6 +18,7 @@ use App\Entity\Person\Person;
 use App\Entity\Person\PersonAddressPostal;
 use App\Enum\Core\ContactPointTypeEnum;
 use App\Enum\Education\CycleEnum;
+use App\Enum\Network\NetworkEnum;
 use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
 use App\Enum\Person\AddressPostalPersonTypeEnum;
 use App\Repository\Core\CountryRepository;
@@ -33,6 +34,7 @@ use Elastica\Param;
 use libphonenumber\NumberParseException;
 use libphonenumber\PhoneNumberUtil;
 use Psr\Log\LoggerInterface;
+use SebastianBergmann\Type\RuntimeException;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\String\ByteString;
 use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
@@ -267,6 +269,7 @@ class OrganizationFactory
         $organization->setName($organizationCreationRequest->getName());
         $organization->setLegalStatus($organizationCreationRequest->getLegalStatus());
         $organization->setPrincipalType($organizationCreationRequest->getPrincipalType());
+        $organization->setIdentifier($organizationCreationRequest->getIdentifier());
 
         $this->entityManager->persist($organization);
 
@@ -374,6 +377,13 @@ class OrganizationFactory
 
         $network = $networkOrganization->getNetwork();
 
+        // Si réseau CMF, on vérifie que le matricule est valide
+        if ($network->getId() === NetworkEnum::CMF->value) {
+            if (!preg_match("/FR\d{12}/", $organizationCreationRequest->getIdentifier())) {
+                throw new \RuntimeException("CMF identifier is missing or invalid.");
+            }
+        }
+
         $networkOrganization = new NetworkOrganization();
         $networkOrganization->setParent($parent);
         $networkOrganization->setNetwork($network);

+ 58 - 1
tests/Unit/Service/Organization/OrganizationFactoryTest.php

@@ -21,6 +21,7 @@ use App\Entity\Person\Person;
 use App\Entity\Person\PersonAddressPostal;
 use App\Enum\Core\ContactPointTypeEnum;
 use App\Enum\Education\CycleEnum;
+use App\Enum\Network\NetworkEnum;
 use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
 use App\Enum\Organization\LegalEnum;
 use App\Enum\Organization\PrincipalTypeEnum;
@@ -835,7 +836,63 @@ class OrganizationFactoryTest extends TestCase
         $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
     }
 
-    public function testMakeAdminAccess()
+    public function testMakeNetworkOrganizationIsCMFInvalidIdentifier(): void {
+        $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
+
+        $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
+        $organizationCreationRequest->method('getParentId')->willReturn(123);
+        $organizationCreationRequest->method('getIdentifier')->willReturn('invalid');
+
+        $parent = $this->getMockBuilder(Organization::class)->getMock();
+        $this->organizationRepository->method('find')->with(123)->willReturn($parent);
+
+        $network = $this->getMockBuilder(Network::class)->getMock();
+        $network->method('getId')->willReturn(NetworkEnum::CMF->value);
+
+        $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization->method('getNetwork')->willReturn($network);
+
+        $this->organizationUtils
+            ->method('getActiveNetworkOrganization')
+            ->with($parent)
+            ->willReturn($networkOrganization);
+
+        $this->expectException(\RuntimeException::class);
+        $this->expectExceptionMessage("CMF identifier is missing or invalid.");
+
+        $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
+    }
+
+    public function testMakeNetworkOrganizationIsNotCMFInvalidIdentifier(): void {
+        $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
+
+        $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
+        $organizationCreationRequest->method('getParentId')->willReturn(123);
+        $organizationCreationRequest->method('getIdentifier')->willReturn('invalid');
+
+        $parent = $this->getMockBuilder(Organization::class)->getMock();
+        $this->organizationRepository->method('find')->with(123)->willReturn($parent);
+
+        $network = $this->getMockBuilder(Network::class)->getMock();
+        $network->method('getId')->willReturn(NetworkEnum::OUTOFNET->value);
+
+        $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization->method('getNetwork')->willReturn($network);
+
+        $this->organizationUtils
+            ->method('getActiveNetworkOrganization')
+            ->with($parent)
+            ->willReturn($networkOrganization);
+
+        $result = $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
+
+        $this->assertEquals(
+            $network,
+            $result->getNetwork()
+        );
+    }
+
+    public function testMakeAdminAccess(): void
     {
         $organizationFactory = $this->getOrganizationFactoryMockFor('makeAdminAccess');