notification: send an email to addressesEmails

This commit is contained in:
Julien Fastré 2022-04-13 22:50:32 +02:00
parent 24d28b0a52
commit a41d6cf744
5 changed files with 86 additions and 0 deletions

View File

@ -65,6 +65,17 @@ class NotificationController extends AbstractController
$this->translator = $translator;
}
/**
* @Route("/{id}/access_key", name="chill_main_notification_grant_access_by_access_key")
*/
public function getAccessByAccessKey(Notification $notification, Request $request): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
return new Response('Invalid access key');
}
/**
* @Route("/create", name="chill_main_notification_create")
*/

View File

@ -183,6 +183,14 @@ class Notification implements TrackUpdateInterface
return $this;
}
/**
* @return array
*/
public function getAddedAddresses(): array
{
return $this->addedAddresses;
}
public function addComment(NotificationComment $comment): self
{
if (!$this->comments->contains($comment)) {

View File

@ -73,6 +73,17 @@ class NotificationMailer
* Send a email after a notification is persisted.
*/
public function postPersistNotification(Notification $notification, LifecycleEventArgs $eventArgs): void
{
$this->sendNotificationEmailsToAddresses($notification);
$this->sendNotificationEmailsToAddressesEmails($notification);
}
public function postUpdateNotification(Notification $notification, LifecycleEventArgs $eventArgs): void
{
$this->sendNotificationEmailsToAddressesEmails($notification);
}
private function sendNotificationEmailsToAddresses(Notification $notification): void
{
foreach ($notification->getAddressees() as $addressee) {
if (null === $addressee->getEmail()) {
@ -108,4 +119,31 @@ class NotificationMailer
}
}
}
private function sendNotificationEmailsToAddressesEmails(Notification $notification): void
{
foreach ($notification->getAddressesEmailsAdded() as $emailAddress) {
$email = new TemplatedEmail();
$email
->textTemplate('@ChillMain/Notification/email_non_system_notification_content_to_email.fr.md.twig')
->context([
'notification' => $notification,
'dest' => $emailAddress,
]);
$email
->subject($notification->getTitle())
->to($emailAddress);
try {
$this->mailer->send($email);
} catch (TransportExceptionInterface $e) {
$this->logger->warning('[NotificationMailer] could not send an email notification', [
'to' => $emailAddress,
'error_message' => $e->getMessage(),
'error_trace' => $e->getTraceAsString(),
]);
}
}
}
}

View File

@ -0,0 +1,20 @@
{{ dest }},
{{ notification.sender.label }} a créé une notification pour vous:
> {{ notification.title }}
>
>
{%- for line in notification.message|split("\n") %}
> {{ line }}
{%- if not loop.last %}
>
{%- endif %}
{%- endfor %}
Vous pouvez cliquer sur ce lien pour obtenir un accès permanent à la notification:
{{ absolute_url(path('chill_main_notification_grant_access_by_access_key', {'_locale': 'fr', 'id': notification.id, 'accessKey': notification.accessKey, 'email': dest})) }}
--
Le logiciel Chill

View File

@ -61,6 +61,15 @@ services:
# set the 'lazy' option to TRUE to only instantiate listeners when they are used
lazy: true
method: 'postPersistNotification'
-
name: 'doctrine.orm.entity_listener'
event: 'postUpdate'
entity: 'Chill\MainBundle\Entity\Notification'
# set the 'lazy' option to TRUE to only instantiate listeners when they are used
lazy: true
method: 'postUpdateNotification'
-
name: 'doctrine.orm.entity_listener'
event: 'postPersist'