Browse Source

make organization deletion operations non-blocking

Olivier Massot 1 year ago
parent
commit
6f6687d56a

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

@@ -21,6 +21,10 @@ use Symfony\Component\Validator\Constraints as Assert;
 )]
 class OrganizationDeletionRequest
 {
+    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.
@@ -37,6 +41,12 @@ class OrganizationDeletionRequest
     #[Assert\Email(message: 'The email {{ value }} is not a valid email.')]
     private ?string $sendConfirmationEmailAt = null;
 
+    /**
+     * Statut de l'opération
+     * @var string
+     */
+    private string $status = self::STATUS_PENDING;
+
     /**
      * For testing purposes only
      * @var bool
@@ -76,6 +86,17 @@ class OrganizationDeletionRequest
         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;

+ 65 - 34
src/Service/Organization/OrganizationFactory.php

@@ -592,7 +592,7 @@ class OrganizationFactory
     /**
      * /!\ Danger zone /!\
      *
-     * @param Organization $organization
+     * @param OrganizationDeletionRequest $organizationDeletionRequest
      * @return void
      * @throws Exception
      */
@@ -606,11 +606,15 @@ class OrganizationFactory
 
         $this->entityManager->beginTransaction();
 
+        $withError = false;
+
+        $personsIds = [];
+
         try {
-            $this->deleteOrganizationAccesses($organization);
-            $this->deleteNetworkOrganizationsChildren($organization);
-            $this->deleteParameters($organization);
-            $this->deleteOrganizationFiles($organization);
+//            $this->deleteOrganizationAccesses($organization);
+
+            // TODO: voir si nécessaire :
+//            $this->deleteParameters($organization);
 
             $this->entityManager->remove($organization);
 
@@ -622,11 +626,52 @@ class OrganizationFactory
             throw $e;
         }
 
-//        $this->deleteTypo3Website($organization);
+        try {
+            $this->deleteTypo3Website($organization);
+        } catch (\Exception $e) {
+            $this->logger->critical("An error happened while deleting the Typo3 website, please proceed manually.");
+            $this->logger->debug($e);
+            $withError = true;
+        }
+
+        try {
+            $this->deleteDolibarrSociety($organization);
+        } catch (\Exception $e) {
+            $this->logger->critical("An error happened while deleting the Dolibarr society, please proceed manually.");
+            $this->logger->debug($e);
+            $withError = true;
+        }
+
+        try {
+            $this->deleteLocalDirectories($organization);
+        } catch (\Exception $e) {
+            $this->logger->critical("An error happened while deleting the Dolibarr society, please proceed manually.");
+            $this->logger->debug($e);
+            $withError = true;
+        }
 
-//        $this->deleteDolibarrSociety($organization);
+        try {
+            $this->deleteDirectoriesV1($organization);
+        } catch (\Exception $e) {
+            $this->logger->critical("An error happened while deleting the Dolibarr society, please proceed manually.");
+            $this->logger->debug($e);
+            $withError = true;
+        }
+
+        try {
+            $this->deleteDirectories59($organization);
+        } catch (\Exception $e) {
+            $this->logger->critical("An error happened while deleting the Dolibarr society, please proceed manually.");
+            $this->logger->debug($e);
+            $withError = true;
+        }
 
-//        $this->deleteDirectories($organization);
+        if ($withError) {
+            $organizationCreationRequest->setStatus(OrganizationDeletionRequest::STATUS_OK_WITH_ERRORS);
+            $this->logger->warning("-- Operation ended with errors, check the logs for more information --");
+        } else {
+            $organizationCreationRequest->setStatus(OrganizationDeletionRequest::STATUS_OK);
+        }
     }
 
     /**
@@ -641,37 +686,16 @@ class OrganizationFactory
         foreach ($organization->getAccesses() as $access) {
             $person = $access->getPerson();
             if ($person->getAccesses()->count() === 1) {
-                $this->deletePerson($person);
+                $this->entityManager->remove($person);
             }
             $this->entityManager->remove($access);
         }
     }
 
-    protected function deletePerson(Person $person): void {
-        foreach ($person->getPersonFiles() as $personFile) {
-            $this->entityManager->remove($personFile);
-        }
-
-        $this->entityManager->remove($person);
-    }
-
-    protected function deleteNetworkOrganizationsChildren(Organization $organization): void {
-        foreach ($organization->getNetworkOrganizationChildren() as $networkOrganization) {
-            $this->entityManager->remove($networkOrganization);
-        }
-    }
-
     protected function deleteParameters(Organization $organization): void {
         $this->entityManager->remove($organization->getParameters());
     }
 
-    // TODO: nettoyer les fichiers sur le disque aussi
-    protected function deleteOrganizationFiles(Organization $organization): void {
-        foreach ($organization->getFiles() as $file) {
-            $this->entityManager->remove($file);
-        }
-    }
-
     // TODO: à revoir, c'est du many to many
 //    protected function removeTypeOfPractices(Organization $organization): void {
 //        foreach ($organization->getTypeOfPractices() as $typeOfPractice) {
@@ -696,7 +720,8 @@ class OrganizationFactory
 
     protected function deleteTypo3Website(Organization $organization): void
     {
-        $this->typo3Service->deleteSite($organization->getId());
+        // TODO: implement
+//        $this->typo3Service->deleteSite($organization->getId());
     }
 
     protected function deleteDolibarrSociety(Organization $organization): void
@@ -704,12 +729,18 @@ class OrganizationFactory
         // TODO: implement
     }
 
-    protected function deleteDirectories(Organization $organization): void
+    protected function deleteLocalDirectories(Organization $organization): void
     {
         // TODO: implement
     }
 
+    protected function deleteDirectoriesV1(Organization $organization): void
+    {
+        // TODO: implement
+    }
 
-
-
+    protected function deleteDirectories59(Organization $organization): void
+    {
+        // TODO: implement
+    }
 }