Skip to content

Commit

Permalink
Merge pull request #1940 from MTES-MCT/feature/1668-handle-file-new-form
Browse files Browse the repository at this point in the history
[Back] Traitement des fichiers lors de la soumission d'un signalement
  • Loading branch information
emilschn authored Nov 17, 2023
2 parents f4668b4 + c2871e3 commit eebee3d
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 5 deletions.
28 changes: 28 additions & 0 deletions migrations/Version20231114155429.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20231114155429 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add new fields (document type and desordre slug)';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE file ADD document_type VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE file ADD desordre_slug VARCHAR(255) DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE file DROP document_type');
$this->addSql('ALTER TABLE file DROP desordre_slug');
}
}
47 changes: 45 additions & 2 deletions src/DataFixtures/Files/signalement_draft_payload/locataire.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,43 @@
"type_logement_rdc": "non",
"categorieDisorders": {
"batiment": [
"desordres_batiment_bruit"
"desordres_batiment_bruit",
"desordres_batiment_proprete"
],
"logement": [
"desordres_logement_electricite",
"desordres_logement_nuisibles"
]
},
"bail_dpe_dpe_upload": [
{
"key": "documents",
"file": "Capture-d-ecran-dpe-1.png",
"titre": "Capture d’écran DPE 1.png"
},
{
"key": "documents",
"file": "Capture-d-ecran-dpe-2.png",
"titre": "Capture d’écran DPE 2.png"
},
{
"key": "documents",
"file": "Capture-d-ecran-dpe-3.png",
"titre": "Capture d’écran DPE 3.png"
}
],
"bail_dpe_bail_upload": [
{
"key": "documents",
"file": "Capture-d-ecran-bail-1.png",
"titre": "Capture d’écran BAIL 1.png"
},
{
"key": "documents",
"file": "Capture-d-ecran-bail-2.png",
"titre": "Capture d’écran BAIL 2.png"
}
],
"zone_concernee_zone": "batiment_logement",
"type_logement_nature": "appartement",
"uuidSignalementDraft": "00000000-0000-0000-2023-locataire001",
Expand Down Expand Up @@ -106,5 +136,18 @@
"informations_complementaires_situation_occupants_beneficiaire_rsa": "non",
"desordres_logement_electricite_manque_prises_details_pieces_cuisine": 1,
"desordres_logement_electricite_manque_prises_details_pieces_piece_a_vivre": 1,
"desordres_logement_electricite_manque_prises_details_pieces_salle_de_bain": 1
"desordres_logement_electricite_manque_prises_details_pieces_salle_de_bain": 1,
"desordres_batiment_proprete_photos_upload": [
{
"key": "photos",
"file": "Capture-d-ecran-1-desordre.png",
"titre": "Capture écran 1.png"
},
{
"key": "photos",
"file": "Capture-d-ecran-2-desordre.png",
"titre": "Capture écran 2.png"
}

]
}
16 changes: 16 additions & 0 deletions src/Dto/Request/Signalement/SignalementDraftRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class SignalementDraftRequest

public const PATTERN_PHONE_KEY = '/.*(_tel|_tel_secondaire)$/';

public const PATTERN_FILE_UPLOAD = '/\w+_upload/';
public const FILE_UPLOAD_KEY = 'files';

private ?string $profil = null;
private ?string $currentStep = null;
private ?string $adresseLogementAdresse = null;
Expand Down Expand Up @@ -127,6 +130,7 @@ class SignalementDraftRequest
private ?string $informationsComplementairesLogementNombreEtages = null;
private ?string $informationsComplementairesLogementAnneeConstruction = null;
private ?string $messageAdministration = null;
private ?array $files = null;

public function getProfil(): ?string
{
Expand Down Expand Up @@ -1430,4 +1434,16 @@ public function setMessageAdministration(?string $messageAdministration): self

return $this;
}

public function getFiles(): ?array
{
return $this->files;
}

public function setFiles(?array $files): self
{
$this->files = $files;

return $this;
}
}
21 changes: 21 additions & 0 deletions src/Entity/Enum/DocumentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Entity\Enum;

