Skip to content

Commit

Permalink
Referrals email vars (#11522)
Browse files Browse the repository at this point in the history
* [Referrals] Fix email vars

* Send report email

---------

Co-authored-by: Rémi <[email protected]>
  • Loading branch information
Remg and Rémi authored Feb 26, 2025
1 parent e98e1e3 commit 4b44982
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 14 deletions.
6 changes: 5 additions & 1 deletion features/api/referrals.feature
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ Feature:
{
"rcpt": "[email protected]",
"vars": [
{
"content": "Michelle",
"name": "referrer_first_name"
},
{
"content": "Jane",
"name": "first_name"
"name": "referred_first_name"
},
{
"content": "http://test.renaissance.code/invitation/adhesion/@string@",
Expand Down
17 changes: 17 additions & 0 deletions src/Adherent/Referral/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Entity\Referral;
use App\Mailer\MailerService;
use App\Mailer\Message\Renaissance\Referral\ReferralAdhesionCreatedMessage;
use App\Mailer\Message\Renaissance\Referral\ReferralReportedMessage;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class Notifier
Expand All @@ -21,6 +22,7 @@ public function sendAdhesionMessage(Referral $referral): void
{
$this->transactionalMailer->sendMessage(
ReferralAdhesionCreatedMessage::create(
$referral->referrer->getFirstName(),
$referral->emailAddress,
$referral->firstName,
$this->generateUrl(
Expand All @@ -39,6 +41,21 @@ public function sendAdhesionMessage(Referral $referral): void
);
}

public function sendReportMessage(Referral $referral): void
{
if (!$referral->referrer) {
return;
}

$this->transactionalMailer->sendMessage(
ReferralReportedMessage::create(
$referral->referrer->getEmailAddress(),
$referral->referrer->getFirstName(),
$referral->firstName
)
);
}

private function generateUrl(string $routeName, array $parameters = []): string
{
return $this->urlGenerator->generate($routeName, $parameters, UrlGeneratorInterface::ABSOLUTE_URL);
Expand Down
8 changes: 6 additions & 2 deletions src/Adherent/Referral/ReportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@

class ReportHandler
{
public function __construct(private readonly EntityManagerInterface $entityManager)
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly Notifier $notifier,
) {
}

public function report(Referral $referral): void
{
$referral->report();

$this->entityManager->flush();

$this->notifier->sendReportMessage($referral);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
class ReferralAdhesionCreatedMessage extends AbstractRenaissanceMessage
{
public static function create(
string $email,
string $firstName,
string $referrerFirstName,
string $referredEmail,
string $referredFirstName,
string $adhesionLink,
string $reportLink,
): self {
return new self(
Uuid::uuid4(),
$email,
$firstName,
$referredEmail,
$referredFirstName,
'Nouveau parrainage',
[],
[
'first_name' => self::escape($firstName),
'referrer_first_name' => self::escape($referrerFirstName),
'referred_first_name' => self::escape($referredFirstName),
'adhesion_link' => $adhesionLink,
'report_link' => $reportLink,
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Mailer\Message\Renaissance\Referral;

use App\Mailer\Message\Renaissance\AbstractRenaissanceMessage;
use Ramsey\Uuid\Uuid;

class ReferralReportedMessage extends AbstractRenaissanceMessage
{
public static function create(
string $referrerEmail,
string $referrerFirstName,
string $referredFirstName,
): self {
return new self(
Uuid::uuid4(),
$referrerEmail,
$referrerFirstName,
'Parrainage signalé',
[],
[
'referrer_first_name' => self::escape($referrerFirstName),
'referred_first_name' => self::escape($referredFirstName),
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Adherent\Referral\StatusEnum;
use App\Entity\Referral;
use App\Mailer\Message\Renaissance\Referral\ReferralReportedMessage;
use App\Repository\ReferralRepository;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
Expand All @@ -20,13 +21,14 @@ class FormControllerTest extends AbstractWebTestCase
private ?ReferralRepository $referralRepository = null;

#[DataProvider('provideReportableReferralIdentifiers')]
public function testReferralCanBeReported(string $referralIdentifier): void
public function testReferralCanBeReported(string $identifier, string $referrerEmail): void
{
$referral = $this->referralRepository->findOneBy(['identifier' => $referralIdentifier]);
$referral = $this->referralRepository->findOneBy(['identifier' => $identifier]);
$this->assertInstanceOf(Referral::class, $referral);
$this->assertNotEquals(StatusEnum::REPORTED, $referral->status);
$this->assertCountMails(0, ReferralReportedMessage::class, $referrerEmail);

$crawler = $this->client->request(Request::METHOD_GET, "/invitation/$referralIdentifier/signaler");
$crawler = $this->client->request(Request::METHOD_GET, "/invitation/$identifier/signaler");
$this->assertStatusCode(Response::HTTP_OK, $this->client);
$this->assertStringContainsString(
'Voulez-vous vraiment signaler ce parrainage ?',
Expand All @@ -43,8 +45,9 @@ public function testReferralCanBeReported(string $referralIdentifier): void
$crawler->text()
);

$referral = $this->referralRepository->findOneBy(['identifier' => $referralIdentifier]);
$referral = $this->referralRepository->findOneBy(['identifier' => $identifier]);
$this->assertEquals(StatusEnum::REPORTED, $referral->status);
$this->assertCountMails(1, ReferralReportedMessage::class, $referrerEmail);
}

#[DataProvider('provideAlreadyReportedReferralIdentifiers')]
Expand All @@ -64,8 +67,8 @@ public function testReferralIsAlreadyReported(string $referralIdentifier): void

public static function provideReportableReferralIdentifiers(): iterable
{
yield ['PAB123'];
yield ['P789YZ'];
yield ['PAB123', '[email protected]'];
yield ['P789YZ', '[email protected]'];
}

public static function provideAlreadyReportedReferralIdentifiers(): iterable
Expand Down

0 comments on commit 4b44982

Please sign in to comment.