feat: email reminder about pending feedbacks for hotel managers
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Cron;
|
||||
|
||||
use App\BusProNet\DataProvider\HotelDataProvider;
|
||||
use App\Email\Mailer;
|
||||
use App\Entity\Feedback;
|
||||
use App\Entity\User;
|
||||
use App\Repository\DispositionRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class FeedbackReminderService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DispositionRepository $dispositionRepository,
|
||||
private readonly HotelDataProvider $hotelDataProvider,
|
||||
private readonly UserRepository $userRepository,
|
||||
private readonly Mailer $mailer,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
public function sendFeedbackReminders(): string
|
||||
{
|
||||
/** @var Feedback[] $pendingFeedbacks */
|
||||
$pendingFeedbacks = $this->dispositionRepository->getPendingFeedbacks();
|
||||
|
||||
$sortedFeedbacks = [];
|
||||
|
||||
foreach ($pendingFeedbacks as $feedback) {
|
||||
// Get hotel by bus pro id
|
||||
$hotelBusProId = $feedback->getDestination()->getHotelBusProId();
|
||||
$hotel = $this->hotelDataProvider->get($hotelBusProId);
|
||||
|
||||
if (null === $hotel) {
|
||||
$this->logger->error('Could not find hotel by bus pro id '.$hotelBusProId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get hotel base code to find hotel manager later
|
||||
$hotelBaseCode = substr($hotel->getCode(), 0, 3);
|
||||
|
||||
if (false === array_key_exists($hotelBaseCode, $sortedFeedbacks)) {
|
||||
$sortedFeedbacks[$hotelBaseCode] = [];
|
||||
}
|
||||
|
||||
// Sort feedback by hotel base code
|
||||
$sortedFeedbacks[$hotelBaseCode][] = $feedback;
|
||||
}
|
||||
|
||||
// Determine hotel managers to notify
|
||||
$hotelBaseCodes = array_keys($sortedFeedbacks);
|
||||
|
||||
/** @var User[] $hotelManagers */
|
||||
$hotelManagers = $this->userRepository->findBy([
|
||||
'hotelCode' => $hotelBaseCodes,
|
||||
]);
|
||||
|
||||
foreach ($hotelManagers as $manager) {
|
||||
$this->mailer->createAndSendEmail([
|
||||
'feedbacks' => $sortedFeedbacks[$manager->getHotelCode()],
|
||||
], [
|
||||
'to' => $manager->getEmail(),
|
||||
'subject' => 'Reminder: Offene Feedbacks',
|
||||
'template' => 'email/reminder_pending_feedback.html.twig',
|
||||
]);
|
||||
}
|
||||
|
||||
return 'Sent feedback reminders to '.count($hotelManagers).' hotel managers';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Cron;
|
||||
|
||||
use Doctrine\DBAL\Exception;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class FlushLogsService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
public function flush(): array
|
||||
{
|
||||
// Deletes all log entries older than 30 days
|
||||
$dueDate = (new \DateTimeImmutable())->modify('-30 days');
|
||||
|
||||
try {
|
||||
$result = $this
|
||||
->entityManager
|
||||
->getConnection()
|
||||
->executeQuery('DELETE FROM log WHERE log.timestamp < ?', [$dueDate->format('Y-m-d H:i:s')]);
|
||||
$message = 'Deleted '.$result->rowCount().' log entries';
|
||||
$success = true;
|
||||
$this->logger->info($message);
|
||||
} catch (Exception $e) {
|
||||
$message = 'Unable to delete log entries: '.$e->getMessage();
|
||||
$success = false;
|
||||
$this->logger->error($message);
|
||||
}
|
||||
|
||||
return [$success, $message];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Cron;
|
||||
|
||||
use App\Email\Mailer;
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\Upload;
|
||||
use App\Repository\DispositionRepository;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class UploadReminderService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DispositionRepository $dispositionRepository,
|
||||
private readonly Mailer $mailer,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
public function sendContractUploadReminders(): string
|
||||
{
|
||||
// Contracts have to be uploaded before assignment's begin date. Reminder is
|
||||
// sent three days before end of this period thus due date is today in three days.
|
||||
$contractDueDate = (new \DateTimeImmutable())->modify('+3 days');
|
||||
|
||||
// Find dispositions of assignments with due contract upload
|
||||
$qb = $this->dispositionRepository->createQueryBuilder('disposition');
|
||||
|
||||
$dispositions = $qb
|
||||
->select('disposition', 'assignment', 'destination')
|
||||
->innerJoin('disposition.assignment', 'assignment')
|
||||
->innerJoin('assignment.destination', 'destination')
|
||||
->leftJoin('disposition.documents', 'document', Join::WITH, 'document.type = :documentType')
|
||||
->where($qb->expr()->andX(
|
||||
$qb->expr()->isNull('document'),
|
||||
$qb->expr()->orX(
|
||||
// Due date can be determined by assignment's date which potentially overrides destination date
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->isNotNull('assignment.dateFrom'),
|
||||
$qb->expr()->eq('assignment.dateFrom', ':dateFrom')
|
||||
),
|
||||
$qb->expr()->eq('destination.dateFrom', ':dateFrom'),
|
||||
)
|
||||
))
|
||||
->setParameters([
|
||||
'documentType' => Upload::TYPE_CONTRACT,
|
||||
'dateFrom' => $contractDueDate,
|
||||
])
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
|
||||
if (0 === $count = count($dispositions)) {
|
||||
$message = 'No reminders regarding due contract uploads to be sent';
|
||||
$this->logger->info($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
foreach ($dispositions as $disposition) {
|
||||
$teamer = $disposition->getTeamer();
|
||||
|
||||
$this->mailer->createAndSendEmail([
|
||||
'disposition' => $disposition,
|
||||
], [
|
||||
'to' => $teamer->getCommunication()->getEmail(),
|
||||
'subject' => 'Reminder: Fehlende Dokumente',
|
||||
'template' => 'email/reminder_contract_upload.html.twig',
|
||||
]);
|
||||
}
|
||||
|
||||
$message = 'Sent '.$count.' reminders about due contract uploads';
|
||||
$this->logger->info($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function sendInvoiceUploadReminders(): string
|
||||
{
|
||||
// Invoices have to be uploaded until 14 days after end of assignment. Reminder
|
||||
// is sent three days before end of this period thus on the 11th day after end of assignment.
|
||||
$invoiceDueDate = (new \DateTimeImmutable())->modify('-11 days');
|
||||
|
||||
// Find dispositions of assignments with due invoice upload
|
||||
$qb = $this->dispositionRepository->createQueryBuilder('disposition');
|
||||
|
||||
$dispositions = $qb
|
||||
->select('disposition', 'assignment', 'destination')
|
||||
->innerJoin('disposition.assignment', 'assignment')
|
||||
->innerJoin('assignment.destination', 'destination')
|
||||
->leftJoin('disposition.documents', 'document', Join::WITH, 'document.type = :documentType')
|
||||
->where($qb->expr()->andX(
|
||||
$qb->expr()->isNull('document'),
|
||||
$qb->expr()->orX(
|
||||
// Due date can be determined by assignment's date which potentially overrides destination date
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->isNotNull('assignment.dateTo'),
|
||||
$qb->expr()->eq('assignment.dateTo', ':dateTo')
|
||||
),
|
||||
$qb->expr()->eq('destination.dateTo', ':dateTo'),
|
||||
)
|
||||
))
|
||||
->setParameters([
|
||||
'documentType' => Upload::TYPE_INVOICE,
|
||||
'dateTo' => $invoiceDueDate,
|
||||
])
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
|
||||
if (0 === $count = count($dispositions)) {
|
||||
$message = 'No reminders regarding due invoice uploads to be sent';
|
||||
$this->logger->info($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
foreach ($dispositions as $disposition) {
|
||||
$teamer = $disposition->getTeamer();
|
||||
|
||||
$this->mailer->createAndSendEmail([
|
||||
'disposition' => $disposition,
|
||||
], [
|
||||
'to' => $teamer->getCommunication()->getEmail(),
|
||||
'subject' => 'Reminder: Fehlende Dokumente',
|
||||
'template' => 'email/reminder_invoice_upload.html.twig',
|
||||
]);
|
||||
}
|
||||
|
||||
$message = 'Sent '.$count.' reminders about due invoice uploads';
|
||||
$this->logger->info($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user