enum DocumentType: String
{
case SITUATION_FOYER_BAIL = 'SITUATION_FOYER_BAIL';
case SITUATION_FOYER_DPE = 'SITUATION_FOYER_DPE';
case SITUATION_FOYER_ETAT_DES_LIEUX = 'SITUATION_FOYER_ETAT_DES_LIEUX';
case SITUATION_DIAGNOSTIC_PLOMB_AMIANTE = 'SITUATION_DIAGNOSTIC_PLOMB_AMIANTE';
case PROCEDURE_MISE_EN_DEMEURE = 'PROCEDURE_MISE_EN_DEMEURE';
case PROCEDURE_RAPPORT_DE_VISITE = 'PROCEDURE_RAPPORT_DE_VISITE';
case PROCEDURE_ARRETE_MUNICIPAL = 'PROCEDURE_ARRETE_MUNICIPAL';
case PROCEDURE_ARRETE_PREFECTORAL = 'PROCEDURE_ARRETE_PREFECTORAL';
case PROCEDURE_SAISINE = 'PROCEDURE_SAISINE';
case BAILLEUR_DEVIS_POUR_TRAVAUX = 'BAILLEUR_DEVIS_POUR_TRAVAUX';
case BAILLEUR_REPONSE_BAILLEUR = 'BAILLEUR_REPONSE_BAILLEUR';
case AUTRE = 'AUTRE';
case SITUATION = 'SITUATION';
case VISITE = 'VISITE';
}
32 changes: 32 additions & 0 deletions src/Entity/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Entity;

use App\Entity\Enum\DocumentType;
use App\Repository\FileRepository;
use Doctrine\ORM\Mapping as ORM;

Expand All @@ -12,6 +13,7 @@ class File
public const FILE_TYPE_PHOTO = 'photo';
public const INPUT_NAME_PHOTOS = 'photos';
public const INPUT_NAME_DOCUMENTS = 'documents';

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
Expand Down Expand Up @@ -40,6 +42,12 @@ class File
#[ORM\JoinColumn(nullable: true)]
private ?Intervention $intervention = null;

#[ORM\Column(type: 'string', enumType: DocumentType::class, nullable: true)]
private ?DocumentType $documentType = null;

#[ORM\Column(nullable: true)]
private ?string $desordreSlug = null;

public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
Expand Down Expand Up @@ -133,4 +141,28 @@ public function setIntervention(?Intervention $intervention): self

return $this;
}

public function getDocumentType(): ?DocumentType
{
return $this->documentType;
}

public function setDocumentType(DocumentType $documentType): self
{
$this->documentType = $documentType;

return $this;
}

public function getDesordreSlug(): ?string
{
return $this->desordreSlug;
}

