src/Controller/FideliteController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CouponUser;
  4. use App\Repository\CouponUserRepository;
  5. use App\Repository\OffreRepository;
  6. use App\Repository\UserRepository;
  7. use App\Service\EmailService;
  8. use App\Service\QrcodeService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class FideliteController extends AbstractController
  16. {
  17.     public function __construct(
  18.         private readonly EntityManagerInterface $entityManager,
  19.         private readonly UserRepository $userRepository,
  20.         private readonly OffreRepository $offreRepository,
  21.         private readonly QrcodeService $qrcodeService,
  22.         private readonly EmailService $emailService,
  23.         private readonly CouponUserRepository $couponUserRepository
  24.     )
  25.     {
  26.     }
  27.     #[Route('/fidelite/ma-carte'name'app_fidelite')]
  28.     public function index(): Response
  29.     {
  30.         $offres $this->offreRepository->findByPointDesc();
  31.         $user $this->getUser();
  32.         $coupon $this->couponUserRepository->findBy(['user' => $user], ['createdAt' => 'DESC']);
  33.         $couponAndTicket $this->couponUserRepository->findCouponAndTicket($user);
  34.         return $this->render('fidelite/carte_fidelite.html.twig', [
  35.             'user' => $user,
  36.             'offres' => $offres,
  37.             'coupons' => $coupon,
  38.             'couponAndTicket' => $couponAndTicket,
  39.         ]);
  40.     }
  41.     #[Route('/subscribe/'name'subscribe_coupon'methods"POST")]
  42.     public function subscribeCoupon(Request $request): Response
  43.     {
  44.         $data json_decode($request->getContent(), true);
  45.         $userId $data['userId'];
  46.         $offreId $data['offreId'];
  47.         $user $this->userRepository->findOneById($userId);
  48.         $offre $this->offreRepository->findOneById($offreId);
  49.         $newUserPoint $user->getCard()->getPoints() - $offre->getPoint();
  50.         $date = new \DateTimeImmutable('now');
  51.         $uniqId uniqid();
  52.         if (!$user || !$offre) {
  53.             return new JsonResponse(['message' => 'Invalid user or offer'], 400);
  54.         }
  55.         if ($user->getCard()->getPoints() < $offre->getPoint()) {
  56.             return new JsonResponse(['message' => 'Not enough points'], 400);
  57.         }
  58.         $couponUser = new CouponUser();
  59.         $couponUser->setUser($user);
  60.         $couponUser->setOffre($offre);
  61.         $couponUser->setIsActive(1);
  62.         $couponUser->setCreatedAt($date);
  63.         $couponUser->setSlug('PROVISOIRE');
  64.         $couponUser->setQrCodePath('PROVISOIRE');
  65.         // Persist the couponUser to get the ID
  66.         $this->entityManager->persist($couponUser);
  67.         $this->entityManager->flush();
  68.         // Now that the ID is available, update the slug
  69.         $slug $user->getSlug() . '/c-' $couponUser->getId() . '-' $uniqId;
  70.         $couponUser->setSlug($slug);
  71.         // Generate the QR code and update the couponUser
  72.         $qrCodePath $this->qrcodeService->generateQrCodeCoupon($user$slug);
  73.         $couponUser->setQrCodePath($qrCodePath);
  74.         // Persist the couponUser again with the updated slug and QR code path
  75.         $this->entityManager->persist($couponUser);
  76.         $this->entityManager->flush();
  77.         // Update user points
  78.         $user->getCard()->setPoints($newUserPoint);
  79.         $this->entityManager->persist($user);
  80.         $this->entityManager->flush();
  81.         // Send success email
  82.         $this->emailService->sendSubscribeSuccess(
  83.             $user->getFirstName() . ', félicitation ! Profitez de -' $offre->getReduction() .'% sur vos chats chez IMMY BEAUTY',
  84.             $user,
  85.             $couponUser
  86.         );
  87.         return new JsonResponse(['message' => 'User subscribed to the offer successfully']);
  88.     }
  89. }