Browse Source

organization creation : make post operations non blocking

Olivier Massot 1 year ago
parent
commit
5a24b84766

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

@@ -27,6 +27,10 @@ class OrganizationCreationRequest
 {
     private const FRANCE_COUNTRY_INTERNAL_ID = 72;
 
+    public const STATUS_PENDING = 'pending';
+    public const STATUS_OK = 'ok';
+    public const STATUS_OK_WITH_ERRORS = 'ok with errors';
+
     /**
      * Id 'bidon' ajouté par défaut pour permettre la construction
      * de l'IRI par api platform.
@@ -125,6 +129,12 @@ class OrganizationCreationRequest
      */
     private bool $client = false;
 
+    /**
+     * Statut de l'opération
+     * @var int
+     */
+    private string $status = self::STATUS_PENDING;
+
     /**
      * For testing purposes only
      * @var bool
@@ -362,6 +372,17 @@ class OrganizationCreationRequest
         return $this;
     }
 
+    public function getStatus(): string
+    {
+        return $this->status;
+    }
+
+    public function setStatus(string $status): self
+    {
+        $this->status = $status;
+        return $this;
+    }
+
     public function isAsync(): bool
     {
         return $this->async;

+ 35 - 9
src/Service/Organization/OrganizationFactory.php

@@ -113,24 +113,50 @@ class OrganizationFactory
             throw $e;
         }
 
+        $withError = false;
+
         // Création de la société Dolibarr
-        $dolibarrId = $this->dolibarrApiService->createSociety(
-            $organization,
-            $organizationCreationRequest->isClient()
-        );
-        $this->logger->info("New dolibarr structure created (uid : " . $dolibarrId . ")");
+        try {
+            $dolibarrId = $this->dolibarrApiService->createSociety(
+                $organization,
+                $organizationCreationRequest->isClient()
+            );
+            $this->logger->info("New dolibarr structure created (uid : " . $dolibarrId . ")");
+        } catch (\Exception $e) {
+            $this->logger->critical("An error happened while creating the dolibarr society, please proceed manually.");
+            $this->logger->debug($e);
+            $withError = true;
+        }
 
         // Register the subdomain into the BindFile (takes up to 5min to take effect)
-        $this->bindFileService->registerSubdomain($organizationCreationRequest->getSubdomain());
-        $this->logger->info("Subdomain registered");
-
+        try {
+            $this->bindFileService->registerSubdomain($organizationCreationRequest->getSubdomain());
+            $this->logger->info("Subdomain registered");
+        } catch (\Exception $e) {
+            $this->logger->critical("An error happened while updating the bind file, please proceed manually.");
+            $this->logger->debug($e);
+            $withError = true;
+        }
         // Création du site typo3 (on est obligé d'attendre que l'organisation soit persistée en base)
         if ($organizationCreationRequest->getCreateWebsite()) {
-            $this->createTypo3Website($organization);
+            try {
+                $this->createTypo3Website($organization);
+            } catch (\Exception $e) {
+                $this->logger->critical("An error happened while creating the typo3 website, please proceed manually.");
+                $this->logger->debug($e);
+                $withError = true;
+            }
         } else {
             $this->logger->warning("Typo3 website creation was not required");
         }
 
+        if ($withError) {
+            $organizationCreationRequest->setStatus(OrganizationCreationRequest::STATUS_OK_WITH_ERRORS);
+            $this->logger->warning("-- Operation ended with errors, check the logs for more information --");
+        } else {
+            $organizationCreationRequest->setStatus(OrganizationCreationRequest::STATUS_OK);
+        }
+
         return $organization;
     }