parent
c251eac2fb
commit
2836ccb137
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Poll;
|
||||
use FOS\RestBundle\Controller\Annotations\Get;
|
||||
use FOS\RestBundle\Controller\Annotations\Route;
|
||||
|
||||
/**
|
||||
* Class DefaultController
|
||||
* @package App\Controller
|
||||
* @Route("/admin",name="admin_homepage")
|
||||
*/
|
||||
class AdminController extends FramadateController {
|
||||
/**
|
||||
* @Get(path ="/",
|
||||
* name = "_get_default")
|
||||
*/
|
||||
public function indexAction() {
|
||||
|
||||
return $this->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 );
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Owner;
|
||||
use FOS\RestBundle\Controller\Annotations\Get;
|
||||
use FOS\RestBundle\Controller\Annotations\Route;
|
||||
use JMS\Serializer\Type\Exception\Exception;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
|
||||
/**
|
||||
* Class DefaultController
|
||||
* @package App\Controller
|
||||
* @Route("/user",name="user_homepage")
|
||||
*/
|
||||
class OwnerController extends FramadateController {
|
||||
/**
|
||||
* @Get(path ="/",
|
||||
* name = "get_default")
|
||||
*/
|
||||
public function indexAction() {
|
||||
|
||||
return $this->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 );
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue