feat: send an email when a domain has been modified

This commit is contained in:
Maël Gangloff 2024-07-21 13:59:00 +02:00
parent 63f8dc168d
commit b45bbe63f5
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
6 changed files with 92 additions and 7 deletions

1
.env
View File

@ -38,6 +38,7 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0
###> symfony/mailer ### ###> symfony/mailer ###
# MAILER_DSN=null://null # MAILER_DSN=null://null
MAILER_SENDER_EMAIL=notifications@example.com
###< symfony/mailer ### ###< symfony/mailer ###
###> nelmio/cors-bundle ### ###> nelmio/cors-bundle ###

View File

@ -4,12 +4,15 @@
# Put parameters here that don't need to change on each machine where the app is deployed # Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters: parameters:
mailer_sender_email: '%env(MAILER_SENDER_EMAIL)%'
services: services:
# default configuration for services in *this* file # default configuration for services in *this* file
_defaults: _defaults:
autowire: true # Automatically injects dependencies in your services. autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$mailerSenderEmail: '%mailer_sender_email%'
# makes classes in src/ available to be used as services # makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name # this creates a service per class whose id is the fully-qualified class name

View File

@ -5,6 +5,7 @@ namespace App\MessageHandler;
use App\Config\TriggerAction; use App\Config\TriggerAction;
use App\Entity\Domain; use App\Entity\Domain;
use App\Entity\DomainEvent; use App\Entity\DomainEvent;
use App\Entity\User;
use App\Entity\WatchList; use App\Entity\WatchList;
use App\Entity\WatchListTrigger; use App\Entity\WatchListTrigger;
use App\Message\SendNotifWatchListTrigger; use App\Message\SendNotifWatchListTrigger;
@ -12,22 +13,29 @@ use App\Repository\WatchListRepository;
use App\Service\RDAPService; use App\Service\RDAPService;
use DateTimeImmutable; use DateTimeImmutable;
use Exception; use Exception;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Mime\Email;
use Throwable; use Throwable;
#[AsMessageHandler] #[AsMessageHandler]
final class SendNotifWatchListTriggerHandler readonly final class SendNotifWatchListTriggerHandler
{ {
public function __construct( public function __construct(
private WatchListRepository $watchListRepository, private WatchListRepository $watchListRepository,
private RDAPService $RDAPService private RDAPService $RDAPService,
private MailerInterface $mailer,
private string $mailerSenderEmail
) )
{ {
} }
/** /**
* @throws Exception * @throws Exception
* @throws TransportExceptionInterface
*/ */
public function __invoke(SendNotifWatchListTrigger $message): void public function __invoke(SendNotifWatchListTrigger $message): void
{ {
@ -40,24 +48,26 @@ final class SendNotifWatchListTriggerHandler
->diff(new DateTimeImmutable('now'))->days >= 7) as $domain ->diff(new DateTimeImmutable('now'))->days >= 7) as $domain
) { ) {
$updatedAt = $domain->getUpdatedAt(); $updatedAt = $domain->getUpdatedAt();
try { try {
$domain = $this->RDAPService->registerDomain($domain->getLdhName()); $domain = $this->RDAPService->registerDomain($domain->getLdhName());
} catch (Throwable) { } catch (Throwable) {
$this->sendEmailDomainUpdateError($domain, $watchList->getUser());
continue;
} }
/** @var DomainEvent $event */ /** @var DomainEvent $event */
foreach ($domain->getEvents()->filter(fn($event) => $updatedAt < $event->getDate()) as $event) { foreach ($domain->getEvents()->filter(fn($event) => $updatedAt < $event->getDate()) as $event) {
$watchListTriggers = $watchList->getWatchListTriggers() $watchListTriggers = $watchList->getWatchListTriggers()
->filter(fn($trigger) => $trigger->getAction() === $event->getAction()); ->filter(fn($trigger) => $trigger->getEvent() === $event->getAction());
/** @var WatchListTrigger $watchListTrigger */ /** @var WatchListTrigger $watchListTrigger */
foreach ($watchListTriggers->getIterator() as $watchListTrigger) { foreach ($watchListTriggers->getIterator() as $watchListTrigger) {
switch ($watchListTrigger->getAction()) { switch ($watchListTrigger->getAction()) {
case TriggerAction::SendEmail: case TriggerAction::SendEmail:
//TODO: To be implemented $this->sendEmailDomainUpdated($event, $watchList->getUser());
throw new Exception('To be implemented');
} }
} }
} }
@ -65,4 +75,44 @@ final class SendNotifWatchListTriggerHandler
} }
} }
/**
* @throws TransportExceptionInterface
*/
public function sendEmailDomainUpdateError(Domain $domain, User $user): Email
{
$email = (new TemplatedEmail())
->from($this->mailerSenderEmail)
->to($user->getEmail())
->subject('An error occurred while updating a domain name')
->htmlTemplate('emails/errors/domain_update.html.twig')
->locale('en')
->context([
"domain" => $domain
]);
$this->mailer->send($email);
return $email;
}
/**
* @throws TransportExceptionInterface
*/
public function sendEmailDomainUpdated(DomainEvent $domainEvent, User $user): Email
{
$email = (new TemplatedEmail())
->from($this->mailerSenderEmail)
->to($user->getEmail())
->priority(Email::PRIORITY_HIGHEST)
->subject('A domain name has been changed')
->htmlTemplate('emails/domain_updated.html.twig')
->locale('en')
->context([
"event" => $domainEvent
]);
$this->mailer->send($email);
return $email;
}
} }

View File

@ -21,7 +21,7 @@ final readonly class SendNotifWatchListTriggerSchedule implements ScheduleProvid
{ {
return (new Schedule()) return (new Schedule())
->add( ->add(
RecurringMessage::every('1 week', new SendNotifWatchListTrigger()), RecurringMessage::every('10 seconds', new SendNotifWatchListTrigger()),
) )
->stateful($this->cache) ->stateful($this->cache)
; ;

View File

@ -0,0 +1,14 @@
<p>
Hello,<br />
<br />
We are pleased to inform you that a new action has been detected on a domain name in your watchlist.<br />
Domain name : <b>{{ event.domain.ldhName }}</b><br />
Action : <b>{{ event.action.value }}</b><br />
Effective Date : <b>{{ event.date | date("c") }}</b><br />
<br />
<br />
Thank you for your understanding,<br />
Sincerely<br />
<br />
Domain Watchdog
</p>

View File

@ -0,0 +1,17 @@
<p>
Hello,<br />
<br />
We would like to inform you that an error occurred while updating the information for the following domain name.
<br/>
Domain name : <b>{{ domain.ldhName }}</b><br/>
<br/>
Here are some possible explanations:<br/>
* It is likely that the domain will be available for registration again.<br/>
* A temporary outage affects the provision of domain name information.<br/>
<br/>
<br/>
Thank you for your understanding,<br/>
Sincerely<br/>
<br/>
Domain Watchdog
</p>