Skip to content

Commit

Permalink
SRSImport now checks for already existing import users in kissj db (#269
Browse files Browse the repository at this point in the history
)

* SRSImport now checks for alreadyu existing users, and if those users are not in state paid||approved, then it deletes their Participant and creates new Ist Participant on them.

* refactored PR for small changes

---------

Co-authored-by: Lung <[email protected]>
  • Loading branch information
paaton and Lung authored Apr 21, 2024
1 parent 48d24fe commit 4116c1a
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 97 deletions.
56 changes: 39 additions & 17 deletions src/Import/ImportSrs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,30 @@
use kissj\Event\Event;
use kissj\Event\EventType\Obrok\EventTypeObrok;
use kissj\FlashMessages\FlashMessagesInterface;
use kissj\Participant\Ist\Ist;
use kissj\Participant\Ist\IstRepository;
use kissj\Participant\ParticipantRole;
use kissj\Participant\Ist\IstService;
use kissj\Participant\Participant;
use kissj\Participant\ParticipantRepository;
use kissj\Payment\PaymentService;
use kissj\Payment\PaymentStatus;
use kissj\User\User;
use kissj\User\UserRepository;
use kissj\User\UserService;
use kissj\User\UserStatus;
use League\Csv\Exception as LeagueCsvException;
use LeanMapper\Exception\InvalidStateException;
use Slim\Psr7\UploadedFile;
use Symfony\Contracts\Translation\TranslatorInterface;

readonly class ImportSrs
{
public function __construct(
private IstService $istService,
private IstRepository $istRepository,
private UserService $userService,
private UserRepository $userRepository,
private PaymentService $paymentService,
private ParticipantRepository $participantRepository,
private CsvParser $csvParser,
private FlashMessagesInterface $flashMessages,
private TranslatorInterface $translator,
Expand Down Expand Up @@ -61,8 +67,16 @@ public function importIst(UploadedFile $istsDataFile, Event $event): void
$existingCount++;
continue;
}

$newIst = $this->mapDataIntoNewIst($istData, $event);
$dbUser = $this->userRepository->findUserFromEmail($istData['E-mail'], $event);
if ($dbUser instanceof User) {
if ($dbUser->status === UserStatus::Approved || $dbUser->status === UserStatus::Paid) {
$errorCount++;
continue;
}
$newIst = $this->mapDataIntoNewIst($istData, $event, $dbUser);
} else {
$newIst = $this->mapDataIntoNewIst($istData, $event);
}
if ($newIst === null) {
$errorCount++;
} else {
Expand All @@ -79,11 +93,11 @@ public function importIst(UploadedFile $istsDataFile, Event $event): void
],
));
}

/**
* @param array<string,string> $data
* @throws InvalidStateException
*/
private function mapDataIntoNewIst(array $data, Event $event): ?User
private function mapDataIntoNewIst(array $data, Event $event, ?User $existingUser = null): ?Participant
{
$notes = [];
$notes['id'] = $data['id'];
Expand All @@ -109,9 +123,9 @@ private function mapDataIntoNewIst(array $data, Event $event): ?User
$userStatus = UserStatus::Paid;
}

$continget = EventTypeObrok::CONTINGENT_VOLUNTEER;
$contingent = EventTypeObrok::CONTINGENT_VOLUNTEER;
if ($data['role'] === 'Organizační tým - registruj se, pokud jsi v týmu Obroku 24') {
$continget = EventTypeObrok::CONTINGENT_ORG;
$contingent = EventTypeObrok::CONTINGENT_ORG;
}


Expand All @@ -134,19 +148,29 @@ private function mapDataIntoNewIst(array $data, Event $event): ?User
'Ano' => true,
default => false,
};

