From 98b762cbfad246c2664febc0cff78cdb45caec1c Mon Sep 17 00:00:00 2001 From: Lung Date: Sun, 19 May 2024 12:21:53 +0200 Subject: [PATCH] added route for full troop entry --- src/Application/Route.php | 8 +++- src/Entry/EntryController.php | 77 ++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/src/Application/Route.php b/src/Application/Route.php index 78ea772e..1006a592 100755 --- a/src/Application/Route.php +++ b/src/Application/Route.php @@ -307,9 +307,13 @@ public function addRoutesInto(App $app): App $app->post('/code/{entryCode}', EntryController::class . '::entry') ->setName('entry'); - $app->post('/participant/{participantId}', EntryController::class . '::entryFromWebApp') + $app->post('/participant/{participantId}', EntryController::class . '::entryParticipantFromWebApp') ->add(ApiAuthorizedOnlyMiddleware::class) - ->setName('entry-from-web-app'); + ->setName('entry-participant-from-web-app'); + + $app->post('/troop/{participantId}', EntryController::class . '::entryTroopFromWebApp') + ->add(ApiAuthorizedOnlyMiddleware::class) + ->setName('entry-troop-from-web-app'); }); $app->group('/event/{eventSlug}', function (RouteCollectorProxy $app) { diff --git a/src/Entry/EntryController.php b/src/Entry/EntryController.php index bbd5b205..67b55ddd 100755 --- a/src/Entry/EntryController.php +++ b/src/Entry/EntryController.php @@ -8,6 +8,7 @@ use kissj\Event\Event; use kissj\Participant\ParticipantRepository; use kissj\Participant\ParticipantService; +use kissj\Participant\Troop\TroopLeader; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -43,39 +44,18 @@ public function entry( // TODO solve code ABCDEFGH - default code for DB - block or generate better data in DB $participant = $this->participantRepository->findOneByEntryCode($entryCode); if ($participant === null) { - return $this->getResponseWithJson( - $response, - [ - 'status' => EntryStatus::ENTRY_STATUS_INVALID, - 'reason' => 'participant not found', - ], - 403, - ); + return $this->createErrorEntryResponse($response, 'participant not found'); } $bodyJson = $this->getParsedJsonFromBody($request); if (!array_key_exists('eventSecret', $bodyJson)) { - return $this->getResponseWithJson( - $response, - [ - 'status' => EntryStatus::ENTRY_STATUS_INVALID, - 'reason' => 'in JSON request key eventSecret is missing', - ], - 422, - ); + return $this->createErrorEntryResponse($response, 'in JSON request key eventSecret is missing'); } $eventSecret = $bodyJson['eventSecret']; $event = $participant->getUserButNotNull()->event; if ($event->apiSecret !== $eventSecret) { - return $this->getResponseWithJson( - $response, - [ - 'status' => EntryStatus::ENTRY_STATUS_INVALID, - 'reason' => 'invalid event secret', - ], - 403, - ); + return $this->createErrorEntryResponse($response, 'invalid event secret'); } $participantInfo = [ @@ -105,21 +85,14 @@ public function entry( ); } - public function entryFromWebApp( + public function entryParticipantFromWebApp( Response $response, Event $authorizedEvent, int $participantId, ): Response { $participant = $this->participantRepository->findParticipantById($participantId, $authorizedEvent); if ($participant === null) { - return $this->getResponseWithJson( - $response, - [ - 'status' => EntryStatus::ENTRY_STATUS_INVALID, - 'reason' => 'participant not found', - ], - 403, - ); + return $this->createErrorEntryResponse($response, 'participant not found'); } if ($participant->entryDate !== null) { @@ -141,4 +114,42 @@ public function entryFromWebApp( ], ); } + + public function entryTroopFromWebApp( + Response $response, + Event $authorizedEvent, + int $participantId, + ): Response { + $participant = $this->participantRepository->findParticipantById($participantId, $authorizedEvent); + if ($participant instanceof TroopLeader === false) { + return $this->createErrorEntryResponse($response, 'troop not found'); + } + + foreach (array_merge([$participant], $participant->troopParticipants) as $troopParticipant) { + if ($troopParticipant->entryDate === null) { + $this->participantService->setAsEntered($troopParticipant); + } + } + + return $this->getResponseWithJson( + $response, + [ + 'status' => EntryStatus::ENTRY_STATUS_VALID, + ], + ); + } + + private function createErrorEntryResponse( + Response $response, + string $reason, + ): Response { + return $this->getResponseWithJson( + $response, + [ + 'status' => EntryStatus::ENTRY_STATUS_INVALID, + 'reason' => $reason, + ], + 403, + ); + } }