1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

up debug message in poll creation

This commit is contained in:
Tykayn 2022-02-10 09:53:24 +01:00 committed by tykayn
parent 9414951370
commit 46d6d21c6b
2 changed files with 528 additions and 513 deletions

View File

@ -25,14 +25,16 @@ use Symfony\Component\HttpFoundation\Response;
* @package App\Controller * @package App\Controller
* @Route("/api/v1/poll",name="api_") * @Route("/api/v1/poll",name="api_")
*/ */
class PollController extends EmailsController { class PollController extends EmailsController
{
/** /**
* @Get( * @Get(
* path = "/", * path = "/",
* name = "get_all_polls" * name = "get_all_polls"
* ) * )
*/ */
public function getAllPollsAction( PollRepository $pollRepository ): Response { public function getAllPollsAction(PollRepository $pollRepository): Response
{
$data = $pollRepository->findAll(); $data = $pollRepository->findAll();
@ -41,28 +43,28 @@ class PollController extends EmailsController {
$pollData = [ $pollData = [
'message' => 'here are your polls', 'message' => 'here are your polls',
'count' => count( $polls ), 'count' => count($polls),
]; ];
$debug = 1; $debug = 1;
if ( $debug ) { if ($debug) {
foreach ( $polls as $poll ) { foreach ($polls as $poll) {
$titles[] = [ $titles[] = [
'title' => $poll->getTitle(), 'title' => $poll->getTitle(),
'slug' => $poll->getCustomUrl(), 'slug' => $poll->getCustomUrl(),
]; ];
} }
$pollData[ 'polls' ] = $titles; $pollData['polls'] = $titles;
} }
return $this->json( $pollData ); return $this->json($pollData);
} }
/** /**
* get a poll config by its custom URL, we do not want polls to be reachable by their numeric id * get a poll config by its public custom URL, we do not want polls to be reachable by their numeric id
* @Get( * @Get(
* path = "/{customUrl}", * path = "/{customUrl}",
* name = "get_poll" * name = "get_poll"
@ -77,12 +79,13 @@ class PollController extends EmailsController {
SerializerInterface $serializer, SerializerInterface $serializer,
$customUrl, $customUrl,
Request $request Request $request
) { )
$repository = $this->getDoctrine()->getRepository( Poll::class ); {
$poll = $repository->findOneByCustomUrl( $customUrl ); $repository = $this->getDoctrine()->getRepository(Poll::class);
$poll = $repository->findOneByCustomUrl($customUrl);
if ( ! $poll ) { if (!$poll) {
return $this->notFoundPoll( $customUrl ); return $this->notFoundPoll($customUrl);
} }
$comments = $poll->getComments(); $comments = $poll->getComments();
@ -95,17 +98,17 @@ class PollController extends EmailsController {
/** /**
* password protected content * password protected content
*/ */
if ( $pass ) { if ($pass) {
// no password possibly given by this route // no password possibly given by this route
return $this->json( [ return $this->json([
'message' => 'this is protected by a password,but you did not provide the encoded password parameter, and you should feel bad. ', 'message' => 'this is protected by a password,but you did not provide the encoded password parameter, and you should feel bad. ',
], ],
403 ); 403);
} else { } else {
// free access to poll // free access to poll
$pollResult = $poll->display(); $pollResult = $poll->display();
return $this->json( $pollResult ); return $this->json($pollResult);
} }
} }
@ -116,11 +119,12 @@ class PollController extends EmailsController {
* *
* @return JsonResponse * @return JsonResponse
*/ */
public function notFoundPoll( $id ): Response { public function notFoundPoll($id): Response
return $this->json( [ {
return $this->json([
'message' => $id . ' : poll not found', 'message' => $id . ' : poll not found',
], ],
404 ); 404);
} }
/** /**
@ -132,22 +136,23 @@ class PollController extends EmailsController {
* *
* @return JsonResponse|Response * @return JsonResponse|Response
*/ */
function getOwnerPolls( $owner_email , LoggerInterface $logger ) { function getOwnerPolls($owner_email, LoggerInterface $logger)
$repository = $this->getDoctrine()->getRepository( Owner::class ); {
$owner = $repository->findOneByEmail( $owner_email ); $repository = $this->getDoctrine()->getRepository(Owner::class);
if ( ! $owner ) { $owner = $repository->findOneByEmail($owner_email);
return $this->json( [ 'message' => "Owner $owner_email non trouvé" ], 404 ); if (!$owner) {
return $this->json(['message' => "Owner $owner_email non trouvé"], 404);
} else { } else {
$polls = $owner->getPolls(); $polls = $owner->getPolls();
$pollsDisplay = []; $pollsDisplay = [];
foreach ( $polls as $p ) { foreach ($polls as $p) {
$pollsDisplay[] = $p->displayForAdmin(); $pollsDisplay[] = $p->displayForAdmin();
} }
$mail_sent = $this->sendOwnerPollsAction( $owner ); $mail_sent = $this->sendOwnerPollsAction($owner);
$logger->info('getOwnerPolls : Email sent : '.$mail_sent); $logger->info('getOwnerPolls : Email sent : ' . $mail_sent);
return $this->json( [ 'mail_sent' => $mail_sent ], $mail_sent ? 200 : 404 ); return $this->json(['mail_sent' => $mail_sent], $mail_sent ? 200 : 404);
// return $this->json(['owner' => $owner->displayForAdmin(), 'polls' => $pollsDisplay], 200); // return $this->json(['owner' => $owner->displayForAdmin(), 'polls' => $pollsDisplay], 200);
} }
@ -165,27 +170,28 @@ class PollController extends EmailsController {
* *
* @return JsonResponse|Response * @return JsonResponse|Response
*/ */
function getProtectedPoll( $customUrl, $md5, SerializerInterface $serializer ) { function getProtectedPoll($customUrl, $md5, SerializerInterface $serializer)
$repository = $this->getDoctrine()->getRepository( Poll::class ); {
$poll = $repository->findOneByCustomUrl( $customUrl ); $repository = $this->getDoctrine()->getRepository(Poll::class);
$poll = $repository->findOneByCustomUrl($customUrl);
if ( ! $poll ) { if (!$poll) {
return $this->notFoundPoll( $customUrl ); return $this->notFoundPoll($customUrl);
} }
if ( md5( $poll->getPassword() ) === $md5 ) { if (md5($poll->getPassword()) === $md5) {
// good matching pass // good matching pass
return $this->json( $poll->display() ); return $this->json($poll->display());
} else { } else {
// wrong pass // wrong pass
return $this->json( [ return $this->json([
'message' => 'this is protected by a password, your password hash "' . $md5 . '" is wrong, and you should feel bad', 'message' => 'this is protected by a password, your password hash "' . $md5 . '" is wrong, and you should feel bad',
'pass' => $md5 , 'pass' => $md5,
'md5' => md5( $md5 ), 'md5' => md5($md5),
'md5( $poll->getPassword() )' => md5( $poll->getPassword() ), 'md5( $poll->getPassword() )' => md5($poll->getPassword()),
'data' => null, 'data' => null,
], ],
403 ); 403);
} }
@ -193,7 +199,9 @@ class PollController extends EmailsController {
/** /**
* as an administrator of a poll, get a poll config by its custom URL, we do not want polls to be reachable by their numeric id * as an administrator of a poll,
* get a poll config by its custom URL,
* we do not want polls to be reachable by their numeric id
* @Get( * @Get(
* path = "admin/{admin_key}", * path = "admin/{admin_key}",
* name = "get_admin_poll", * name = "get_admin_poll",
@ -204,16 +212,17 @@ class PollController extends EmailsController {
* *
* @return JsonResponse|Response * @return JsonResponse|Response
*/ */
function getAdminPoll( $admin_key, $md5, SerializerInterface $serializer ) { function getAdminPoll($admin_key, $md5, SerializerInterface $serializer)
$repository = $this->getDoctrine()->getRepository( Poll::class ); {
$poll = $repository->findOneByAdminKey( $admin_key ); $repository = $this->getDoctrine()->getRepository(Poll::class);
$poll = $repository->findOneByAdminKey($admin_key);
if ( ! $poll ) { if (!$poll) {
return $this->notFoundPoll( $admin_key ); return $this->notFoundPoll($admin_key);
} }
// good matching pass // good matching pass
return $this->json( $poll->displayForAdmin() ); return $this->json($poll->displayForAdmin());
} }
@ -224,12 +233,13 @@ class PollController extends EmailsController {
* @param $serializer * @param $serializer
* @return Response * @return Response
*/ */
function returnPollData( $poll, $serializer ) { function returnPollData($poll, $serializer)
$jsonResponse = $serializer->serialize( $poll, 'json' ); {
$jsonResponse = $serializer->serialize($poll, 'json');
$response = new Response( $jsonResponse ); $response = new Response($jsonResponse);
$response->headers->set( 'Content-Type', 'application/json' ); $response->headers->set('Content-Type', 'application/json');
$response->setStatusCode( 200 ); $response->setStatusCode(200);
return $response; return $response;
} }
@ -245,23 +255,24 @@ class PollController extends EmailsController {
Poll $poll, Poll $poll,
string $token, string $token,
Request $request Request $request
) { )
if ( $poll->getAdminKey() !== $token ) { {
return $this->json( [ if ($poll->getAdminKey() !== $token) {
return $this->json([
'message' => 'you are NOT allowed to update the poll ' . $poll->getTitle(), 'message' => 'you are NOT allowed to update the poll ' . $poll->getTitle(),
], ],
403 ); 403);
} }
// TODO check validity of request // TODO check validity of request
// update only if we have the admin key // update only if we have the admin key
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist( $poll ); $em->persist($poll);
$em->flush(); $em->flush();
return $this->json( $poll->displayForAdmin() return $this->json($poll->displayForAdmin()
, ,
200 ); 200);
} }
/** /**
@ -274,127 +285,129 @@ class PollController extends EmailsController {
* create a new poll * create a new poll
* @return JsonResponse * @return JsonResponse
*/ */
public function newPollAction( Request $request ) { public function newPollAction(Request $request)
{
$data = $request->getContent(); $data = $request->getContent();
$data = json_decode( $data, true ); $data = json_decode($data, true);
// search for existing custom url, which must be unique // search for existing custom url, which must be unique
$custom_url = $data[ 'custom_url' ]; $custom_url = $data['custom_url'];
$repository = $this->getDoctrine()->getRepository( Poll::class ); $repository = $this->getDoctrine()->getRepository(Poll::class);
$poll = $repository->findOneByCustomUrl( $custom_url ); $poll = $repository->findOneByCustomUrl($custom_url);
if ( $poll ) { if ($poll) {
throw new \JsonException( 'NOPE, ce sondage existe déjà: ' . $custom_url ); return $this->json(["message"=> 'NOPE, ce sondage existe déjà: ' . $custom_url], 403 );
} }
$newpoll = new Poll(); $newpoll = new Poll();
$newpoll $newpoll
->setModificationPolicy( isset( $data[ 'modification_policy' ] ) ? $data[ 'modification_policy' ] : 'everybody' ) ->setModificationPolicy(isset($data['modification_policy']) ? $data['modification_policy'] : 'everybody')
->setTitle( $data[ 'title' ] ) ->setTitle($data['title'])
->setKind( $data[ 'kind' ] ) ->setKind($data['kind'])
->setCustomUrl( $data[ 'custom_url' ] ); ->setCustomUrl($custom_url);
if ( count( $data[ 'allowed_answers' ] ) ) { if (count($data['allowed_answers'])) {
// TODO check this one // TODO check this one
$newpoll->setAllowedAnswers( $data[ 'allowed_answers' ] ); $newpoll->setAllowedAnswers($data['allowed_answers']);
} }
// define a maximum expiration // define a maximum expiration
$expiracyCalculated = min($newpoll->addDaysToDate( new DateTime(), $expiracyCalculated = min($newpoll->addDaysToDate(new DateTime(),
$data[ 'default_expiracy_days_from_now' ] ), $newpoll->addDaysToDate( new DateTime(), $data['default_expiracy_days_from_now']), $newpoll->addDaysToDate(new DateTime(),
360 )); 360));
$newpoll->setExpiracyDate( $expiracyCalculated ); $newpoll->setExpiracyDate($expiracyCalculated);
$emOwner = $this->getDoctrine()->getRepository( Owner::class ); $emOwner = $this->getDoctrine()->getRepository(Owner::class);
$foundOwner = $emOwner->findOneByEmail( $data[ 'owner' ][ 'email' ] ); $foundOwner = $emOwner->findOneByEmail($data['owner']['email']);
$userWasFound = false; $userWasFound = false;
if ( ! $foundOwner ) { if (!$foundOwner) {
//create a new owner //create a new owner
$owner = new Owner(); $owner = new Owner();
$owner->setPseudo( $data[ 'owner' ][ 'pseudo' ] ); $owner->setPseudo($data['owner']['pseudo']);
$owner->setEmail( $data[ 'owner' ][ 'email' ] ); $owner->setEmail($data['owner']['email']);
$foundOwner = $owner; $foundOwner = $owner;
} else { } else {
$userWasFound = true; $userWasFound = true;
} }
// link the owner and the poll // link the owner and the poll
$newpoll->setOwner( $foundOwner ); $newpoll->setOwner($foundOwner);
$foundOwner->addPoll( $newpoll ); $foundOwner->addPoll($newpoll);
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist( $newpoll ); $em->persist($newpoll);
$em->persist( $foundOwner ); $em->persist($foundOwner);
// emails // emails
$newpoll->setMailOnComment( true ); $newpoll->setMailOnComment(true);
$newpoll->setMailOnVote( $data[ 'isOwnerNotifiedByEmailOnNewVote' ] ); $newpoll->setMailOnVote($data['isOwnerNotifiedByEmailOnNewVote']);
$newpoll->setMailOnComment( $data[ 'isOwnerNotifiedByEmailOnNewComment' ] ); $newpoll->setMailOnComment($data['isOwnerNotifiedByEmailOnNewComment']);
$newpoll->setIsZeroKnowledge( $data[ 'is_zero_knowledge' ] ); $newpoll->setIsZeroKnowledge($data['is_zero_knowledge']);
$newpoll->setDescription( $data[ 'description' ] ); $newpoll->setDescription($data['description']);
$newpoll->setHideResults( $data[ 'hideResults' ] ); $newpoll->setHideResults($data['hideResults']);
// possible answers // possible answers
$newpoll->setAllowedAnswers( $data[ 'allowed_answers' ] ); $newpoll->setAllowedAnswers($data['allowed_answers']);
$newpoll->setVotesMax( $data[ 'maxCountOfAnswers' ] ); $newpoll->setVotesMax($data['maxCountOfAnswers']);
$newpoll->setCommentsAllowed( $data[ 'allowComments' ] ); $newpoll->setCommentsAllowed($data['allowComments']);
// setup the password, converting the raw with md5 hash // setup the password, converting the raw with md5 hash
if ( $data[ 'password' ] ) { if ($data['password']) {
$newpoll->setPassword( $data[ 'password' ] ); $newpoll->setPassword($data['password']);
} }
$choices_debug = '';
// text kind of answers, dates are below // text kind of answers, dates are below
if ( $data[ 'kind' ] == 'text' ) { if ($data['kind'] == 'text') {
// manage choices // manage choices
$choices = $data[ 'choices' ]; $choices = $data['choicesText'];
foreach ( $choices as $c ) { foreach ($choices as $c) {
$newChoice = new Choice(); $newChoice = new Choice();
$newChoice $newChoice
->setPoll( $newpoll ) ->setPoll($newpoll)
->setName( $c[ 'literal' ] ); ->setName($c['literal']);
$em->persist( $newChoice ); $em->persist($newChoice);
$newpoll->addChoice( $newChoice ); $newpoll->addChoice($newChoice);
} }
} // date kind of poll } // date kind of poll
elseif ( $data[ 'kind' ] == 'date' ) { elseif ($data['kind'] == 'date') {
$choices = $data[ 'dateChoices' ]; $choices = $data['dateChoices'];
if ( isset( $data[ 'hasSeveralHours' ] ) && $data[ 'hasSeveralHours' ] == true ) { $choices_debug .= 'debug count recieved' . count($choices);
if (isset($data['hasSeveralHours']) && $data['hasSeveralHours'] == true) {
// different hours spans make more choices // different hours spans make more choices
foreach ( $choices as $c ) { foreach ($choices as $c) {
$currentDate = $c[ 'literal' ]; $currentDate = $c['literal'];
$timeSlicesOfThisChoice = $c['timeSlices'];
$timeSlicesOfThisChoice = $c[ 'timeSlices' ]; foreach ($timeSlicesOfThisChoice as $t) {
foreach ( $timeSlicesOfThisChoice as $t ) {
$newChoice = new Choice(); $newChoice = new Choice();
$newChoice $newChoice
->setPoll( $newpoll ) ->setPoll($newpoll)
->setName( $currentDate . ' >>> ' . $t[ 'literal' ] ); ->setName($currentDate . ' >>> ' . $t['literal']);
$em->persist( $newChoice ); $em->persist($newChoice);
$newpoll->addChoice( $newChoice ); $newpoll->addChoice($newChoice);
} }
} }
} else { } else {
// all choices will be having the same time slices from timeSlices // all choices will be having the same time slices from timeSlices
$timeSlicesForAllChoices = $data[ 'timeSlices' ]; $timeSlicesForAllChoices = $data['timeSlices'];
foreach ( $choices as $c ) { foreach ($choices as $c) {
$currentDate = $c[ 'date_object' ]; $currentDate = $c['date_object'];
foreach ( $timeSlicesForAllChoices as $t ) { foreach ($timeSlicesForAllChoices as $t) {
$newChoice = new Choice(); $newChoice = new Choice();
$newChoice $newChoice
->setPoll( $newpoll ) ->setPoll($newpoll)
->setName( $currentDate . ' >>> ' . $t[ 'literal' ] ); ->setName($currentDate . ' >>> ' . $t['literal']);
$em->persist( $newChoice ); $em->persist($newChoice);
$newpoll->addChoice( $newChoice ); $newpoll->addChoice($newChoice);
} }
@ -404,25 +417,24 @@ class PollController extends EmailsController {
} }
$em->persist( $newpoll ); $em->persist($newpoll);
$em->flush(); $em->flush();
$precision = ''; $precision = '';
if ( $userWasFound ) { if ($userWasFound) {
$precision = 'from an existing user : ' . $foundOwner->getEmail(); $precision = 'from an existing user : ' . $foundOwner->getEmail();
} }
$this->sendCreationMailAction( $foundOwner, $newpoll ); $this->sendCreationMailAction($foundOwner, $newpoll);
$newChoices = $newpoll->display()[ 'choices' ]; return $this->json([
'message' => 'you created the poll ' . $newpoll->getCustomUrl() . ' ' . $precision,
return $this->json( [ 'debug' => $choices_debug,
'message' => 'you created the poll ' . $newpoll->getCustomUrl() . $precision,
'id' => $newpoll->getId(), 'id' => $newpoll->getId(),
'poll' => $newpoll->displayForAdmin(), 'poll' => $newpoll->displayForAdmin(),
'password_protected' => is_string( $newpoll->getPassword() ), 'password_protected' => is_string($newpoll->getPassword()),
], ],
201 ); 201);
} }
@ -444,14 +456,15 @@ class PollController extends EmailsController {
// public function sendCreationMailAction( Owner $admin_user, Poll $poll, \Swift_Mailer $mailer) { // public function sendCreationMailAction( Owner $admin_user, Poll $poll, \Swift_Mailer $mailer) {
public function testSendCreationMailAction( public function testSendCreationMailAction(
$emailChoice = 'tktest_commentateur@tktest.com' $emailChoice = 'tktest_commentateur@tktest.com'
) { )
$em = $this->getDoctrine()->getRepository( Poll::class ); {
$foundPoll = $em->findOneByCustomUrl( 'dessin-anime' ); $em = $this->getDoctrine()->getRepository(Poll::class);
$em = $this->getDoctrine()->getRepository( Owner::class ); $foundPoll = $em->findOneByCustomUrl('dessin-anime');
$foundOwner = $em->findOneByEmail( $emailChoice ); $em = $this->getDoctrine()->getRepository(Owner::class);
$foundOwner = $em->findOneByEmail($emailChoice);
return $this->render( 'emails/creation-mail.html.twig', return $this->render('emails/creation-mail.html.twig',
[ 'poll' => $foundPoll, 'owner' => $foundPoll->getOwner() ] ); ['poll' => $foundPoll, 'owner' => $foundPoll->getOwner()]);
} }
@ -470,24 +483,25 @@ class PollController extends EmailsController {
public public
function deletePollAction( function deletePollAction(
$admin_key $admin_key
) { )
{
$emPoll = $this->getDoctrine()->getRepository( Poll::class ); $emPoll = $this->getDoctrine()->getRepository(Poll::class);
$found = $emPoll->findOneByAdminKey( $admin_key ); $found = $emPoll->findOneByAdminKey($admin_key);
if ( $found ) { if ($found) {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->remove( $found ); $em->remove($found);
$em->flush(); $em->flush();
return $this->json( [ return $this->json([
'message' => 'boom! le sondage et ses objets assocités a été supprimé', 'message' => 'boom! le sondage et ses objets assocités a été supprimé',
] ); ]);
} else { } else {
return $this->json( [ return $this->json([
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
] ); ]);
} }
} }
@ -499,38 +513,39 @@ class PollController extends EmailsController {
* name = "check_slug_is_unique", * name = "check_slug_is_unique",
* ) * )
*/ */
public function checkSlugIsUniqueAction( string $customUrl ) { public function checkSlugIsUniqueAction(string $customUrl)
$emPoll = $this->getDoctrine()->getRepository( Poll::class ); {
$found = $emPoll->findOneByCustomUrl( $customUrl ); $emPoll = $this->getDoctrine()->getRepository(Poll::class);
$found = $emPoll->findOneByCustomUrl($customUrl);
$elaborated_message_version = false; $elaborated_message_version = false;
if ( $found ) { if ($found) {
if ( ! $elaborated_message_version ) { if (!$elaborated_message_version) {
return $this->json( null, return $this->json(null,
204 ); 204);
} }
// we should use an other slug // we should use an other slug
return $this->json( [ return $this->json([
'message' => ' NO, this custom_url is already taken on this Framadate instance ', 'message' => ' NO, this custom_url is already taken on this Framadate instance ',
'data' => [ 'data' => [
'slug' => $customUrl, 'slug' => $customUrl,
], ],
], ],
204 ); 204);
} }
if ( ! $elaborated_message_version ) { if (!$elaborated_message_version) {
return $this->json( null, return $this->json(null,
404 ); 404);
} }
return $this->json( [ return $this->json([
'message' => ' yes this slug is available on this Framadate instance ', 'message' => ' yes this slug is available on this Framadate instance ',
'data' => [ 'data' => [
'slug' => $customUrl, 'slug' => $customUrl,
], ],
], ],
404 ); 404);
} }
@ -546,11 +561,12 @@ class PollController extends EmailsController {
* *
* @return JsonResponse|Response * @return JsonResponse|Response
*/ */
public function getAdministrationConfig( SerializerInterface $serializer, $token ) { public function getAdministrationConfig(SerializerInterface $serializer, $token)
$emPoll = $this->getDoctrine()->getRepository( Poll::class ); {
$pollFound = $emPoll->findOneByAdminKey( $token ); $emPoll = $this->getDoctrine()->getRepository(Poll::class);
$pollFound = $emPoll->findOneByAdminKey($token);
if ( $pollFound ) { if ($pollFound) {
$poll = $pollFound; $poll = $pollFound;
@ -559,17 +575,17 @@ class PollController extends EmailsController {
'poll' => $poll->displayForAdmin(), 'poll' => $poll->displayForAdmin(),
]; ];
return $this->json( $returnedPoll, return $this->json($returnedPoll,
200 );; 200);;
} }
return $this->json( [ return $this->json([
'message' => 'You are not allowed to do anything with this token', 'message' => 'You are not allowed to do anything with this token',
'data' => [ 'data' => [
'token' => $token, 'token' => $token,
], ],
], ],
403 ); 403);
} }
@ -584,19 +600,20 @@ class PollController extends EmailsController {
* *
* @return JsonResponse|Response * @return JsonResponse|Response
*/ */
public function getExpiredPollsCleanup( $token ) { public function getExpiredPollsCleanup($token)
{
if ( $token !== 'superCaligistriixpirlidouciousse' ) { if ($token !== 'superCaligistriixpirlidouciousse') {
return $this->json( [ return $this->json([
'message' => 'not allowed', 'message' => 'not allowed',
'data' => [ 'data' => [
'token' => $token, 'token' => $token,
], ],
], ],
403 ); 403);
} }
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$emPoll = $this->getDoctrine()->getRepository( Poll::class ); $emPoll = $this->getDoctrine()->getRepository(Poll::class);
$deletablePollsFound = $emPoll->findDeletableExpiredPolls(); // dead by more than 30 days $deletablePollsFound = $emPoll->findDeletableExpiredPolls(); // dead by more than 30 days
$expiredPollsFound = $emPoll->findExpiredPolls(); // just dead $expiredPollsFound = $emPoll->findExpiredPolls(); // just dead
$soon_expired_polls = $emPoll->findSoonExpiredPolls(); // will die in 30 days $soon_expired_polls = $emPoll->findSoonExpiredPolls(); // will die in 30 days
@ -608,42 +625,42 @@ class PollController extends EmailsController {
$really_delete = false; $really_delete = false;
foreach ( $soon_expired_polls as $item ) { foreach ($soon_expired_polls as $item) {
$soon_expired_title[] = $item->getTitle(); $soon_expired_title[] = $item->getTitle();
} }
foreach ( $expiredPollsFound as $item ) { foreach ($expiredPollsFound as $item) {
$expiredTitle[] = $item->getTitle(); $expiredTitle[] = $item->getTitle();
$item->setVotesAllowed( false ); $item->setVotesAllowed(false);
$em->persist( $item ); $em->persist($item);
} }
foreach ( $deletablePollsFound as $item ) { foreach ($deletablePollsFound as $item) {
$deletedTitle[] = $item->getTitle(); $deletedTitle[] = $item->getTitle();
if ( $really_delete ) { if ($really_delete) {
$item->setVotesAllowed( false ); $item->setVotesAllowed(false);
$em->remove( $item ); $em->remove($item);
} }
} }
$em->flush(); $em->flush();
return $this->json( [ return $this->json([
'message' => 'cleanup report', 'message' => 'cleanup report',
'really_delete' => $really_delete, 'really_delete' => $really_delete,
'deleted' => count( $deletablePollsFound ), 'deleted' => count($deletablePollsFound),
'deleted_titles' => $deletedTitle, 'deleted_titles' => $deletedTitle,
'expired' => count( $expiredPollsFound ), 'expired' => count($expiredPollsFound),
'expired_titles' => $expiredTitle, 'expired_titles' => $expiredTitle,
'soon_to_be_expired' => count( $soon_expired_polls ), 'soon_to_be_expired' => count($soon_expired_polls),
'soon_to_be_expired_titles' => $soon_expired_title, 'soon_to_be_expired_titles' => $soon_expired_title,
'data' => [ 'data' => [
'token' => $token, 'token' => $token,
], ],
], ],
200 ); 200);
} }

View File

@ -306,8 +306,6 @@ class Poll
// handle sub time slices // handle sub time slices
if (count($boom) == 2) { if (count($boom) == 2) {
if (!isset($grouped_dates[$boom[0]])) { if (!isset($grouped_dates[$boom[0]])) {
$grouped_dates[$boom[0]] = [ $grouped_dates[$boom[0]] = [