Skip to content

Commit

Permalink
Merge pull request #3345 from MTES-MCT/feature/3322-command-init-ban-id
Browse files Browse the repository at this point in the history
[Signalement] Enregistrement de l'identifiant BAN
  • Loading branch information
numew authored Dec 2, 2024
2 parents 5bbbd59 + ff1e74f commit 3d5e893
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 119 deletions.
14 changes: 1 addition & 13 deletions assets/scripts/vanilla/services/component_search_address.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ export function attacheAutocompleteAddressEvent (inputAdresse) {
if (document?.querySelector('#' + idForm + ' [data-autocomplete-insee]')) {
document.querySelector('#' + idForm + ' [data-autocomplete-insee]').value = ''
}
if (document?.querySelector('#' + idForm + ' [data-autocomplete-geoloclng]')) {
document.querySelector('#' + idForm + ' [data-autocomplete-geoloclng]').value = ''
}
if (document?.querySelector('#' + idForm + ' [data-autocomplete-geoloclat]')) {
document.querySelector('#' + idForm + ' [data-autocomplete-geoloclat]').value = ''
}
}
})

Expand All @@ -88,7 +82,7 @@ export function attacheAutocompleteAddressEvent (inputAdresse) {
addressGroup.innerHTML = ''
selectionIndex = -1
}

