Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve email service #175

Merged
merged 34 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f497fd3
improve(email) : add new function to send specific email
LordPax Nov 24, 2023
d66ce82
feat(providerRequest) : send email
LordPax Nov 28, 2023
105ebcc
feat(email) : send welcome email when register
LordPax Nov 30, 2023
ae790d7
Merge branch 'develop' of github.com:Pietrucci-Blacher/Challenge-Seme…
LordPax Nov 30, 2023
521d8aa
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 1, 2023
8f632e2
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 1, 2023
5256e1d
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 1, 2023
72f9619
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 1, 2023
ca68a85
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 1, 2023
84d7b3b
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 2, 2023
9dcf592
Merge branch 'develop' into improve-email-service
LordPax Dec 4, 2023
98f6012
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 5, 2023
13b0fbb
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 6, 2023
1fbe12f
Merge branch 'develop' of github.com:Pietrucci-Blacher/Challenge-Seme…
LordPax Dec 8, 2023
6754013
minor fix
LordPax Dec 10, 2023
5cf5f02
Merge branch 'improve-email-service' of github.com:Pietrucci-Blacher/…
LordPax Dec 10, 2023
5c8ac90
Fix conflict
Pietrucci-Blacher Dec 10, 2023
84a6efd
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 19, 2023
a142dd5
Temporarily disable gitignore rule for .env
Pietrucci-Blacher Dec 19, 2023
35a3058
Temporarily disable gitignore rule for .env again
Pietrucci-Blacher Dec 19, 2023
27f8434
Rollback .env changes on gitignore and remove original env file
Pietrucci-Blacher Dec 19, 2023
ec74d58
Remove comment for notifications to admins
Pietrucci-Blacher Dec 19, 2023
d05cfd0
Try fixes
Pietrucci-Blacher Dec 19, 2023
de1842d
Set key as empty string in .env and inject it in CI
Pietrucci-Blacher Dec 19, 2023
597aee1
Update Gitignore
Pietrucci-Blacher Dec 19, 2023
5e08ace
Fix: Fix double brackets in CI
Pietrucci-Blacher Dec 19, 2023
aca893e
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 19, 2023
1ca0338
Fix: issue with env variable in CI
Pietrucci-Blacher Dec 19, 2023
4dba61f
Merge branch 'improve-email-service' of github.com:Pietrucci-Blacher/…
Pietrucci-Blacher Dec 19, 2023
b5ab3a4
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 19, 2023
039972f
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 20, 2023
48415fe
Merge branch 'develop' into improve-email-service
Pietrucci-Blacher Dec 25, 2023
b9753eb
email ok
Ossama9 Dec 26, 2023
69db72a
fix(email) : get firstame user instead of name
LordPax Dec 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions back/api/.env

This file was deleted.

15 changes: 15 additions & 0 deletions back/api/src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public function upgradePassword(PasswordAuthenticatedUserInterface $user, string
$this->getEntityManager()->flush();
}

public function save(User $entity): void
{
$this->getEntityManager()->persist($entity);
$this->getEntityManager()->flush();
}

public function findByRole($value): array
{
return $this->createQueryBuilder('u')
->andWhere('u.roles LIKE :role')
->setParameter('role', "%$value%")
->getQuery()
->getResult();
}

// /**
// * @return User[] Returns an array of User objects
// */
Expand Down
62 changes: 58 additions & 4 deletions back/api/src/Service/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Brevo\Client\Model\SendSmtpEmail;
use Exception;
use GuzzleHttp\Client;
use App\Entity\User;

