فهرست منبع

add a Notifier service

Olivier Massot 3 سال پیش
والد
کامیت
5d82ca71ae
4فایلهای تغییر یافته به همراه189 افزوده شده و 5 حذف شده
  1. 70 1
      src/Entity/Core/Notification.php
  2. 5 1
      src/Enum/Core/NotificationTypeEnum.php
  3. 12 3
      src/Message/Handler/ExportHandler.php
  4. 102 0
      src/Service/Notifier.php

+ 70 - 1
src/Entity/Core/Notification.php

@@ -6,6 +6,7 @@ namespace App\Entity\Core;
 
 
 use ApiPlatform\Core\Annotation\ApiResource;
 use ApiPlatform\Core\Annotation\ApiResource;
 use App\Entity\Access\Access;
 use App\Entity\Access\Access;
+use App\Entity\Organization\Organization;
 use App\Repository\Core\NotificationRepository;
 use App\Repository\Core\NotificationRepository;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\Collection;
 use Doctrine\Common\Collections\Collection;
@@ -48,9 +49,19 @@ class Notification
     #[ORM\JoinColumn(nullable: false)]
     #[ORM\JoinColumn(nullable: false)]
     private ?Access $recipientAccess;
     private ?Access $recipientAccess;
 
 
+    #[ORM\ManyToOne(inversedBy: 'notifications')]
+    #[ORM\JoinColumn(nullable: false)]
+    private ?Organization $recipientOrganization;
+
     #[ORM\Column(length: 40, nullable: true)]
     #[ORM\Column(length: 40, nullable: true)]
     private ?string $name = null;
     private ?string $name = null;
 
 
+    #[ORM\Column(type: 'date', nullable: true)]
+    private ?\DateTimeInterface $createDate;
+
+    #[ORM\Column(type: 'date', nullable: true)]
+    private ?\DateTimeInterface $updateDate;
+
     #[ORM\Column(type: 'json', length: 4294967295, nullable: true)]
     #[ORM\Column(type: 'json', length: 4294967295, nullable: true)]
     private mixed $message = [];
     private mixed $message = [];
 
 
@@ -67,6 +78,10 @@ class Notification
     #[ORM\OneToMany(mappedBy: 'notification', targetEntity: NotificationUser::class, cascade: ['persist'], orphanRemoval: true)]
     #[ORM\OneToMany(mappedBy: 'notification', targetEntity: NotificationUser::class, cascade: ['persist'], orphanRemoval: true)]
     private Collection $notificationUsers;
     private Collection $notificationUsers;
 
 
+    // Todo: le champs name sert à qqchose au final?
+    // Todo: les champs activate, isSystem, legacyId, createdBy, updatedBy, draft, content, priorityLevelEnum, datetimeStart, datetimeEnd et legalStatus sont inutilisés, on les garde ou pas ?
+    // Todo: j'imagine que le champs tipsAccessFilters ne s'applique pas aux notifs?
+
     #[Pure] public function __construct()
     #[Pure] public function __construct()
     {
     {
         $this->notificationUsers = new ArrayCollection();
         $this->notificationUsers = new ArrayCollection();
@@ -99,6 +114,60 @@ class Notification
         return $this->recipientAccess;
         return $this->recipientAccess;
     }
     }
 
 
+    /**
+     * @return Organization|null
+     */
+    public function getRecipientOrganization(): ?Organization
+    {
+        return $this->recipientOrganization;
+    }
+
+    /**
+     * @param Organization|null $recipientOrganization
+     */
+    public function setRecipientOrganization(?Organization $recipientOrganization): self
+    {
+        $this->recipientOrganization = $recipientOrganization;
+
+        return $this;
+    }
+
+    /**
+     * @return \DateTimeInterface|null
+     */
+    public function getCreateDate(): ?\DateTimeInterface
+    {
+        return $this->createDate;
+    }
+
+    /**
+     * @param \DateTimeInterface|null $createDate
+     */
+    public function setCreateDate(?\DateTimeInterface $createDate): self
+    {
+        $this->createDate = $createDate;
+
+        return $this;
+    }
+
+    /**
+     * @return \DateTimeInterface|null
+     */
+    public function getUpdateDate(): ?\DateTimeInterface
+    {
+        return $this->updateDate;
+    }
+
+    /**
+     * @param \DateTimeInterface|null $updateDate
+     */
+    public function setUpdateDate(?\DateTimeInterface $updateDate): self
+    {
+        $this->updateDate = $updateDate;
+
+        return $this;
+    }
+
     public function setMessage(mixed $message): self
     public function setMessage(mixed $message): self
     {
     {
         $this->message = $message;
         $this->message = $message;
@@ -171,4 +240,4 @@ class Notification
 
 
         return $this;
         return $this;
     }
     }
-}
+}

+ 5 - 1
src/Enum/Core/NotificationTypeEnum.php

