Feat: Implement email reminders in cli command to be executed by cron
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Email\Mailer;
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\Upload;
|
||||
use Doctrine\DBAL\Exception;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:cron',
|
||||
description: 'Executes several tasks triggered on a daily basis',
|
||||
)]
|
||||
class CronCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly Mailer $mailer,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$this->flushLogs($io);
|
||||
$this->sendContractUploadReminders($io);
|
||||
$this->sendInvoiceUploadReminders($io);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function flushLogs(SymfonyStyle $io): void
|
||||
{
|
||||
// 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';
|
||||
$io->info($message);
|
||||
$this->logger->info($message);
|
||||
} catch (Exception $e) {
|
||||
$message = 'Unable to delete log entries: '.$e->getMessage();
|
||||
$io->error($message);
|
||||
$this->logger->error($message);
|
||||
}
|
||||
}
|
||||
|
||||
private function sendContractUploadReminders(SymfonyStyle $io): void
|
||||
{
|
||||
// Find dispositions of assignments with due contract upload
|
||||
$repository = $this->entityManager->getRepository(Disposition::class);
|
||||
|
||||
// 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');
|
||||
|
||||
$qb = $repository->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';
|
||||
$io->info($message);
|
||||
$this->logger->info($message);
|
||||
return;
|
||||
}
|
||||
|
||||
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';
|
||||
$io->info($message);
|
||||
$this->logger->info($message);
|
||||
}
|
||||
|
||||
private function sendInvoiceUploadReminders(SymfonyStyle $io): void
|
||||
{
|
||||
// Find dispositions of assignments with due invoice upload
|
||||
$repository = $this->entityManager->getRepository(Disposition::class);
|
||||
|
||||
// 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');
|
||||
|
||||
$qb = $repository->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';
|
||||
$io->info($message);
|
||||
$this->logger->info($message);
|
||||
return;
|
||||
}
|
||||
|
||||
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';
|
||||
$io->info($message);
|
||||
$this->logger->info($message);
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Exception;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:flush-logs',
|
||||
description: 'Deletes historic log entries',
|
||||
)]
|
||||
class FlushLogsCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Connection $connection,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$dueDate = (new \DateTimeImmutable())->modify('-30 days');
|
||||
try {
|
||||
$result = $this
|
||||
->connection
|
||||
->executeQuery('DELETE FROM log WHERE log.timestamp < ?', [$dueDate->format('Y-m-d H:i:s')]);
|
||||
$io->info('Deleted '.$result->rowCount().' log entries');
|
||||
} catch (Exception $e) {
|
||||
$io->error('Unable to delete log entries: '.$e->getMessage());
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
{% extends 'email/layout.html.twig' %}
|
||||
|
||||
{% block body %}
|
||||
<h1>
|
||||
Reminder zu fehlenden Dokumenten
|
||||
</h1>
|
||||
<p>
|
||||
<strong>
|
||||
Honorarvertrag zu deinem Einsatz <a href="{{ url('app_teamer_disposition_detail', { 'uuid': disposition.uuid }) }}">{{ disposition.assignment.destination}}</a>.
|
||||
</strong>
|
||||
</p>
|
||||
<p>
|
||||
Dein Honorarvertrag wurde noch nicht ausgefüllt. Du hast noch 3 Tage Zeit diesen ausgefüllt hochzuladen. Hältst
|
||||
du diese Frist nicht ein, verfällt das Einsatzangebot und deine Teamkolleg:innen erhalten die Chance, diesen
|
||||
Einsatz zu besetzen.
|
||||
</p>
|
||||
<p>
|
||||
Setz dich am besten direkt dran, damit du es nicht vergisst! 😊
|
||||
</p>
|
||||
<p>
|
||||
<a href="{{ url('app_teamer_index') }}" class="button">
|
||||
Zum Portal
|
||||
</a>
|
||||
</p>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,24 @@
|
||||
{% extends 'email/layout.html.twig' %}
|
||||
|
||||
{% block body %}
|
||||
<h1>
|
||||
Reminder zu fehlenden Dokumenten
|
||||
</h1>
|
||||
<p>
|
||||
<strong>
|
||||
Honorarnote zu deinem Einsatz <a href="{{ url('app_teamer_disposition_detail', { 'uuid': disposition.uuid }) }}">{{ disposition.assignment.destination}}</a>.
|
||||
</strong>
|
||||
</p>
|
||||
<p>
|
||||
Deine Honorarnote wurde noch nicht ausgefüllt. Du hast noch 3 Tage Zeit diese ausgefüllt hochzuladen. Hältst du
|
||||
diese Frist nicht ein, verfällt die Honorarnote und du erhältst kein Geld mehr!
|
||||
</p>
|
||||
<p>
|
||||
Setz dich am besten direkt dran, damit du es nicht vergisst! 😊
|
||||
</p>
|
||||
<p>
|
||||
<a href="{{ url('app_teamer_index') }}" class="button">
|
||||
Zum Portal
|
||||
</a>
|
||||
</p>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user