Add command to remove old draft accompanying periods

This commit is contained in:
Julien Fastré 2022-04-29 15:51:54 +02:00
parent 1e06926f6d
commit 770010ceb8
5 changed files with 226 additions and 0 deletions

View File

@ -12,6 +12,7 @@ and this project adheres to
<!-- write down unreleased development here -->
* Load relationships without gender in french fixtures
* Add command to remove old draft accompanying periods
## Test releases

View File

@ -0,0 +1,64 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Command;
use Chill\PersonBundle\Service\AccompanyingPeriod\OldDraftAccompanyingPeriodRemoverInterface;
use DateInterval;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RemoveOldDraftAccompanyingPeriodCommand extends Command
{
private LoggerInterface $logger;
private OldDraftAccompanyingPeriodRemoverInterface $remover;
public function __construct(LoggerInterface $logger, OldDraftAccompanyingPeriodRemoverInterface $remover)
{
parent::__construct('chill:person:remove-old-draft-period');
$this->logger = $logger;
$this->remover = $remover;
}
protected function configure(): void
{
$this
->setDescription('Remove draft accompanying period which are still draft and unused')
->addArgument('interval', InputArgument::OPTIONAL, 'The interval for unactive periods', 'P15D');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->logger->info('[' . $this->getName() . '] started', [
'interval' => $input->getArgument('interval'),
]);
try {
$interval = new DateInterval($input->getArgument('interval'));
} catch (Exception $e) {
$this->logger->error('[' . $this->getName() . '] bad interval');
throw $e;
}
$this->remover->remove($interval);
$this->logger->info('[' . $this->getName() . '] end of command');
return 0;
}
}

View File

@ -0,0 +1,103 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Service\AccompanyingPeriod;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
use DateInterval;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
class OldDraftAccompanyingPeriodRemover implements OldDraftAccompanyingPeriodRemoverInterface
{
private EntityManagerInterface $em;
private LoggerInterface $logger;
public function __construct(EntityManagerInterface $em, LoggerInterface $logger)
{
$this->em = $em;
$this->logger = $logger;
}
public function remove(DateInterval $interval): void
{
$this->logger->debug('[' . __CLASS__ . '] start to remove old periods', [
'interval' => $interval->format('%d days'),
]);
$beforeDate = (new DateTimeImmutable('now'))->sub($interval);
$results = $this->em->wrapInTransaction(static function (EntityManagerInterface $em) use ($beforeDate) {
$subDQL = 'SELECT p FROM ' . AccompanyingPeriod::class . ' p WHERE p.createdAt < :beforeDate AND p.step = :draft';
$parameters = [
'beforeDate' => $beforeDate,
'draft' => AccompanyingPeriod::STEP_DRAFT,
];
$resources = $em->createQuery(
'DELETE ' . AccompanyingPeriod\Resource::class . " r WHERE r.accompanyingPeriod IN ({$subDQL})"
)
->setParameters($parameters)
->getSingleScalarResult();
$participations = $em->createQuery(
'DELETE ' . AccompanyingPeriodParticipation::class . " part WHERE part.accompanyingPeriod IN ({$subDQL})"
)
->setParameters($parameters)
->getSingleScalarResult();
$userHistory = $em->createQuery(
'DELETE ' . AccompanyingPeriod\UserHistory::class . " h WHERE h.accompanyingPeriod IN ({$subDQL})"
)
->setParameters($parameters)
->getSingleScalarResult();
$comments = $em->createQuery(
'DELETE ' . AccompanyingPeriod\Comment::class . " c WHERE c.accompanyingPeriod IN ({$subDQL})"
)
->setParameters($parameters)
->getSingleScalarResult();
$documents = $em->createQuery(
'DELETE ' . AccompanyingCourseDocument::class . " d WHERE d.course IN ({$subDQL})"
)
->setParameters($parameters)
->getSingleScalarResult();
$periods = $em
->createQuery(
'DELETE ' . AccompanyingPeriod::class . ' p WHERE p.createdAt < :beforeDate
AND p.step = :draft'
)
->setParameter(':beforeDate', $beforeDate, Types::DATETIME_IMMUTABLE)
->setParameter(':draft', AccompanyingPeriod::STEP_DRAFT)
->getSingleScalarResult();
return [
'comments' => $comments,
'documents' => $documents,
'participations' => $participations,
'periods' => $periods,
'resources' => $resources,
'userHistory' => $userHistory,
];
});
$this->logger->info('[' . __CLASS__ . '] periods removed', array_merge($results, [
'interval' => $interval->format('%d days'),
]));
}
}

View File

@ -0,0 +1,19 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Service\AccompanyingPeriod;
use DateInterval;
interface OldDraftAccompanyingPeriodRemoverInterface
{
public function remove(DateInterval $interval): void;
}

View File

@ -0,0 +1,39 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20220429133023 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE "accompanying_periods_scopes"
DROP CONSTRAINT "fk_87c4eab032a7a428",
ADD CONSTRAINT "fk_87c4eab032a7a428" FOREIGN KEY (accompanying_period_id) REFERENCES chill_person_accompanying_period(id)
');
}
public function getDescription(): string
{
return 'apply CASCADE DELETE on entity related to accompanying periods';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE "accompanying_periods_scopes"
DROP CONSTRAINT "fk_87c4eab032a7a428",
ADD CONSTRAINT "fk_87c4eab032a7a428" FOREIGN KEY (accompanying_period_id) REFERENCES chill_person_accompanying_period(id) ON DELETE CASCADE
');
}
}