@@ -8,6 +8,10 @@ use MyCLabs\Enum\Enum;
 /**
 /**
  * Type de notifications
  * Type de notifications
  *
  *
+ * @method static FILE()
+ * @method static MESSAGE()
+ * @method static SYSTEM()
+ * @method static ERROR()
  */
  */
 class NotificationTypeEnum extends Enum
 class NotificationTypeEnum extends Enum
 {
 {
@@ -16,4 +20,4 @@ class NotificationTypeEnum extends Enum
     private const MESSAGE = 'MESSAGE';
     private const MESSAGE = 'MESSAGE';
     private const PRINTING = 'PRINTING';
     private const PRINTING = 'PRINTING';
     private const ERROR = 'ERROR';
     private const ERROR = 'ERROR';
-}
+}

+ 12 - 3
src/Message/Handler/ExportHandler.php

@@ -4,17 +4,19 @@ declare(strict_types=1);
 namespace App\Message\Handler;
 namespace App\Message\Handler;
 
 
 use App\Message\Command\Export;
 use App\Message\Command\Export;
+use App\Repository\Access\AccessRepository;
 use App\Service\MercureHub;
 use App\Service\MercureHub;
+use App\Service\Notifier;
 use App\Service\ServiceIterator\ExporterIterator;
 use App\Service\ServiceIterator\ExporterIterator;
-use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
 use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
 use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
-use Symfony\Component\Serializer\Exception\ExceptionInterface;
 
 
 class ExportHandler implements MessageHandlerInterface
 class ExportHandler implements MessageHandlerInterface
 {
 {
     public function __construct(
     public function __construct(
         private ExporterIterator $handler,
         private ExporterIterator $handler,
-        private MercureHub $mercureHub
+        private MercureHub $mercureHub,
+        private AccessRepository $accessRepository,
+        private Notifier $notifier
     ) {}
     ) {}
 
 
     public function __invoke(Export $export)
     public function __invoke(Export $export)
@@ -24,5 +26,12 @@ class ExportHandler implements MessageHandlerInterface
         $file = $exportService->export($exportRequest);
         $file = $exportService->export($exportRequest);
 
 
         $this->mercureHub->publishUpdate($exportRequest->getRequesterId(), $file);
         $this->mercureHub->publishUpdate($exportRequest->getRequesterId(), $file);
+
+        $this->notifier->notifyExport(
+            $this->accessRepository->find($exportRequest->getRequesterId()),
+            $file
+        );
     }
     }
+
+
 }
 }

+ 102 - 0
src/Service/Notifier.php

@@ -0,0 +1,102 @@
+<?php
+
+namespace App\Service;
+
+use App\Entity\Access\Access;
+use App\Entity\Core\File;
+use App\Entity\Core\Notification;
+use App\Enum\Core\NotificationTypeEnum;
+use Doctrine\ORM\EntityManagerInterface;
+
+class Notifier
+{
+    public function __construct(
+        private EntityManagerInterface $em,
+        private MercureHub $mercureHub
+    ) {}
+
+    public function notify(
+        Access $access,
+        string $name,
+        string $type,
+        array $message,
+        string $link = null
+    ): Notification
+    {
+        $now = new \DateTime();
+
+        $notification = new Notification();
+        $notification->setName($name)
+            ->setRecipientAccess($access)
+            ->setRecipientOrganization($access->getOrganization())
+            ->setType($type)
+            ->setMessage($message)
+            ->setLink($link)
+            ->setCreateDate($now)
+            ->setUpdateDate($now);
+
+        $this->em->persist($notification);
+        $this->em->flush();
+
+        $this->mercureHub->publishCreate($access->getId(), $notification);
+
+        return $notification;
+    }
+
+    public function notifyExport(
+        Access $access,
+        File $file
+    ): Notification
+    {
+        return $this->notify(
+            $access,
+            'export',
+            NotificationTypeEnum::FILE()->getValue(),
+            ['fileName' => $file->getName()],
+            '/api/files/' . $file->getId() . '/download'
+        );
+    }
+
+    public function notifyMessage(
+        Access $access,
+        array $message
+    ): Notification
+    {
+        return $this->notify(
+            $access,
+            'message',
+            NotificationTypeEnum::MESSAGE()->getValue(),
+            $message
+        );
+    }
+
+    public function notifySystem(
+        Access $access,
+        array $message,
+        string $link
+    ): Notification
+    {
+        return $this->notify(
+            $access,
+            'message',
+            NotificationTypeEnum::SYSTEM()->getValue(),
+            $message,
+            $link
+        );
+    }
+
+    public function notifyError(
+        Access $access,
+        string $name,
+        array $message
+    ): Notification
+    {
+        return $this->notify(
+            $access,
+            $name,
+            NotificationTypeEnum::ERROR()->getValue(),
+            $message
+        );
+    }
+
+}