Skip to content

Commit

Permalink
Merge pull request #3638 from MTES-MCT/hotfix/3635-prevent-doublon-su…
Browse files Browse the repository at this point in the history
…ivi-intervention

[BUG] doublonge des création de suivi pour arrétés au 29/01/2025
  • Loading branch information
hmeneuvrier authored Jan 29, 2025
2 parents d6a3736 + cda2091 commit 8be8a77
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
32 changes: 32 additions & 0 deletions migrations/Version20250129102407.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20250129102407 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add on delete cascade on notification suivi_id and delete suivis for arretes and visites created on 2025-01-29';
}

public function up(Schema $schema): void
{
// add on delete cascade on notification suivi_id
$this->addSql('ALTER TABLE notification DROP FOREIGN KEY FK_BF5476CA7FEA59C0');
$this->addSql('ALTER TABLE notification ADD CONSTRAINT FK_BF5476CA7FEA59C0 FOREIGN KEY (suivi_id) REFERENCES suivi (id) ON DELETE CASCADE');
// delete suivis for arretes and visites created on 2025-01-29
$this->addSql("DELETE FROM suivi WHERE description LIKE '%pris dans le dossier de%' AND context = 'intervention' AND created_at LIKE '2025-01-29%'");
$this->addSql("DELETE FROM suivi WHERE description LIKE 'Visite réalisée : une visite du logement situé%' AND context = 'intervention' AND created_at LIKE '2025-01-29%'");
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE notification DROP FOREIGN KEY FK_BF5476CA7FEA59C0');
$this->addSql('ALTER TABLE notification ADD CONSTRAINT FK_BF5476CA7FEA59C0 FOREIGN KEY (suivi_id) REFERENCES suivi (id)');
}
}
6 changes: 5 additions & 1 deletion src/Controller/SignalementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -425,6 +427,8 @@ public function suiviProcedure(
SuiviManager $suiviManager,
EntityManagerInterface $entityManager,
SignalementDesordresProcessor $signalementDesordresProcessor,
#[Autowire(service: 'html_sanitizer.sanitizer.app.message_sanitizer')]
HtmlSanitizerInterface $htmlSanitizer,
): RedirectResponse|Response {
$signalement = $signalementRepository->findOneByCodeForPublic($code);
if (!$signalement) {
Expand Down Expand Up @@ -475,7 +479,7 @@ public function suiviProcedure(
if (Suivi::POURSUIVRE_PROCEDURE === $suiviAuto) {
$description = $user->getNomComplet().' ('.$type.') a indiqué vouloir poursuivre la procédure.';
$suiviPoursuivreProcedure = $suiviManager->findOneBy([
'description' => $description,
'description' => $htmlSanitizer->sanitize($description),
'signalement' => $signalement,
]);
if (null !== $suiviPoursuivreProcedure) {
Expand Down
1 change: 1 addition & 0 deletions src/Entity/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Notification
private ?Signalement $signalement;

#[ORM\ManyToOne(targetEntity: Suivi::class)]
#[ORM\JoinColumn(onDelete: 'CASCADE')]
private ?Suivi $suivi;

#[ORM\Column(type: 'datetime_immutable')]
Expand Down
6 changes: 5 additions & 1 deletion src/EventSubscriber/InterventionCreatedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
use App\Service\Intervention\InterventionDescriptionGenerator;
use App\Service\Mailer\NotificationMailerType;
use App\Service\Signalement\VisiteNotifier;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;

class InterventionCreatedSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly VisiteNotifier $visiteNotifier,
private readonly SuiviManager $suiviManager,
#[Autowire(service: 'html_sanitizer.sanitizer.app.message_sanitizer')]
private readonly HtmlSanitizerInterface $htmlSanitizer,
) {
}

Expand Down Expand Up @@ -43,7 +47,7 @@ private function createSuiviAndNotify(InterventionCreatedEvent $event): void
$description = (string) InterventionDescriptionGenerator::generate($intervention, InterventionCreatedEvent::NAME);
$foundSuivi = $this->suiviManager->findOneBy([
'signalement' => $intervention->getSignalement(),
'description' => $description,
'description' => $this->htmlSanitizer->sanitize($description),
]);

if (!$foundSuivi) {
Expand Down
6 changes: 5 additions & 1 deletion src/Service/Import/Signalement/SignalementImportLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;

class SignalementImportLoader
{
Expand Down Expand Up @@ -66,6 +68,8 @@ public function __construct(
private SignalementQualificationUpdater $signalementQualificationUpdater,
private FileManager $fileManager,
private FilesystemOperator $fileStorage,
#[Autowire(service: 'html_sanitizer.sanitizer.app.message_sanitizer')]
private HtmlSanitizerInterface $htmlSanitizer,
) {
}

Expand Down Expand Up @@ -259,7 +263,7 @@ private function loadSuivi(Signalement $signalement, array $dataMapped): ArrayCo
$description = trim(preg_replace(self::REGEX_DATE_FORMAT_CSV, '', $suivi));

$suivi = $this->suiviManager->findOneBy([
'description' => $description,
'description' => $this->htmlSanitizer->sanitize($description),
'createdBy' => $this->userSystem,
'signalement' => $signalement,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testBuildVisiteDescription(): void
$eventDispatcher = new EventDispatcher();
$visiteNotifier = static::getContainer()->get(VisiteNotifier::class);
$suiviManager = static::getContainer()->get(SuiviManager::class);
$htmlSanitizer = self::getContainer()->get('html_sanitizer.sanitizer.app.message_sanitizer');

/** @var InterventionRepository $interventionRepository */
$interventionRepository = $this->entityManager->getRepository(Intervention::class);
Expand All @@ -49,7 +50,7 @@ public function testBuildVisiteDescription(): void
/** @var UserRepository $userRepository */
$userRepository = $this->entityManager->getRepository(User::class);
$user = $userRepository->findOneBy(['email' => '[email protected]']);
$interventionCreatedSubscriber = new InterventionCreatedSubscriber($visiteNotifier, $suiviManager);
$interventionCreatedSubscriber = new InterventionCreatedSubscriber($visiteNotifier, $suiviManager, $htmlSanitizer);
$eventDispatcher->addSubscriber($interventionCreatedSubscriber);

$intervention = $interventions[0];
Expand Down Expand Up @@ -100,6 +101,7 @@ private function testNbMailSent(\DateTimeImmutable $date, $type): void
$eventDispatcher = new EventDispatcher();
$visiteNotifier = static::getContainer()->get(VisiteNotifier::class);
$suiviManager = static::getContainer()->get(SuiviManager::class);
$htmlSanitizer = self::getContainer()->get('html_sanitizer.sanitizer.app.message_sanitizer');

/** @var InterventionRepository $interventionRepository */
$interventionRepository = $this->entityManager->getRepository(Intervention::class);
Expand All @@ -111,7 +113,7 @@ private function testNbMailSent(\DateTimeImmutable $date, $type): void
/** @var UserRepository $userRepository */
$userRepository = $this->entityManager->getRepository(User::class);
$user = $userRepository->findOneBy(['email' => '[email protected]']);
$interventionCreatedSubscriber = new InterventionCreatedSubscriber($visiteNotifier, $suiviManager);
$interventionCreatedSubscriber = new InterventionCreatedSubscriber($visiteNotifier, $suiviManager, $htmlSanitizer);
$eventDispatcher->addSubscriber($interventionCreatedSubscriber);

$intervention->setScheduledAt($date);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;

class SignalementImportLoaderTest extends KernelTestCase
{
Expand All @@ -37,6 +38,7 @@ class SignalementImportLoaderTest extends KernelTestCase
private SignalementQualificationUpdater $signalementQualificationUpdater;
private FileManager $fileManager;
private MockObject|FilesystemOperator $filesystemOperator;
private HtmlSanitizerInterface $htmlSanitizerInterface;

protected function setUp(): void
{
Expand All @@ -53,6 +55,7 @@ protected function setUp(): void
$this->fileManager = self::getContainer()->get(FileManager::class);
$this->filesystemOperator = $this->createMock(FilesystemOperator::class);
$this->entityManager = $kernel->getContainer()->get('doctrine')->getManager();
$this->htmlSanitizerInterface = self::getContainer()->get('html_sanitizer.sanitizer.app.message_sanitizer');
}

/**
Expand All @@ -74,6 +77,7 @@ public function testLoadSignalementImport()
$this->signalementQualificationUpdater,
$this->fileManager,
$this->filesystemOperator,
$this->htmlSanitizerInterface
);

$territory = $this->entityManager->getRepository(Territory::class)->findOneBy(['zip' => '01']);
Expand Down

0 comments on commit 8be8a77

Please sign in to comment.