Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Mise à jour affectation #3655

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ services:
tags:
- { name: 'kernel.event_listener', event: 'kernel.exception' }


App\EventListener\SecurityApiExceptionListener:
tags:
- { name: 'kernel.event_listener', event: 'kernel.exception' }

App\EventListener\SeoPageNotFoundRedirectListener:
tags:
- { name: 'kernel.event_listener', event: 'kernel.request' }
Expand Down
29 changes: 29 additions & 0 deletions migrations/Version20250130150909.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20250130150909 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add uuid column to affectation table';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE affectation ADD uuid VARCHAR(255) NOT NULL AFTER territory_id');
$this->addSql('UPDATE affectation SET uuid = UUID()');
$this->addSql('CREATE UNIQUE INDEX UNIQ_F4DD61D3D17F50A6 ON affectation (uuid)');
}

public function down(Schema $schema): void
{
$this->addSql('DROP INDEX UNIQ_F4DD61D3D17F50A6 ON affectation');
$this->addSql('ALTER TABLE affectation DROP uuid');
}
}
230 changes: 230 additions & 0 deletions src/Controller/Api/AffectationUpdateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

namespace App\Controller\Api;

use App\Dto\Api\Request\AffectationRequest;
use App\Dto\Api\Response\AffectationResponse;
use App\Entity\Affectation;
use App\Entity\Enum\AffectationNewStatus;
use App\Entity\Enum\MotifCloture;
use App\Entity\Enum\MotifRefus;
use App\Entity\User;
use App\EventListener\SecurityApiExceptionListener;
use App\Exception\Suivi\UsagerNotificationRequiredException;
use App\Manager\AffectationManager;
use App\Security\Voter\AffectationVoter;
use OpenApi\Attributes as OA;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;

#[When('dev')]
#[When('test')]
#[Route('/api')]
class AffectationUpdateController extends AbstractController
{
public function __construct(private readonly AffectationManager $affectationManager)
{
}

/**
* @throws UsagerNotificationRequiredException
*/
#[Route('/affectations/{uuid:affectation}', name: 'api_affectations_update', methods: 'PATCH')]
#[OA\Patch(
path: '/api/affectations/{uuid}',
description: 'Mise à jour d\'une affectation',
summary: 'Mise à jour d\'une affectation',
security: [['Bearer' => []]],
requestBody: new OA\RequestBody(
content: new OA\JsonContent(
examples: [
new OA\Examples(
example: 'Dossier en cours',
summary: 'Affectation passant de NOUVEAU à EN_COURS',
value: [
'statut' => 'EN_COURS',
]
),
new OA\Examples(
example: 'Dossier fermé',
summary: 'Affectation passant EN_COURS à FERME',
value: [
'statut' => 'FERME',
'motifCloture' => 'REFUS_DE_VISITE',
'message' => 'Lorem ipsum dolor sit amet',
]
),
new OA\Examples(
example: 'Dossier refusé',
summary: 'Affectation passant de NOUVEAU à REFUSE',
value: [
'statut' => 'FERME',
'motifCloture' => 'REFUS_DE_VISITE',
'message' => 'Lorem ipsum dolor sit amet',
]
),
new OA\Examples(
example: 'Dossier de nouveau ouvert',
summary: 'Affectation passant de FERME à NOUVEAU',
value: [
'statut' => 'NOUVEAU',
'notifyUsager' => true,
]
),
],
ref: '#/components/schemas/AffectationRequest'
),
),
tags: ['Affectations']
)]
#[OA\Response(
response: Response::HTTP_OK,
description: 'Une affectation',
content: new OA\JsonContent(ref: '#/components/schemas/Affectation')
)]
#[OA\Response(
response: Response::HTTP_NOT_FOUND,
description: 'Affectation introuvable',
content: new OA\JsonContent(
properties: [
new OA\Property(
property: 'message',
type: 'string',
example: 'Affectation introuvable'
),
new OA\Property(
property: 'statut',
type: 'int',
example: Response::HTTP_NOT_FOUND
),
],
type: 'object'
)
)]
#[OA\Response(
response: Response::HTTP_BAD_REQUEST,
description: 'Mauvaise payload (données invalides).',
content: new OA\JsonContent(
properties: [
new OA\Property(
property: 'message',
type: 'string',
example: 'Valeurs invalides pour les champs suivants :'
),
new OA\Property(
property: 'status',
type: 'integer',
example: 400
),
new OA\Property(
property: 'errors',
type: 'array',
items: new OA\Items(
properties: [
new OA\Property(
property: 'property',
type: 'string',
example: 'statut'
),
new OA\Property(
property: 'message',
type: 'string',
example: 'Cette valeur doit être l\'un des choix suivants : \"NOUVEAU\", \"EN_COURS\", \"FERME\", \"REFUSE\"'
),
new OA\Property(
property: 'invalidValue',
type: 'string',
example: 'NOUVEAddU'
),
],
type: 'object'
)
),
],
type: 'object'
)
)]
#[OA\Response(
response: Response::HTTP_FORBIDDEN,
description: 'Accès à la ressource non autorisée.',
sfinx13 marked this conversation as resolved.
Show resolved Hide resolved
content: new OA\JsonContent(
properties: [
new OA\Property(
property: 'message',
type: 'string',
example: 'Vous n\'avez pas l\'autorisation d\'accéder à cette ressource.'
),
new OA\Property(
property: 'statut',
type: 'int',
example: Response::HTTP_FORBIDDEN
),
],
type: 'object'
)
)]
public function index(
#[MapRequestPayload] AffectationRequest $affectationRequest,
?Affectation $affectation = null,
): JsonResponse {
if (null === $affectation) {
return new JsonResponse(
['message' => 'Affectation introuvable.', 'status' => Response::HTTP_NOT_FOUND],
Response::HTTP_NOT_FOUND
);
}
$affectation->setNextStatut(AffectationNewStatus::mapStatus($affectationRequest->statut));
$this->denyAccessUnlessGranted(AffectationVoter::ANSWER, $affectation, SecurityApiExceptionListener::ACCESS_DENIED);
$this->denyAccessUnlessGranted(AffectationVoter::UPDATE_STATUT, $affectation, SecurityApiExceptionListener::TRANSITION_STATUT_DENIED);
$this->applyUsagerNotification($affectationRequest, $affectation);

$affectation = $this->update($affectationRequest, $affectation);

return new JsonResponse(new AffectationResponse($affectation), Response::HTTP_OK);
}

