fix: remind house managers of their own pending list at the teamer reminder pace
This commit is contained in:
1 parent
ab6ffd4eda
commit
77d7000df3
11 files changed
+515
-73
No files matched your search
@@ -8,6 +8,7 @@ use App\Entity\Destination;
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\QuestionnaireResponse;
|
||||
use App\Entity\Teamer;
|
||||
use App\Entity\User;
|
||||
use App\Enum\QuestionnaireResponseStatus;
|
||||
use App\Model\DestinationFilterDto;
|
||||
use App\Repository\Traits\QueryHelperTrait;
|
||||
@@ -169,6 +170,16 @@ class DestinationRepository extends ServiceEntityRepository
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* getPendingForHouseManagerQuery() for one house manager.
|
||||
*
|
||||
* @return list<array{0: Destination, questionnaireStatus: ?string, placements: int, openFeedbacks: int}>
|
||||
*/
|
||||
public function findPendingForHouseManager(User $houseManager, \DateTimeImmutable $goLive, \DateTimeImmutable $endedBy): array
|
||||
{
|
||||
return $this->getPendingForHouseManagerQuery($houseManager->getHotelCodes(), $goLive, $endedBy, $houseManager->getTeamer())->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* The ended travels a house manager still owes something for, one row per travel: the
|
||||
* questionnaire is not submitted (only for travels ended on or after $goLive), or a placement
|
||||
|
||||
@@ -121,6 +121,28 @@ class UserRepository extends ServiceEntityRepository
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* The house managers the nightly reminder may write to: not deleted and not blocked - a blocked
|
||||
* account cannot log in to do what it is reminded of. Hotel codes and muted notifications are
|
||||
* the caller's to check.
|
||||
*
|
||||
* @return User[]
|
||||
*/
|
||||
public function findHouseManagersToRemind(): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('user');
|
||||
|
||||
return $qb
|
||||
->where('JSON_CONTAINS(user.roles, :role) = 1')
|
||||
->andWhere($qb->expr()->isNull('user.deletedAt'))
|
||||
->andWhere($qb->expr()->isNull('user.disabledAt'))
|
||||
->setParameter('role', json_encode('ROLE_HOUSE_MANAGER'))
|
||||
->orderBy('user.id')
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
}
|
||||
|
||||
public function getAutocompletionData(string $search, string $role): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('user');
|
||||
|
||||
@@ -2,18 +2,25 @@
|
||||
|
||||
namespace App\Service\Cron;
|
||||
|
||||
use App\BusProNet\DataProvider\HotelDataProvider;
|
||||
use App\Config\QuestionnaireCatalog;
|
||||
use App\Email\Mailer;
|
||||
use App\Entity\Destination;
|
||||
use App\Enum\QuestionnaireResponseStatus;
|
||||
use App\Repository\DestinationRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Reminds house managers, daily, of the travels they still owe something for: the questionnaire
|
||||
* about the travel or a teamer's rating - the house manager's pending list
|
||||
* (DestinationRepository::getPendingForHouseManagerQuery()).
|
||||
* Reminds house managers of the travels they still owe something for: the questionnaire about the
|
||||
* travel or a teamer's rating.
|
||||
*
|
||||
* Each house manager gets exactly their dashboard's pending list
|
||||
* (DestinationRepository::getPendingForHouseManagerQuery() with their own hotel codes and teamer),
|
||||
* so they are never reminded of a rating they cannot give - their own placement on a travel - and
|
||||
* the mail never disagrees with what they find after logging in.
|
||||
*
|
||||
* At the pace of ReminderCadence, like the teamer questionnaire reminder: a house manager is mailed
|
||||
* on a night when any of their pending travels is due, and the mail lists all of them.
|
||||
*/
|
||||
class FeedbackReminderService
|
||||
{
|
||||
@@ -21,7 +28,6 @@ class FeedbackReminderService
|
||||
|
||||
public function __construct(
|
||||
private readonly DestinationRepository $destinationRepository,
|
||||
private readonly HotelDataProvider $hotelDataProvider,
|
||||
private readonly UserRepository $userRepository,
|
||||
private readonly Mailer $mailer,
|
||||
private readonly LoggerInterface $logger,
|
||||
@@ -30,74 +36,65 @@ class FeedbackReminderService
|
||||
$this->goLive = QuestionnaireCatalog::parseGoLive($houseManagerQuestionnaireGoLive);
|
||||
}
|
||||
|
||||
public function sendFeedbackReminders(): string
|
||||
public function sendFeedbackReminders(?\DateTimeImmutable $today = null): string
|
||||
{
|
||||
/** @var list<Destination> $pendingTravels */
|
||||
$pendingTravels = array_column(
|
||||
$this->destinationRepository->getPendingForHouseManagerQuery(null, $this->goLive, new \DateTimeImmutable('today'))->getResult(),
|
||||
0,
|
||||
);
|
||||
$today = ($today ?? new \DateTimeImmutable('today'))->setTime(0, 0);
|
||||
$count = 0;
|
||||
|
||||
$sortedFeedbacks = [];
|
||||
|
||||
foreach ($pendingTravels as $destination) {
|
||||
// Get hotel by bus pro id
|
||||
$hotelBusProId = $destination->getHotelBusProId();
|
||||
$hotel = $this->hotelDataProvider->get($hotelBusProId);
|
||||
|
||||
if (null === $hotel) {
|
||||
$this->logger->error('Could not find hotel by bus pro id '.$hotelBusProId);
|
||||
foreach ($this->userRepository->findHouseManagersToRemind() as $houseManager) {
|
||||
if (true === $houseManager->isMuteNotifications() || [] === $houseManager->getHotelCodes()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get hotel code to find hotel manager later
|
||||
$hotelCode = $hotel->getCode();
|
||||
// ended yesterday or earlier, like the teamer reminder: the day a travel ends is not yet
|
||||
// the time to ask about it
|
||||
$pending = $this->destinationRepository->findPendingForHouseManager($houseManager, $this->goLive, $today->modify('-1 day'));
|
||||
$due = array_filter($pending, fn (array $row): bool => ReminderCadence::isDue($row[0]->getDateTo(), $today));
|
||||
|
||||
if (false === array_key_exists($hotelCode, $sortedFeedbacks)) {
|
||||
$sortedFeedbacks[$hotelCode] = [];
|
||||
}
|
||||
|
||||
// Sort feedback by hotel base code
|
||||
$sortedFeedbacks[$hotelCode][] = $destination;
|
||||
}
|
||||
|
||||
// find hotel managers by hotel codes starting or ending with base code
|
||||
$hotelCodes = array_keys($sortedFeedbacks);
|
||||
$hotelBaseCodes = [];
|
||||
foreach ($hotelCodes as $hotelCode) {
|
||||
$hotelBaseCodes[] = substr($hotelCode, 0, 3);
|
||||
$hotelBaseCodes[] = substr($hotelCode, -3, 3);
|
||||
}
|
||||
|
||||
$hotelManagers = $this
|
||||
->userRepository
|
||||
->getUsersByRoleAndHotelCode('ROLE_HOUSE_MANAGER', $hotelBaseCodes)
|
||||
;
|
||||
|
||||
if (0 === count($hotelManagers)) {
|
||||
return 'No feedback reminders to be sent to hotel managers';
|
||||
}
|
||||
|
||||
foreach ($hotelManagers as $manager) {
|
||||
if (true === $manager->isMuteNotifications()) {
|
||||
if ([] === $due) {
|
||||
continue;
|
||||
}
|
||||
$feedbackKeys = array_filter(array_keys($sortedFeedbacks), function (string $key) use ($manager) {
|
||||
return $manager->hasHotelCodeMatch($key);
|
||||
});
|
||||
$feedbacksForManager = array_filter($sortedFeedbacks, function ($key) use ($feedbackKeys) {
|
||||
return in_array($key, $feedbackKeys);
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
|
||||
$this->mailer->createAndSendEmail([
|
||||
'feedbacks' => $feedbacksForManager,
|
||||
'travels' => array_map($this->describe(...), $pending),
|
||||
], [
|
||||
'to' => $manager->getEmail(),
|
||||
'to' => $houseManager->getEmail(),
|
||||
'subject' => 'Reminder: Offene Feedbacks und Fragebögen',
|
||||
'template' => 'email/reminder_pending_feedback.html.twig',
|
||||
'transport' => Mailer::MAILJET_TRANSPORT,
|
||||
]);
|
||||
|
||||
++$count;
|
||||
}
|
||||
|
||||
return 'Sent feedback reminders to '.count($hotelManagers).' hotel managers';
|
||||
$message = 'Sent feedback reminders to '.$count.' house managers';
|
||||
$this->logger->info($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* What the mail says about one pending travel: whether the questionnaire is still owed - only
|
||||
* for travels ended since it went live, as on the pending page - and how many teamers are left
|
||||
* to rate.
|
||||
*
|
||||
* @param array{0: Destination, questionnaireStatus: ?string, openFeedbacks: int} $row
|
||||
*
|
||||
* @return array{destination: Destination, questionnaire: ?string, openRatings: int}
|
||||
*/
|
||||
private function describe(array $row): array
|
||||
{
|
||||
$destination = $row[0];
|
||||
$questionnaire = null;
|
||||
|
||||
if ($destination->getDateTo() >= $this->goLive && QuestionnaireResponseStatus::SUBMITTED->value !== $row['questionnaireStatus']) {
|
||||
$questionnaire = QuestionnaireResponseStatus::DRAFT->value === $row['questionnaireStatus'] ? 'draft' : 'open';
|
||||
}
|
||||
|
||||
return [
|
||||
'destination' => $destination,
|
||||
'questionnaire' => $questionnaire,
|
||||
'openRatings' => (int) $row['openFeedbacks'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -11,18 +11,14 @@ use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* not count - at the pace of ReminderCadence. 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: 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;
|
||||
|
||||
/**
|
||||
@@ -77,14 +73,8 @@ 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;
|
||||
return ReminderCadence::isDue($disposition->getAssignment()->getDestination()->getDateTo(), $today);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Cron;
|
||||
|
||||
/**
|
||||
* When a reminder about something owed since a trip ended is due: every night for the first week
|
||||
* after the trip, then once a week, so whoever never gets round to it is not mailed every night for
|
||||
* the rest of the season.
|
||||
*
|
||||
* Shared by the teamer questionnaire reminder and the house manager reminder, so both slow down
|
||||
* alike. Like the reminders themselves it records nothing - whether a reminder is due is a function
|
||||
* of the day, and the cron runs once a day.
|
||||
*/
|
||||
final class ReminderCadence
|
||||
{
|
||||
/** nights after the trip with a reminder each, before it turns weekly */
|
||||
public const int DAILY_NIGHTS = 7;
|
||||
|
||||
/**
|
||||
* Nights 1 to 7 after the trip, then nights 14, 21, ... Night 0, the day the trip ends, is
|
||||
* never due: the callers only look at trips that ended yesterday or earlier.
|
||||
*/
|
||||
public static function isDue(\DateTimeImmutable $endedOn, \DateTimeImmutable $today): bool
|
||||
{
|
||||
$nights = (int) $endedOn->setTime(0, 0)->diff($today->setTime(0, 0))->days;
|
||||
|
||||
return $nights > 0 && ($nights <= self::DAILY_NIGHTS || 0 === $nights % 7);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user