Skip to content

Commit

Permalink
added leave API route
Browse files Browse the repository at this point in the history
  • Loading branch information
Lung committed May 31, 2024
1 parent 01af873 commit 0ccb193
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/Application/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ public function addRoutesInto(App $app): App
->setName('deal-catch-data-from-google-form');

$app->group('/entry', function (RouteCollectorProxy $app) {

$app->get('/list', EntryController::class . '::list')
->add(ApiAuthorizedOnlyMiddleware::class)
->setName('entry-list');
Expand All @@ -333,14 +332,26 @@ public function addRoutesInto(App $app): App
->add(AddCorsHeaderForAppDomainsMiddleware::class)
->setName('entry-troop-from-web-app');
});
$app->group('/vendor', function (RouteCollectorProxy $app) {

$app->group('/leave', function (RouteCollectorProxy $app) {
$app->map(['POST', 'OPTIONS'], '/participant/{participantId}', EntryController::class . '::leaveParticipantFromWebApp')
->add(ApiAuthorizedOnlyMiddleware::class)
->add(AddCorsHeaderForAppDomainsMiddleware::class)
->setName('leave-participant-from-web-app');

$app->map(['POST', 'OPTIONS'], '/troop/{participantId}', EntryController::class . '::leaveTroopFromWebApp')
->add(ApiAuthorizedOnlyMiddleware::class)
->add(AddCorsHeaderForAppDomainsMiddleware::class)
->setName('leave-troop-from-web-app');
});

$app->group('/vendor', function (RouteCollectorProxy $app) {
$app->map(['POST', 'OPTIONS'], '/bearercheck', function (Response $response) { return $response->withStatus(200); })
->add(ApiAuthorizedOnlyMiddleware::class)
->add(AddCorsHeaderForAppDomainsMiddleware::class)
->setName('entry-participant-from-web-app');

$app->map(['GET', 'OPTIONS'],'/participant/{tieCode}', ParticipantVendorController::class . '::retrieveParticipantByTieCode')
$app->map(['GET', 'OPTIONS'], '/participant/{tieCode}', ParticipantVendorController::class . '::retrieveParticipantByTieCode')
->add(ApiAuthorizedOnlyMiddleware::class)
->add(AddCorsHeaderForAppDomainsMiddleware::class)
->setName('entry-troop-from-web-app');
Expand Down
22 changes: 22 additions & 0 deletions src/Application/migrations/20240531104721_add_leave_date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddLeaveDate extends AbstractMigration
{
public function up(): void
{
$tableParticipant = $this->table('participant');
$tableParticipant->addColumn('leave_date', 'datetime', ['null' => true]);
$tableParticipant->save();
}

public function down(): void
{
$participantTable = $this->table('participant');
$participantTable->removeColumn('leave_date');
$participantTable->save();
}
}
30 changes: 30 additions & 0 deletions src/Entry/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,36 @@ public function entryParticipantFromWebApp(
);
}

public function leaveParticipantFromWebApp(
Response $response,
Event $authorizedEvent,
int $participantId,
): Response {
$participant = $this->participantRepository->findParticipantById($participantId, $authorizedEvent);
if ($participant === null) {
return $this->createErrorEntryResponse($response, 'participant not found');
}

if ($participant->leaveDate !== null) {
return $this->getResponseWithJson(
$response,
[
'status' => EntryStatus::ENTRY_STATUS_VALID,
'entryDateTime' => null,
],
);
}

$this->participantService->setAsLeaved($participant);

return $this->getResponseWithJson(
$response,
[
'status' => EntryStatus::ENTRY_STATUS_LEAVED,
],
);
}

public function entryFromAdmin(Request $request, Response $response, Event $event, int $participantId): Response
{
$participant = $this->participantRepository->getParticipantById($participantId, $event);
Expand Down
3 changes: 2 additions & 1 deletion src/Entry/EntryStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ enum EntryStatus: string
case ENTRY_STATUS_VALID = 'valid';
case ENTRY_STATUS_USED = 'used';
case ENTRY_STATUS_INVALID = 'invalid';
case ENTRY_STATUS_LEAVED = 'leaved';

public static function fromDatetime(?DateTimeInterface $entryDate): self
public static function entryFromDatetime(?DateTimeInterface $entryDate): self
{
return match ($entryDate) {
null => self::ENTRY_STATUS_VALID,
Expand Down
1 change: 1 addition & 0 deletions src/Participant/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* @property string $tieCode
* @property string $entryCode
* @property DateTimeInterface|null $entryDate m:passThru(dateFromString|dateToString)
* @property DateTimeInterface|null $leaveDate m:passThru(dateFromString|dateToString)
*
* @property Payment[] $payment m:belongsToMany
* @property Deal[] $deals m:belongsToMany
Expand Down
6 changes: 3 additions & 3 deletions src/Participant/ParticipantRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ public function findOneByTieCodeAndEvent(string $tieCode, Event $authorizedEvent
if ($participant === null) {
return null;
}
if ($participant->user->event->id === $authorizedEvent->id) {

if ($participant->user?->event->id === $authorizedEvent->id) {
return $participant;
}

Expand Down Expand Up @@ -443,7 +443,7 @@ private function mapDataToEntryParticipant(Row $row): EntryParticipant
$array['patrol_name'] ?? '',
$array['tie_code'],
$array['birth_date'] ?? DateTimeUtils::getDateTime(),
EntryStatus::fromDatetime($array['entry_date']),
EntryStatus::entryFromDatetime($array['entry_date']),
$array['sfh_done'] ?? false,
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Participant/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,21 @@ public function setAsUnentered(Participant $participant): Participant

return $participant;
}
public function setAsLeaved(Participant $participant): Participant
{
$participant->leaveDate = DateTimeUtils::getDateTime();
$this->participantRepository->persist($participant);

return $participant;
}

public function setAsUnleaved(Participant $participant): Participant
{
$participant->leaveDate = null;
$this->participantRepository->persist($participant);

return $participant;
}

public function tryChangeRoleWithMessages(string $roleFromBody, Participant $participant, Event $event): bool
{
Expand Down

0 comments on commit 0ccb193

Please sign in to comment.