diff --git a/.env b/.env index 524daeb..0b9b4b5 100755 --- a/.env +++ b/.env @@ -15,6 +15,7 @@ ###> symfony/framework-bundle ### APP_ENV=dev +ADMIN_TOKEN=erfd456ref4ety4h56jy4i5opuoipm564iyuyn312b1s6er78g897ryjt7thsb32d1gfb APP_SECRET=597b0529ac702d27dcb9089f7e69c362 # Base website url, should contain https:// and having no trailing slash. example: BASE_URL=https://framadate.org BASE_URL=https://framadate-api.cipherbliss.com diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php new file mode 100755 index 0000000..d52425f --- /dev/null +++ b/src/Controller/AdminController.php @@ -0,0 +1,71 @@ +json( [ "message" => "welcome to the framadate admin api, ask /api/v1/doc.json for endpoints" ], + 200 ); + } + + /** + * Delete all expired polls and their children + * @Get( + * path = "/polls/clean/{token}", + * name = "_clean_expired_polls", + * ) + * token is set up in the main env file + */ + public + function cleanExpiredPolls( + string $token + ) { + if ( $this->getParameter( 'ADMIN_TOKEN' ) !== $token ) { + return $this->json( [ + 'message' => 'clean routine can NOT be done, your admin token is bad, and you should feel bad.', + ], + 403 ); + } + $em = $this->getDoctrine()->getManager(); + $emPoll = $this->getDoctrine()->getRepository( Poll::class ); + + $queryFind = $em->createQuery( + 'SELECT p + FROM App\Entity\Poll p + WHERE p.expiracyDate < CURRENT_DATE()' + ); + $queryDelete = $em->createQuery( + 'DELETE + FROM App\Entity\Poll p + WHERE p.expiracyDate < CURRENT_DATE()' + ); + + $foundPolls = $queryFind->getResult(); + + $em->flush(); + + + return $this->json( [ + 'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ), + 'data' => [ + 'count' => count( $foundPolls ), + ], + ], + 200 ); + } + +} diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 40278c2..7c04835 100755 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -16,65 +16,7 @@ use Symfony\Component\Mailer\Exception\TransportExceptionInterface; * @Route("/api/v1",name="api_") */ class DefaultController extends FramadateController { - /** - * @var MailService - */ - protected $mail_service; - /** - * Send a mail with all the data to one user - * @Get( - * path = "/send-polls-to-user/{email}", - * name = "send_user_polls" - * ) - * - * @param string $email - * - * @return JsonResponse - */ - public function sendPollsToUserAction( string $email ) { - $repository = $this->getDoctrine()->getRepository( Owner::class ); - - - // find user by email - $owner = $repository->findOneByEmail($email); - - if ( $owner ) { - $templateVars = [ - 'owner' => $owner, - 'polls' => $owner->getPolls(), - 'title' => 'Mes sondages - ' . $owner->getEmail(), - ]; - - // send email - $mailSent = 0; - try { - $mailSent = $this->sendOwnerPollsAction( $owner ); - } catch ( Exception $e ) { - } catch ( TransportExceptionInterface $e ) { - } - - if ( $mailSent ) { - return $this->json( [ - 'message' => 'mail succefully sent to user ' . $owner->getEmail(), - 'data' => '', - ], - 200 ); - } - return $this->json( [ - 'message' => 'no sucess sending email ' . $owner->getEmail(), - 'data' => '', - ], - 400 ); - } - return $this->json( [ - 'message' => 'no user found for email ' . $email, - 'data' => '', - ], - 400 ); - - } - } diff --git a/src/Controller/OwnerController.php b/src/Controller/OwnerController.php new file mode 100755 index 0000000..4423221 --- /dev/null +++ b/src/Controller/OwnerController.php @@ -0,0 +1,85 @@ +json( [ "message" => "welcome to the framadate user api, ask /api/v1/doc.json for endpoints" ], + 200 ); + } + + + /** + * Send a mail with all the data to one user + * @Get( + * path = "/{email}/polls/send-by-email", + * name = "_polls_send_by_email" + * ) + * + * @param string $email + * + * @return JsonResponse + */ + public function sendPollsToUserAction( string $email ) { + $repository = $this->getDoctrine()->getRepository( Owner::class ); + + + // find user by email + $owner = $repository->findOneByEmail( $email ); + + if ( $owner ) { + $templateVars = [ + 'owner' => $owner, + 'polls' => $owner->getPolls(), + 'title' => 'Mes sondages - ' . $owner->getEmail(), + ]; + + // send email + $mailSent = 0; + try { + $mailSent = $this->sendOwnerPollsAction( $owner ); + } catch ( Exception $e ) { + } catch ( TransportExceptionInterface $e ) { + } + + if ( $mailSent ) { + return $this->json( [ + 'message' => 'mail succefully sent to user ' . $owner->getEmail(), + 'data' => '', + ], + 200 ); + } + + return $this->json( [ + 'message' => 'no sucess sending email ' . $owner->getEmail(), + 'data' => '', + ], + 400 ); + } + + return $this->json( [ + 'message' => 'no user found for email ' . $email, + 'data' => '', + ], + 400 ); + + } + +} diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 8a43b03..6752cb6 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -317,51 +317,14 @@ class PollController extends FramadateController { } - /** - * Delete all expired polls and their children - * @Get( - * path = "/clean-polls", - * name = "clean_expired_polls", - * ) - */ - public - function cleanExpiredPolls() { - $em = $this->getDoctrine()->getManager(); - $emPoll = $this->getDoctrine()->getRepository( Poll::class ); - - $queryFind = $em->createQuery( - 'SELECT p - FROM App\Entity\Poll p - WHERE p.expiracyDate < CURRENT_DATE()' - ); - $queryDelete = $em->createQuery( - 'DELETE - FROM App\Entity\Poll p - WHERE p.expiracyDate < CURRENT_DATE()' - ); - - $foundPolls = $queryFind->getResult(); - - $em->flush(); - - - return $this->json( [ - 'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ), - 'data' => [ - 'count' => count( $foundPolls ), - ], - ], - 200 ); - } - /** * Check is a slug is already taken by a poll * @Get( - * path = "/check-slug-is-unique/{slug}", + * path = "/poll/slug/{slug}", * name = "check_slug_is_unique", * ) */ - public function checkSlugIsUniqueAction( $slug ) { + public function checkSlugIsUniqueAction( string $slug ) { $emPoll = $this->getDoctrine()->getRepository( Poll::class ); $found = $emPoll->findOneBySlug( $slug ); if ( $found ) { @@ -387,10 +350,10 @@ class PollController extends FramadateController { /** - * Delete all expired polls and their children + * Get Admin poll config * @Get( * path = "/admin/{token}", - * name = "check_slug_is_unique", + * name = "get_admin_config", * ) */ public function getAdministrationConfig( $token ) {