Przeglądaj źródła

send confirmation email on organization creation completion

Olivier Massot 1 rok temu
rodzic
commit
f5408e4499

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

@@ -32,6 +32,13 @@ class OrganizationCreationRequest
     #[ApiProperty(identifier: true)]
     private int $id = 0;
 
+    /**
+     * A quelle adresse email notifier la création de l'organisation, ou d'éventuelles erreurs ?
+     * @var string|null
+     */
+    #[Assert\Email(message: 'The email {{ value }} is not a valid email.')]
+    private ?string $sendConfirmationEmailAt;
+
     private string $name;
 
     private LegalEnum $legalStatus;
@@ -119,6 +126,17 @@ class OrganizationCreationRequest
         return $this;
     }
 
+    public function getSendConfirmationEmailAt(): ?string
+    {
+        return $this->sendConfirmationEmailAt;
+    }
+
+    public function setSendConfirmationEmailAt(?string $sendConfirmationEmailAt): self
+    {
+        $this->sendConfirmationEmailAt = $sendConfirmationEmailAt;
+        return $this;
+    }
+
     public function getName(): string
     {
         return $this->name;

+ 30 - 3
src/Message/Handler/OrganizationCreationHandler.php

@@ -6,21 +6,48 @@ namespace App\Message\Handler;
 
 use App\Message\Command\OrganizationCreationCommand;
 use App\Service\Organization\OrganizationFactory;
+use Symfony\Component\Mailer\MailerInterface;
 use Symfony\Component\Messenger\Attribute\AsMessageHandler;
+use Symfony\Component\Mime\Address;
+use Symfony\Component\Mime\Email as SymfonyEmail;
 
 #[AsMessageHandler(priority: 1)]
 class OrganizationCreationHandler
 {
     public function __construct(
-        private readonly OrganizationFactory $organizationFactory
+        private readonly OrganizationFactory $organizationFactory,
+        private readonly MailerInterface $symfonyMailer,
     ) {}
 
     public function __invoke(OrganizationCreationCommand $organizationCreationCommand): void
     {
         $organizationCreationRequest = $organizationCreationCommand->getOrganizationCreationRequest();
+        $error = null;
 
-        $organization = $this->organizationFactory->create($organizationCreationRequest);
+        try {
+            $organization = $this->organizationFactory->create($organizationCreationRequest);
+        } catch (\Exception $e) {
+            $error = $e->getMessage();
+        }
 
-        // TODO: send confirmation email
+        if ($organizationCreationRequest->getSendConfirmationEmailAt() !== null) {
+
+            if ($error !== null) {
+                $subject = 'Organization creation : an error occured';
+                $message = 'An error occured while creating the new organization : \n' . $error;
+            } else {
+                $subject = 'New organization created';
+                $message = 'The organization "' . $organization->getName() . '" has been created successfully.';
+            }
+
+            $symfonyMail = (new SymfonyEmail())
+                ->from('mail.report@opentalent.fr')
+                ->replyTo('mail.report@opentalent.fr')
+                ->returnPath(Address::create('mail.report@opentalent.fr'))
+                ->to($organizationCreationRequest->getSendConfirmationEmailAt())
+                ->subject($subject)
+                ->text($message);
+            $this->symfonyMailer->send($symfonyMail);
+        }
     }
 }

+ 1 - 1
src/Service/Organization/OrganizationFactory.php

@@ -495,7 +495,7 @@ class OrganizationFactory
             $mobileNumber = $phoneUtil->parse($organizationMemberCreationRequest->getMobile());
             $contactPoint->setMobilPhone($mobileNumber);
         }
-        
+
         $this->entityManager->persist($contactPoint);
         return $contactPoint;
     }