Skip to content

Commit

Permalink
added route for full troop entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Lung committed May 19, 2024
1 parent 77c22b1 commit 98b762c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
8 changes: 6 additions & 2 deletions src/Application/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
77 changes: 44 additions & 33 deletions src/Entry/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
);
}
}

0 comments on commit 98b762c

Please sign in to comment.