From 5ac02483ea725eb9c674cb78ca2305735a324c6e Mon Sep 17 00:00:00 2001 From: Lung Date: Wed, 15 May 2024 23:36:07 +0200 Subject: [PATCH] added patrol and troop participants into entry endpoint --- src/Entry/EntryParticipant.php | 21 +-- src/Participant/ParticipantRepository.php | 161 ++++++++++++++-------- 2 files changed, 116 insertions(+), 66 deletions(-) diff --git a/src/Entry/EntryParticipant.php b/src/Entry/EntryParticipant.php index 46052793..872bd1c4 100644 --- a/src/Entry/EntryParticipant.php +++ b/src/Entry/EntryParticipant.php @@ -4,17 +4,20 @@ namespace kissj\Entry; -readonly class EntryParticipant +class EntryParticipant { + /** @var EntryParticipant[] */ + public array $participants = []; + public function __construct( - public int $id, - public string $firstname, - public string $lastname, - public string $nickname, - public ?string $patrolName, - public string $tieCode, - public \DateTimeInterface $birthDate, - public bool $sfh, + readonly public int $id, + readonly public string $firstname, + readonly public string $lastname, + readonly public string $nickname, + readonly public ?string $patrolName, + readonly public string $tieCode, + readonly public \DateTimeInterface $birthDate, + readonly public bool $sfh, ) { } } diff --git a/src/Participant/ParticipantRepository.php b/src/Participant/ParticipantRepository.php index 99eeea02..6e9b86e9 100755 --- a/src/Participant/ParticipantRepository.php +++ b/src/Participant/ParticipantRepository.php @@ -190,63 +190,6 @@ private function filterEmptyParticipants(array $participants): array ); } - /** - * @return array> - */ - public function getParticipantsForEntry(Event $event): array - { - $qb = $this->connection->select(' - participant.id, - participant.first_name, - participant.last_name, - participant.nickname, - participant.patrol_name, - participant.tie_code, - participant.birth_date, - participant.role, - d.is_done AS sfh_done - ')->from($this->getTable()); - - $qb->join('user')->as('u')->on('u.id = participant.user_id'); - $qb->leftJoin('deal')->as('d')->on('participant.id = d.participant_id')->and('d.slug = %s', 'sfh'); - - $qb->where('u.role = %s', UserRole::Participant); - $qb->where('u.status = %s', UserStatus::Paid); - $qb->where('u.event_id = %i', $event->id); - - $this->addOrdersBy($qb, [new Order('id')]); - - $rows = $qb->fetchAll(); - - $participants = []; - /** @var array{ - * id: int, - * first_name: string|null, - * last_name: string|null, - * nickname: string|null, - * patrol_name: string|null, - * tie_code: string, - * birth_date: \DateTimeInterface|null, - * sfh_done: bool|null, - * role: string|null - * } $row - */ - foreach($rows as $row) { - $participants[$row['role']][] = new EntryParticipant( - $row['id'], - $row['first_name'] ?? '', - $row['last_name'] ?? '', - $row['nickname'] ?? '', - $row['patrol_name'] ?? '', - $row['tie_code'], - $row['birth_date'] ?? DateTimeUtils::getDateTime(), - $row['sfh_done'] ?? false, - ); - } - - return $participants; - } - /** * @param string[] $contingents * @return StatisticUserValueObject[] @@ -389,4 +332,108 @@ public function getParticipantById(int $participantId, Event $event): Participan return $participant; } + + /** + * @return array> + */ + public function getParticipantsForEntry(Event $event): array + { + $rows = $this->getRowsForEntryParticipant($event, [ + ParticipantRole::PatrolLeader, + ParticipantRole::TroopLeader, + ParticipantRole::Ist, + ParticipantRole::Guest, + ]); + + $participants = []; + foreach($rows as $row) { + /** @var string $role */ + $role = $row['role']; + $participants[$role][$row['id']] = $this->mapDataToEntryParticipant($row); + } + + $rowsDependableParticipants = $this->getRowsForEntryParticipant($event, [ + ParticipantRole::PatrolParticipant, + ParticipantRole::TroopParticipant, + ]); + + foreach ($rowsDependableParticipants as $rowDependableParticipant) { + $role = match ($rowDependableParticipant['role']) { + ParticipantRole::TroopParticipant->value => ParticipantRole::TroopLeader->value, + ParticipantRole::PatrolParticipant->value => ParticipantRole::PatrolLeader->value, + default => 'unknown', + }; + + $leader = $participants[$role][$rowDependableParticipant['patrol_leader_id']] ?? null; + if ($leader instanceof EntryParticipant) { + $leader->participants[$rowDependableParticipant['id']] + = $this->mapDataToEntryParticipant($rowDependableParticipant); + } + } + + return $participants; + } + + /** + * @param Event $event + * @param array $participantRoles + * @return array + * / + */ + private function getRowsForEntryParticipant(Event $event, array $participantRoles): array + { + $qb = $this->connection->select(' + participant.id, + participant.first_name, + participant.last_name, + participant.nickname, + participant.patrol_name, + participant.tie_code, + participant.birth_date, + participant.role, + participant.patrol_leader_id, + d.is_done AS sfh_done + ')->from($this->getTable()); + + $qb->join('user')->as('u')->on('u.id = participant.user_id'); + $qb->leftJoin('deal')->as('d')->on('participant.id = d.participant_id')->and('d.slug = %s', 'sfh'); + + $qb->where('u.role = %s', UserRole::Participant); + $qb->where('u.status = %s', UserStatus::Paid); + $qb->where('u.event_id = %i', $event->id); + + $qb->where('participant.role IN %in', $participantRoles); + + $this->addOrdersBy($qb, [new Order('id')]); + + return $qb->fetchAll(); + } + + private function mapDataToEntryParticipant(Row $row): EntryParticipant + { + /** @var array{ + * id: int, + * first_name: string|null, + * last_name: string|null, + * nickname: string|null, + * patrol_name: string|null, + * tie_code: string, + * birth_date: \DateTimeInterface|null, + * patrol_leader_id: int|null, + * sfh_done: bool|null, + * role: string|null + * } $array */ + $array = $row->toArray(); + + return new EntryParticipant( + $array['id'], + $array['first_name'] ?? '', + $array['last_name'] ?? '', + $array['nickname'] ?? '', + $array['patrol_name'] ?? '', + $array['tie_code'], + $array['birth_date'] ?? DateTimeUtils::getDateTime(), + $array['sfh_done'] ?? false, + ); + } }