Skip to content

Commit

Permalink
Merge pull request #1959 from MTES-MCT/feature/1665-back-enregistrer-…
Browse files Browse the repository at this point in the history
…les-desordres

[Back] Remplir les 3 tables de désordres à partir du fichier xls
  • Loading branch information
sfinx13 authored Nov 27, 2023
2 parents 0d9cd30 + 5254ddc commit ed4d484
Show file tree
Hide file tree
Showing 18 changed files with 979 additions and 24 deletions.
105 changes: 105 additions & 0 deletions migrations/Version20231120144804.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20231120144804 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add relation between signalement and desordre';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE desordre_categorie_signalement
(desordre_categorie_id INT NOT NULL,
signalement_id INT NOT NULL,
INDEX IDX_E365880CECF01477 (desordre_categorie_id),
INDEX IDX_E365880C65C5E57E (signalement_id),
PRIMARY KEY(desordre_categorie_id, signalement_id))
DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');

$this->addSql('CREATE TABLE desordre_critere_signalement
(desordre_critere_id INT NOT NULL,
signalement_id INT NOT NULL,
INDEX IDX_689D9BA81C3935AB (desordre_critere_id),
INDEX IDX_689D9BA865C5E57E (signalement_id),
PRIMARY KEY(desordre_critere_id, signalement_id))
DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');

$this->addSql('CREATE TABLE desordre_precision_signalement
(desordre_precision_id INT NOT NULL,
signalement_id INT NOT NULL,
INDEX IDX_D390215F9FB07E9C (desordre_precision_id),
INDEX IDX_D390215F65C5E57E (signalement_id),
PRIMARY KEY(desordre_precision_id, signalement_id))
DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');

$this->addSql('ALTER TABLE desordre_categorie_signalement
ADD CONSTRAINT FK_E365880CECF01477 FOREIGN KEY (desordre_categorie_id)
REFERENCES desordre_categorie (id) ON DELETE CASCADE');

$this->addSql('ALTER TABLE desordre_categorie_signalement
ADD CONSTRAINT FK_E365880C65C5E57E FOREIGN KEY (signalement_id) REFERENCES signalement (id) ON DELETE CASCADE');

$this->addSql('ALTER TABLE desordre_critere_signalement
ADD CONSTRAINT FK_689D9BA81C3935AB FOREIGN KEY (desordre_critere_id)
REFERENCES desordre_critere (id) ON DELETE CASCADE');

$this->addSql('ALTER TABLE desordre_critere_signalement
ADD CONSTRAINT FK_689D9BA865C5E57E FOREIGN KEY (signalement_id) REFERENCES signalement (id) ON DELETE CASCADE');

$this->addSql('ALTER TABLE desordre_precision_signalement
ADD CONSTRAINT FK_D390215F9FB07E9C FOREIGN KEY (desordre_precision_id)
REFERENCES desordre_precision (id) ON DELETE CASCADE');

$this->addSql('ALTER TABLE desordre_precision_signalement
ADD CONSTRAINT FK_D390215F65C5E57E FOREIGN KEY (signalement_id) REFERENCES signalement (id) ON DELETE CASCADE');

$this->addSql('ALTER TABLE desordre_categorie
CHANGE created_at created_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\',
CHANGE updated_at updated_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\'');

$this->addSql('ALTER TABLE desordre_critere
CHANGE created_at created_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\',
CHANGE updated_at updated_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\'');

$this->addSql('ALTER TABLE desordre_precision
CHANGE created_at created_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\',
CHANGE updated_at updated_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\'');

$this->addSql('ALTER TABLE desordre_precision CHANGE coef coef DOUBLE PRECISION NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE desordre_categorie_signalement DROP FOREIGN KEY FK_E365880CECF01477');
$this->addSql('ALTER TABLE desordre_categorie_signalement DROP FOREIGN KEY FK_E365880C65C5E57E');
$this->addSql('ALTER TABLE desordre_critere_signalement DROP FOREIGN KEY FK_689D9BA81C3935AB');
$this->addSql('ALTER TABLE desordre_critere_signalement DROP FOREIGN KEY FK_689D9BA865C5E57E');
$this->addSql('ALTER TABLE desordre_precision_signalement DROP FOREIGN KEY FK_D390215F9FB07E9C');
$this->addSql('ALTER TABLE desordre_precision_signalement DROP FOREIGN KEY FK_D390215F65C5E57E');
$this->addSql('DROP TABLE desordre_categorie_signalement');
$this->addSql('DROP TABLE desordre_critere_signalement');
$this->addSql('DROP TABLE desordre_precision_signalement');

$this->addSql('ALTER TABLE desordre_categorie
CHANGE created_at created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\',
CHANGE updated_at updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\'');

$this->addSql('ALTER TABLE desordre_critere
CHANGE created_at created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\',
CHANGE updated_at updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\'');

$this->addSql('ALTER TABLE desordre_precision
CHANGE created_at created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\',
CHANGE updated_at updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\'');

$this->addSql('ALTER TABLE desordre_precision CHANGE coef coef INT NOT NULL');
}
}
10 changes: 8 additions & 2 deletions scripts/upload-s3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# option - The type of file to upload (grid, signalement, or image)
# zip - The code of the department
#
# Example 0: ./scripts/upload-s3.sh desordres
# Example 1: ./scripts/upload-s3.sh grid 33
# Example 2: ./scripts/upload-s3.sh signalement 33
# Example 3: ./scripts/upload-s3.sh image 33
Expand All @@ -29,11 +30,16 @@ else
option=$1
zip=$2
debug=${3:null}
if [ -z "$zip" ]; then
if [ -z "$zip" ] && [ "$option" != "desordres" ]; then
echo "zip argument is missing: ./scripts/upload-s3.sh [option] [zip]"
exit 1
fi
case "$option" in
"desordres")
echo "Upload desordres_tables.csv to cloud..."
aws s3 cp data/desordres_tables.csv s3://${BUCKET_URL}/csv/ ${debug}
aws s3 ls s3://${BUCKET_URL}/csv/desordres_tables.csv
;;
"grid")
echo "Upload grille_affectation_$2.csv to cloud..."
aws s3 cp data/grid-affectation/grille_affectation_${zip}.csv s3://${BUCKET_URL}/csv/ ${debug}
Expand Down Expand Up @@ -73,7 +79,7 @@ else
fi
;;
*)
echo "Invalid argument. Please use 'grid' or 'signalement' or 'image' or 'mapping-doc' or 'process-all'"
echo "Invalid argument. Please use 'desordres' or 'grid' or 'signalement' or 'image' or 'mapping-doc' or 'process-all'"
;;
esac
fi
60 changes: 60 additions & 0 deletions src/Command/ImportDesordresTablesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Command;

use App\Service\Import\CsvParser;
use App\Service\Import\Desordres\DesordresTablesLoader;
use App\Service\UploadHandlerService;
use League\Flysystem\FilesystemOperator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

#[AsCommand(
name: 'app:import-desordres-tables',
description: 'Import DesordreCategorie, DesordreCritere, DesordrePrecision',
)]
class ImportDesordresTablesCommand extends Command
{
public function __construct(
private CsvParser $csvParser,
private ParameterBagInterface $parameterBag,
private FilesystemOperator $fileStorage,
private UploadHandlerService $uploadHandlerService,
private DesordresTablesLoader $desordresTablesLoader,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$fromFile = 'csv/desordres_tables.csv';
$toFile = $this->parameterBag->get('uploads_tmp_dir').'desordres_tables.csv';
if (!$this->fileStorage->fileExists($fromFile)) {
$io->error('CSV File does not exists');

return Command::FAILURE;
}

$this->uploadHandlerService->createTmpFileFromBucket($fromFile, $toFile);

$this->desordresTablesLoader->load(
$this->csvParser->parseAsDict($toFile),
$output
);

$metadata = $this->desordresTablesLoader->getMetadata();

$io->success(sprintf('%s desordre_categorie have been created', $metadata['count_desordre_categorie_created']));
$io->success(sprintf('%s desordre_critere have been created', $metadata['count_desordre_critere_created']));
$io->success(sprintf('%s desordre_precision have been created', $metadata['count_desordre_precision_created']));
$io->success(sprintf('%s desordre_precision have been updated', $metadata['count_desordre_precision_updated']));

return Command::SUCCESS;
}
}
35 changes: 32 additions & 3 deletions src/Entity/DesordreCategorie.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: DesordreCategorieRepository::class)]
#[ORM\HasLifecycleCallbacks()]
class DesordreCategorie
{
use TimestampableTrait;
Expand All @@ -24,9 +25,13 @@ class DesordreCategorie
#[ORM\OneToMany(mappedBy: 'desordreCategorie', targetEntity: DesordreCritere::class, orphanRemoval: true)]
private Collection $desordreCriteres;

#[ORM\ManyToMany(targetEntity: Signalement::class, inversedBy: 'desordreCategories')]
private Collection $signalement;

public function __construct()
{
$this->desordreCriteres = new ArrayCollection();
$this->signalement = new ArrayCollection();
}

public function getId(): ?int
Expand All @@ -39,7 +44,7 @@ public function getLabel(): ?string
return $this->label;
}

