Skip to content
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

Adherent can not change committee if in recent election #10961

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Controller/Api/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use App\Repository\CommitteeRepository;
use App\Repository\DonationRepository;
use App\Repository\TaxReceiptRepository;
use App\Repository\VotingPlatform\VoterRepository;
use App\Utils\HttpUtils;
use Doctrine\ORM\EntityManagerInterface;
use League\Flysystem\FilesystemOperator;
Expand Down Expand Up @@ -270,8 +271,12 @@ public function myInstances(UserInterface $adherent, CommitteeRepository $commit

#[Route(path: '/committees/{uuid}/join', methods: ['PUT'])]
#[Security('is_granted("ROLE_OAUTH_SCOPE_WRITE:PROFILE") and user.isRenaissanceAdherent()')]
public function saveMyNewCommittee(Committee $committee, UserInterface $adherent, CommitteeMembershipManager $committeeMembershipManager): Response
{
public function saveMyNewCommittee(
Committee $committee,
UserInterface $adherent,
CommitteeMembershipManager $committeeMembershipManager,
VoterRepository $voterRepository,
): Response {
/** @var Adherent $adherent */
if (
!array_intersect(
Expand All @@ -284,6 +289,21 @@ public function saveMyNewCommittee(Committee $committee, UserInterface $adherent
], Response::HTTP_BAD_REQUEST);
}

$currentCommittee = $adherent->getCommitteeV2Membership()?->getCommittee();

if (
$currentCommittee
&& $voterRepository->isInVoterListForCommitteeElection(
$adherent,
$currentCommittee,
new \DateTime('-3 months')
)
) {
return $this->json([
'message' => 'Vous avez participé à une élection interne il y a moins de 3 mois dans votre comité. Il ne vous est pas possible d\'en changer.',
], Response::HTTP_BAD_REQUEST);
}

$committeeMembershipManager->followCommittee(
$adherent,
$committee,
Expand Down
49 changes: 35 additions & 14 deletions src/Repository/VotingPlatform/VoterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Repository\VotingPlatform;

use App\Entity\Adherent;
use App\Entity\Committee;
use App\Entity\VotingPlatform\Election;
use App\Entity\VotingPlatform\ElectionRound;
use App\Entity\VotingPlatform\Vote;
Expand Down Expand Up @@ -171,20 +172,40 @@ public function findVotedForElection(Election $election): array
;
}

public function isInVoterListForCommitteeElection(Adherent $adherent): bool
{
return 0 < (int) $this->createQueryBuilder('voter')
->select('COUNT(1)')
->innerJoin('voter.votersLists', 'list')
->innerJoin('list.election', 'election')
->innerJoin('election.designation', 'designation', Join::WITH, 'designation.type = :designation_type')
->innerJoin('election.electionEntity', 'election_entity')
->innerJoin('election_entity.committee', 'committee', Join::WITH, 'committee.version = 2')
->where('voter.adherent = :adherent')
->setParameters([
'adherent' => $adherent,
'designation_type' => DesignationTypeEnum::COMMITTEE_SUPERVISOR,
])
public function isInVoterListForCommitteeElection(
Adherent $adherent,
?Committee $committee = null,
?\DateTimeInterface $after = null,
): bool {
$qb = $this->createQueryBuilder('voter')
->select('COUNT(1)')
->innerJoin('voter.votersLists', 'list')
->innerJoin('list.election', 'election')
->innerJoin('election.designation', 'designation', Join::WITH, 'designation.type = :designation_type')
->innerJoin('election.electionEntity', 'election_entity')
->innerJoin('election_entity.committee', 'committee', Join::WITH, 'committee.version = 2')
->where('voter.adherent = :adherent')
->setParameters([
'adherent' => $adherent,
'designation_type' => DesignationTypeEnum::COMMITTEE_SUPERVISOR,
])
;

if ($committee) {
$qb
->andWhere('committee = :committee')
->setParameter('committee', $committee)
;
}

if ($after) {
$qb
->andWhere('designation.voteEndDate >= :after')
->setParameter('after', $after)
;
}

return 0 < (int) $qb
->getQuery()
->getSingleScalarResult()
;
Expand Down