Skip to content

Commit

Permalink
Merge pull request #1491 from MTES-MCT/fix/esabora-sish
Browse files Browse the repository at this point in the history
[ESABORA] Ajustement arrêté préfectoral et visite de contrôle
  • Loading branch information
hmeneuvrier authored Jul 24, 2023
2 parents d847221 + e37a714 commit 93a5e0f
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 133 deletions.
13 changes: 8 additions & 5 deletions src/Entity/Enum/InterventionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ enum InterventionType: string
case VISITE_CONTROLE = 'VISITE_CONTROLE';
case ARRETE_PREFECTORAL = 'ARRETE_PREFECTORAL';

public const INTERVENTION_TYPE_LABEL = [
'VISITE' => 'Visite',
'VISITE_CONTROLE' => 'Visite de contrôle',
'ARRETE_PREFECTORAL' => 'Arrêté préfectoral',
];

public function label(): string
{
return self::getLabelList()[$this->name];
}

public static function getLabelList(): array
{
return [
'VISITE' => 'Visite',
'VISITE_CONTROLE' => 'Visite contrôle',
'ARRETE_PREFECTORAL' => 'Arrêté préfectoral',
];
return self::INTERVENTION_TYPE_LABEL;
}

public static function tryFromLabel(string $label): ?self
{
$label = str_contains($label, 'contrôle') ? self::INTERVENTION_TYPE_LABEL['VISITE_CONTROLE'] : $label;
$key = array_search($label, self::getLabelList());

return self::tryFrom($key);
Expand Down
65 changes: 35 additions & 30 deletions src/EventSubscriber/InterventionCreatedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace App\EventSubscriber;

use App\Entity\Enum\InterventionType;
use App\Entity\Intervention;
use App\Entity\Suivi;
use App\Event\InterventionCreatedEvent;
use App\Manager\SuiviManager;
use App\Service\Intervention\InterventionDescriptionGenerator;
use App\Service\Mailer\NotificationMailerType;
use App\Service\Signalement\VisiteNotifier;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class InterventionCreatedSubscriber implements EventSubscriberInterface
{
public function __construct(
private VisiteNotifier $visiteNotifier,
private SuiviManager $suiviManager,
private readonly VisiteNotifier $visiteNotifier,
private readonly SuiviManager $suiviManager,
) {
}

Expand All @@ -28,32 +29,36 @@ public static function getSubscribedEvents(): array
public function onInterventionCreated(InterventionCreatedEvent $event): void
{
$intervention = $event->getIntervention();
if (InterventionType::VISITE === $intervention->getType()) {
$partnerName = $intervention->getPartner() ? $intervention->getPartner()->getNom() : 'Non renseigné';
$description = 'Visite programmée : une visite du logement situé '.$intervention->getSignalement()->getAdresseOccupant();
$description .= ' est prévue le '.$intervention->getScheduledAt()->format('d/m/Y').'.';
$description .= '<br>';
$description .= 'La visite sera effectuée par '.$partnerName.'.';
$suivi = $this->suiviManager->createSuivi(
user: $event->getUser(),
signalement: $intervention->getSignalement(),
params: [
'description' => $description,
'type' => Suivi::TYPE_AUTO,
],
isPublic: true,
context: Suivi::CONTEXT_INTERVENTION,
);
$this->suiviManager->save($suivi);

$this->visiteNotifier->notifyUsagers($intervention, NotificationMailerType::TYPE_VISITE_CREATED_TO_USAGER);

$this->visiteNotifier->notifyAgents(
intervention: $intervention,
suivi: $suivi,
currentUser: $event->getUser(),
notificationMailerType: null,
);
}
$suivi = $this->suiviManager->createSuivi(
user: $event->getUser(),
signalement: $intervention->getSignalement(),
params: $this->getParams($intervention),
isPublic: true,
context: Suivi::CONTEXT_INTERVENTION,
);
$this->suiviManager->save($suivi);

$this->visiteNotifier->notifyUsagers(
$intervention,
NotificationMailerType::TYPE_VISITE_CREATED_TO_USAGER
);

$this->visiteNotifier->notifyAgents(
intervention: $intervention,
suivi: $suivi,
currentUser: $event->getUser(),
notificationMailerType: null,
);
}

private function getParams(Intervention $intervention): array
{
return [
'type' => Suivi::TYPE_AUTO,
'description' => InterventionDescriptionGenerator::generate(
$intervention,
InterventionCreatedEvent::NAME
),
];
}
}
7 changes: 6 additions & 1 deletion src/Manager/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public function createUsagerFromSignalement(Signalement $signalement, string $ty
if (null === $user) {
$user = $this->userFactory->createInstanceFrom(
roleLabel: User::ROLES['Usager'],
territory: null,
partner: null,
territory: null,
firstname: $prenom,
lastname: $nom,
email: $mail
Expand All @@ -157,4 +157,9 @@ public function createUsagerFromSignalement(Signalement $signalement, string $ty

return null;
}

public function getSystemUser(): ?User
{
return $this->getRepository()->findOneBy(['email' => $this->parameterBag->get('user_system_email')]);
}
}
42 changes: 17 additions & 25 deletions src/Service/Esabora/EsaboraManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
use App\Entity\Enum\InterventionType;
use App\Entity\Intervention;
use App\Entity\User;
use App\Event\InterventionCreatedEvent;
use App\Factory\InterventionFactory;
use App\Manager\AffectationManager;
use App\Manager\SuiviManager;
use App\Manager\UserManager;
use App\Repository\InterventionRepository;
use App\Service\Esabora\Enum\EsaboraStatus;
use App\Service\Esabora\Response\DossierResponseInterface;
use App\Service\Esabora\Response\Model\DossierArreteSISH;
use App\Service\Esabora\Response\Model\DossierVisiteSISH;
use App\Service\Intervention\InterventionDescriptionGenerator;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class EsaboraManager
{
Expand All @@ -24,6 +28,8 @@ public function __construct(
private readonly SuiviManager $suiviManager,
private readonly InterventionRepository $interventionRepository,
private readonly InterventionFactory $interventionFactory,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly UserManager $userManager,
private readonly LoggerInterface $logger,
) {
}
Expand Down Expand Up @@ -128,6 +134,10 @@ public function createOrUpdateVisite(Affectation $affectation, DossierVisiteSISH
doneBy: $dossierVisiteSISH->getVisitePar(),
);
$this->interventionRepository->save($newIntervention, true);
$this->eventDispatcher->dispatch(
new InterventionCreatedEvent($newIntervention, $this->userManager->getSystemUser()),
InterventionCreatedEvent::NAME
);
}
}
}
Expand All @@ -154,11 +164,15 @@ public function createOrUpdateArrete(Affectation $affectation, DossierArreteSISH
status: Intervention::STATUS_DONE,
providerName: InterfacageType::ESABORA->value,
providerId: $dossierArreteSISH->getArreteId(),
details: $this->buildDetailArrete($dossierArreteSISH),
details: InterventionDescriptionGenerator::buildDescriptionArreteCreated($dossierArreteSISH),
additionalInformation: $additionalInformation
);

$this->interventionRepository->save($intervention, true);
$this->eventDispatcher->dispatch(
new InterventionCreatedEvent($intervention, $this->userManager->getSystemUser()),
InterventionCreatedEvent::NAME
);
}
}