public function setLabel(string $label): static
public function setLabel(string $label): self
{
$this->label = $label;

Expand All @@ -54,7 +59,7 @@ public function getDesordreCriteres(): Collection
return $this->desordreCriteres;
}

public function addDesordreCritere(DesordreCritere $desordreCritere): static
public function addDesordreCritere(DesordreCritere $desordreCritere): self
{
if (!$this->desordreCriteres->contains($desordreCritere)) {
$this->desordreCriteres->add($desordreCritere);
Expand All @@ -64,7 +69,7 @@ public function addDesordreCritere(DesordreCritere $desordreCritere): static
return $this;
}

public function removeDesordreCritere(DesordreCritere $desordreCritere): static
public function removeDesordreCritere(DesordreCritere $desordreCritere): self
{
if ($this->desordreCriteres->removeElement($desordreCritere)) {
// set the owning side to null (unless already changed)
Expand All @@ -75,4 +80,28 @@ public function removeDesordreCritere(DesordreCritere $desordreCritere): static

return $this;
}

/**
* @return Collection<int, Signalement>
*/
public function getSignalement(): Collection
{
return $this->signalement;
}

public function addSignalement(Signalement $signalement): self
{
if (!$this->signalement->contains($signalement)) {
$this->signalement->add($signalement);
}

return $this;
}

public function removeSignalement(Signalement $signalement): self
{
$this->signalement->removeElement($signalement);

return $this;
}
}
Loading

0 comments on commit ed4d484

Please sign in to comment.