$user = $this->userService->createSkautisUserParticipantPayment(
if (!$existingUser) {

Check failure on line 151 in src/Import/ImportSrs.php

View workflow job for this annotation

GitHub Actions / test / Static Analysis

Only booleans are allowed in a negated boolean, kissj\User\User|null given.
$existingUser = $this->userService->createSkautisUser(
$event,
(int)$skautisUserId,
$email,
$userStatus,
);
} else {
$participant = $this->participantRepository->findParticipantFromUser($existingUser);
if ($participant !== null) {
$this->participantRepository->delete($participant);
}
}
return $this->istService->createIstPayment(
$existingUser,
$event,
(int)$skautisUserId,
$email,
$userStatus,
ParticipantRole::Ist,
$continget,
$contingent,
$data['first_name'],
$data['last_name'],
$data['nick_name'],
$address,
$data['phone'] === 'NULL' ? '' : $data['phone'],
$email,
$data['unit'] === 'NULL' ? '' : $data['unit'],
DateTimeUtils::getDateTime($data['birthdate']),
$data['Alergie (konkrétní jídlo, hmyz, pyly apod.)'],
Expand All @@ -169,8 +193,6 @@ private function mapDataIntoNewIst(array $data, Event $event): ?User
$event->swift,
DateTimeUtils::getDateTime('now + 14 days'),
);

return $user;
}

private function getArrivalDate(string $arrivaaDate): DateTimeImmutable
Expand Down
2 changes: 2 additions & 0 deletions src/Participant/Ist/IstRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use kissj\Event\Event;
use kissj\Orm\Order;
use kissj\Orm\Repository;
use kissj\Participant\ParticipantRole;

/**
* @table participant
Expand All @@ -24,6 +25,7 @@ public function isIstExisting(string $email, Event $event): bool
$qb = $this->createFluent();

$qb->where('participant.email = %s', $email);
$qb->where('participant.role = %s', ParticipantRole::Ist);
$qb->join('user')->as('u')->on('u.id = participant.user_id');
$qb->where('u.event_id = %i', $event->id);

Expand Down
104 changes: 104 additions & 0 deletions src/Participant/Ist/IstService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace kissj\Participant\Ist;

use DateTimeImmutable;
use kissj\Event\Event;
use kissj\Participant\Participant;
use kissj\Participant\ParticipantRepository;
use kissj\Participant\ParticipantRole;
use kissj\Payment\Payment;
use kissj\Payment\PaymentRepository;
use kissj\Payment\PaymentStatus;
use kissj\User\User;

readonly class IstService
{
public function __construct(
private ParticipantRepository $participantRepository,
private PaymentRepository $paymentRepository,
) {
}

/**
* @param array<string> $preferredPosition
*/
public function createIstPayment(
User $user,
Event $event,
string $contingent,
string $firstName,
string $lastName,
string $nickname,
string $permanentResidence,
string $telephoneNumber,
string $email,
string $scoutUnit,
DateTimeImmutable $birthDate,
string $healthProblems,
string $medicaments,
string $psychicalHealthProblems,
string $foodPreferences,
DateTimeImmutable $arrivalDate,
string $skills,
array $preferredPosition,
bool $printedHandbook,
string $notes,
DateTimeImmutable $registrationCloseDate,
DateTimeImmutable $registrationApproveDate,
?DateTimeImmutable $registrationPayDate,
string $variableSymbol,
int $price,
PaymentStatus $paymentStatus,
string $accountNumber,
string $iban,
string $swift,
DateTimeImmutable $due,
): Participant {
$participant = new Participant();
$participant->user = $user;
$participant->role = ParticipantRole::Ist;
$participant->contingent = $contingent;
$participant->firstName = $firstName;
$participant->lastName = $lastName;
$participant->nickname = $nickname;
$participant->permanentResidence = $permanentResidence;
$participant->telephoneNumber = $telephoneNumber;
$participant->email = $email;
$participant->scoutUnit = $scoutUnit;
$participant->birthDate = $birthDate;
$participant->healthProblems = $healthProblems;
$participant->medicaments = $medicaments;
$participant->psychicalHealthProblems = $psychicalHealthProblems;
$participant->foodPreferences = $foodPreferences;
$participant->arrivalDate = $arrivalDate;
$participant->skills = $skills;
$participant->preferredPosition = $preferredPosition;
$participant->printedHandbook = $printedHandbook;
$participant->notes = $notes;
$participant->registrationCloseDate = $registrationCloseDate;
$participant->registrationApproveDate = $registrationApproveDate;
$participant->registrationPayDate = $registrationPayDate;

$this->participantRepository->persist($participant);

$payment = new Payment();
$payment->variableSymbol = $variableSymbol;
$payment->price = (string)$price;
$payment->currency = '';
$payment->status = $paymentStatus;
$payment->purpose = 'fee';
$payment->accountNumber = $accountNumber;
$payment->iban = $iban;
$payment->swift = $swift;
$payment->due = $due;
$payment->note = $event->slug . ' ' . $participant->getFullName();
$payment->participant = $participant;

$this->paymentRepository->persist($payment);

return $participant;
}
}
80 changes: 1 addition & 79 deletions src/User/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

