<?php
namespace App\Controller;
use App\Entity\CouponUser;
use App\Repository\CouponUserRepository;
use App\Repository\OffreRepository;
use App\Repository\UserRepository;
use App\Service\EmailService;
use App\Service\QrcodeService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FideliteController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly UserRepository $userRepository,
private readonly OffreRepository $offreRepository,
private readonly QrcodeService $qrcodeService,
private readonly EmailService $emailService,
private readonly CouponUserRepository $couponUserRepository
)
{
}
#[Route('/fidelite/ma-carte', name: 'app_fidelite')]
public function index(): Response
{
$offres = $this->offreRepository->findByPointDesc();
$user = $this->getUser();
$coupon = $this->couponUserRepository->findBy(['user' => $user], ['createdAt' => 'DESC']);
$couponAndTicket = $this->couponUserRepository->findCouponAndTicket($user);
return $this->render('fidelite/carte_fidelite.html.twig', [
'user' => $user,
'offres' => $offres,
'coupons' => $coupon,
'couponAndTicket' => $couponAndTicket,
]);
}
#[Route('/subscribe/', name: 'subscribe_coupon', methods: "POST")]
public function subscribeCoupon(Request $request): Response
{
$data = json_decode($request->getContent(), true);
$userId = $data['userId'];
$offreId = $data['offreId'];
$user = $this->userRepository->findOneById($userId);
$offre = $this->offreRepository->findOneById($offreId);
$newUserPoint = $user->getCard()->getPoints() - $offre->getPoint();
$date = new \DateTimeImmutable('now');
$uniqId = uniqid();
if (!$user || !$offre) {
return new JsonResponse(['message' => 'Invalid user or offer'], 400);
}
if ($user->getCard()->getPoints() < $offre->getPoint()) {
return new JsonResponse(['message' => 'Not enough points'], 400);
}
$couponUser = new CouponUser();
$couponUser->setUser($user);
$couponUser->setOffre($offre);
$couponUser->setIsActive(1);
$couponUser->setCreatedAt($date);
$couponUser->setSlug('PROVISOIRE');
$couponUser->setQrCodePath('PROVISOIRE');
// Persist the couponUser to get the ID
$this->entityManager->persist($couponUser);
$this->entityManager->flush();
// Now that the ID is available, update the slug
$slug = $user->getSlug() . '/c-' . $couponUser->getId() . '-' . $uniqId;
$couponUser->setSlug($slug);
// Generate the QR code and update the couponUser
$qrCodePath = $this->qrcodeService->generateQrCodeCoupon($user, $slug);
$couponUser->setQrCodePath($qrCodePath);
// Persist the couponUser again with the updated slug and QR code path
$this->entityManager->persist($couponUser);
$this->entityManager->flush();
// Update user points
$user->getCard()->setPoints($newUserPoint);
$this->entityManager->persist($user);
$this->entityManager->flush();
// Send success email
$this->emailService->sendSubscribeSuccess(
$user->getFirstName() . ', félicitation ! Profitez de -' . $offre->getReduction() .'% sur vos chats chez IMMY BEAUTY',
$user,
$couponUser
);
return new JsonResponse(['message' => 'User subscribed to the offer successfully']);
}
}