Browse Source

handle errors on structure creation

Olivier Massot 5 months ago
parent
commit
c211a2a5fa

+ 29 - 19
src/Service/Shop/ShopService.php

@@ -51,7 +51,7 @@ class ShopService
         private EntityManagerInterface $entityManager,
         private EntityManagerInterface $entityManager,
         private Mailer $mailer,
         private Mailer $mailer,
         private string $publicBaseUrl,
         private string $publicBaseUrl,
-        private string $publicAdminBaseUrl,
+        private string $adminBaseUrl,
         private OrganizationFactory $organizationFactory,
         private OrganizationFactory $organizationFactory,
         private SerializerInterface $serializer,
         private SerializerInterface $serializer,
         private LoggerInterface $logger,
         private LoggerInterface $logger,
@@ -191,29 +191,39 @@ class ShopService
             return;
             return;
         }
         }
 
 
-        // Convert the stored JSON data to a NewStructureArtistPremiumTrialRequest object
-        $data = $shopRequest->getData();
-        $trialRequest = $this->serializer->deserialize(
-            json_encode($data),
-            NewStructureArtistPremiumTrialRequest::class,
-            'json'
-        );
+        try {
+            // Convert the stored JSON data to a NewStructureArtistPremiumTrialRequest object
+            $data = $shopRequest->getData();
+            $trialRequest = $this->serializer->deserialize(
+                json_encode($data),
+                NewStructureArtistPremiumTrialRequest::class,
+                'json'
+            );
+
+            $organization = $this->createOrganization($trialRequest);
 
 
-        $organization = $this->createOrganization($trialRequest);
+            // Set the admin account password
+            $this->organizationFactory->setAdminAccountPassword($organization, $trialRequest->getPassword());
 
 
-        // Set the admin account password
-        $this->organizationFactory->setAdminAccountPassword($organization, $trialRequest->getPassword());
+            // Start the artist premium trial
+            $this->trial->startArtistPremiumTrialForNewStructure($organization, $trialRequest);
 
 
-        // Start the artist premium trial
-        $this->trial->startArtistPremiumTrialForNewStructure($organization, $trialRequest);
+            // Send email to sales administration
+            $this->sendMailToSalesAdministration($trialRequest);
 
 
-        // Send email to sales administration
-        $this->sendMailToSalesAdministration($trialRequest);
+            // Send email to representative
+            $this->sendConfirmationMailToRepresentative($trialRequest);
 
 
-        // Send email to representative
-        $this->sendConfirmationMailToRepresentative($trialRequest);
+            $this->logger->info('Successfully processed NewStructureArtistPremiumTrial for token: '.$token);
+        } catch (\Throwable $e) {
+            $shopRequest->setStatus(ShopRequestStatus::ERROR);
+            $this->entityManager->persist($shopRequest);
+            $this->entityManager->flush();
 
 
-        $this->logger->info('Successfully processed NewStructureArtistPremiumTrial for token: '.$token);
+            $this->logger->error('Error processing NewStructureArtistPremiumTrial for token: '.$token.'. Error: '.$e->getMessage());
+
+            throw $e;
+        }
     }
     }
 
 
     /**
     /**
@@ -324,7 +334,7 @@ class ShopService
         $adminUsername = 'admin' . $trialRequest->getStructureIdentifier();
         $adminUsername = 'admin' . $trialRequest->getStructureIdentifier();
 
 
         // Create the admin login URL
         // Create the admin login URL
-        $adminLoginUrl = UrlBuilder::concat($this->publicAdminBaseUrl, ['#/login/']);
+        $adminLoginUrl = UrlBuilder::concat($this->adminBaseUrl, ['#/login/']);
 
 
         // Create the email model
         // Create the email model
         $model = new ConfirmationToRepresentativeModel();
         $model = new ConfirmationToRepresentativeModel();

+ 4 - 4
templates/emails/shop/new-structure-artist-premium-trial-account-creation.html.twig

@@ -7,12 +7,12 @@
 
 
     <p>Votre demande d'essai pour Opentalent Artist Premium a été validée.</p>
     <p>Votre demande d'essai pour Opentalent Artist Premium a été validée.</p>
 
 
-    <p>Votre compte administrateur a été créé avec l'identifiant <strong>{{ adminUsername }}</strong>.</p>
+    <p>Votre compte administrateur a été créé avec l'identifiant <code style="background-color: #f4f4f4; padding: 3px 5px; border-radius: 4px; font-family: monospace; border: 1px solid #ddd;">{{ adminUsername }}</code>.</p>
 
 
     <p>Pour accéder à votre espace, veuillez cliquer sur le lien ci-dessous :</p>
     <p>Pour accéder à votre espace, veuillez cliquer sur le lien ci-dessous :</p>
 
 
     <p style="text-align: center; margin: 30px 0;">
     <p style="text-align: center; margin: 30px 0;">
-        <a href="{{ adminLoginUrl }}" style="background-color: #4CAF50; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 10px;">
+        <a href="{{ adminLoginUrl }}" style="background-color: #4CAF50; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 6px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); border: 1px solid #43a047; font-weight: bold; letter-spacing: 0.5px; transition: all 0.3s;">
             Accéder au logiciel
             Accéder au logiciel
         </a>
         </a>
     </p>
     </p>
@@ -20,8 +20,8 @@
     <p>Besoin d'aide ? Consultez notre FAQ complète pour découvrir toutes les fonctionnalités du logiciel et trouver les réponses à vos questions :</p>
     <p>Besoin d'aide ? Consultez notre FAQ complète pour découvrir toutes les fonctionnalités du logiciel et trouver les réponses à vos questions :</p>
 
 
     <p style="text-align: center; margin: 30px 0;">
     <p style="text-align: center; margin: 30px 0;">
-        <a href="{{ faqUrl }}" style="background-color: #008CBA; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 10px;">
-            Tout savoir Opentalent Artist
+        <a href="{{ faqUrl }}" style="background-color: #008CBA; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 6px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); border: 1px solid #0078a8; font-weight: bold; letter-spacing: 0.5px; transition: all 0.3s;">
+            Tout savoir sur Opentalent Artist
         </a>
         </a>
     </p>
     </p>
 
 

+ 58 - 0
tests/Unit/Service/Shop/ShopServiceTest.php

@@ -385,6 +385,64 @@ class ShopServiceTest extends TestCase
         $shopService->handleNewStructureArtistPremiumTrialRequest('non-existent-token');
         $shopService->handleNewStructureArtistPremiumTrialRequest('non-existent-token');
     }
     }
 
 
+    /**
+     * Test handleNewStructureArtistPremiumTrialRequest method when an exception occurs.
+     */
+    public function testHandleNewStructureArtistPremiumTrialRequestWithException(): void
+    {
+        $shopService = $this->getShopServiceMockFor('handleNewStructureArtistPremiumTrialRequest');
+
+        $token = 'test-token';
+        $data = [
+            'representativeEmail' => 'test@example.com',
+            'representativeFirstName' => 'John',
+            'representativeLastName' => 'Doe',
+            'structureName' => 'Test Structure',
+        ];
+
+        $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
+        $shopRequest->method('getToken')->willReturn($token);
+        $shopRequest->method('getData')->willReturn($data);
+
+        // Expect setStatus to be called with ERROR
+        $shopRequest->expects(self::once())
+            ->method('setStatus')
+            ->with(ShopRequestStatus::ERROR);
+
+        $this->entityManager
+            ->expects(self::once())
+            ->method('find')
+            ->with(ShopRequest::class, $token)
+            ->willReturn($shopRequest);
+
+        // Simulate an exception during deserialization
+        $this->serializer
+            ->expects(self::once())
+            ->method('deserialize')
+            ->willThrowException(new \Exception('Test exception'));
+
+        // Expect persist and flush to be called to save the ERROR status
+        $this->entityManager
+            ->expects(self::once())
+            ->method('persist')
+            ->with($shopRequest);
+
+        $this->entityManager
+            ->expects(self::once())
+            ->method('flush');
+
+        // Expect error to be logged
+        $this->logger->expects(self::once())
+            ->method('error')
+            ->with(self::stringContains('Error processing NewStructureArtistPremiumTrial for token: '.$token));
+
+        // Expect the exception to be re-thrown
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Test exception');
+
+        $shopService->handleNewStructureArtistPremiumTrialRequest($token);
+    }
+
     /**
     /**
      * Test createOrganization method.
      * Test createOrganization method.
      */
      */