namespace kissj\User;

use DateTimeImmutable;
use kissj\Application\DateTimeUtils;
use kissj\Event\Event;
use kissj\Mailer\Mailer;
use kissj\Participant\Participant;
use kissj\Participant\ParticipantRepository;
use kissj\Participant\ParticipantRole;
use kissj\Payment\Payment;
use kissj\Payment\PaymentRepository;
use kissj\Payment\PaymentStatus;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteContext;

Expand All @@ -23,7 +19,6 @@ public function __construct(
private LoginTokenRepository $loginTokenRepository,
private ParticipantRepository $participantRepository,
private UserRepository $userRepository,
private PaymentRepository $paymentRepository,
private Mailer $mailer,
) {
}
Expand Down Expand Up @@ -134,42 +129,11 @@ public function createParticipantSetRole(User $user, string $role): Participant
return $participant;
}

/**
* @param array<string> $preferredPosition
*/
public function createSkautisUserParticipantPayment(
public function createSkautisUser(
Event $event,
int $skautisId,
string $email,
UserStatus $userStatus,
ParticipantRole $participantRole,
string $contingent,
string $firstName,
string $lastName,
string $nickname,
string $permanentResidence,
string $telephoneNumber,
string $scoutUnit,
DateTimeImmutable $birthDate,
string $healthProblems,
string $medicaments,
string $psychicalHealthProblems,
string $foodPreferences,
DateTimeImmutable $arrivalDate,
string $skills,
array $preferredPosition,
bool $printedHandbook,
string $notes,
DateTimeImmutable $registrationCloseDate,
DateTimeImmutable $registrationApproveDate,
?DateTimeImmutable $registrationPayDate,
string $variableSymbol,
int $price,
PaymentStatus $paymentStatus,
string $accountNumber,
string $iban,
string $swift,
DateTimeImmutable $due,
): User {
$user = new User();
$user->email = $email;
Expand All @@ -182,48 +146,6 @@ public function createSkautisUserParticipantPayment(

$this->userRepository->persist($user);

$participant = new Participant();
$participant->user = $user;
$participant->role = $participantRole;
$participant->contingent = $contingent;
$participant->firstName = $firstName;
$participant->lastName = $lastName;
$participant->nickname = $nickname;
$participant->permanentResidence = $permanentResidence;
$participant->telephoneNumber = $telephoneNumber;
$participant->email = $email;
$participant->scoutUnit = $scoutUnit;
$participant->birthDate = $birthDate;
$participant->healthProblems = $healthProblems;
$participant->medicaments = $medicaments;
$participant->psychicalHealthProblems = $psychicalHealthProblems;
$participant->foodPreferences = $foodPreferences;
$participant->arrivalDate = $arrivalDate;
$participant->skills = $skills;
$participant->preferredPosition = $preferredPosition;
$participant->printedHandbook = $printedHandbook;
$participant->notes = $notes;
$participant->registrationCloseDate = $registrationCloseDate;
$participant->registrationApproveDate = $registrationApproveDate;
$participant->registrationPayDate = $registrationPayDate;

$this->participantRepository->persist($participant);

$payment = new Payment();
$payment->variableSymbol = $variableSymbol;
$payment->price = (string)$price;
$payment->currency = '';
$payment->status = $paymentStatus;
$payment->purpose = 'fee';
$payment->accountNumber = $accountNumber;
$payment->iban = $iban;
$payment->swift = $swift;
$payment->due = $due;
$payment->note = $event->slug . ' ' . $participant->getFullName();
$payment->participant = $participant;

$this->paymentRepository->persist($payment);

return $user;
}

Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Payment/PaymentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function testGenerateVariableNumber(): void
Mockery::mock(LoginTokenRepository::class),
Mockery::mock(ParticipantRepository::class),
Mockery::mock(UserRepository::class),
Mockery::mock(PaymentRepository::class),
$mailerMock,
),
Mockery::mock(FlashMessagesBySession::class),
Expand Down

0 comments on commit 4116c1a

Please sign in to comment.