private function update(AffectationRequest $affectationRequest, Affectation $affectation): Affectation
{
/** @var User $user */
$user = $this->getUser();

$statut = $affectation->getNextStatut();
if (Affectation::STATUS_CLOSED === $statut) {
$motifCloture = MotifCloture::tryFrom($affectationRequest->motifCloture);

return $this->affectationManager->closeAffectation(
$affectation,
$user,
$motifCloture,
$affectationRequest->message,
true
);
}
$motifRefus = $message = null;
if (Affectation::STATUS_REFUSED === $statut) {
$motifRefus = MotifRefus::tryFrom($affectationRequest->motifRefus)->value;
$message = $affectationRequest->message;
}

return $this->affectationManager->updateAffectation($affectation, $user, $statut, $motifRefus, $message);
}

/**
* @throws UsagerNotificationRequiredException
*/
private function applyUsagerNotification(AffectationRequest $affectationRequest, Affectation $affectation): void
{
if (Affectation::STATUS_CLOSED === $affectation->getStatut()
&& Affectation::STATUS_WAIT === $affectation->getNextStatut()) {
if (null === $affectationRequest->notifyUsager) {
throw new UsagerNotificationRequiredException($affectationRequest);
sfinx13 marked this conversation as resolved.
Show resolved Hide resolved
}

$affectation->setHasNotificationUsagerToCreate($affectationRequest->notifyUsager);
}
}
}
63 changes: 8 additions & 55 deletions src/Controller/Back/AffectationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,37 @@

use App\Entity\Affectation;
use App\Entity\Signalement;
use App\Entity\Suivi;
use App\Entity\User;
use App\Event\AffectationAnsweredEvent;
use App\Factory\Interconnection\Idoss\DossierMessageFactory;
use App\Manager\AffectationManager;
use App\Manager\SignalementManager;
use App\Manager\SuiviManager;
use App\Manager\UserManager;
use App\Messenger\InterconnectionBus;
use App\Repository\AffectationRepository;
use App\Repository\PartnerRepository;
use App\Security\Voter\AffectationVoter;
use App\Service\Signalement\SearchFilterOptionDataProvider;
use App\Specification\Signalement\FirstAffectationAcceptedSpecification;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

