Skip to content

Commit

Permalink
added approving participants into v3 API + deleted old CEJ24 stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtomklima committed Nov 24, 2024
1 parent b668ec3 commit 47d94fa
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 64 deletions.
15 changes: 14 additions & 1 deletion src/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace kissj;

use DI\Attribute\Inject;
use GuzzleHttp\Utils;
use kissj\Entry\EntryStatus;
use kissj\Event\Event;
use kissj\FileHandler\SaveFileHandler;
Expand Down Expand Up @@ -68,12 +69,24 @@ protected function redirect(
->withStatus(302);
}

protected function getJsonResponseFromException(Response $response, TranslatableException $e): Response
{
return $this->getResponseWithJson(
$response,
[
'translationKey' => $e->translationKey,
'translationMessage' => $this->translator->trans($e->translationKey),
],
$e->httpStatus,
);
}

/**
* @param array<string,int|string|EntryStatus|null|array<mixed>> $json
*/
protected function getResponseWithJson(Response $response, array $json, int $statusCode = 200): Response
{
$encodedJson = guzzleJsonEncode($json);
$encodedJson = Utils::jsonEncode($json);
$response->getBody()->write($encodedJson);

return $response
Expand Down
8 changes: 7 additions & 1 deletion src/Application/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DI\Container;
use kissj\Deal\DealController;
use kissj\Entry\EntryController;
use kissj\Participant\Admin\AdminJsonController;
use kissj\ParticipantVendor\ParticipantVendorController;
use kissj\Event\EventController;
use kissj\Export\ExportController;
Expand Down Expand Up @@ -373,9 +374,14 @@ public function addRoutesInto(App $app): App
$app->group('/event/{eventSlug}', function (RouteCollectorProxy $app) {
$app->group('/admin', function (RouteCollectorProxy $app) {
$app->group('/{participantId}', function (RouteCollectorProxy $app) {
$app->post('/adminNote', AdminController::class . '::changeAdminNote')
$app->post('/adminNote', AdminJsonController::class . '::changeAdminNote')
->setName('admin-change-note');
});

$app->group('/approving', function (RouteCollectorProxy $app) {
$app->post('/approveParticipant/{participantId}', AdminJsonController::class . '::approveParticipant')
->setName('admin-approve-json');
});
})->add(AdminsOnlyMiddleware::class)->add(LoggedOnlyMiddleware::class);
});
});
Expand Down
7 changes: 0 additions & 7 deletions src/Mailer/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ public function sendRegistrationApprovedWithNonFirstPayment(
$this->sendMailWithPayment($participant, $payment, 'payment-nonfirst-info');
}

public function sendRegistrationApprovedForSpecialPayment(
Participant $participant,
Payment $payment,
): void {
$this->sendMailWithPayment($participant, $payment, 'payment-cs-contingent-info');
}

private function sendMailWithPayment(
Participant $participant,
Payment $payment,
Expand Down
18 changes: 16 additions & 2 deletions src/Participant/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace kissj\Participant\Admin;

use kissj\Participant\Guest\Guest;
use kissj\Participant\ParticipantException;
use kissj\Participant\Patrol\PatrolLeader;
use kissj\Participant\Troop\TroopParticipant;
use RuntimeException;
use kissj\AbstractController;
use kissj\BankPayment\BankPayment;
Expand Down Expand Up @@ -258,8 +261,19 @@ public function approveParticipant(
): Response {
$participant = $this->participantRepository->getParticipantById($participantId, $event);

$this->participantService->approveRegistration($participant);
$this->logger->info('Approved registration for participant with ID ' . $participant->id);
try {
$participant = $this->participantService->approveRegistration($participant);

if ($participant instanceof Guest) {
$this->flashMessages->success('flash.success.guestApproved');
} elseif ($participant instanceof TroopParticipant) {
$this->flashMessages->success('flash.success.tpApproved');
} else {
$this->flashMessages->success('flash.success.approved');
}
} catch (ParticipantException $e) {
$this->flashMessages->warning($e->translationKey);
}

return $this->redirect($request, $response, 'admin-show-approving');
}
Expand Down
59 changes: 59 additions & 0 deletions src/Participant/Admin/AdminJsonController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace kissj\Participant\Admin;

use kissj\AbstractController;
use kissj\Event\Event;
use kissj\Participant\ParticipantException;
use kissj\Participant\ParticipantRepository;
use kissj\Participant\ParticipantService;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class AdminJsonController extends AbstractController
{
public function __construct(
private readonly ParticipantService $participantService,
private readonly ParticipantRepository $participantRepository,
) {
}

public function approveParticipant(
Response $response,
Event $event,
int $participantId,
): Response {
$participant = $this->participantRepository->getParticipantById($participantId, $event);

try {
$participant = $this->participantService->approveRegistration($participant);
} catch (ParticipantException $e) {
return $this->getJsonResponseFromException($response, $e);
}

return $this->getResponseWithJson(
$response,
[
'userStatus' => $participant->getUserButNotNull()->status->value,
]
);
}

public function changeAdminNote(
Request $request,
Response $response,
Event $event,
int $participantId,
): Response {
$participant = $this->participantRepository->getParticipantById($participantId, $event);

$this->participantService->setAdminNote(
$participant,
$this->getParameterFromBody($request, 'adminNote'),
);

return $this->getResponseWithJson($response, ['adminNote' => $participant->adminNote]);
}
}
11 changes: 0 additions & 11 deletions src/Participant/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,6 @@ public function getNoncanceledPayments(): array
});
}

