diff --git a/src/Import/ImportSrs.php b/src/Import/ImportSrs.php index 24914fd3..59dce410 100644 --- a/src/Import/ImportSrs.php +++ b/src/Import/ImportSrs.php @@ -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, @@ -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 { @@ -79,11 +93,11 @@ public function importIst(UploadedFile $istsDataFile, Event $event): void ], )); } - /** * @param array $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']; @@ -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; } @@ -134,19 +148,29 @@ private function mapDataIntoNewIst(array $data, Event $event): ?User 'Ano' => true, default => false, }; - - $user = $this->userService->createSkautisUserParticipantPayment( + if (!$existingUser) { + $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.)'], @@ -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 diff --git a/src/Participant/Ist/IstRepository.php b/src/Participant/Ist/IstRepository.php index 7a1d646f..140a092f 100755 --- a/src/Participant/Ist/IstRepository.php +++ b/src/Participant/Ist/IstRepository.php @@ -8,6 +8,7 @@ use kissj\Event\Event; use kissj\Orm\Order; use kissj\Orm\Repository; +use kissj\Participant\ParticipantRole; /** * @table participant @@ -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); diff --git a/src/Participant/Ist/IstService.php b/src/Participant/Ist/IstService.php new file mode 100644 index 00000000..f7e38806 --- /dev/null +++ b/src/Participant/Ist/IstService.php @@ -0,0 +1,104 @@ + $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 = 'Kč'; + $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; + } +} diff --git a/src/User/UserService.php b/src/User/UserService.php index b74909f1..6e0e9caf 100755 --- a/src/User/UserService.php +++ b/src/User/UserService.php @@ -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; @@ -23,7 +19,6 @@ public function __construct( private LoginTokenRepository $loginTokenRepository, private ParticipantRepository $participantRepository, private UserRepository $userRepository, - private PaymentRepository $paymentRepository, private Mailer $mailer, ) { } @@ -134,42 +129,11 @@ public function createParticipantSetRole(User $user, string $role): Participant return $participant; } - /** - * @param array $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; @@ -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 = 'Kč'; - $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; } diff --git a/tests/Unit/Payment/PaymentServiceTest.php b/tests/Unit/Payment/PaymentServiceTest.php index 27d0f2fc..dca4d665 100755 --- a/tests/Unit/Payment/PaymentServiceTest.php +++ b/tests/Unit/Payment/PaymentServiceTest.php @@ -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),