diff --git a/config/email_texts.yaml b/config/email_texts.yaml index 40c5db0..028dbed 100644 --- a/config/email_texts.yaml +++ b/config/email_texts.yaml @@ -190,8 +190,8 @@ texts: Im E&P-Team Portal findest du unter "Saisonunterlagen" die Vorlagen zum Herunterladen und kannst deine unterschriebenen Unterlagen direkt hochladen. - # Daily, from the day after the trip ends, for as long as a questionnaire of the placement is - # not submitted. One mail per teamer, listing every such placement. + # Nightly for the first week after the trip, then weekly, for as long as a questionnaire of the + # placement is not submitted. One mail per teamer, listing every such placement. reminder_questionnaire: label: 'Erinnerung: Feedbackbogen ausfüllen' placeholders: ['destinations'] diff --git a/src/Repository/DispositionRepository.php b/src/Repository/DispositionRepository.php index f62ca06..5637332 100644 --- a/src/Repository/DispositionRepository.php +++ b/src/Repository/DispositionRepository.php @@ -111,8 +111,8 @@ class DispositionRepository extends ServiceEntityRepository } /** - * Every teamer's placements with a questionnaire still to be submitted, for the daily reminder - * (QuestionnaireReminderService): the dashboard's rules, but only from the day after the trip + * Every teamer's placements with a questionnaire still to be submitted, for the reminder + * (QuestionnaireReminderService, which picks the nights each is due on): the dashboard's rules, but only from the day after the trip * ended, and only teamers who can still log in to submit - not deleted, with a user account that * is not blocked. Otherwise they would be reminded every day, forever. * diff --git a/src/Service/Cron/QuestionnaireReminderService.php b/src/Service/Cron/QuestionnaireReminderService.php index 137be77..8f928ea 100644 --- a/src/Service/Cron/QuestionnaireReminderService.php +++ b/src/Service/Cron/QuestionnaireReminderService.php @@ -10,15 +10,19 @@ use App\Repository\DispositionRepository; use Psr\Log\LoggerInterface; /** - * Reminds teamers of their unsubmitted questionnaires, every day from the day after the trip ended - * until they are submitted - a saved draft does not count. One mail per teamer, listing every such - * placement. + * Reminds teamers of their unsubmitted questionnaires until they are submitted - a saved draft does + * not count: every night for the first week after the trip, then once a week, so a teamer who never + * submits is not mailed every night for the rest of the season. One mail per teamer on a night when + * any of their placements is due, listing every placement they still owe. * - * Like the other reminders it records nothing: the cron runs once a day, and that is what makes it - * daily. + * Like the other reminders it records nothing: whether a placement is due is a function of the day, + * and the cron runs once a day. */ class QuestionnaireReminderService { + /** nights after the trip with a reminder each, before it turns weekly */ + private const int DAILY_NIGHTS = 7; + private readonly \DateTimeImmutable $questionnaireGoLive; /** @@ -47,9 +51,10 @@ class QuestionnaireReminderService $count = 0; foreach ($perTeamer as $dispositions) { + $due = array_filter($dispositions, fn (Disposition $disposition): bool => $this->isDue($disposition, $today)); $email = $dispositions[0]->getTeamer()->getCommunication()?->getEmail(); - if (null === $email) { + if ([] === $due || null === $email) { continue; } @@ -70,4 +75,15 @@ class QuestionnaireReminderService return $message; } + + /** + * Nights 1 to 7 after the trip, then nights 14, 21, ... The repository only returns + * placements that ended yesterday or earlier. + */ + private function isDue(Disposition $disposition, \DateTimeImmutable $today): bool + { + $nights = (int) $disposition->getAssignment()->getDestination()->getDateTo()->setTime(0, 0)->diff($today)->days; + + return $nights <= self::DAILY_NIGHTS || 0 === $nights % 7; + } } diff --git a/tests/Service/Cron/QuestionnaireReminderServiceTest.php b/tests/Service/Cron/QuestionnaireReminderServiceTest.php index 7788305..72a4653 100644 --- a/tests/Service/Cron/QuestionnaireReminderServiceTest.php +++ b/tests/Service/Cron/QuestionnaireReminderServiceTest.php @@ -73,11 +73,44 @@ class QuestionnaireReminderServiceTest extends TestCase $this->assertSame('Sent 2 questionnaire reminders to teamers', $this->service->sendReminders(new \DateTimeImmutable('2026-11-20'))); $this->assertSame(['kim@example.org', 'alex@example.org'], array_keys($sent)); - // labelled as everywhere in the reminders, by Destination::__toString() + // the placement on its 13th night is not due by itself, but comes along with the one that + // is; labelled as everywhere in the reminders, by Destination::__toString() $this->assertSame('07.11.26 - 14.11.26 Skireise, 31.10.26 - 07.11.26 Skireise', $sent['kim@example.org']); $this->assertSame('07.11.26 - 14.11.26 Skireise', $sent['alex@example.org']); } + /** + * Nightly for a week, then weekly: a teamer who never submits is not mailed every night for + * the rest of the season. + */ + public function testItRemindsNightlyForAWeekThenWeekly(): void + { + $this->repository->method('findQuestionnaireReminders')->willReturn([$this->placement($this->teamer('kim@example.org'), '2026-11-14')]); + + $nights = []; + + for ($night = 1; $night <= 30; ++$night) { + $today = new \DateTimeImmutable('2026-11-14')->modify(sprintf('+%d days', $night)); + + if ('Sent 1 questionnaire reminders to teamers' === $this->service->sendReminders($today)) { + $nights[] = $night; + } + } + + $this->assertSame([1, 2, 3, 4, 5, 6, 7, 14, 21, 28], $nights); + } + + /** + * On its 13th night a placement waits for the weekly 14th. + */ + public function testAPlacementBetweenItsWeeklyNightsAloneSendsNothing(): void + { + $kim = $this->teamer('kim@example.org'); + $this->repository->method('findQuestionnaireReminders')->willReturn([$this->placement($kim, '2026-11-07')]); + + $this->assertSame('Sent 0 questionnaire reminders to teamers', $this->service->sendReminders(new \DateTimeImmutable('2026-11-20'))); + } + public function testATeamerWithoutAnAddressIsSkipped(): void { $this->repository->method('findQuestionnaireReminders')->willReturn([$this->placement(new Teamer(), '2026-11-14')]);