public function isInCzechContingent(): bool
{
return $this->contingent === EventTypeCej::CONTINGENT_CZECHIA;
}

public function isInSpecialPaymentContingent(): bool
{
return $this->contingent === EventTypeCej::CONTINGENT_CZECHIA
|| $this->contingent === EventTypeCej::CONTINGENT_HUNGARY;
}

public function getQrParticipantInfoString(): string
{
return $this->entryCode;
Expand Down
11 changes: 11 additions & 0 deletions src/Participant/ParticipantException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace kissj\Participant;

use kissj\TranslatableException;

class ParticipantException extends TranslatableException
{
}
19 changes: 5 additions & 14 deletions src/Participant/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,13 @@ public function denyRegistration(Participant $participant, string $reason): Part
return $participant;
}

/**
* @throws ParticipantException
*/
public function approveRegistration(Participant $participant): Participant
{
if ($participant->getUserButNotNull()->status === UserStatus::Approved) {
$this->flashMessages->warning('flash.warning.notApproved');

return $participant;
throw new ParticipantException('flash.warning.notApproved');
}

$this->userService->setUserApproved($participant->getUserButNotNull());
Expand All @@ -363,28 +364,18 @@ public function approveRegistration(Participant $participant): Participant
if ($participant instanceof Guest) {
$this->userService->setUserPaid($participant->getUserButNotNull());
$this->mailer->sendGuestRegistrationFinished($participant);
$this->flashMessages->success('flash.success.guestApproved');

return $participant;
}

if ($participant instanceof TroopParticipant) {
$this->mailer->sendTroopParticipantRegistrationFinished($participant);
$this->flashMessages->success('flash.success.tpApproved');

return $participant;
}

$payment = $this->paymentService->createAndPersistNewPayment($participant);

if ($participant->isInCzechContingent()) {
$this->mailer->sendRegistrationApprovedForSpecialPayment($participant, $payment);
} elseif ($participant->isInSpecialPaymentContingent()) {
$this->mailer->sendRegistrationApprovedWithoutPayment($participant);
} else {
$this->mailer->sendRegistrationApprovedWithPayment($participant, $payment);
}
$this->flashMessages->success('flash.success.approved');
$this->mailer->sendRegistrationApprovedWithPayment($participant, $payment);

return $participant;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use kissj\Middleware\UserAuthenticationMiddleware;
use kissj\Orm\Mapper;
use kissj\Participant\Admin\AdminController;
use kissj\Participant\Admin\AdminJsonController;
use kissj\Participant\Admin\AdminRepository;
use kissj\Participant\Admin\AdminService;
use kissj\Participant\Guest\GuestRepository;
Expand Down Expand Up @@ -153,6 +154,7 @@ public function getContainerDefinition(string $envPath, string $envFilename): ar
// https://php-di.org/doc/performances.html#optimizing-for-compilation
$container = [
AdminController::class => autowire(),
AdminJsonController::class => autowire(),
AdminRepository::class => autowire(),
AdminService::class => autowire(),
AdminsOnlyMiddleware::class => autowire(),
Expand Down
28 changes: 0 additions & 28 deletions src/Templates/translatable/emails/payment-cs-contingent-info.twig

This file was deleted.

17 changes: 17 additions & 0 deletions src/TranslatableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace kissj;

use Exception;

class TranslatableException extends Exception
{
public function __construct(
readonly public string $translationKey,
readonly public int $httpStatus = 400,
) {
parent::__construct($translationKey);
}
}

0 comments on commit 47d94fa

Please sign in to comment.