Skip to content

Commit

Permalink
SRSImport now checks for alreadyu existing users, and if those users …
Browse files Browse the repository at this point in the history
…are not in state paid||approved, then it deletes their Participant and creates new Ist Participant on them.
  • Loading branch information
paaton committed Apr 16, 2024
1 parent 3139a27 commit 4ba2218
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 90 deletions.
58 changes: 43 additions & 15 deletions src/Import/ImportSrs.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,34 @@
use kissj\FlashMessages\FlashMessagesInterface;
use kissj\Participant\Ist\Ist;
use kissj\Participant\Ist\IstRepository;
use kissj\Participant\Ist\IstService;
use kissj\Participant\Participant;
use kissj\Participant\ParticipantRepository;
use kissj\Participant\ParticipantRole;
use kissj\Payment\PaymentService;
use kissj\Payment\PaymentStatus;
use kissj\User\User;
use kissj\User\UserRepository;
use kissj\User\UserRole;
use kissj\User\UserService;
use kissj\User\UserStatus;
use League\Csv\Exception as LeagueCsvException;
use LeanMapper\Exception\InvalidStateException;
use setasign\Fpdi\PdfParser\Type\PdfNull;
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 @@ -62,8 +74,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 @@ -80,11 +100,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 @@ -110,9 +130,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 @@ -135,19 +155,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.)'],
Expand All @@ -170,8 +200,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: 1 addition & 1 deletion src/Participant/Admin/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function handlePayments(
}

$correctPayment->participant = $participantTo;
$this->paymentRepository->persist($correctPayment);
$this->paymentRepository-> persist($correctPayment);

return $correctPayment;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Participant/Ist/IstRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

namespace kissj\Participant\Ist;

use DateTimeImmutable;
use Dibi\Row;
use kissj\Event\Event;
use kissj\Orm\Order;
use kissj\Orm\Repository;
use kissj\Participant\Participant;
use kissj\Participant\ParticipantRole;
use kissj\Payment\Payment;
use kissj\Payment\PaymentStatus;

/**
* @table participant
Expand All @@ -24,9 +29,11 @@ public function findIst(string $email, Event $event): ?Ist
$qb = $this->createFluent();

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


/** @var Row $row */
$row = $qb->fetch();
/** @var ?Ist $ist */
Expand Down
105 changes: 105 additions & 0 deletions src/Participant/Ist/IstService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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;
}
}
76 changes: 2 additions & 74 deletions src/User/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 +133,12 @@ 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,47 +151,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

0 comments on commit 4ba2218

Please sign in to comment.