public function setDesordreSlug(?string $desordreSlug): self
{
$this->desordreSlug = $desordreSlug;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function onSignalementDraftCompleted(SignalementDraftCompletedEvent $even
->withSituationFoyer()
->withProcedure()
->withInformationComplementaire()
->withFiles()
->build();

$this->signalementManager->save($signalement);
Expand Down
41 changes: 41 additions & 0 deletions src/Factory/FileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Factory;

use App\Entity\Enum\DocumentType;
use App\Entity\File;
use App\Entity\Intervention;
use App\Entity\Signalement;
use App\Entity\User;
use App\Service\Signalement\SignalementDocumentTypeMapper;

class FileFactory
{
Expand All @@ -16,6 +18,8 @@ public function createInstanceFrom(
?Signalement $signalement = null,
?User $user = null,
?Intervention $intervention = null,
?DocumentType $documentType = null,
?string $desordreSlug = null,
): ?File {
$file = (new File())
->setFilename($filename)
Expand All @@ -34,6 +38,43 @@ public function createInstanceFrom(
$file->setIntervention($intervention);
}

if (null !== $documentType) {
$file->setDocumentType($documentType);
}

if (null !== $desordreSlug) {
$file->setDesordreSlug($desordreSlug);
}

return $file;
}

/**
* @param array $file The array representing the file.
* - 'slug' (string): The slug value.
* - 'file' (string): The file path.
* - 'titre' (string): The title of the file.
*/
public function createFromFileArray(
array $file,
?Signalement $signalement = null,
?User $user = null,
?Intervention $intervention = null,
): ?File {
$documentType = SignalementDocumentTypeMapper::map($file['slug']);
$desordreSlug = DocumentType::SITUATION === $documentType ? $file['slug'] : null;

return $this->createInstanceFrom(
filename: $file['file'],
title: $file['titre'],
type: 'pdf' === pathinfo($file['file'], \PATHINFO_EXTENSION)
? File::FILE_TYPE_DOCUMENT
: File::FILE_TYPE_PHOTO,
signalement: $signalement,
user: $user,
intervention: $intervention,
documentType: SignalementDocumentTypeMapper::map($file['slug']),
desordreSlug: $desordreSlug
);
}
}
2 changes: 2 additions & 0 deletions src/Serializer/SignalementDraftRequestNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function denormalize($data, $type, $format = null, array $context = []):
'phone_number' => $value,
];
$transformedData[$key] = $phone;
} elseif (preg_match(SignalementDraftRequest::PATTERN_FILE_UPLOAD, $key, $matches)) {
$transformedData[SignalementDraftRequest::FILE_UPLOAD_KEY][$key] = $data[$key];
} else {
$transformedData[$key] = $value;
}
Expand Down
23 changes: 22 additions & 1 deletion src/Service/Signalement/SignalementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
use App\Entity\Enum\ProfileDeclarant;
use App\Entity\Signalement;
use App\Entity\SignalementDraft;
use App\Factory\FileFactory;
use App\Factory\Signalement\InformationComplementaireFactory;
use App\Factory\Signalement\InformationProcedureFactory;
use App\Factory\Signalement\SituationFoyerFactory;
use App\Factory\Signalement\TypeCompositionLogementFactory;
use App\Repository\TerritoryRepository;
use App\Serializer\SignalementDraftRequestSerializer;
use App\Service\Token\TokenGeneratorInterface;
use App\Service\UploadHandlerService;
use App\Utils\Json;
use Symfony\Bundle\SecurityBundle\Security;

class SignalementBuilder
{
Expand All @@ -32,7 +35,10 @@ public function __construct(
private TypeCompositionLogementFactory $typeCompositionLogementFactory,
private SituationFoyerFactory $situationFoyerFactory,
private InformationProcedureFactory $informationProcedureFactory,
private InformationComplementaireFactory $informationComplementaireFactory
private InformationComplementaireFactory $informationComplementaireFactory,
private FileFactory $fileFactory,
private UploadHandlerService $uploadHandlerService,
private Security $security,
) {
}

Expand Down Expand Up @@ -145,6 +151,21 @@ public function withInformationComplementaire(): self
return $this;
}

public function withFiles(): self
{
if ($files = $this->signalementDraftRequest->getFiles()) {
foreach ($files as $key => $fileList) {
foreach ($fileList as $fileItem) {
$fileItem['slug'] = $key;
$this->signalement->addFile($file = $this->fileFactory->createFromFileArray(file: $fileItem));
$this->uploadHandlerService->moveFromBucketTempFolder($file->getFilename());
}
}
}

return $this;
}

public function build(): Signalement
{
return $this->signalement;
Expand Down
25 changes: 25 additions & 0 deletions src/Service/Signalement/SignalementDocumentTypeMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Service\Signalement;

use App\Entity\Enum\DocumentType;

class SignalementDocumentTypeMapper
{
public static function map(string $value): DocumentType
{
if (str_starts_with($value, 'desordres_')) {
return DocumentType::SITUATION;
}

if (str_contains($value, 'dpe_bail')) {
return DocumentType::SITUATION_FOYER_BAIL;
}

if (str_contains($value, 'dpe_dpe')) {
return DocumentType::SITUATION_FOYER_DPE;
}

return DocumentType::AUTRE;
}
}
2 changes: 1 addition & 1 deletion src/Utils/DataPropertyArrayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static function filterByPrefix(array $data, array $prefixes): array
$arrayFiltered = [];
foreach ($data as $property => $value) {
foreach ($prefixes as $prefix) {
if (str_starts_with($property, $prefix)) {
if (str_starts_with($property, $prefix) && !str_ends_with($property, '_upload')) {
$arrayFiltered[$property] = $value;
break;
}
Expand Down
Loading

0 comments on commit eebee3d

Please sign in to comment.