Expand All @@ -175,34 +189,12 @@ private function updateFromDossierArrete(Intervention $intervention, DossierArre
{
$intervention
->setScheduledAt(DateParser::parse($dossierArreteSISH->getArreteDate()))
->setDetails($this->buildDetailArrete($dossierArreteSISH))
->setStatus(Intervention::STATUS_DONE)
->setDetails($this->buildDetailArrete($dossierArreteSISH));
->setDetails(InterventionDescriptionGenerator::buildDescriptionArreteCreated($dossierArreteSISH))
->setStatus(Intervention::STATUS_DONE);

$this->interventionRepository->save($intervention, true);
}

private function buildDetailArrete(DossierArreteSISH $dossierArreteSISH): string
{
$description = sprintf(
'Il existe 1 arrêté de type %s de n°%s daté du %s dans le dossier de n°%s.'.\PHP_EOL,
$dossierArreteSISH->getArreteType(),
$dossierArreteSISH->getArreteNumero(),
$dossierArreteSISH->getArreteDate(),
$dossierArreteSISH->getDossNum()
);

if ($dossierArreteSISH->getArreteMLNumero()) {
$description .= sprintf(
'Pour cet arrêté, il a également été pris un arrêté de mainlevée n°%s en date du %s.',
$dossierArreteSISH->getArreteMLNumero(),
$dossierArreteSISH->getArreteMLDate()
);
}

return $description;
}

private function shouldBeAcceptedViaEsabora(string $esaboraDossierStatus, int $currentStatus): bool
{
return EsaboraStatus::ESABORA_IN_PROGRESS->value === $esaboraDossierStatus
Expand Down
61 changes: 61 additions & 0 deletions src/Service/Intervention/InterventionDescriptionGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Service\Intervention;

use App\Entity\Enum\InterventionType;
use App\Entity\Intervention;
use App\Event\InterventionCreatedEvent;
use App\Service\Esabora\Response\Model\DossierArreteSISH;

class InterventionDescriptionGenerator
{
public static function generate(Intervention $intervention, string $eventName): ?string
{
if (InterventionCreatedEvent::NAME === $eventName) {
if (InterventionType::ARRETE_PREFECTORAL === $intervention->getType()) {
return $intervention->getDetails();
}

return self::buildDescriptionVisiteCreated($intervention);
}

return null;
}

public static function buildDescriptionVisiteCreated(Intervention $intervention): string
{
$labelVisite = strtolower($intervention->getType()->label());
$partnerName = $intervention->getPartner() ? $intervention->getPartner()->getNom() : 'Non renseigné';

return sprintf(
'%s programmée : une %s du logement situé %s est prévue le %s.<br>La %s sera effectuée par %s.',
ucfirst($labelVisite),
$labelVisite,
$intervention->getSignalement()->getAdresseOccupant(),
$intervention->getScheduledAt()->format('d/m/Y'),
$labelVisite,
$partnerName
);
}

public static function buildDescriptionArreteCreated(DossierArreteSISH $dossierArreteSISH): string
{
$description = sprintf(
'Il existe 1 arrêté de type %s de n°%s daté du %s dans le dossier de n°%s.'.\PHP_EOL,
$dossierArreteSISH->getArreteType(),
$dossierArreteSISH->getArreteNumero(),
$dossierArreteSISH->getArreteDate(),
$dossierArreteSISH->getDossNum()
);

if ($dossierArreteSISH->getArreteMLNumero()) {
$description .= sprintf(
'Pour cet arrêté, il a également été pris un arrêté de mainlevée n°%s en date du %s.',
$dossierArreteSISH->getArreteMLNumero(),
$dossierArreteSISH->getArreteMLDate()
);
}

return $description;
}
}
6 changes: 6 additions & 0 deletions templates/back/signalement/view/visites/visite-item.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<div class="fr-grid-row fr-mt-3v fr-mb-3w">
<div class="fr-col-12 fr-col-md-6">
{% if intervention is defined and intervention.type == enum('App\\Entity\\Enum\\InterventionType').VISITE_CONTROLE %}
<div class="fr-mb-3v">
<strong>Type de visite :</strong>
{{ intervention.type.label() }}
</div>
{% endif %}
<div class="fr-mb-3v">
{% include 'back/signalement/view/visites/visite-status.html.twig' %}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% endif %}
{% include 'back/signalement/view/visites/visite-item.html.twig' %}
{% else %}
{% for intervention in signalement.interventions %}
{% for intervention in signalement.interventions | filter(intervention => intervention.type != enum('App\\Entity\\Enum\\InterventionType').ARRETE_PREFECTORAL) %}
<div class="fr-grid-row">
<div class="fr-col-12 fr-col-md-6 fr-h6 fr-mb-3v">
Visite #{{loop.index}}
Expand All @@ -29,7 +29,7 @@
</div>
</div>
{% include 'back/signalement/view/visites/visite-item.html.twig' %}
{% if not loop.last %}
{% if loop.last is defined and not loop.last %}
<hr class="fr-hr--sm">
{% endif %}
{% endfor %}
Expand Down
17 changes: 16 additions & 1 deletion tests/FixturesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use App\Entity\Affectation;
use App\Entity\Critere;
use App\Entity\Criticite;
use App\Entity\Enum\InterventionType;
use App\Entity\Enum\PartnerType;
use App\Entity\File;
use App\Entity\Intervention;
use App\Entity\Partner;
use App\Entity\Signalement;
use App\Entity\Situation;
Expand Down Expand Up @@ -319,7 +321,7 @@ public function getPartner(): Partner

return (new Partner())
->setId(1)
->setNom($faker->company())
->setNom('ARS')
->setType(PartnerType::ARS)
->setEmail($faker->email())
->setTerritory($this->getTerritory());
Expand Down Expand Up @@ -350,4 +352,17 @@ public function getPhotoFile(): File
->setFileType(File::FILE_TYPE_PHOTO)
->setCreatedAt(new \DateTimeImmutable('2022-12-02'));
}

public function getIntervention(
InterventionType $interventionType,
\DateTimeImmutable $scheduledAt,
string $status
): Intervention {
return (new Intervention())
->setSignalement($this->getSignalement())
->setPartner($this->getPartner())
->setType($interventionType)
->setScheduledAt($scheduledAt)
->setStatus($status);
}
}
Loading

0 comments on commit 93a5e0f

Please sign in to comment.