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

View File

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