Skip to content

Commit

Permalink
Internal: Fix session duplication and reinscription logic - refs BT#2…
Browse files Browse the repository at this point in the history
…2057
  • Loading branch information
christianbeeznest committed Nov 29, 2024
1 parent 9cbcca1 commit ba09140
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
67 changes: 37 additions & 30 deletions src/CoreBundle/Command/ReinscriptionCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,45 +73,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
));
}

// Check if the session is marked as the last repetition
if ($session->getLastRepetition()) {
if ($debug) {
$output->writeln('The session is marked as the last repetition. Skipping...');
}
continue;
}

// Find a valid child session
$validChildSession = $this->sessionRepository->findValidChildSession($session);
$validSession = $this->findValidSessionInHierarchy($session);

if ($validChildSession) {
$this->enrollUserInSession($user, $validChildSession);
if ($validSession) {
$this->enrollUserInSession($user, $validSession);
if ($debug) {
$output->writeln(sprintf(
'User %d re-enrolled into the valid child session %d.',
'User %d re-enrolled into session %d.',
$user->getId(),
$validChildSession->getId()
));
}
continue;
}

// If no valid child session exists, check the parent session
$validParentSession = $this->sessionRepository->findValidParentSession($session);

if ($validParentSession) {
$this->enrollUserInSession($user, $validParentSession);
if ($debug) {
$output->writeln(sprintf(
'User %d re-enrolled into the valid parent session %d.',
$user->getId(),
$validParentSession->getId()
$validSession->getId()
));
}
} else {
if ($debug) {
$output->writeln(sprintf(
'No valid child or parent session found for user %d.',
'No valid session found for user %d.',
$user->getId()
));
}
Expand Down Expand Up @@ -164,4 +140,35 @@ private function findUserSubscriptionInSession($user, $session)
'session' => $session,
]);
}

private function findValidSessionInHierarchy(Session $session): ?Session
{
$childSessions = $this->sessionRepository->findChildSessions($session);

/* @var Session $child */
foreach ($childSessions as $child) {
$validUntil = (clone $child->getAccessEndDate())->modify("-{$child->getDaysToReinscription()} days");
if (new \DateTime() <= $validUntil) {
return $child;
}

$validChild = $this->findValidSessionInHierarchy($child);
if ($validChild) {
return $validChild;
}
}

/* @var Session $parentSession */
$parentSession = $this->sessionRepository->findParentSession($session);

if ($parentSession) {
$validUntil = (clone $parentSession->getAccessEndDate())->modify("-{$parentSession->getDaysToReinscription()} days");
if (new \DateTime() <= $validUntil) {
return $parentSession;
}
}

return null;
}

}
6 changes: 3 additions & 3 deletions src/CoreBundle/Command/SessionRepetitionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function duplicateSession(Session $session, bool $debug, OutputInterface $output): Session
{
// Calculate new session dates based on the duration of the original session
$duration = $session->getAccessEndDate()->diff($session->getAccessStartDate());
$duration = $session->getAccessEndDate()->diff($session->getAccessStartDate())->days;
$newStartDate = (clone $session->getAccessEndDate())->modify('+1 day');
$newEndDate = (clone $newStartDate)->add($duration);
$newEndDate = (clone $newStartDate)->modify("+{$duration} days");

if ($debug) {
$output->writeln(sprintf(
Expand All @@ -105,7 +105,7 @@ private function duplicateSession(Session $session, bool $debug, OutputInterface
->setCoachAccessStartDate($newStartDate)
->setCoachAccessEndDate($newEndDate)
->setVisibility($session->getVisibility())
->setDuration($session->getDuration())
->setDuration($duration)
->setDescription($session->getDescription() ?? '')
->setShowDescription($session->getShowDescription() ?? false)
->setCategory($session->getCategory())
Expand Down

0 comments on commit ba09140

Please sign in to comment.