diff --git a/src/Controller/api/v1/PollController.php b/src/Controller/api/v1/PollController.php index 8ea4031..e4bb7a9 100644 --- a/src/Controller/api/v1/PollController.php +++ b/src/Controller/api/v1/PollController.php @@ -121,6 +121,36 @@ class PollController extends EmailsController { 404 ); } + /** + * get a poll config by its custom URL, we do not want polls to be reachable by their numeric id + * @Get( + * path = "/owner/{owner_email}/", + * name = "get_owner_poll", + * ) + * + * @return JsonResponse|Response + */ + function getOwnerPolls( $owner_email ) { + $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 ) { + $pollsDisplay[] = $p->displayForAdmin(); + } + $mail_sent = $this->sendOwnerPollsAction( $owner ); + + return $this->json( [ 'mail_sent' => $mail_sent ], $mail_sent ? 200 : 404 ); +// return $this->json(['owner' => $owner->displayForAdmin(), 'polls' => $pollsDisplay], 200); + } + + } + /** * get a poll config by its custom URL, we do not want polls to be reachable by their numeric id * @Get( diff --git a/src/Repository/PollRepository.php b/src/Repository/PollRepository.php index a829e7c..65119cc 100755 --- a/src/Repository/PollRepository.php +++ b/src/Repository/PollRepository.php @@ -35,15 +35,15 @@ class PollRepository extends ServiceEntityRepository { return $this->findExpirationPollOfDay( - 30 ); } - public function findExpirationPollOfDay( $count_of_days){ + public function findExpirationPollOfDay( $count_of_days ) { - $today = new \DateTime(); + $today = new \DateTime(); - if($count_of_days > -1){ + if ( $count_of_days > - 1 ) { - $date_soon = $today->add( new \DateInterval( 'P' . $count_of_days . 'D' ) ); - }else{ - $date_soon = $today->sub( new \DateInterval( 'P' . abs($count_of_days) . 'D' ) ); + $date_soon = $today->add( new \DateInterval( 'P' . $count_of_days . 'D' ) ); + } else { + $date_soon = $today->sub( new \DateInterval( 'P' . abs( $count_of_days ) . 'D' ) ); } @@ -55,4 +55,21 @@ class PollRepository extends ServiceEntityRepository { ->getResult(); } + /** + * find all the polls of an owner + * + * @param $email + * + * @return int|mixed|string + */ + public function findAllByOwnerEmail( $email ) { + return $this->createQueryBuilder( 'p' ) + ->andWhere( 'p.owner.email = :email' ) + ->setParameter( 'email', $email ) + ->orderBy( 'p.id', 'DESC' ) + ->getQuery() + ->getResult(); + } + + }