Skip to content

Commit

Permalink
Merge pull request #1578 from MTES-MCT/hotfix/1449-visite-date-error
Browse files Browse the repository at this point in the history
[BO - Visites] Ajout contrôle sur la saisie de date de visite
  • Loading branch information
sfinx13 authored Aug 4, 2023
2 parents 4f6827e + 7583647 commit ed8f19f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/Controller/Back/SignalementVisitesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;

#[Route('/bo/signalements')]
class SignalementVisitesController extends AbstractController
Expand Down Expand Up @@ -73,6 +74,7 @@ public function addVisiteToSignalement(
UploadHandlerService $uploadHandler,
EventDispatcherInterface $eventDispatcher,
FilenameGenerator $filenameGenerator,
ValidatorInterface $validator,
): Response {
$this->denyAccessUnlessGranted('SIGN_ADD_VISITE', $signalement);

Expand Down Expand Up @@ -102,7 +104,10 @@ public function addVisiteToSignalement(
document: $fileName,
);

if ($intervention = $interventionManager->createVisiteFromRequest($signalement, $visiteRequest)) {
$errorMessage = $this->validateRequest($visiteRequest, $validator);
if ($errorMessage) {
$this->addFlash('error', sprintf("Erreurs lors de l'enregistrement de la visite : %s", $errorMessage));
} elseif ($intervention = $interventionManager->createVisiteFromRequest($signalement, $visiteRequest)) {
$todayDate = new \DateTimeImmutable();
if ($intervention->getScheduledAt() <= $todayDate) {
$this->addFlash('success', self::SUCCESS_MSG_CONFIRM);
Expand Down Expand Up @@ -168,6 +173,7 @@ public function rescheduleVisiteFromSignalement(
UploadHandlerService $uploadHandler,
EventDispatcherInterface $eventDispatcher,
FilenameGenerator $filenameGenerator,
ValidatorInterface $validator,
): Response {
$requestRescheduleData = $request->get('visite-reschedule');

Expand Down Expand Up @@ -205,7 +211,10 @@ public function rescheduleVisiteFromSignalement(
document: $fileName,
);

if ($intervention = $interventionManager->rescheduleVisiteFromRequest($signalement, $visiteRequest)) {
$errorMessage = $this->validateRequest($visiteRequest, $validator);
if ($errorMessage) {
$this->addFlash('error', sprintf('Erreurs lors de la modification de la visite : %s', $errorMessage));
} elseif ($intervention = $interventionManager->rescheduleVisiteFromRequest($signalement, $visiteRequest)) {
if ($intervention->getScheduledAt() <= new \DateTimeImmutable()) {
$this->addFlash('success', self::SUCCESS_MSG_CONFIRM);
} else {
Expand Down Expand Up @@ -320,4 +329,19 @@ public function editVisiteFromSignalement(

return $this->redirectToRoute('back_signalement_view', ['uuid' => $signalement->getUuid()]);
}

private function validateRequest(VisiteRequest $visiteRequest, ValidatorInterface $validator): string
{
$errorMessage = '';

$errors = $validator->validate($visiteRequest);
if (\count($errors) > 0) {
$errorMessage = '';
foreach ($errors as $error) {
$errorMessage .= $error->getMessage().' ';
}
}

return $errorMessage;
}
}
3 changes: 3 additions & 0 deletions src/Dto/Request/Signalement/VisiteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\Dto\Request\Signalement;

use Symfony\Component\Validator\Constraints as Assert;

class VisiteRequest
{
public function __construct(
private readonly ?int $idIntervention = null,
#[Assert\DateTime('Y-m-d')]
private readonly ?string $date = null,
private readonly ?string $time = null,
private readonly ?int $idPartner = null,
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Manager/InterventionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function setUp(): void
$this->workflow,
$this->signalementQualificationUpdater,
$this->fileFactory,
$this->security
$this->security,
);
}

Expand Down

0 comments on commit ed8f19f

Please sign in to comment.