#[Route('/bo/signalements')]
class AffectationController extends AbstractController
{
public function __construct(
private SignalementManager $signalementManager,
private AffectationManager $affectationManager,
private PartnerRepository $partnerRepository,
private InterconnectionBus $interconnectionBus,
private EventDispatcherInterface $eventDispatcher,
private readonly SignalementManager $signalementManager,
private readonly AffectationManager $affectationManager,
private readonly PartnerRepository $partnerRepository,
private readonly InterconnectionBus $interconnectionBus,
) {
}

/**
* @throws ExceptionInterface
* @throws InvalidArgumentException
*/
#[Route('/{uuid:signalement}/affectation/toggle', name: 'back_signalement_toggle_affectation')]
Expand Down Expand Up @@ -122,63 +114,24 @@ public function removePartnerAffectation(
methods: 'POST'
)]
public function affectationResponseSignalement(
SuiviManager $suiviManager,
UserManager $userManager,
ParameterBagInterface $parameterBag,
Signalement $signalement,
Affectation $affectation,
User $user,
Request $request,
FirstAffectationAcceptedSpecification $firstAcceptedAffectationSpecification,
DossierMessageFactory $dossierMessageFactory,
MessageBusInterface $bus,
): Response {
$this->denyAccessUnlessGranted(AffectationVoter::ANSWER, $affectation);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce qu'on ne devrait pas ici ajouter un check du statut existant pour autoriser uniquement si STATUS_WAIT ?

Copy link
Collaborator Author

@sfinx13 sfinx13 Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je pourrais à la place appliquer le nouveau voter la ou il est applicable ? (Je voudrais le faire valider avant et faire un autre ticket)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pour ce cas précis le workflow me parait pas assez restrictif, on veux uniquement que ce soit possible si le statut est STATUS_WAIT (mais ca peux en effet attendre le ticket dédié d'application du voter)

if ($this->isCsrfTokenValid('signalement_affectation_response_'.$signalement->getId(), $request->get('_token'))
&& $response = $request->get('signalement-affectation-response')
) {
$status = isset($response['accept']) ? Affectation::STATUS_ACCEPTED : Affectation::STATUS_REFUSED;
$motifRefus = (Affectation::STATUS_REFUSED === $status) ? $response['motifRefus'] : null;
$affectation = $this->affectationManager->updateAffectation($affectation, $user, $status, $motifRefus);

if ($firstAcceptedAffectationSpecification->isSatisfiedBy($signalement, $affectation)) {
$adminEmail = $parameterBag->get('user_system_email');
$adminUser = $userManager->findOneBy(['email' => $adminEmail]);
$suiviManager->createSuivi(
user: $adminUser,
signalement: $signalement,
description: $parameterBag->get('suivi_message')['first_accepted_affectation'],
type: Suivi::TYPE_AUTO,
isPublic: true,
context: Suivi::CONTEXT_NOTIFY_USAGER_ONLY,
);
}

if (Affectation::STATUS_REFUSED == $status) {
$this->dispatchAffectationAnsweredEvent($affectation, $response);
}
if ($dossierMessageFactory->supports($affectation)) {
$bus->dispatch($dossierMessageFactory->createInstance($affectation));
}
$message = $response['suivi'] ?? null;
$this->affectationManager->updateAffectation($affectation, $user, $status, $motifRefus, $message);
$this->addFlash('success', 'Affectation mise à jour avec succès !');
} else {
$this->addFlash('error', "Une erreur est survenu lors de l'affectation");
}

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

private function dispatchAffectationAnsweredEvent(
Affectation $affectation,
array $response,
): void {
/** @var User $user */
$user = $this->getUser();
if (isset($response['suivi'])) {
$this->eventDispatcher->dispatch(
new AffectationAnsweredEvent($affectation, $user, $response),
AffectationAnsweredEvent::NAME
);
}
}
}
Loading
Loading