if (e.key === 'ArrowDown') {
if (addressGroup.children.length > 0) {
selectionIndex = Math.min(selectionIndex + 1, addressGroup.children.length - 1)
Expand Down Expand Up @@ -143,12 +137,6 @@ function attachAddressSuggestionEvent (inputAdresse, suggestion, feature) {
if (document?.querySelector('#' + idForm + ' [data-autocomplete-insee]')) {
document.querySelector('#' + idForm + ' [data-autocomplete-insee]').value = feature.properties.citycode
}
if (document?.querySelector('#' + idForm + ' [data-autocomplete-geolocLng]')) {
document.querySelector('#' + idForm + ' [data-autocomplete-geolocLng]').value = feature.geometry.coordinates[0]
}
if (document?.querySelector('#' + idForm + ' [data-autocomplete-geolocLat]')) {
document.querySelector('#' + idForm + ' [data-autocomplete-geolocLat]').value = feature.geometry.coordinates[1]
}
const addressGroup = document?.querySelector(inputAdresse.dataset.autocompleteQuerySelector)
addressGroup.innerHTML = ''
})
Expand Down
20 changes: 10 additions & 10 deletions assets/scripts/vanilla/services/tabs_manager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const currentHash = window.location.hash != '' ? window.location.hash.substring(1) : ''
const btnElements = document.querySelectorAll(`.fr-tabs__tab`);
const tabElements = document.querySelectorAll(`.fr-tabs__panel`);
const buttonElement = document.getElementById('tabpanel-'+currentHash)
const tabElement = document.getElementById('tabpanel-'+currentHash+'-panel');
if(buttonElement && tabElement){
const currentHash = window.location.hash !== '' ? window.location.hash.substring(1) : ''
const btnElements = document.querySelectorAll('.fr-tabs__tab')
const tabElements = document.querySelectorAll('.fr-tabs__panel')
const buttonElement = document.getElementById('tabpanel-' + currentHash)
const tabElement = document.getElementById('tabpanel-' + currentHash + '-panel')
if (buttonElement && tabElement) {
if (!tabElement.disabled) {
btnElements.forEach((btnElement) => {
btnElement.setAttribute('aria-selected', 'false')
Expand All @@ -16,7 +16,7 @@ if(buttonElement && tabElement){
}
}
btnElements.forEach((btnElement) => {
btnElement.addEventListener('click', (e) => {
window.location.hash = btnElement.id.substring(9)
})
})
btnElement.addEventListener('click', (e) => {
window.location.hash = btnElement.id.substring(9)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ export default defineComponent({
if (suggestions[0] !== undefined) {
formStore.data.adresse_logement_adresse_detail_commune = suggestions[0].properties.city
formStore.data.adresse_logement_adresse_detail_insee = suggestions[0].properties.citycode
formStore.data.adresse_logement_adresse_detail_geoloc_lng = suggestions[0].geometry.coordinates[0]
formStore.data.adresse_logement_adresse_detail_geoloc_lat = suggestions[0].geometry.coordinates[1]
formStore.data.adresse_logement_adresse = formStore.data.adresse_logement_adresse_detail_numero + ' ' + formStore.data.adresse_logement_adresse_detail_code_postal + ' ' + formStore.data.adresse_logement_adresse_detail_commune
formStore.data.adresse_logement_adresse_suggestion = formStore.data.adresse_logement_adresse_detail_numero + ' ' + formStore.data.adresse_logement_adresse_detail_code_postal + ' ' + formStore.data.adresse_logement_adresse_detail_commune
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,6 @@ export default defineComponent({
this.formStore.data[this.id + '_detail_code_postal'] = this.suggestions[index].properties.postcode
this.formStore.data[this.id + '_detail_commune'] = this.suggestions[index].properties.city
this.formStore.data[this.id + '_detail_insee'] = this.suggestions[index].properties.citycode
this.formStore.data[this.id + '_detail_geoloc_lng'] = this.suggestions[index].geometry.coordinates[0]
this.formStore.data[this.id + '_detail_geoloc_lat'] = this.suggestions[index].geometry.coordinates[1]
this.formStore.data[this.id + '_detail_manual'] = 0
this.suggestions.length = 0
setTimeout(() => {
Expand Down
26 changes: 26 additions & 0 deletions migrations/Version20241125151642.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20241125151642 extends AbstractMigration
{
public function getDescription(): string
{
return 'add ban_id_occupant to signalement table';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE signalement ADD ban_id_occupant VARCHAR(50) DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE signalement DROP ban_id_occupant');
}
}
80 changes: 80 additions & 0 deletions src/Command/InitIdBanCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Command;

use App\Entity\Signalement;
use App\Manager\HistoryEntryManager;
use App\Manager\SignalementManager;
use App\Repository\SignalementRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'app:init-id-ban',
description: 'Search BAN ID when missing in Adresse Occupant of Signalement',
)]
class InitIdBanCommand extends Command
{
private const int BATCH_SIZE = 20;

public function __construct(
private readonly SignalementManager $signalementManager,
private readonly SignalementRepository $signalementRepository,
private readonly EntityManagerInterface $entityManager,
private readonly HistoryEntryManager $historyEntryManager,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->historyEntryManager->removeEntityListeners();

$io = new SymfonyStyle($input, $output);

$signalementIdsWithouBanId = $this->signalementRepository->findNullBanId();
$nbSignalementWithoutBanId = \count($signalementIdsWithouBanId);

$nb = 0;
$progressBar = new ProgressBar($output, $nbSignalementWithoutBanId);
$progressBar->start();

$nbBatch = ceil($nbSignalementWithoutBanId / self::BATCH_SIZE);
for ($i = 0; $i < $nbBatch; ++$i) {
$signalementsBatch = array_splice($signalementIdsWithouBanId, 0, self::BATCH_SIZE);
$signalementsIdsBatch = [];
foreach ($signalementsBatch as $signalementBatch) {
$signalementsIdsBatch[] = $signalementBatch['id'];
}
$listSignalementBanIdNull = $this->signalementRepository->findBy(['id' => $signalementsIdsBatch]);

/** @var Signalement $signalement */
foreach ($listSignalementBanIdNull as $signalement) {
$this->signalementManager->updateAddressOccupantFromBanData(
signalement: $signalement,
updateGeoloc: false,
);
if (!empty($signalement->getBanIdOccupant())) {
++$nb;
}
$progressBar->advance();
}
$this->entityManager->flush();
}

$progressBar->finish();
$nbSignalementWithoutBanId = $this->signalementRepository->count(['banIdOccupant' => '0']);
$io->success(\sprintf(
'%s BAN IDs have been initialized, but %s signalements remain with no BAN ID',
$nb,
$nbSignalementWithoutBanId
));

return Command::SUCCESS;
}
}
16 changes: 0 additions & 16 deletions src/Dto/Request/Signalement/AdresseOccupantRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ public function __construct(
private readonly ?string $numAppart = null,
#[Assert\Length(max: 255, maxMessage: 'Le champ Autre ne peut pas dépasser {{ limit }} caractères.')]
private readonly ?string $autre = null,
#[Assert\Length(max: 50, maxMessage: 'La longitude ne peut pas dépasser {{ limit }} caractères.')]
#[Assert\Regex(pattern: '/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/', message: 'La longitude doit être un nombre décimal.')]
private readonly ?string $geolocLng = null,
#[Assert\Length(max: 50, maxMessage: 'La latitude ne peut pas dépasser {{ limit }} caractères.')]
#[Assert\Regex(pattern: '/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/', message: 'La latitude doit être un nombre décimal.')]
private readonly ?string $geolocLat = null,
#[Assert\Regex(pattern: '/^[0-9][0-9A-Za-z][0-9]{3}$/', message: 'Le code insee doit être composé de 5 caractères.')]
private readonly ?string $insee = null,
#[Assert\Choice(choices: ['1'], message: 'Le champ "manual" est incorrect.')]
Expand Down Expand Up @@ -75,16 +69,6 @@ public function getAutre(): ?string
return $this->autre;
}

public function getGeolocLng(): ?string
{
return $this->geolocLng;
}

public function getGeolocLat(): ?string
{
return $this->geolocLat;
}

public function getInsee(): ?string
{
return $this->insee;
Expand Down
36 changes: 0 additions & 36 deletions src/Dto/Request/Signalement/SignalementDraftRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ class SignalementDraftRequest
#[Assert\NotBlank(message: 'Merci de saisir un code INSEE.')]
#[Assert\Regex(pattern: '/^[0-9][0-9A-Za-z][0-9]{3}$/', message: 'Le code insee doit être composé de 5 caractères.')]
private ?string $adresseLogementAdresseDetailInsee = null;
#[Assert\Length(max: 50, maxMessage: 'La latitude ne peut pas dépasser {{ limit }} caractères.')]
#[Assert\Regex(
pattern: '/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/',
message: 'La latitude doit être un nombre décimal.'
)]
private ?float $adresseLogementAdresseDetailGeolocLat = null;
#[Assert\Length(max: 50, maxMessage: 'La longitude ne peut pas dépasser {{ limit }} caractères.')]
#[Assert\Regex(
pattern: '/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/',
message: 'La longitude doit être un nombre décimal.'
)]
private ?float $adresseLogementAdresseDetailGeolocLng = null;
private ?bool $adresseLogementAdresseDetailManual = null;
#[Assert\Length(max: 3, maxMessage: 'L\'escalier ne doit pas dépasser {{ limit }} caractères')]
private ?string $adresseLogementComplementAdresseEscalier = null;
Expand Down Expand Up @@ -564,30 +552,6 @@ public function setAdresseLogementAdresseDetailInsee(?string $adresseLogementAdr
return $this;
}

public function getAdresseLogementAdresseDetailGeolocLat(): ?float
{
return $this->adresseLogementAdresseDetailGeolocLat;
}

public function setAdresseLogementAdresseDetailGeolocLat(?float $adresseLogementAdresseDetailGeolocLat): self
{
$this->adresseLogementAdresseDetailGeolocLat = $adresseLogementAdresseDetailGeolocLat;

return $this;
}

public function getAdresseLogementAdresseDetailGeolocLng(): ?float
{
return $this->adresseLogementAdresseDetailGeolocLng;
}

public function setAdresseLogementAdresseDetailGeolocLng(?float $adresseLogementAdresseDetailGeolocLng): self
{
$this->adresseLogementAdresseDetailGeolocLng = $adresseLogementAdresseDetailGeolocLng;

return $this;
}

public function getAdresseLogementAdresseDetailManual(): ?bool
{
return $this->adresseLogementAdresseDetailManual;
Expand Down
15 changes: 15 additions & 0 deletions src/Entity/Signalement.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ class Signalement implements EntityHistoryInterface, EntityHistoryCollectionInte
#[ORM\Column(type: 'string', length: 100, nullable: true)]
private $villeOccupant;

#[ORM\Column(type: 'string', length: 50, nullable: true)]
private $banIdOccupant;

#[ORM\Column(type: 'boolean')]
private $isCguAccepted;

Expand Down Expand Up @@ -1051,6 +1054,18 @@ public function getAddressCompleteOccupant(): ?string
);
}

public function getBanIdOccupant(): ?string
{
return $this->banIdOccupant;
}

public function setBanIdOccupant(?string $banIdOccupant): self
{
$this->banIdOccupant = $banIdOccupant;

return $this;
}

public function getIsCguAccepted(): ?bool
{
return $this->isCguAccepted;
Expand Down
63 changes: 44 additions & 19 deletions src/Manager/SignalementManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

class SignalementManager extends AbstractManager
{
public const float SCORE_IF_BAN_ID_ACCEPTED = 0.9;

public function __construct(
protected ManagerRegistry $managerRegistry,
private Security $security,
Expand Down Expand Up @@ -193,6 +195,45 @@ public function updateAddressOccupantFromAddress(Signalement $signalement, Addre
}
}

public function updateAddressOccupantFromBanData(Signalement $signalement, bool $updateGeoloc = true): void
{
$addressResult = $this->addressService->getAddress($signalement->getAddressCompleteOccupant());
if ($addressResult->getScore() > self::SCORE_IF_BAN_ID_ACCEPTED) {
$signalement->setBanIdOccupant($addressResult->getBanId());
if ($updateGeoloc) {
$signalement
->setInseeOccupant($addressResult->getInseeCode())
->setGeoloc([
'lat' => $addressResult->getLatitude(),
'lng' => $addressResult->getLongitude(),
]);
}

return;
} elseif ($updateGeoloc && !empty($signalement->getCpOccupant()) && !empty($signalement->getVilleOccupant())) {
$inseeResult = $this->addressService->getAddress($signalement->getCpOccupant().' '.$signalement->getVilleOccupant());
if (!empty($inseeResult->getCity())) {
$signalement
->setBanIdOccupant(0)
->setVilleOccupant($inseeResult->getCity())
->setInseeOccupant($inseeResult->getInseeCode())
->setGeoloc([
'lat' => $inseeResult->getLatitude(),
'lng' => $inseeResult->getLongitude(),
]);

return;
}
}

$signalement->setBanIdOccupant(0);
if ($updateGeoloc) {
$signalement
->setInseeOccupant(null)
->setGeoloc([]);
}
}

public function findAllPartners(Signalement $signalement): array
{
/** @var PartnerRepository $partnerRepository */
Expand Down Expand Up @@ -339,32 +380,16 @@ public function updateFromAdresseOccupantRequest(
->setCpOccupant($adresseOccupantRequest->getCodePostal())
->setVilleOccupant($adresseOccupantRequest->getVille())
->setInseeOccupant($adresseOccupantRequest->getInsee())
->setGeoloc([
'lat' => $adresseOccupantRequest->getGeolocLat(),
'lng' => $adresseOccupantRequest->getGeolocLng(),
])

->setEtageOccupant($adresseOccupantRequest->getEtage())
->setEscalierOccupant($adresseOccupantRequest->getEscalier())
->setNumAppartOccupant($adresseOccupantRequest->getNumAppart())
->setAdresseAutreOccupant($adresseOccupantRequest->getAutre())
->setManualAddressOccupant('1' === $adresseOccupantRequest->getManual());

if ('1' === $adresseOccupantRequest->getNeedResetInsee()) {
$resetAddress = $this->addressService->getAddress($adresseOccupantRequest->getCodePostal().' '.$adresseOccupantRequest->getVille());
if (!empty($resetAddress->getCity())) {
$signalement->setVilleOccupant($resetAddress->getCity());
}
if (!empty($resetAddress->getInseeCode())) {
$signalement->setInseeOccupant($resetAddress->getInseeCode());
}
if (!empty($resetAddress->getLatitude())) {
$signalement->setGeoloc([
'lat' => $resetAddress->getLatitude(),
'lng' => $resetAddress->getLongitude(),
]);
}
}
$this->updateAddressOccupantFromBanData(
signalement: $signalement,
);

$this->save($signalement);

Expand Down
Loading

0 comments on commit 3d5e893

Please sign in to comment.