Repository navigation
Migre l'entité TicketSpecialPrice de Ting vers Doctrine #2417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Korbeil
wants to merge
1
commit into
afup:master
Choose a base branch
from
Korbeil:afup-migrer-ting-ticketspecialprice-vers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+442
−535
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
sources/AppBundle/Event/Entity/Repository/TicketSpecialPriceRepository.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace AppBundle\Event\Entity\Repository; | ||
|
|
||
| use AppBundle\Doctrine\EntityRepository; | ||
| use AppBundle\Association\Entity\Utilisateur; | ||
| use AppBundle\Event\Entity\Inscription; | ||
| use AppBundle\Event\Entity\TicketSpecialPrice; | ||
| use AppBundle\Event\Model\Ticket; | ||
| use DateTimeImmutable; | ||
| use Doctrine\DBAL\ArrayParameterType; | ||
| use Doctrine\Persistence\ManagerRegistry; | ||
|
|
||
| /** | ||
| * @extends EntityRepository<TicketSpecialPrice> | ||
| */ | ||
| final class TicketSpecialPriceRepository extends EntityRepository | ||
| { | ||
| public function __construct(ManagerRegistry $registry) | ||
| { | ||
| parent::__construct($registry, TicketSpecialPrice::class); | ||
| } | ||
|
|
||
| /** | ||
| * Retourne le token de prix spécial s'il est dans sa fenêtre de validité | ||
| * et qu'aucune inscription (hors annulations) ne l'a déjà consommé. | ||
| */ | ||
| public function findUnusedToken(int $eventId, ?string $token): ?TicketSpecialPrice | ||
| { | ||
| if (!is_string($token) || $token === '') { | ||
| return null; | ||
| } | ||
|
|
||
| $now = new DateTimeImmutable(); | ||
|
|
||
| /** @var TicketSpecialPrice|null $ticketSpecialPrice */ | ||
| $ticketSpecialPrice = $this->createQueryBuilder('specialPrice') | ||
| ->where('specialPrice.token = :token') | ||
| ->andWhere('specialPrice.eventId = :eventId') | ||
| ->andWhere('specialPrice.dateStart <= :now') | ||
| ->andWhere('specialPrice.dateEnd >= :now') | ||
| ->andWhere('NOT EXISTS ( | ||
| SELECT inscription.id | ||
| FROM ' . Inscription::class . ' inscription | ||
| WHERE inscription.specialPriceToken = specialPrice.token | ||
| AND inscription.etat <> :etatAnnule | ||
| )') | ||
| ->setMaxResults(1) | ||
| ->setParameter('token', $token) | ||
| ->setParameter('eventId', $eventId) | ||
| ->setParameter('now', $now) | ||
| ->setParameter('etatAnnule', Ticket::STATUS_CANCELLED) | ||
| ->getQuery() | ||
| ->getOneOrNullResult() | ||
| ; | ||
|
|
||
| return $ticketSpecialPrice; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<string> $tokens | ||
| * @return list<TicketSpecialPrice> | ||
| */ | ||
| public function findByTokens(array $tokens): array | ||
| { | ||
| $tokens = array_values(array_unique($tokens)); | ||
| if ($tokens === []) { | ||
| return []; | ||
| } | ||
|
|
||
| /** @var list<TicketSpecialPrice> $specialPrices */ | ||
| $specialPrices = $this->createQueryBuilder('specialPrice') | ||
| ->where('specialPrice.token IN (:tokens)') | ||
| ->setParameter('tokens', $tokens, ArrayParameterType::STRING) | ||
| ->getQuery() | ||
| ->getResult() | ||
| ; | ||
|
|
||
| return $specialPrices; | ||
| } | ||
|
|
||
| /** | ||
| * Liste des tokens d'un évènement avec le nom du créateur et l'indicateur | ||
| * d'utilisation (une inscription non annulée a consommé le token). | ||
| * | ||
| * @return list<array{specialPrice: TicketSpecialPrice, creatorPrenom: ?string, creatorNom: ?string, used: bool}> | ||
| */ | ||
| public function getByEvent(int $eventId): array | ||
| { | ||
| /** @var list<TicketSpecialPrice> $specialPrices */ | ||
| $specialPrices = $this->findBy(['eventId' => $eventId], ['id' => 'DESC']); | ||
| if ($specialPrices === []) { | ||
| return []; | ||
| } | ||
|
|
||
| // Comptage des inscriptions (hors annulées) qui ont consommé chaque token | ||
| $tokens = array_map( | ||
| static fn(TicketSpecialPrice $specialPrice): string => $specialPrice->token, | ||
| $specialPrices, | ||
| ); | ||
|
|
||
| $queryBuilder = $this->getEntityManager()->createQueryBuilder(); | ||
| $queryBuilder | ||
| ->select('inscription.specialPriceToken AS token', 'COUNT(inscription.id) AS nbInscriptions') | ||
| ->from(Inscription::class, 'inscription') | ||
| ->where('inscription.specialPriceToken IN (:tokens)') | ||
| ->andWhere('inscription.etat <> :etatAnnule') | ||
| ->groupBy('inscription.specialPriceToken') | ||
| ->setParameter('tokens', $tokens, ArrayParameterType::STRING) | ||
| ->setParameter('etatAnnule', Ticket::STATUS_CANCELLED) | ||
| ; | ||
|
|
||
| $usedCounts = []; | ||
| foreach ($queryBuilder->getQuery()->getArrayResult() as $row) { | ||
| if (!is_array($row) || !is_string($row['token'] ?? null)) { | ||
| continue; // le groupe par token garantit la présence de la clé | ||
| } | ||
|
|
||
| $token = $row['token']; | ||
| $nbInscriptions = $row['nbInscriptions'] ?? null; | ||
| if (is_int($nbInscriptions)) { | ||
| $usedCounts[$token] = $nbInscriptions > 0; | ||
| } | ||
| } | ||
|
|
||
| // Noms des créateurs, chargés depuis l'entité Utilisateur (table afup_personnes_physiques) | ||
| $creatorIds = array_map( | ||
| static fn(TicketSpecialPrice $specialPrice): int => $specialPrice->creatorId, | ||
| $specialPrices, | ||
| ); | ||
| $creators = $this->getEntityManager() | ||
| ->getRepository(Utilisateur::class) | ||
| ->findBy(['id' => array_values(array_unique($creatorIds))]) | ||
| ; | ||
| $creatorsById = []; | ||
| foreach ($creators as $creator) { | ||
| if ($creator->id !== null) { | ||
| $creatorsById[$creator->id] = $creator; | ||
| } | ||
| } | ||
|
|
||
| $byEvent = []; | ||
| foreach ($specialPrices as $specialPrice) { | ||
| $creator = $creatorsById[$specialPrice->creatorId] ?? null; | ||
| $byEvent[] = [ | ||
| 'specialPrice' => $specialPrice, | ||
| 'creatorPrenom' => $creator?->firstname, | ||
| 'creatorNom' => $creator?->lastname, | ||
| 'used' => $usedCounts[$specialPrice->token] ?? false, | ||
| ]; | ||
| } | ||
|
|
||
| return $byEvent; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On passera les propriétés en français dans un second temps