feat: cli command to clean up outdated or stale booking draft records

This commit is contained in:
Björn Fromme
2026-03-16 12:02:27 +01:00
parent e09e5858d8
commit 1fa17a7df7
6 changed files with 142 additions and 3 deletions
+79
View File
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Repository\BookingEditDraftRepository;
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\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:draft:cleanup',
description: 'Delete booking edit drafts for departed travels',
)]
class DraftCleanupCommand extends Command
{
public function __construct(
private readonly BookingEditDraftRepository $draftRepository,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show what would be deleted without actually deleting');
}
/**
* @see \Symfony\Component\Console\Command\Command::execute()
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$dryRun = $input->getOption('dry-run');
if ($dryRun) {
$expiredDrafts = $this->draftRepository->createQueryBuilder('d')
->where('d.travelDate < :today')
->setParameter('today', new \DateTimeImmutable('today'))
->getQuery()
->getResult();
$count = \count($expiredDrafts);
if (0 === $count) {
$io->success('No expired drafts found.');
return Command::SUCCESS;
}
$io->note(sprintf('[DRY RUN] Would delete %d expired draft(s)', $count));
return Command::SUCCESS;
}
$deletedCount = $this->draftRepository->deleteExpiredDrafts();
if (0 === $deletedCount) {
$io->success('No expired drafts found.');
return Command::SUCCESS;
}
$this->logger->info('Deleted expired booking edit drafts', [
'count' => $deletedCount,
]);
$io->success(sprintf('Deleted %d expired draft(s).', $deletedCount));
return Command::SUCCESS;
}
}
+2 -1
View File
@@ -75,6 +75,7 @@ class DraftInspectCommand extends Command
$draft->getId(),
$draft->getBookingId(),
$draft->getUser()->getEmail(),
$draft->getTravelDate()->format('Y-m-d'),
$draft->getCreatedAt()->format('Y-m-d H:i:s'),
$draft->getUpdatedAt()->format('Y-m-d H:i:s'),
$this->getAgeDays($draft->getUpdatedAt()),
@@ -83,7 +84,7 @@ class DraftInspectCommand extends Command
}
$io->table(
['ID', 'Booking ID', 'User Email', 'Created At', 'Updated At', 'Days Old', 'Participants'],
['ID', 'Booking ID', 'User Email', 'Travel Date', 'Created At', 'Updated At', 'Days Old', 'Participants'],
$rows
);