class Email {
private BrevoConfiguration $config;
private TransactionalEmailsApi $apiInstance;
private static string $emailFrom = '[email protected]';

public function __construct()
{
Expand All @@ -24,17 +26,17 @@ public function __construct()
/**
* @throws Exception
*/
public function sendEmail($email, $emailReceiver, $subject, $body): void
public function sendEmail($emailFrom, $emailTo, $subject, $body): void
{
$sendSMPTEmail = new SendSmtpEmail(
[
'sender' => [
'name' => $email,
'email' => $email,
'name' => $emailFrom,
'email' => $emailFrom,
],
'to' => [
[
'email' => $emailReceiver,
'email' => $emailTo,
],
],
'subject' => $subject,
Expand All @@ -47,4 +49,56 @@ public function sendEmail($email, $emailReceiver, $subject, $body): void
throw new ApiException($e->getMessage());
}
}

public function sendWelcomeEmail(string $emailTo, string $name)
{
$subject = 'Bienvenue';
$body = 'Bonjour '.$name.',<br><br> Bienvenue chez NOM';
$this->sendEmail(Email::$emailFrom, $emailTo, $subject, $body);
}

public function sendResetPasswordEmail(string $emailTo, string $name, string $token)
{
$subject = 'Réinitialisation de mot de passe';
$body = 'Bonjour '.$name.',<br><br> Pour réinitialiser votre mot de passe, veuillez cliquer sur le lien ci-dessous : <br><br> <a href="https://localhost/auth/reset-password/validate/'.$token.'">Réinitialiser le mot de passe</a>';
$this->sendEmail(Email::$emailFrom, $emailTo, $subject, $body);
}

public function sendRequestProviderEmail(array $adminEmails, string $name, string $requestId)
{
$subject = 'Nouvelle demande prestataire';
$body = 'Bonjour,<br><br> Une nouvelle demande prestataire a été effectuée par '.$name.'.<br><br> Pour la consulter, veuillez cliquer sur le lien ci-dessous : <br><br> <a href="https://localhost:8080/admin/requests/'.$requestId.'">Consulter la demande</a>';

foreach($adminEmails as $emailTo) {
$this->sendEmail(Email::$emailFrom, $emailTo, $subject, $body);
}
}

public function sendProviderConfimationEmail(string $emailTo, string $name)
{
$subject = 'Demande prestataire';
$body = 'Bonjour '.$name.',<br><br> Votre demande prestataire a bien été prise en compte.';
$this->sendEmail(Email::$emailFrom, $emailTo, $subject, $body);
}

public function sendProviderAcceptedEmail(string $emailTo, string $name)
{
$subject = 'Demande prestataire';
$body = 'Bonjour '.$name.',<br><br> Votre demande prestataire a été acceptée.';
$this->sendEmail(Email::$emailFrom, $emailTo, $subject, $body);
}

public function sendProviderRefusedEmail(string $emailTo, string $name)
{
$subject = 'Demande prestataire';
$body = 'Bonjour '.$name.',<br><br> Votre demande prestataire a été refusée.';
$this->sendEmail(Email::$emailFrom, $emailTo, $subject, $body);
}

public function sendReservationEmail(string $emailTo, string $name)
{
$subject = 'Réservation';
$body = 'Bonjour '.$name.',<br><br> Votre réservation a bien été prise en compte.';
$this->sendEmail(Email::$emailFrom, $emailTo, $subject, $body);
}
}
53 changes: 35 additions & 18 deletions back/api/src/Subscriber/ProviderRequestSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use App\Service\Email;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Bundle\SecurityBundle\Security;

final class ProviderRequestSubscriber implements EventSubscriberInterface
{
public function __construct(private EntityManagerInterface $entityManager)
{
private Email $email;
private EntityManagerInterface $entityManager;
private UserRepository $userRepository;

public function __construct(
EntityManagerInterface $entityManager,
UserRepository $userRepository,
) {
$this->entityManager = $entityManager;
$this->userRepository = $userRepository;
$this->email = new Email();
}

public static function getSubscribedEvents()
Expand All @@ -29,31 +42,35 @@ public function onRequestEvent(ResponseEvent $event)
$response = $event->getResponse();
$statusCode = $response->getStatusCode();
$resourceClass = $request->attributes->get("_api_resource_class");
$currentData = $request->attributes->get("data");

if ($resourceClass !== ProviderRequest::class) return;
if ($statusCode === 200 && $method === "PATCH"){
/**
* @var $currentData ProviderRequest
*/
$currentData = $request->attributes->get("data");
/**
* @var $previousData ProviderRequest
*/

if ($statusCode === 200 && $method === "PATCH") {
$previousData = $request->attributes->get("previous_data");
$previousStatus = $previousData->getStatus();
$currentStatus = $currentData->getStatus();
if ($previousStatus === "pending" && $currentStatus != $previousStatus){
// TODO: Faire l'envoie de mail pour informer le client que ça demande a été refusé ou accepté

if ($previousStatus === "pending" && $currentStatus != $previousStatus) {
$user = $currentData->getCreatedBy();
if ($currentStatus === "approved") {
$user = $currentData->getCreatedBy();
$user->setRoles(["ROLE_PROVIDER"]);
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->userRepository->save($user);
$this->email->sendProviderAcceptedEmail($user->getEmail(), $user->getFirstname());
} else if ($currentStatus === "rejected") {
$this->email->sendProviderRejectedEmail($user->getEmail(), $user->getFirstname());
}
}
}
if ($statusCode === 201 && $method === "POST"){
// TODO: Faire l'envoie de mail vers l'admin pour lui informer que une demande est dispo

if ($statusCode === 201 && $method === "POST") {
$content = json_decode($response->getContent(), false);
$user = $content->createdBy;
/* $requestId = $content->id; */
/* $admins = $this->userRepository->findByRole("ROLE_ADMIN"); */

/* $this->email->sendRequestProviderEmail($admins, $user->name, $requestId); */
Pietrucci-Blacher marked this conversation as resolved.
Show resolved Hide resolved
$this->email->sendProviderConfimationEmail($user->email, $user->firstname);
}
}

}
52 changes: 52 additions & 0 deletions back/api/src/Subscriber/UserSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Subscriber;

use ApiPlatform\Symfony\EventListener\EventPriorities;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use App\Service\Email;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Bundle\SecurityBundle\Security;

final class UserSubscriber implements EventSubscriberInterface
{
private Email $email;
private EntityManagerInterface $entityManager;
private UserRepository $userRepository;

public function __construct(
EntityManagerInterface $entityManager,
UserRepository $userRepository,
) {
$this->entityManager = $entityManager;
$this->userRepository = $userRepository;
$this->email = new Email();
}

public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => ['onRequestEvent', EventPriorities::POST_WRITE],
];
}

public function onRequestEvent(ResponseEvent $event)
{
$request = $event->getRequest();
$method = $request->getMethod();
$response = $event->getResponse();
$statusCode = $response->getStatusCode();
$resourceClass = $request->attributes->get("_api_resource_class");

if ($resourceClass !== User::class) return;

if ($statusCode === 201 && $method === "POST") {
$user = json_decode($response->getContent(), false);
$this->email->sendWelcomeEmail($user->email, $user->firstname);
}
}
}
Loading