Send an email when a workflow is send to an external

- create an event subscriber to catch the workflow which arrive to a "sentExternal" step;
- add a messenger's message to handle the generation of the email;
- add a simple message, and a simple controller for viewing the document
- add dedicated tests
This commit is contained in:
Julien Fastré 2024-10-04 13:40:50 +02:00
parent 7913a377c8
commit a0b5c208eb
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
11 changed files with 417 additions and 4 deletions

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowSend;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class WorkflowViewSendPublicController
{
#[Route('/public/main/workflow/send/{uuid}/view/{verificationKey}', methods: ['GET'], name: 'chill_main_workflow_send_view_public')]
public function __invoke(EntityWorkflowSend $workflowSend, string $verificationKey): Response
{
return new Response('ok');
}
}

View File

@ -124,6 +124,16 @@ class EntityWorkflowSend implements TrackCreationInterface
return $this->uuid;
}
public function getExpireAt(): \DateTimeImmutable
{
return $this->expireAt;
}
public function getViews(): Collection
{
return $this->views;
}
public function increaseErrorTrials(): void
{
$this->numberOfErrorTrials = $this->numberOfErrorTrials + 1;

View File

@ -303,6 +303,14 @@ class EntityWorkflowStep
return $this->signatures;
}
/**
* @return Collection<int, EntityWorkflowSend>
*/
public function getSends(): Collection
{
return $this->sends;
}
public function getId(): ?int
{
return $this->id;

View File

@ -0,0 +1,6 @@
Un message vous a été envoyé. Vous pouvez le consulter à cette adresse
{{ absolute_url(path('chill_main_workflow_send_view_public', {'uuid': send.uuid, 'verificationKey': send.privateToken})) }}
{{ 'workflow.send_external_message.document_available_until'|trans({ 'expiration': send.expireAt}, null, lang) }}

View File

@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Workflow\EventSubscriber;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\EntityWorkflowMarkingStore;
use Chill\MainBundle\Workflow\EventSubscriber\EntityWorkflowPrepareEmailOnSendExternalEventSubscriber;
use Chill\MainBundle\Workflow\Messenger\PostSendExternalMessage;
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Workflow\DefinitionBuilder;
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Workflow;
use Symfony\Component\Workflow\WorkflowInterface;
/**
* @internal
*
* @coversNothing
*/
class EntityWorkflowPrepareEmailOnSendExternalEventSubscriberTest extends TestCase
{
use ProphecyTrait;
private Transition $transitionSendExternal;
private Transition $transitionRegular;
public function testToSendExternalGenerateMessage(): void
{
$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch(Argument::type(PostSendExternalMessage::class))
->will(fn ($args) => new Envelope($args[0]))
->shouldBeCalled();
$registry = $this->buildRegistry($messageBus->reveal());
$entityWorkflow = $this->buildEntityWorkflow();
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$workflow->apply(
$entityWorkflow,
$this->transitionSendExternal->getName(),
['context' => $dto, 'byUser' => new User(), 'transition' => $this->transitionSendExternal->getName(),
'transitionAt' => new \DateTimeImmutable()]
);
// at this step, prophecy should check that the dispatch method has been called
}
public function testToRegularDoNotGenerateMessage(): void
{
$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch(Argument::type(PostSendExternalMessage::class))
->shouldNotBeCalled();
$registry = $this->buildRegistry($messageBus->reveal());
$entityWorkflow = $this->buildEntityWorkflow();
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$workflow->apply(
$entityWorkflow,
$this->transitionRegular->getName(),
['context' => $dto, 'byUser' => new User(), 'transition' => $this->transitionRegular->getName(),
'transitionAt' => new \DateTimeImmutable()]
);
// at this step, prophecy should check that the dispatch method has been called
}
private function buildEntityWorkflow(): EntityWorkflow
{
$entityWorkflow = new EntityWorkflow();
$entityWorkflow->setWorkflowName('dummy');
// set an id
$reflectionClass = new \ReflectionClass($entityWorkflow);
$idProperty = $reflectionClass->getProperty('id');
$idProperty->setValue($entityWorkflow, 1);
return $entityWorkflow;
}
private function buildRegistry(MessageBusInterface $messageBus): Registry
{
$builder = new DefinitionBuilder(
['initial', 'sendExternal', 'regular'],
[
$this->transitionSendExternal = new Transition('toSendExternal', 'initial', 'sendExternal'),
$this->transitionRegular = new Transition('toRegular', 'initial', 'regular'),
]
);
$builder
->setInitialPlaces('initial')
->setMetadataStore(new InMemoryMetadataStore(
placesMetadata: [
'sendExternal' => ['isSentExternal' => true],
]
));
$entityMarkingStore = new EntityWorkflowMarkingStore();
$registry = new Registry();
$eventSubscriber = new EntityWorkflowPrepareEmailOnSendExternalEventSubscriber($registry, $messageBus);
$eventSubscriber->setLocale('fr');
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber($eventSubscriber);
$workflow = new Workflow($builder->build(), $entityMarkingStore, $eventDispatcher, 'dummy');
$registry->addWorkflow($workflow, new class () implements WorkflowSupportStrategyInterface {
public function supports(WorkflowInterface $workflow, object $subject): bool
{
return true;
}
});
return $registry;
}
}

View File

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Workflow\Messenger;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Workflow\Messenger\PostSendExternalMessage;
use Chill\MainBundle\Workflow\Messenger\PostSendExternalMessageHandler;
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\BodyRendererInterface;
/**
* @internal
*
* @coversNothing
*/
class PostSendExternalMessageHandlerTest extends TestCase
{
use ProphecyTrait;
public function testSendMessageHappyScenario(): void
{
$entityWorkflow = $this->buildEntityWorkflow();
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
$dto->futureDestineeEmails = ['external@example.com'];
$dto->futureDestineeThirdParties = [(new ThirdParty())->setEmail('3party@example.com')];
$entityWorkflow->setStep('send_external', $dto, 'to_send_external', new \DateTimeImmutable(), new User());
$repository = $this->prophesize(EntityWorkflowRepository::class);
$repository->find(1)->willReturn($entityWorkflow);
$mailer = $this->prophesize(MailerInterface::class);
$mailer->send(Argument::that($this->buildCheckAddressCallback('3party@example.com')))->shouldBeCalledOnce();
$mailer->send(Argument::that($this->buildCheckAddressCallback('external@example.com')))->shouldBeCalledOnce();
$bodyRenderer = $this->prophesize(BodyRendererInterface::class);
$bodyRenderer->render(Argument::type(TemplatedEmail::class))->shouldBeCalledTimes(2);
$handler = new PostSendExternalMessageHandler($repository->reveal(), $mailer->reveal(), $bodyRenderer->reveal());
$handler(new PostSendExternalMessage(1, 'fr'));
// prophecy should do the check at the end of this test
}
private function buildCheckAddressCallback(string $emailToCheck): callable
{
return fn(TemplatedEmail $email): bool => in_array($emailToCheck, array_map(fn (Address $addr) => $addr->getAddress(), $email->getTo()), true);
}
private function buildEntityWorkflow(): EntityWorkflow
{
$entityWorkflow = new EntityWorkflow();
$reflection = new \ReflectionClass($entityWorkflow);
$idProperty = $reflection->getProperty('id');
$idProperty->setValue($entityWorkflow, 1);
return $entityWorkflow;
}
}

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Workflow\EventSubscriber;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Messenger\PostSendExternalMessage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Workflow\Event\CompletedEvent;
use Symfony\Component\Workflow\Registry;
use Symfony\Contracts\Translation\LocaleAwareInterface;
class EntityWorkflowPrepareEmailOnSendExternalEventSubscriber implements EventSubscriberInterface, LocaleAwareInterface
{
private string $locale;
public function __construct(private readonly Registry $registry, private readonly MessageBusInterface $messageBus) {}
public static function getSubscribedEvents(): array
{
return [
'workflow.completed' => 'onWorkflowCompleted',
];
}
public function onWorkflowCompleted(CompletedEvent $event): void
{
$entityWorkflow = $event->getSubject();
if (!$entityWorkflow instanceof EntityWorkflow) {
return;
}
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$store = $workflow->getMetadataStore();
$mustSend = false;
foreach ($event->getTransition()->getTos() as $to) {
$metadata = $store->getPlaceMetadata($to);
if ($metadata['isSentExternal'] ?? false) {
$mustSend = true;
}
}
if ($mustSend) {
$this->messageBus->dispatch(new PostSendExternalMessage($entityWorkflow->getId(), $this->getLocale()));
}
}
public function setLocale(string $locale): void
{
$this->locale = $locale;
}
public function getLocale(): string
{
return $this->locale;
}
}

View File

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Workflow\Messenger;
class PostSendExternalMessage
{
public function __construct(
public readonly int $entityWorkflowId,
public readonly string $lang,
) {}
}

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Workflow\Messenger;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowSend;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Mime\BodyRendererInterface;
final readonly class PostSendExternalMessageHandler implements MessageHandlerInterface
{
public function __construct(
private EntityWorkflowRepository $entityWorkflowRepository,
private MailerInterface $mailer,
private BodyRendererInterface $bodyRenderer,
) {}
public function __invoke(PostSendExternalMessage $message): void
{
$entityWorkflow = $this->entityWorkflowRepository->find($message->entityWorkflowId);
if (null === $entityWorkflow) {
throw new UnrecoverableMessageHandlingException(sprintf('Entity workflow with id %d not found', $message->entityWorkflowId));
}
foreach ($entityWorkflow->getCurrentStep()->getSends() as $send) {
$this->sendEmailToDestinee($send, $message);
}
}
private function sendEmailToDestinee(EntityWorkflowSend $send, PostSendExternalMessage $message): void
{
$email = new TemplatedEmail();
$email
->to($send->getDestineeThirdParty()?->getEmail() ?? $send->getDestineeEmail())
->htmlTemplate('@ChillMain/Workflow/workflow_send_external_email_to_destinee.html.twig')
->context([
'send' => $send,
'lang' => $message->lang,
]);
$this->bodyRenderer->render($email);
$this->mailer->send($email);
}
}

View File

@ -33,15 +33,15 @@ services:
# workflow related
Chill\MainBundle\Workflow\:
resource: '../Workflow/'
autowire: true
autoconfigure: true
Chill\MainBundle\Workflow\EntityWorkflowManager:
autoconfigure: true
autowire: true
arguments:
$handlers: !tagged_iterator chill_main.workflow_handler
# seems to have no alias on symfony 5.4
Symfony\Component\Mime\BodyRendererInterface:
alias: 'twig.mime_body_renderer'
# other stuffes
chill.main.helper.translatable_string:

View File

@ -67,6 +67,8 @@ workflow:
one {Signature demandée}
other {Signatures demandées}
}
send_external_message:
document_available_until: Le lien sera valable jusqu'au {expiration, date, long} à {expiration, time, short}.
duration:
minute: >-